-
Notifications
You must be signed in to change notification settings - Fork 4
/
McMessage.cpp
344 lines (296 loc) · 7.7 KB
/
McMessage.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2015-2017 Jeeva Kandasamy ([email protected])
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Jeeva Kandasamy (jkandasa)
* @since 1.0.0
*/
#include "McMessage.h"
#include <stdio.h>
#include <stdlib.h>
int mmax (int a, int b) { return a > b ? a : b; }
int mmin (int a, int b) { return a < b ? a : b; }
McMessage::McMessage() {
clear();
}
McMessage::McMessage(char *_sensorId, char *_type, char *_subType) {
clear();
setSensorId(_sensorId);
setType(_type);
setSubType(_subType);
}
void McMessage::clear() {
/*
(void)memset(sensorId, 0u, sizeof(sensorId));
(void)memset(type, 0u, sizeof(type));
(void)memset(subType, 0u, sizeof(subType));
(void)memset(data, 0u, sizeof(data));
*/
sensorId = NULL;
type = NULL;
subType = NULL;
ack = 0;
(void)memset(data, 0u, sizeof(data));
}
/* Getters for payload converted to desired form */
void* McMessage::getCustom() const {
return (void *)data;
}
const char* McMessage::getString() const {
uint8_t payloadType = miGetPayloadType();
if (payloadType == P_STRING) {
return data;
} else {
return NULL;
}
}
// handles single character hex (0 - 15)
char McMessage::i2h(uint8_t i) const {
uint8_t k = i & 0x0F;
if (k <= 9)
return '0' + k;
else
return 'A' + k - 10;
}
char* McMessage::getCustomString(char *buffer) const {
for (uint8_t i = 0; i < miGetLength(); i++)
{
buffer[i * 2] = i2h(data[i] >> 4);
buffer[(i * 2) + 1] = i2h(data[i]);
}
buffer[miGetLength() * 2] = '\0';
return buffer;
}
char* McMessage::getStream(char *buffer) const {
if (strcmp(type, C_STREAM) == 0 && (buffer != NULL)) {
return getCustomString(buffer);
} else {
return NULL;
}
}
char* McMessage::getString(char *buffer) const {
uint8_t payloadType = miGetPayloadType();
if (buffer != NULL) {
if (payloadType == P_STRING) {
strncpy(buffer, data, miGetLength());
buffer[miGetLength()] = 0;
} else if (payloadType == P_BYTE) {
itoa(bValue, buffer, 10);
} else if (payloadType == P_INT16) {
itoa(iValue, buffer, 10);
} else if (payloadType == P_UINT16) {
utoa(uiValue, buffer, 10);
} else if (payloadType == P_LONG32) {
ltoa(lValue, buffer, 10);
} else if (payloadType == P_ULONG32) {
ultoa(ulValue, buffer, 10);
} else if (payloadType == P_FLOAT32) {
dtostrf(fValue,2,mmin(fPrecision, 8),buffer);
} else if (payloadType == P_CUSTOM) {
return getCustomString(buffer);
}
return buffer;
} else {
return NULL;
}
}
bool McMessage::getBool() const {
return getByte();
}
uint8_t McMessage::getByte() const {
if (miGetPayloadType() == P_BYTE) {
return data[0];
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
float McMessage::getFloat() const {
if (miGetPayloadType() == P_FLOAT32) {
return fValue;
} else if (miGetPayloadType() == P_STRING) {
return atof(data);
} else {
return 0;
}
}
int32_t McMessage::getLong() const {
if (miGetPayloadType() == P_LONG32) {
return lValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}
uint32_t McMessage::getULong() const {
if (miGetPayloadType() == P_ULONG32) {
return ulValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}
int16_t McMessage::getInt() const {
if (miGetPayloadType() == P_INT16) {
return iValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
uint16_t McMessage::getUInt() const {
if (miGetPayloadType() == P_UINT16) {
return uiValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
McMessage& McMessage::setType(char *_type) {
//type = (char *) malloc(strlen(_type));
//strcpy(type, _type);
type = _type;
return *this;
}
McMessage& McMessage::setSensorId(char *_sensorId) {
//sensorId = (char *) malloc(strlen(_sensorId));
//strcpy(sensorId, _sensorId);
sensorId = _sensorId;
return *this;
}
McMessage& McMessage::setSubType(char *_subType) {
//subType = (char *) malloc(strlen(_subType));
//strcpy(subType, _subType);
subType = _subType;
return *this;
}
// Set payload
McMessage& McMessage::set(void* value, uint8_t length) {
uint8_t payloadLength = value == NULL ? 0 : mmin(length, MAX_PAYLOAD);
miSetLength(payloadLength);
miSetPayloadType(P_CUSTOM);
memcpy(data, value, payloadLength);
return *this;
}
McMessage& McMessage::set(const char* value) {
uint8_t length = value == NULL ? 0 : mmin(strlen(value), MAX_PAYLOAD);
miSetLength(length);
miSetPayloadType(P_STRING);
if (length) {
strncpy(data, value, length);
}
// null terminate string
data[length] = 0;
return *this;
}
McMessage& McMessage::set(bool value) {
miSetLength(1);
miSetPayloadType(P_BYTE);
data[0] = value;
return *this;
}
McMessage& McMessage::set(uint8_t value) {
miSetLength(1);
miSetPayloadType(P_BYTE);
data[0] = value;
return *this;
}
McMessage& McMessage::set(float value, uint8_t decimals) {
miSetLength(5); // 32 bit float + persi
miSetPayloadType(P_FLOAT32);
fValue=value;
fPrecision = decimals;
return *this;
}
McMessage& McMessage::set(uint32_t value) {
miSetPayloadType(P_ULONG32);
miSetLength(4);
ulValue = value;
return *this;
}
McMessage& McMessage::set(int32_t value) {
miSetPayloadType(P_LONG32);
miSetLength(4);
lValue = value;
return *this;
}
McMessage& McMessage::set(uint16_t value) {
miSetPayloadType(P_UINT16);
miSetLength(2);
uiValue = value;
return *this;
}
McMessage& McMessage::set(int16_t value) {
miSetPayloadType(P_INT16);
miSetLength(2);
iValue = value;
return *this;
}
McMessage& McMessage::update(char *_sensorId, char *_type, char *_subType, int8_t ack){
setSensorId(_sensorId);
setType(_type);
setSubType(_subType);
setAck(ack);
set(""); // set payload EMPTY string
}
bool McMessage::isTypeOf(char *_type) {
return strcmp(type, _type) == 0;
}
bool McMessage::isTypeOf(char *_type, char *_subType) {
return strcmp(type, _type) == 0 && strcmp(subType, _subType) == 0;
}
bool McMessage::isSubTypeOf(char *_subType) {
return strcmp(subType, _subType) == 0;
}
bool McMessage::isSensorOf(char *_sensorId) {
return strcmp(sensorId, _sensorId) == 0;
}
bool McMessage::isTypeOf(String _type) {
return isTypeOf(_type.c_str());
}
bool McMessage::isTypeOf(String _type, String _subType) {
return isTypeOf(_type.c_str(), _subType.c_str());
}
bool McMessage::isSubTypeOf(String _subType) {
return isSubTypeOf(_subType.c_str());
}
bool McMessage::isSensorOf(String _sensorId) {
return isSensorOf(_sensorId.c_str());
}
bool McMessage::isValid() {
return valid;
}
McMessage& McMessage::setAck(uint8_t _ack) {
ack = _ack;
return *this;
}
bool McMessage::isAckRequest(){
return ack == ACK_REQUEST;
}
bool McMessage::isAckResponse(){
return ack == ACK_RESPONSE;
}
#ifdef ENABLE_TRACE
void McMessage::printOnSerial() {
char *_tmpPayload = "";
MC_SERIAL.printf("MC[T]: McMessage(SensorId:%s, Type:%s, SubType:%s, Payload:{%s})\n", sensorId, type, subType, getString(_tmpPayload));
}
#endif