Tiny protocol  0.7.0
Tiny communication protocol for microcontrollers
TinyPacket.h
Go to the documentation of this file.
1 /*
2  Copyright 2016,2018 (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 namespace Tiny {
39 
44 class Packet
45 {
46 public:
53  Packet(char *buf, size_t size) { m_len = 0; m_size = size; m_buf = (uint8_t*)buf; m_uid =0; m_p=0; }
54 
58  inline void clear () { m_len = 0; m_p = 0; }
59 
65  inline void put (uint8_t byte) { m_buf[m_len++] = byte; }
66 
72  inline void put (char chr) { put((uint8_t)chr); }
73 
78  inline void put (uint16_t data) { m_buf[m_len++] = data & 0x00FF;
79  m_buf[m_len++] = data >> 8; }
80 
85  inline void put (uint32_t data) { put((uint16_t)(data & 0x0000FFFF));
86  put((uint16_t)(data >> 16)); }
87 
92  inline void put (int16_t data) { put((uint16_t)data); }
93 
98  inline void put (const char *str){ strncpy((char *)&m_buf[m_len], str, m_size - m_len);
99  m_len += strlen(str);
100  m_buf[m_len++] = 0; }
101 
106  inline void put (const Packet &pkt){ memcpy(&m_buf[m_len], pkt.m_buf, pkt.m_len); m_len += pkt.m_len; }
107 
115  inline void putUid (uint16_t uid) { m_uid = uid; }
116 
121  inline uint8_t getByte () { return m_buf[m_p++]; }
122 
127  inline char getChar () { return (char)Packet::getByte(); }
128 
133  inline uint16_t getUint16() { uint16_t t = m_buf[m_p++]; return t | ((uint16_t)m_buf[m_p++] << 8); }
134 
139  inline int16_t getInt16 () { return (int16_t)(getUint16()); }
140 
145  inline uint32_t getUint32() { return getUint16() | ((uint32_t)getUint16())<<16; }
146 
151  inline char* getString () { char *p = (char *)&m_buf[m_p]; m_p += strlen((char*)m_buf) + 1; return p; }
152 
160  inline uint16_t getUid () const { return m_uid; }
161 
166  inline size_t size () const { return m_len; }
167 
172  inline size_t maxSize () const { return m_size; }
173 
178  inline char *data () { return (char*)m_buf; }
179 
183  uint8_t &operator[] (size_t idx) { return m_buf[idx]; }
184 
189  Packet &operator= (char chr){ put(chr); return *this; }
190 
191 private:
192  friend class Proto;
193  friend class ProtoHd;
194  friend class ProtoLight;
195 
196  uint8_t* m_buf;
197  uint16_t m_uid;
198  uint8_t m_size;
199  uint8_t m_len;
200  uint8_t m_p;
201 };
202 
203 
204 } // Tiny namespace
205 
206 #endif
207 
size_t size() const
Definition: TinyPacket.h:166
void put(uint8_t byte)
Definition: TinyPacket.h:65
uint16_t getUint16()
Definition: TinyPacket.h:133
Definition: TinyLightProtocol.h:39
Definition: TinyProtocolHd.h:49
Packet(char *buf, size_t size)
Definition: TinyPacket.h:53
uint8_t getByte()
Definition: TinyPacket.h:121
void putUid(uint16_t uid)
Definition: TinyPacket.h:115
void put(const Packet &pkt)
Definition: TinyPacket.h:106
void put(uint16_t data)
Definition: TinyPacket.h:78
size_t maxSize() const
Definition: TinyPacket.h:172
Definition: TinyPacket.h:44
Packet & operator=(char chr)
Definition: TinyPacket.h:189
void put(char chr)
Definition: TinyPacket.h:72
void put(const char *str)
Definition: TinyPacket.h:98
void put(uint32_t data)
Definition: TinyPacket.h:85
char * getString()
Definition: TinyPacket.h:151
Definition: TinyLightProtocol.h:46
int16_t getInt16()
Definition: TinyPacket.h:139
Definition: TinyProtocol.h:47
void clear()
Definition: TinyPacket.h:58
uint16_t getUid() const
Definition: TinyPacket.h:160
char * data()
Definition: TinyPacket.h:178
void put(int16_t data)
Definition: TinyPacket.h:92
uint32_t getUint32()
Definition: TinyPacket.h:145
char getChar()
Definition: TinyPacket.h:127
uint8_t & operator[](size_t idx)
Definition: TinyPacket.h:183