Ares LoRa Serial Driver Core
Loading...
Searching...
No Matches
ares_frame.hpp
Go to the documentation of this file.
1
10
11#ifndef ARES_ARES_FRAME_HPP
12#define ARES_ARES_FRAME_HPP
13
14#include <cstdint>
15#include <exception>
16#include <string>
17#include <sys/types.h>
18#include <tuple>
19#include <utility>
20#include <variant>
21#include <vector>
22
28class AresFrameError : public std::exception {
29 public:
34 explicit AresFrameError(std::string msg) : msg_(std::move(msg)) {}
35
40 [[nodiscard]] const char *what() const noexcept override {
41 return msg_.c_str();
42 }
43
44 private:
45 std::string msg_;
46};
47
54class AresFrame {
55 public:
61 enum AresFrameType : unsigned int {
62 SETTING = 0,
63 START = 1,
65 LED = 3,
67 CLAIM = 5,
68 LOG = 6,
69 LOG_ACK = 7,
70 VERSION = 8,
71 ACK = 9,
73 DBG = 11,
74 PKT_RX = 12,
75 PKT_TX = 13,
77 };
78
84 struct Setting {
85 Setting() = default;
86
87 bool set = false;
88 uint16_t setting_id = 0;
89 uint32_t value = 0;
90 };
91
97 struct Start {
98 Start() = default;
99
100 int64_t sec = -1;
101 uint64_t nsec = 0;
102 uint16_t id = 0;
104 uint16_t packet_id = 0;
106 bool broadcast = false;
109 uint8_t seq_cnt = 0;
111 };
112
118 struct LoraConfig {
119 LoraConfig() = default;
120
124 uint32_t frequency = 0;
125
129 uint16_t preamble_length = 0;
130
134 uint8_t bandwidth = 0;
135
139 uint8_t data_rate = 0;
140
144 uint8_t coding_rate = 0;
145
149 int8_t tx_power = 0;
150
163 uint8_t cad_mode = 0;
164
170 uint8_t cad_num_symbols = 0;
171
177 uint8_t cad_det_peak = 0;
178
184 uint8_t cad_det_min = 0;
185 };
186
192 struct Led {
198 enum LedState : uint8_t {
199 OFF = 0,
200 ON = 1,
201 BLINK = 2,
202 FADE = 3,
203 FETCH = 4,
204 };
205
209 uint8_t led = 0;
210
215 };
216
222 struct Heartbeat {
223 Heartbeat() = default;
224
228 bool ready = false;
229
233 bool broadcast = false;
234
238 uint8_t tx_cnt = 0;
239
243 uint16_t id = 0;
244 };
245
251 struct Claim {
256 uint16_t id = 0;
257 };
258
264 struct Log {
274 Log(bool broadcast, uint8_t tx_cnt, uint16_t id, uint16_t log_id,
275 std::string msg)
277 msg(std::move(msg)) {}
278
282 Log() = default;
283
287 bool broadcast = false;
288
292 uint8_t tx_cnt = 1;
293
298 uint8_t part = 1;
299
304 uint8_t num_parts = 1;
305
311 uint16_t id = 0;
312
316 uint16_t log_id = 0;
317
322 std::string msg;
323
324 friend class AresFrame;
325
326 private:
327 std::vector<std::string> _msg_split;
328 size_t _idx = 0;
329 // used for serialization
330 uint8_t _part = 0;
331 uint8_t _num_parts = 1;
332 bool _preprocessed = false;
333 static constexpr size_t _overhead = sizeof(broadcast) + sizeof(part) +
334 sizeof(num_parts) + sizeof(id) +
335 sizeof(tx_cnt) + sizeof(log_id);
336 };
337
343 struct LogAck {
347 uint8_t part = 0;
348
352 uint8_t num_parts = 0;
353
357 uint16_t id = 0;
358
362 uint16_t log_id = 0;
363
369 bool operator==(const LogAck &other) const {
370 return (part == other.part) && (num_parts == other.num_parts) &&
371 (id == other.id) && (log_id == other.log_id);
372 }
373 };
374
380 struct Version {
384 uint32_t app = 0;
385
389 uint32_t ncs = 0;
390
394 uint32_t kernel = 0;
395 };
396
402 using AckErrorCode = int32_t;
403
409 enum FramingError : uint8_t {
413 };
414
420 struct Dbg {
424 int32_t code = 0;
425 };
426
432 struct PktRx {
436 uint16_t packet_id = 0;
440 uint16_t src_id = 0;
441
445 uint8_t seq_cnt = 0;
446 };
447
453 struct PktTx {
457 uint32_t count = 0;
458 };
459
465 using TxTypes = std::variant<std::monostate, Setting, Start, LoraConfig,
467
473 using RxTypes = std::variant<std::monostate, Setting, Start, AckErrorCode,
476
482 using ResponseTypes = std::variant<std::monostate, Setting, AckErrorCode,
484
501
507 explicit AresFrame(AresFrameType type, TxTypes payload);
508
515 explicit AresFrame(const std::vector<uint8_t> &bytearray);
516
521
526 AresFrame(const AresFrame &other);
527
531 ~AresFrame() = default;
532
541 static std::tuple<ssize_t, ssize_t, ssize_t>
542 frame_present(const uint8_t *serial_data, size_t len,
543 bool error_no_footer = true);
544
552 static std::tuple<ssize_t, ssize_t, ssize_t>
553 frame_present(const std::vector<uint8_t> &bytearray,
554 bool error_no_footer = true);
555
567 void serialize(std::vector<uint8_t> &bytearray);
568
578 void parse(const uint8_t *serial_data, size_t start_index, size_t len);
579
588 void parse(const std::vector<uint8_t> &bytearray, size_t start_index);
589
596 [[nodiscard]] Decoded get_parsed_frame() const;
597
604 [[nodiscard]] bool frame_available() const;
605
612 [[nodiscard]] size_t total_frames() const;
613
614 private:
615 enum FrameDirection { TX, RX, UNSPECIFIED };
616 bool _new_frame = true;
617
618 FrameDirection _direction = UNSPECIFIED;
619 AresFrameType _type = UNKNOWN;
620 TxTypes _tx_payload;
621 RxTypes _rx_payload;
622
623 [[nodiscard]] uint16_t _payload_size() const;
624
625 void _preprocess_serialize();
626 static void _preprocess_log(Log &payload);
627
628 static void _serialize_setting(const Setting &payload,
629 std::vector<uint8_t> &buffer);
630 static void _serialize_start(const Start &payload,
631 std::vector<uint8_t> &buffer);
632 static void _serialize_lora_config(const LoraConfig &payload,
633 std::vector<uint8_t> &buffer);
634 static void _serialize_led(const Led &payload,
635 std::vector<uint8_t> &buffer);
636 static void _serialize_heartbeat(const Heartbeat &payload,
637 std::vector<uint8_t> &buffer);
638 static void _serialize_claim(const Claim &payload,
639 std::vector<uint8_t> &buffer);
640 static void _serialize_log(const Log &payload,
641 std::vector<uint8_t> &buffer);
642 static void _serialize_version(const Version &payload,
643 std::vector<uint8_t> &buffer);
644
645 void _deserialize_setting(const uint8_t *buf, size_t len);
646 void _deserialize_led(const uint8_t *buf, size_t len);
647 void _deserialize_start(const uint8_t *buf, size_t len);
648 void _deserialize_heartbeat(const uint8_t *buf, size_t len);
649 void _deserialize_claim(const uint8_t *buf, size_t len);
650 void _deserialize_log(const uint8_t *buf, size_t len);
651 void _deserialize_log_ack(const uint8_t *buf, size_t len);
652 void _deserialize_version(const uint8_t *buf, size_t len);
653 void _deserialize_ack(const uint8_t *buf, size_t len);
654 void _deserialize_framing_error(const uint8_t *buf, size_t len);
655 void _deserialize_dbg(const uint8_t *buf, size_t len);
656 void _deserialize_pkt_rx(const uint8_t *buf, size_t len);
657 void _deserialize_pkt_tx(const uint8_t *buf, size_t len);
658};
659
660#endif // ARES_ARES_FRAME_HPP
const char * what() const noexcept override
Definition ares_frame.hpp:40
AresFrameError(std::string msg)
Definition ares_frame.hpp:34
~AresFrame()=default
int32_t AckErrorCode
Definition ares_frame.hpp:402
Decoded get_parsed_frame() const
std::variant< std::monostate, Setting, Start, LoraConfig, Led, Heartbeat, Claim, Log, Version > TxTypes
Definition ares_frame.hpp:465
AresFrame(const AresFrame &other)
void parse(const uint8_t *serial_data, size_t start_index, size_t len)
void parse(const std::vector< uint8_t > &bytearray, size_t start_index)
FramingError
Definition ares_frame.hpp:409
@ NOT_IMPLEMENTED
Frame type not implemented.
Definition ares_frame.hpp:412
@ BAD_FRAME
Bad frame.
Definition ares_frame.hpp:410
@ BAD_TYPE
Bad frame type.
Definition ares_frame.hpp:411
AresFrameType
Definition ares_frame.hpp:61
@ FRAMING_ERROR
Framing error (RX).
Definition ares_frame.hpp:72
@ VERSION
Firmware version (TX/RX).
Definition ares_frame.hpp:70
@ PKT_RX
Packet Received (RX).
Definition ares_frame.hpp:74
@ PKT_TX
Packet transmitted (RX).
Definition ares_frame.hpp:75
@ ACK
Command acknowledge (RX).
Definition ares_frame.hpp:71
@ UNKNOWN
Unknown frame.
Definition ares_frame.hpp:76
@ SETTING
Setting get/set (TX/RX).
Definition ares_frame.hpp:62
@ START
Start time (TX/RX).
Definition ares_frame.hpp:63
@ HEARTBEAT
Send heartbeat (TX/RX).
Definition ares_frame.hpp:66
@ LED
LED state get/set (TX/RX).
Definition ares_frame.hpp:65
@ LORA_CONFIG
LoRa modem configuration (TX).
Definition ares_frame.hpp:64
@ LOG
Log message (TX/RX).
Definition ares_frame.hpp:68
@ LOG_ACK
Log acknowledge (RX).
Definition ares_frame.hpp:69
@ DBG
Debug message (RX).
Definition ares_frame.hpp:73
@ CLAIM
Master claim (TX/RX).
Definition ares_frame.hpp:67
bool frame_available() const
size_t total_frames() const
void serialize(std::vector< uint8_t > &bytearray)
AresFrame(const std::vector< uint8_t > &bytearray)
static std::tuple< ssize_t, ssize_t, ssize_t > frame_present(const uint8_t *serial_data, size_t len, bool error_no_footer=true)
static std::tuple< ssize_t, ssize_t, ssize_t > frame_present(const std::vector< uint8_t > &bytearray, bool error_no_footer=true)
std::variant< std::monostate, Setting, AckErrorCode, FramingError, Led, Version > ResponseTypes
Definition ares_frame.hpp:482
AresFrame(AresFrameType type, TxTypes payload)
std::variant< std::monostate, Setting, Start, AckErrorCode, FramingError, Led, Heartbeat, Claim, Log, Version, LogAck, Dbg, PktRx, PktTx > RxTypes
Definition ares_frame.hpp:473
Definition ares_frame.hpp:251
Definition ares_frame.hpp:420
int32_t code
Definition ares_frame.hpp:424
Definition ares_frame.hpp:490
RxTypes payload
Definition ares_frame.hpp:499
AresFrameType type
Definition ares_frame.hpp:494
Definition ares_frame.hpp:222
bool ready
Definition ares_frame.hpp:228
bool broadcast
Definition ares_frame.hpp:233
uint8_t tx_cnt
Definition ares_frame.hpp:238
Definition ares_frame.hpp:192
uint8_t led
Definition ares_frame.hpp:209
LedState
Definition ares_frame.hpp:198
@ ON
LED on.
Definition ares_frame.hpp:200
@ FADE
LED fading.
Definition ares_frame.hpp:202
@ BLINK
LED blinking at 1 Hz.
Definition ares_frame.hpp:201
@ OFF
LED off.
Definition ares_frame.hpp:199
@ FETCH
Retrieve LED state from firmware.
Definition ares_frame.hpp:203
LedState state
Definition ares_frame.hpp:214
Definition ares_frame.hpp:343
uint8_t num_parts
Definition ares_frame.hpp:352
uint8_t part
Definition ares_frame.hpp:347
uint16_t id
Definition ares_frame.hpp:357
uint16_t log_id
Definition ares_frame.hpp:362
bool operator==(const LogAck &other) const
Definition ares_frame.hpp:369
Definition ares_frame.hpp:264
uint8_t part
Definition ares_frame.hpp:298
Log(bool broadcast, uint8_t tx_cnt, uint16_t id, uint16_t log_id, std::string msg)
Definition ares_frame.hpp:274
uint8_t num_parts
Definition ares_frame.hpp:304
uint16_t id
Definition ares_frame.hpp:311
uint8_t tx_cnt
Definition ares_frame.hpp:292
uint16_t log_id
Definition ares_frame.hpp:316
Log()=default
bool broadcast
Definition ares_frame.hpp:287
std::string msg
Definition ares_frame.hpp:322
Definition ares_frame.hpp:118
uint8_t cad_det_peak
Definition ares_frame.hpp:177
uint8_t cad_mode
Definition ares_frame.hpp:163
int8_t tx_power
Definition ares_frame.hpp:149
uint8_t cad_num_symbols
Definition ares_frame.hpp:170
uint32_t frequency
Definition ares_frame.hpp:124
uint16_t preamble_length
Definition ares_frame.hpp:129
uint8_t cad_det_min
Definition ares_frame.hpp:184
uint8_t bandwidth
Definition ares_frame.hpp:134
uint8_t data_rate
Definition ares_frame.hpp:139
uint8_t coding_rate
Definition ares_frame.hpp:144
Definition ares_frame.hpp:432
uint8_t seq_cnt
Definition ares_frame.hpp:445
uint16_t packet_id
Definition ares_frame.hpp:436
uint16_t src_id
Definition ares_frame.hpp:440
Definition ares_frame.hpp:453
uint32_t count
Definition ares_frame.hpp:457
Definition ares_frame.hpp:84
uint32_t value
The value of the setting.
Definition ares_frame.hpp:89
uint16_t setting_id
The setting ID.
Definition ares_frame.hpp:88
bool set
Set flag for settings.
Definition ares_frame.hpp:87
Definition ares_frame.hpp:97
uint8_t seq_cnt
Definition ares_frame.hpp:109
bool broadcast
Definition ares_frame.hpp:106
int64_t sec
Seconds part for start time.
Definition ares_frame.hpp:100
uint16_t packet_id
Definition ares_frame.hpp:104
uint64_t nsec
Nanoseconds part for start time.
Definition ares_frame.hpp:101
Definition ares_frame.hpp:380
uint32_t kernel
Definition ares_frame.hpp:394
uint32_t ncs
Definition ares_frame.hpp:389
uint32_t app
Definition ares_frame.hpp:384