Tiny protocol  0.9.3
Tiny communication protocol for microcontrollers
TinyPacket.h
Go to the documentation of this file.
1 /*
2  Copyright 2016-2019 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  Protocol Library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Protocol Library 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 Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
28 #ifndef _TINY_PACKET_H_
29 #define _TINY_PACKET_H_
30 
31 #ifdef ARDUINO
32 # include <HardwareSerial.h>
33 #else
34 # include <stdint.h>
35 # include <string.h>
36 #endif
37 
38 #include <stdio.h>
39 
40 namespace Tiny {
41 
46 class IPacket
47 {
48 public:
55  IPacket(char *buf, size_t size) { m_len = 0; m_size = size; m_buf = (uint8_t*)buf; m_p=0; }
56 
60  virtual ~IPacket() = default;
61 
65  void clear () { m_len = 0; m_p = 0; }
66 
72  void put (uint8_t byte) { m_buf[m_len++] = byte; }
73 
79  void put (char chr) { put((uint8_t)chr); }
80 
85  inline void put (uint16_t data) { m_buf[m_len++] = data & 0x00FF;
86  m_buf[m_len++] = data >> 8; }
87 
92  inline void put (uint32_t data) { put((uint16_t)(data & 0x0000FFFF));
93  put((uint16_t)(data >> 16)); }
94 
99  inline void put (int16_t data) { put((uint16_t)data); }
100 
105  inline void put (const char *str){ strncpy((char *)&m_buf[m_len], str, m_size - m_len);
106  m_len += strlen(str);
107  m_buf[m_len++] = 0; }
108 
113  inline void put (const IPacket &pkt){ memcpy(&m_buf[m_len], pkt.m_buf, pkt.m_len); m_len += pkt.m_len; }
114 
119  inline uint8_t getByte () { return m_buf[m_p++]; }
120 
125  inline char getChar () { return (char)IPacket::getByte(); }
126 
131  inline uint16_t getUint16() { uint16_t t = m_buf[m_p++]; return t | ((uint16_t)m_buf[m_p++] << 8); }
132 
137  inline int16_t getInt16 () { return (int16_t)(getUint16()); }
138 
143  inline uint32_t getUint32() { return getUint16() | ((uint32_t)getUint16())<<16; }
144 
149  inline char* getString () { char *p = (char *)&m_buf[m_p]; m_p += strlen(p) + 1; return p; }
150 
155  inline size_t size () const { return m_len; }
156 
161  inline size_t maxSize () const { return m_size; }
162 
167  inline char *data () { return (char*)m_buf; }
168 
172  uint8_t &operator[] (size_t idx) { return m_buf[idx]; }
173 
174 private:
175  friend class ProtoHd;
176  friend class IProtoFd;
177  friend class ProtoLight;
178 
179  uint8_t* m_buf;
180  int m_size;
181  int m_len;
182  int m_p;
183 };
184 
189 template <size_t S>
190 class Packet: public IPacket
191 {
192 public:
196  Packet(): IPacket(m_data, S) {}
197 
198 private:
199  char m_data[S];
200 };
201 
206 class PacketD: public IPacket
207 {
208 public:
213  PacketD(int size): IPacket((char *)(new uint8_t[size]), size) {}
214 
215  ~PacketD() { delete[] (uint8_t *)data(); }
216 
217 private:
218 };
219 
220 
221 } // Tiny namespace
222 
223 #endif
224 
size_t size() const
Definition: TinyPacket.h:155
Packet()
Definition: TinyPacket.h:196
Definition: TinyLightProtocol.h:39
char * getString()
Definition: TinyPacket.h:149
Definition: TinyProtocolHd.h:54
void clear()
Definition: TinyPacket.h:65
void put(uint8_t byte)
Definition: TinyPacket.h:72
void put(int16_t data)
Definition: TinyPacket.h:99
virtual ~IPacket()=default
int16_t getInt16()
Definition: TinyPacket.h:137
char * data()
Definition: TinyPacket.h:167
uint8_t getByte()
Definition: TinyPacket.h:119
IPacket(char *buf, size_t size)
Definition: TinyPacket.h:55
Definition: TinyPacket.h:190
char getChar()
Definition: TinyPacket.h:125
Definition: TinyPacket.h:46
Definition: TinyLightProtocol.h:51
uint8_t & operator[](size_t idx)
Definition: TinyPacket.h:172
Definition: TinyPacket.h:206
Definition: TinyProtocolFd.h:53
PacketD(int size)
Definition: TinyPacket.h:213
void put(uint32_t data)
Definition: TinyPacket.h:92
void put(char chr)
Definition: TinyPacket.h:79
uint16_t getUint16()
Definition: TinyPacket.h:131
void put(uint16_t data)
Definition: TinyPacket.h:85
void put(const IPacket &pkt)
Definition: TinyPacket.h:113
size_t maxSize() const
Definition: TinyPacket.h:161
uint32_t getUint32()
Definition: TinyPacket.h:143
void put(const char *str)
Definition: TinyPacket.h:105