RMW desert 5.0.0
Loading...
Searching...
No Matches
CBorStream.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (C) 2024 Davide Costa *
3 * *
4 * This file is part of RMW desert. *
5 * *
6 * RMW desert is free software: you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or any *
9 * later version. *
10 * *
11 * RMW desert is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with RMW desert. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************/
19
33
34#ifndef CBORSTREAM_H_
35#define CBORSTREAM_H_
36
37#include "TcpDaemon.h"
38#include "TopicsConfig.h"
39
41
42#include <map>
43#include <queue>
44#include <utility>
45#include <vector>
46#include <string>
47#include <locale>
48#include <codecvt>
49#include <cstdint>
50#include <cstdio>
51#include <mutex>
52
54
55#include "half.hpp"
56
57#define PUBLISHER_TYPE 0
58#define SUBSCRIBER_TYPE 1
59#define CLIENT_TYPE 2
60#define SERVICE_TYPE 3
61
62#define MAX_BUFFER_CAPACITY 100
63
64template <typename T, int MaxLen, typename Container=std::deque<T>>
65class CircularQueue : public std::queue<T, Container> {
66 public:
67 void push(const T& value)
68 {
69 if (this->size() == MaxLen)
70 {
71 this->c.pop_front();
72 }
73 std::queue<T, Container>::push(value);
74 }
75};
76
77namespace cbor
78{
79
81{
82 public:
90 TxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier);
91
101 void start_transmission(uint64_t sequence_id);
109 void start_transmission();
116 void end_transmission();
117
122 TxStream & operator<<(const uint64_t n);
127 TxStream & operator<<(const uint32_t n);
132 TxStream & operator<<(const uint16_t n);
137 TxStream & operator<<(const uint8_t n);
142 TxStream & operator<<(const int64_t n);
147 TxStream & operator<<(const int32_t n);
152 TxStream & operator<<(const int16_t n);
157 TxStream & operator<<(const int8_t n);
162 TxStream & operator<<(const char n);
167 TxStream & operator<<(const float f);
172 TxStream & operator<<(const double d);
177 TxStream & operator<<(const std::string s);
182 TxStream & operator<<(const std::u16string s);
187 TxStream & operator<<(const bool b);
188
193 template<typename T>
194 inline TxStream & operator<<(const std::vector<T> v)
195 {
196 *this << static_cast<const uint32_t>(v.size());
197 return serialize_sequence(v.data(), v.size());
198 }
199
204 TxStream & operator<<(const std::vector<bool> v);
205
211 template<typename T>
212 inline TxStream & serialize_sequence(const T * items, size_t size)
213 {
214 for (size_t i = 0; i < size; ++i)
215 {
216 *this << items[i];
217 }
218 return *this;
219 }
220
221 private:
222 // Forward declarations to abstract the implementation
223 struct WRITER;
224 struct ERROR;
225
226 uint8_t _stream_type;
227 std::string _stream_name;
228 uint8_t _stream_identifier;
229
230 bool _overflow;
231 uint8_t * _packet;
232 WRITER * _writer;
233
234 void new_packet();
235 void handle_overrun(ERROR result);
236
237 std::string toUTF8(const std::u16string source);
238
239};
240
242{
243 public:
251 RxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier);
252
256 ~RxStream();
257
267 bool data_available(int64_t sequence_id = 0);
268
275 void clear_buffer();
276
281 RxStream & operator>>(uint64_t & n);
286 RxStream & operator>>(uint32_t & n);
291 RxStream & operator>>(uint16_t & n);
296 RxStream & operator>>(uint8_t & n);
301 RxStream & operator>>(int64_t & n);
306 RxStream & operator>>(int32_t & n);
311 RxStream & operator>>(int16_t & n);
316 RxStream & operator>>(int8_t & n);
317
322 RxStream & operator>>(char & n);
327 RxStream & operator>>(float & f);
332 RxStream & operator>>(double & d);
337 RxStream & operator>>(std::string & s);
342 RxStream & operator>>(std::u16string & s);
347 RxStream & operator>>(bool & b);
348
353 template<typename T>
354 inline RxStream & operator>>(std::vector<T> & v)
355 {
356 uint32_t size;
357 *this >> size;
358 v.resize(size);
359
360 return deserialize_sequence(v.data(), size);
361 }
362
367 RxStream & operator>>(std::vector<bool> & v);
368
374 template<typename T>
375 inline RxStream & deserialize_sequence(T * items, size_t size)
376 {
377 for (size_t i = 0; i < size; ++i)
378 {
379 *this >> items[i];
380 }
381 return *this;
382 }
383
384
389 uint8_t get_type() const;
394 std::string get_name() const;
399 uint8_t get_identifier() const;
400
410 void push_packet(std::vector<std::pair<void *, int>> packet);
411
419 static void interpret_packets();
420
421 private:
422 // Forward declarations to abstract the implementation
423 struct ITEM;
424
425 uint8_t _stream_type;
426 std::string _stream_name;
427 uint8_t _stream_identifier;
428
429 size_t _buffered_iterator;
430
431 // packets: <packet <field, field_type>>
432 std::vector<std::pair<void *, int>> _buffered_packet;
433 CircularQueue<std::vector<std::pair<void *, int>>, MAX_BUFFER_CAPACITY> _received_packets;
434
435 static const std::map<int, int> _stream_type_match_map;
436 static std::vector<RxStream *> _listening_streams;
437
438 union _cbor_value {
439 int8_t i8;
440 int16_t i16;
441 int32_t i32;
442 int64_t i64;
443 float f32;
444 double f64;
445 uint8_t *bin;
446 char *str;
447 uint8_t str_copy[128];
448 };
449
450 static std::mutex _rx_mutex;
451
452 static std::pair<void *, int> interpret_field(ITEM * items, size_t i, union _cbor_value & val);
453 static void destroy_interpreted_field(std::pair<void *, int>& field);
454 std::u16string toUTF16(const std::string source);
455};
456
457} // namespace cbor
458
459
460#endif
Class used to send and receive data from the DESERT socket.
Class used to store configurations.
Definition CBorStream.h:65
Definition CBorStream.h:242
~RxStream()
Destroy the reception stream.
Definition LibMcuCBorStream.cpp:226
std::string get_name() const
Get the topic name of a specific instance.
Definition LibMcuCBorStream.cpp:433
RxStream & operator>>(std::vector< T > &v)
Decode vector.
Definition CBorStream.h:354
static void interpret_packets()
Interpret raw packets and splits them into different communication types.
Definition LibMcuCBorStream.cpp:448
void clear_buffer()
Clear the currently buffered packet.
Definition LibMcuCBorStream.cpp:283
RxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier)
Create a reception stream.
Definition LibMcuCBorStream.cpp:218
bool data_available(int64_t sequence_id=0)
Check if there are data.
Definition LibMcuCBorStream.cpp:243
uint8_t get_identifier() const
Get the stream identifier of a specific instance.
Definition LibMcuCBorStream.cpp:438
uint8_t get_type() const
Get the stream type of a specific instance.
Definition LibMcuCBorStream.cpp:428
void push_packet(std::vector< std::pair< void *, int > > packet)
Add a packet to _received_packets.
Definition LibMcuCBorStream.cpp:443
RxStream & deserialize_sequence(T *items, size_t size)
Deserialize a sequence of uniform elements.
Definition CBorStream.h:375
RxStream & operator>>(uint64_t &n)
Decode uint64.
Definition LibMcuCBorStream.cpp:288
Definition CBorStream.h:81
TxStream & operator<<(const uint64_t n)
Encode uint64.
Definition LibMcuCBorStream.cpp:84
TxStream & serialize_sequence(const T *items, size_t size)
Serialize a sequence of uniform elements.
Definition CBorStream.h:212
TxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier)
Create a transmission stream.
Definition LibMcuCBorStream.cpp:25
TxStream & operator<<(const std::vector< T > v)
Encode vector.
Definition CBorStream.h:194
void end_transmission()
Tell the stream to send down the packet.
Definition LibMcuCBorStream.cpp:62
void start_transmission()
Tell the stream to create a new packet.
Definition LibMcuCBorStream.cpp:52