QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QuectelCodec.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <string_view>
6
7#include "GPSCommand.h"
8#include "NMEASentence.h"
9
11namespace QuectelCodec {
12
14class Fields
15{
16public:
17 explicit Fields(std::string_view body)
18 : _count(NMEA::splitFields(body, _fields))
19 , _overflowed(_count == 0)
20 {}
21
22 size_t size() const { return _count; }
23
24 bool overflowed() const { return _overflowed; }
25
26 std::string_view operator[](size_t index) const { return _fields[index]; }
27
28 std::string_view back() const { return _count ? _fields[_count - 1] : std::string_view{}; }
29
30 void pop_back()
31 {
32 if (_count) {
33 --_count;
34 }
35 }
36
37private:
38 std::array<std::string_view, 12> _fields{};
39 size_t _count;
40 bool _overflowed;
41};
42
43template <typename T>
44bool number(std::string_view text, T& result)
45{
46 if (text.empty() || text.front() == '+') {
47 return false;
48 }
49 const auto parsed = NMEA::number<T>(text);
50 if (!parsed) {
51 return false;
52 }
53 result = *parsed;
54 return true;
55}
56
58[[nodiscard]] inline std::string_view checkedBody(std::string_view line)
59{
60 const auto wire = NMEA::frame(line);
61 return wire && wire->hasValidChecksum() ? wire->body : std::string_view{};
62}
63
64[[nodiscard]] inline bool rejected(const Fields& reply, std::string_view command)
65{
66 unsigned error = 0;
67 return reply.size() == 3 && reply[0] == command && reply[1] == "ERROR" && number(reply[2], error);
68}
69
71[[nodiscard]] inline GPSCommandOutcome acknowledgement(const Fields& reply, std::string_view command)
72{
73 if (reply.size() == 2 && reply[0] == command && reply[1] == "OK") {
75 }
77}
78
79[[nodiscard]] inline GPSCommandOutcome readback(const Fields& reply, std::string_view command, bool matches)
80{
81 if (rejected(reply, command)) {
83 }
84 // Overflow retains the header, but can never verify a truncated readback.
85 if ((reply.size() > 2 || reply.overflowed()) && reply[0] == command && reply[1] == "OK") {
87 }
89}
90
91} // namespace QuectelCodec
GPSCommandOutcome
Definition GPSCommand.h:33
Error error
Borrowed fields for the largest supported PQTM reply, including empty trailing fields.
std::string_view back() const
size_t size() const
bool overflowed() const
std::string_view operator[](size_t index) const
Fields(std::string_view body)
std::optional< Frame > frame(std::string_view text)
PQTM reply fields and the acknowledgement and readback rules every command shares.
GPSCommandOutcome acknowledgement(const Fields &reply, std::string_view command)
"<command>,OK" acknowledges and "<command>,ERROR,<code>" rejects a setting.
std::string_view checkedBody(std::string_view line)
The body of a checksum-valid "$<body>*hh" line; empty otherwise.
bool rejected(const Fields &reply, std::string_view command)
GPSCommandOutcome readback(const Fields &reply, std::string_view command, bool matches)