-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packet.h
87 lines (74 loc) · 2.68 KB
/
Packet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Packet
Copyright(C) 2020 Blackhack
This program is free software : you can redistribute itand /or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see < http://www.gnu.org/licenses/>.
*/
#ifndef Packet_h__
#define Packet_h__
// Arduino have its own definitions of the functions we need.
#ifdef ARDUINO
#include <Arduino.h>
#else
#include <cstring>
#include <cstdint>
#include <stdlib.h>
#endif
class Packet
{
public:
// Two first bytes in the header, those magic numbers are just a basic form of protocol identification.
static const uint8_t byte_check_one = 0x41;
static const uint8_t byte_check_two = 0xB5;
static const size_t max_body_length = 1020;
static const size_t fixed_header_length = 4;
static const size_t max_data_length = max_body_length + fixed_header_length;
static const size_t fixed_float_size = 4;
static const size_t fixed_double_size = 8;
static const size_t fixed_opcode_size = 1;
Packet(uint8_t opcode);
Packet(uint8_t* data);
Packet(Packet& packet);
uint8_t GetOpcodeId() { return _opcode; }
size_t GetSize() { return _data_size; }
void ResetDataPointer() { _data_pointer = fixed_header_length + fixed_opcode_size; }
uint8_t* GetDataRaw() { return _data; }
void AppendUInt8(uint8_t value);
void AppendUInt16(uint16_t value);
void AppendUInt32(uint32_t value);
void AppendUInt64(uint64_t value);
void AppendInt8(int8_t value);
void AppendInt16(int16_t value);
void AppendInt32(int32_t value);
void AppendInt64(int64_t value);
void AppendFloat(float value);
void AppendDouble(double value);
void AppendString(const char* str);
uint8_t ReadUInt8();
uint16_t ReadUInt16();
uint32_t ReadUInt32();
uint64_t ReadUInt64();
int8_t ReadInt8();
int16_t ReadInt16();
int32_t ReadInt32();
int64_t ReadInt64();
float ReadFloat();
double ReadDouble();
char* ReadString();
private:
void UpdateHeader();
private:
uint8_t _opcode;
uint8_t _data[max_data_length] = { 0 };
size_t _data_pointer = fixed_header_length;
size_t _data_size;
};
#endif // Packet_h__