-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubberFrame.cpp
More file actions
175 lines (139 loc) · 3.57 KB
/
ubberFrame.cpp
File metadata and controls
175 lines (139 loc) · 3.57 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "ubberFrame.h"
#include <string.h>
#define FRAME_FLAGS_OFFSET 0
#define FRAME_SOURCEID_OFFSET 1
#define FRAME_DESTID_OFFSET 2
#define FRAME_TYPE_OFFSET 3
#define FRAME_PAYLOADSIZE_OFFSET 4
#define FRAME_PAYLOAD_OFFSET 5
//<<constructor>> Create a empty UbberFrame
UbberFrame::UbberFrame()
{
this->data.payloadSize = 0;
}
//<<desctructor>>
UbberFrame::~UbberFrame()
{
}
//<<constructor>> Create a frame fr
UbberFrame::UbberFrame(const uint8_t data[], const unsigned int length)
{
frameFromChar(data, length);
}
int UbberFrame::frameFromChar(const uint8_t data[], const unsigned int length)
{
if(length < HEADER_SIZE || length != HEADER_SIZE + data[FRAME_PAYLOADSIZE_OFFSET])
return -1;
this->data.flags = data[FRAME_FLAGS_OFFSET];
this->data.sourceID = data[FRAME_SOURCEID_OFFSET];
this->data.destID = data[FRAME_DESTID_OFFSET];
this->data.type = data[FRAME_TYPE_OFFSET];
this->data.payloadSize = data[FRAME_PAYLOADSIZE_OFFSET] < MAX_PAYLOAD_SIZE ? data[FRAME_PAYLOADSIZE_OFFSET] : 256;
if(this->data.payloadSize)
memcpy(this->data.payload, &data[FRAME_PAYLOAD_OFFSET], this->data.payloadSize);
else
this->data.payload[0] = '\0';
return 0;
}
const uint8_t * UbberFrame::frameToChar()
{
return this->data.data;
}
uint8_t UbberFrame::operator[](int index)
{
return this->data.data[index];
}
#define GEN_DATA_GETTER(__type, __name, __attribute) \
__type UbberFrame::__name () { \
return this->data.__attribute ; \
}
#define GEN_DATA_SETTER(__type, __name, __attribute) \
void UbberFrame::__name (__type __attribute) { \
this->data.__attribute = __attribute; \
}
GEN_DATA_GETTER(uint8_t, getFlags, flags)
GEN_DATA_GETTER(uint8_t, getSourceID, sourceID)
GEN_DATA_GETTER(uint8_t, getDestID, destID)
GEN_DATA_GETTER(uint8_t, getType, type)
GEN_DATA_GETTER(uint8_t, getPayloadSize, payloadSize)
GEN_DATA_GETTER(const uint8_t *, getPayload, payload)
GEN_DATA_SETTER(uint8_t, setFlags, flags)
GEN_DATA_SETTER(uint8_t, setSourceID, sourceID)
GEN_DATA_SETTER(uint8_t, setDestID, destID)
GEN_DATA_SETTER(uint8_t, setType, type)
int UbberFrame::setPayload(const uint8_t payload[], unsigned int size)
{
if(size > MAX_PAYLOAD_SIZE)
return -1;
this->data.payloadSize = size;
memcpy(this->data.payload, payload, this->data.payloadSize);
return 0;
}
unsigned int UbberFrame::getLength()
{
return HEADER_SIZE + this->data.payloadSize;
}
const char * UbberFrame::typeString(enum type type)
{
switch(type) {
case PAYLOAD_DEFINED:
return "PAYLOAD_DEFINED";
case PAUSE:
return "PAUSE";
case RENCONTRE:
return "RENCONTRE";
case REPAS:
return "REPAS";
case CLOPE:
return "CLOPE";
case CAFE:
return "CAFE";
case PISCINE:
return "PISCINE";
case EPICURIA:
return "EPICURIA";
case VELO:
return "VELO";
case ACQUITTEMENT:
return "AQUITTEMENT";
case MAJ:
return "MISE A JOUR";
default:
return NULL;
}
}
const char * UbberFrame::getTypeString()
{
return typeString((enum type)this->data.type);
}
const char * UbberFrame::iDString(enum id id)
{
switch(id) {
case BEN_M:
return "Benjamin Mingez";
case CLEMENT:
return "Clément Léger";
case JEJE:
return "Jérome Reybert";
case MARC:
return "Marc Poulhiès";
case GUILLAUME_S:
return "Guillaume Sarrazin";
case DAMS:
return "Damien Cottier";
case GUILLAUME_L:
return "Guillaume Lager";
case ALL:
return "Broadcast";
default:
return NULL;
}
}
const char * UbberFrame::getSourceIDString()
{
return iDString((enum id)this->data.sourceID);
}
const char * UbberFrame::getDestIDString()
{
return iDString((enum id)this->data.destID);
}