INSTINCT Code Coverage Report


Directory: src/
File: util/Vendor/Ublox/UbloxUartSensor.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 1 2 50.0%
Functions: 1 2 50.0%
Branches: 0 0 -%

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 UbloxUartSensor.hpp
10 /// @brief Class to read out Ublox Sensors
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2020-07-22
13
14 #pragma once
15
16 #include <memory>
17 #include <vector>
18
19 #include "uart/sensors/sensors.hpp"
20 #include "util/Logger.hpp"
21
22 namespace NAV::vendor::ublox
23 {
24 /// @brief Class to read out Ublox Sensors
25 class UbloxUartSensor
26 {
27 public:
28 /// @brief Constructor
29 /// @param[in] name Name of the Parent Node
30 explicit UbloxUartSensor(std::string name);
31
32 /// @brief Default constructor
33 UbloxUartSensor() = default;
34 /// @brief Destructor
35 226 ~UbloxUartSensor() = default;
36 /// @brief Copy constructor
37 UbloxUartSensor(const UbloxUartSensor&) = delete;
38 /// @brief Move constructor
39 UbloxUartSensor(UbloxUartSensor&&) = delete;
40 /// @brief Copy assignment operator
41 UbloxUartSensor& operator=(const UbloxUartSensor&) = delete;
42 /// @brief Move assignment operator
43 UbloxUartSensor& operator=(UbloxUartSensor&&) = delete;
44 /// @brief Arrow operator overload
45 uart::sensors::UartSensor* operator->() { return &_sensor; };
46
47 /// @brief Collects data bytes and searches for packages inside of them
48 /// @param[in] dataByte The next data byte
49 /// @return nullptr if no packet found yet, otherwise a pointer to the packet
50 std::unique_ptr<uart::protocol::Packet> findPacket(uint8_t dataByte);
51
52 static constexpr uint8_t BINARY_SYNC_CHAR_1 = 0xB5; ///< ยต - First sync character which begins a new binary message
53 static constexpr uint8_t BINARY_SYNC_CHAR_2 = 0x62; ///< b - Second sync character which begins a new binary message
54 static constexpr uint8_t ASCII_START_CHAR = '$'; ///< Ascii character which begins a new ascii message
55
56 private:
57 /// Name of the Parent Node
58 const std::string _name;
59
60 /// UartSensor object which handles the UART interface
61 uart::sensors::UartSensor _sensor{ ENDIANNESS,
62 packetFinderFunction,
63 this,
64 packetTypeFunction,
65 checksumFunction,
66 isErrorFunction,
67 isResponseFunction,
68 PACKET_HEADER_LENGTH };
69
70 /// @brief Function which is called to find packets in the provided data buffer
71 /// @param[in] data Raw data buffer which has potential packets inside
72 /// @param[in] timestamp Timestamp then the data in the buffer was received
73 /// @param[in] dispatchPacket Function to call when a complete packet was found
74 /// @param[in] dispatchPacketUserData User data to forward to the dispatchPacket function
75 /// @param[in] userData User data provided when regisering this function. Should contain the sensor object
76 static void packetFinderFunction(const std::vector<uint8_t>& data,
77 const uart::xplat::TimeStamp& timestamp,
78 uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void* dispatchPacketUserData,
79 void* userData);
80
81 /// @brief Function which is called to determine the packet type (ascii/binary)
82 /// @param[in] packet Packet to check the type of
83 /// @return The type of the packet
84 static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet& packet);
85
86 /// @brief Function which is called to verify packet integrity
87 /// @param[in] packet Packet to calculate the checksum for
88 /// @return True if the packet is fault free
89 static bool checksumFunction(const uart::protocol::Packet& packet);
90
91 /// @brief Function which determines, if the packet is an Error Packet
92 /// @param[in] packet The packet to check
93 static bool isErrorFunction(const uart::protocol::Packet& packet);
94
95 /// @brief Function which determines, if the packet is a Response
96 /// @param[in] packet The packet to check
97 static bool isResponseFunction(const uart::protocol::Packet& packet);
98
99 static constexpr uart::Endianness ENDIANNESS = uart::Endianness::ENDIAN_LITTLE; ///< Endianess of the sensor
100 static constexpr size_t PACKET_HEADER_LENGTH = 2; ///< Length of the header of each packet
101 static constexpr uint8_t ASCII_END_CHAR_1 = '\r'; ///< First Ascii End character
102 static constexpr uint8_t ASCII_END_CHAR_2 = '\n'; ///< Second Ascii End character
103 static constexpr uint8_t ASCII_ESCAPE_CHAR = '\0'; ///< Ascii Escape charater
104
105 bool _currentlyBuildingAsciiPacket{ false }; ///< Flag if currently a ascii packet is built
106 bool _currentlyBuildingBinaryPacket{ false }; ///< Flag if currently a binary packet is built
107
108 bool _asciiEndChar1Found{ false }; ///< Flag if the first ascii end character was found
109 bool _binarySyncChar2Found{ false }; ///< Flag if the second binary end character was found
110 bool _binaryMsgClassFound{ false }; ///< Flag if the message class was found
111 bool _binaryMsgIdFound{ false }; ///< Flag if the message id was found
112 bool _binaryPayloadLength1Found{ false }; ///< Flag if the first byte of the payload length was found
113 bool _binaryPayloadLength2Found{ false }; ///< Flag if the second byte of the payload length was found
114
115 /// Message class of the current packet
116 uint8_t _binaryMsgClass{ 0 };
117 /// Message id of the current packet
118 uint8_t _binaryMsgId{ 0 };
119 /// Payload length of the current packet
120 uint16_t _binaryPayloadLength{ 0 };
121
122 /// Buffer to collect messages till they are complete
123 std::vector<uint8_t> _buffer;
124
125 /// Used for correlating raw data with where the packet was found for the end user.
126 size_t _runningDataIndex{ 0 };
127 /// Amount of bytes remaining for a complete packet
128 size_t _numOfBytesRemainingForCompletePacket{ 0 };
129
130 #if LOG_LEVEL <= LOG_LEVEL_DATA
131 /// Bytes which were not recognized as messages
132 std::vector<uint8_t> _unrecognizedBytes;
133 #endif
134
135 /// @brief Resets the current message tracking
136 void resetTracking();
137 };
138
139 } // namespace NAV::vendor::ublox
140