| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // This file is part of INSTINCT, the INS Toolkit for Integrated | ||
| 2 | // Navigation Concepts and Training by the Institute of Navigation of | ||
| 3 | // the University of Stuttgart, Germany. | ||
| 4 | // | ||
| 5 | // This Source Code Form is subject to the terms of the Mozilla Public | ||
| 6 | // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| 7 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| 8 | |||
| 9 | /// @file EspressifUartSensor.hpp | ||
| 10 | /// @brief Class to read out Espressif Sensors | ||
| 11 | /// @author R. Lintz (r-lintz@gmx.de) (master thesis) | ||
| 12 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
| 13 | /// @date 2024-01-08 | ||
| 14 | |||
| 15 | #pragma once | ||
| 16 | |||
| 17 | #include <memory> | ||
| 18 | |||
| 19 | #include "uart/sensors/sensors.hpp" | ||
| 20 | |||
| 21 | namespace NAV::vendor::espressif | ||
| 22 | { | ||
| 23 | /// @brief Class to read out Espressif Sensors | ||
| 24 | class EspressifUartSensor | ||
| 25 | { | ||
| 26 | public: | ||
| 27 | /// @brief Constructor | ||
| 28 | /// @param[in] name Name of the Parent Node | ||
| 29 | explicit EspressifUartSensor(std::string name); | ||
| 30 | |||
| 31 | /// @brief Default constructor | ||
| 32 | EspressifUartSensor() = default; | ||
| 33 | /// @brief Destructor | ||
| 34 | 114 | ~EspressifUartSensor() = default; | |
| 35 | /// @brief Copy constructor | ||
| 36 | EspressifUartSensor(const EspressifUartSensor&) = delete; | ||
| 37 | /// @brief Move constructor | ||
| 38 | EspressifUartSensor(EspressifUartSensor&&) = delete; | ||
| 39 | /// @brief Copy assignment operator | ||
| 40 | EspressifUartSensor& operator=(const EspressifUartSensor&) = delete; | ||
| 41 | /// @brief Move assignment operator | ||
| 42 | EspressifUartSensor& operator=(EspressifUartSensor&&) = delete; | ||
| 43 | /// @brief Arrow operator overload | ||
| 44 | ✗ | uart::sensors::UartSensor* operator->() { return &_sensor; }; | |
| 45 | |||
| 46 | /// @brief Collects data bytes and searches for packages inside of them | ||
| 47 | /// @param[in] dataByte The next data byte | ||
| 48 | /// @return nullptr if no packet found yet, otherwise a pointer to the packet | ||
| 49 | std::unique_ptr<uart::protocol::Packet> findPacket(uint8_t dataByte); | ||
| 50 | |||
| 51 | static constexpr uint8_t BINARY_SYNC_CHAR_1 = 0xB5; ///< ยต - First sync character which begins a new binary message | ||
| 52 | static constexpr uint8_t BINARY_SYNC_CHAR_2 = 0x62; ///< b - Second sync character which begins a new binary message | ||
| 53 | |||
| 54 | private: | ||
| 55 | /// Name of the Parent Node | ||
| 56 | const std::string _name; | ||
| 57 | |||
| 58 | /// UartSensor object which handles the UART interface | ||
| 59 | uart::sensors::UartSensor _sensor{ ENDIANNESS, | ||
| 60 | packetFinderFunction, | ||
| 61 | this, | ||
| 62 | packetTypeFunction, | ||
| 63 | checksumFunction, | ||
| 64 | isErrorFunction, | ||
| 65 | isResponseFunction, | ||
| 66 | PACKET_HEADER_LENGTH }; | ||
| 67 | |||
| 68 | /// @brief Function which is called to find packets in the provided data buffer | ||
| 69 | /// @param[in] data Raw data buffer which has potential packets inside | ||
| 70 | /// @param[in] timestamp Timestamp then the data in the buffer was received | ||
| 71 | /// @param[in] dispatchPacket Function to call when a complete packet was found | ||
| 72 | /// @param[in] dispatchPacketUserData User data to forward to the dispatchPacket function | ||
| 73 | /// @param[in] userData User data provided when regisering this function. Should contain the sensor object | ||
| 74 | static void packetFinderFunction(const std::vector<uint8_t>& data, | ||
| 75 | const uart::xplat::TimeStamp& timestamp, | ||
| 76 | uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void* dispatchPacketUserData, | ||
| 77 | void* userData); | ||
| 78 | |||
| 79 | /// @brief Function which is called to determine the packet type (ascii/binary) | ||
| 80 | /// @param[in] packet Packet to check the type of | ||
| 81 | /// @return The type of the packet | ||
| 82 | static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet& packet); | ||
| 83 | |||
| 84 | /// @brief Function which is called to verify packet integrity | ||
| 85 | /// @param[in] packet Packet to calculate the checksum for | ||
| 86 | /// @return True if the packet is fault free | ||
| 87 | static bool checksumFunction(const uart::protocol::Packet& packet); | ||
| 88 | |||
| 89 | /// @brief Function which determines, if the packet is an Error Packet | ||
| 90 | /// @param[in] packet The packet to check | ||
| 91 | static bool isErrorFunction(const uart::protocol::Packet& packet); | ||
| 92 | |||
| 93 | /// @brief Function which determines, if the packet is a Response | ||
| 94 | /// @param[in] packet The packet to check | ||
| 95 | static bool isResponseFunction(const uart::protocol::Packet& packet); | ||
| 96 | |||
| 97 | static constexpr uart::Endianness ENDIANNESS = uart::Endianness::ENDIAN_LITTLE; ///< Endianess of the sensor | ||
| 98 | static constexpr size_t PACKET_HEADER_LENGTH = 2; ///< Length of the header of each packet | ||
| 99 | |||
| 100 | bool _currentlyBuildingBinaryPacket{ false }; ///< Flag if currently a binary packet is built | ||
| 101 | |||
| 102 | bool _binarySyncChar2Found{ false }; ///< Flag if the second binary end character was found | ||
| 103 | bool _binaryPayloadLength1Found{ false }; ///< Flag if the first byte of the payload length was found | ||
| 104 | bool _binaryPayloadLength2Found{ false }; ///< Flag if the second byte of the payload length was found | ||
| 105 | |||
| 106 | /// Payload length of the current packet | ||
| 107 | uint16_t _binaryPayloadLength{ 0 }; | ||
| 108 | |||
| 109 | /// Buffer to collect messages till they are complete | ||
| 110 | std::vector<uint8_t> _buffer; | ||
| 111 | |||
| 112 | /// Used for correlating raw data with where the packet was found for the end user. | ||
| 113 | size_t _runningDataIndex{ 0 }; | ||
| 114 | /// Amount of bytes remaining for a complete packet | ||
| 115 | size_t _numOfBytesRemainingForCompletePacket{ 0 }; | ||
| 116 | |||
| 117 | /// @brief Resets the current message tracking | ||
| 118 | void resetTracking(); | ||
| 119 | }; | ||
| 120 | |||
| 121 | } // namespace NAV::vendor::espressif | ||
| 122 |