tinyproto
TinyPacket.h
Go to the documentation of this file.
1 /*
2  Copyright 2016-2024 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  GNU General Public License Usage
7 
8  Protocol Library is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Protocol Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
20 
21  Commercial License Usage
22 
23  Licensees holding valid commercial Tiny Protocol licenses may use this file in
24  accordance with the commercial license agreement provided in accordance with
25  the terms contained in a written agreement between you and Alexey Dynda.
26  For further information contact via email on github account.
27 */
28 
37 #ifndef _TINY_PACKET_H_
38 #define _TINY_PACKET_H_
39 
40 #ifdef ARDUINO
41 #include <HardwareSerial.h>
42 #else
43 #include <stdint.h>
44 #include <string.h>
45 #endif
46 
47 #include <stdio.h>
48 
49 namespace tinyproto
50 {
51 
56 class IPacket
57 {
58 public:
65  IPacket(char *buf, int size)
66  {
67  m_size = size;
68  m_buf = reinterpret_cast<uint8_t *>(buf);
69  }
70 
71  IPacket(const IPacket &packet)
72  {
73  m_len = packet.m_len;
74  m_size = packet.m_size;
75  m_buf = packet.m_buf;
76  }
77 
78  IPacket()
79  {
80  }
81 
85  virtual ~IPacket() = default;
86 
90  void clear()
91  {
92  m_len = 0;
93  m_p = 0;
94  }
95 
101  void put(uint8_t byte)
102  {
103  m_buf[m_len++] = byte;
104  }
105 
111  void put(char chr)
112  {
113  put(static_cast<uint8_t>(chr));
114  }
115 
120  inline void put(uint16_t data)
121  {
122  m_buf[m_len++] = data & 0x00FF;
123  m_buf[m_len++] = data >> 8;
124  }
125 
130  inline void put(uint32_t data)
131  {
132  put((uint16_t)(data & 0x0000FFFF));
133  put((uint16_t)(data >> 16));
134  }
135 
140  inline void put(int16_t data)
141  {
142  put((uint16_t)data);
143  }
144 
149  inline void put(const char *str)
150  {
151  int room = m_size - m_len - 1;
152  if ( !room ) return;
153  int strSize = static_cast<int>(strlen(str));
154  strncpy((char *)&m_buf[m_len], str, room);
155  m_len += strSize < room ? strSize : room;
156  m_buf[m_len++] = 0;
157  }
158 
163  inline void put(const IPacket &pkt)
164  {
165  memcpy(&m_buf[m_len], pkt.m_buf, pkt.m_len);
166  m_len += pkt.m_len;
167  }
168 
173  inline uint8_t getByte()
174  {
175  return m_buf[m_p++];
176  }
177 
182  inline char getChar()
183  {
184  return (char)IPacket::getByte();
185  }
186 
191  inline uint16_t getUint16()
192  {
193  uint16_t t = m_buf[m_p++];
194  return t | ((uint16_t)m_buf[m_p++] << 8);
195  }
196 
201  inline int16_t getInt16()
202  {
203  return (int16_t)(getUint16());
204  }
205 
210  inline uint32_t getUint32()
211  {
212  return getUint16() | ((uint32_t)getUint16()) << 16;
213  }
214 
219  inline char *getString()
220  {
221  char *p = (char *)&m_buf[m_p];
222  m_p += static_cast<int>(strlen(p)) + 1;
223  return p;
224  }
225 
230  inline int size() const
231  {
232  return m_len;
233  }
234 
239  inline int maxSize() const
240  {
241  return m_size;
242  }
243 
248  inline char *data() const
249  {
250  return (char *)m_buf;
251  }
252 
257  inline size_t availableBytes()
258  {
259  return (size_t)(m_len - m_p);
260  }
261 
265  uint8_t &operator[](int idx)
266  {
267  return m_buf[idx];
268  }
269 
274  IPacket &operator=(const IPacket &source)
275  {
276  m_size = source.m_size;
277  m_buf = source.m_buf;
278  return *this;
279  }
280 
285  void allocate(int bytes)
286  {
287  m_len += bytes;
288  }
289 
290 private:
291  friend class Hdlc;
292  friend class IFd;
293  friend class Light;
294  friend class Proto;
295 
296  uint8_t *m_buf = nullptr;
297  IPacket *m_next = nullptr; // For organizing ring buffers
298  IPacket *m_prev = nullptr; // For organizing ring buffers
299  int m_size = 0; // maximum space available for payload data
300  int m_len = 0; // length of payload data
301  int m_p = 0; // current pointer
302 };
303 
308 template <int S> class StaticPacket: public IPacket
309 {
310 public:
315  : IPacket(m_data, S)
316  {
317  }
318 
319 private:
320  char m_data[S]{};
321 };
322 
327 class HeapPacket: public IPacket
328 {
329 public:
334  explicit HeapPacket(int size)
335  : IPacket((char *)(new uint8_t[size]), size)
336  {
337  }
338 
339  explicit HeapPacket( const IPacket &src )
340  : IPacket((char *)(new uint8_t[src.size()]), src.size())
341  {
342  memcpy(data(), src.data(), src.size());
343  }
344 
345  ~HeapPacket()
346  {
347  delete[](uint8_t *) data();
348  }
349 
350 private:
351 };
352 
353 } // namespace tinyproto
354 
355 #endif
HeapPacket(int size)
Creates packet with dynamically allocated buffer.
Definition: TinyPacket.h:334
ProtoLight class incapsulates Protocol functionality.
Definition: TinyLightProtocol.h:61
void put(char chr)
Puts next char to the packet.
Definition: TinyPacket.h:111
int16_t getInt16()
Reads next signed 16-bit integer from the packet.
Definition: TinyPacket.h:201
uint8_t getByte()
Reads next byte from the packet.
Definition: TinyPacket.h:173
void allocate(int bytes)
Allocates space inside the packet buffer, the next data will be written after allocated block...
Definition: TinyPacket.h:285
void put(const char *str)
Puts next null-terminated string to the packet.
Definition: TinyPacket.h:149
Describes packet entity and provides API methods to manipulate the packet.
Definition: TinyPacket.h:56
StaticPacket()
Creates IPacket instance with statically allocated buffer.
Definition: TinyPacket.h:314
void put(uint32_t data)
Puts next 32-bit unsigned integer to the packet.
Definition: TinyPacket.h:130
virtual ~IPacket()=default
Destroys the object.
void put(int16_t data)
Puts next 16-bit signed integer to the packet.
Definition: TinyPacket.h:140
uint16_t getUint16()
Reads next unsigned 16-bit integer from the packet.
Definition: TinyPacket.h:191
char getChar()
Reads next character from the packet.
Definition: TinyPacket.h:182
IPacket & operator=(const IPacket &source)
Assign operator doesn&#39;t copy the data from the source packet, but it copies only pointers.
Definition: TinyPacket.h:274
Class which allocated buffer for packet dynamically.
Definition: TinyPacket.h:327
IPacket(char *buf, int size)
Creates packet object.
Definition: TinyPacket.h:65
IFd class incapsulates Full Duplex Protocol functionality.
Definition: TinyProtocolFd.h:63
uint32_t getUint32()
Reads next unsigned 32-bit integer from the packet.
Definition: TinyPacket.h:210
int size() const
Returns size of payload data in the received packet.
Definition: TinyPacket.h:230
int maxSize() const
Returns maximum size of packet buffer.
Definition: TinyPacket.h:239
char * getString()
Reads zero-terminated string from the packet.
Definition: TinyPacket.h:219
Template class to create packet with static allocation of buffer Use this class for microcontrollers ...
Definition: TinyPacket.h:308
void clear()
Clears Packet state.
Definition: TinyPacket.h:90
uint8_t & operator[](int idx)
You may refer to Packet payload data directly by using operator [].
Definition: TinyPacket.h:265
void put(const IPacket &pkt)
Adds data from packet to the new packet being built.
Definition: TinyPacket.h:163
Definition: TinySerial.cpp:22
Hdlc class incapsulates hdlc Protocol functionality.
Definition: TinyProtocolHdlc.h:62
size_t availableBytes()
Returns size of remaining bytes (not yet accessed through get*()) in the received packet...
Definition: TinyPacket.h:257
Definition: TinyProtocol.h:59
void put(uint8_t byte)
Puts next byte to the packet.
Definition: TinyPacket.h:101
char * data() const
Returns pointer to payload data in the received packet.
Definition: TinyPacket.h:248
void put(uint16_t data)
Puts next 16-bit unsigned integer to the packet.
Definition: TinyPacket.h:120