forked from mysensors/Raspberry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyMessage.cpp
437 lines (375 loc) · 9.88 KB
/
MyMessage.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
The MySensors library adds a new layer on top of the RF24 library.
It handles radio network routing, relaying and ids.
Created by Henrik Ekblad <[email protected]>
12/10/14 - Ported to Raspberry Pi by OUJABER Mohamed <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "MyMessage.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __Raspberry_Pi
#define min(a,b) (a<b?a:b)
char * itoa(int value, char *result, int base);
char * ltoa(long value, char *result, int base);
char * dtostrf(float f, int width, int decimals, char *result);
char * utoa( unsigned int num, char *str, int radix);
char * ultoa( unsigned long num, char *str, int radix);
#endif
MyMessage::MyMessage() {
destination = 0; // Gateway is default destination
}
MyMessage::MyMessage(uint8_t _sensor, uint8_t _type) {
destination = 0; // Gateway is default destination
sensor = _sensor;
type = _type;
}
bool MyMessage::isAck() const {
return miGetAck();
}
/* Getters for payload converted to desired form */
void* MyMessage::getCustom() const {
return (void *)data;
}
const char* MyMessage::getString() const {
uint8_t payloadType = miGetPayloadType();
if (payloadType == P_STRING) {
return data;
} else {
return NULL;
}
}
// handles single character hex (0 - 15)
char MyMessage::i2h(uint8_t i) const {
uint8_t k = i & 0x0F;
if (k <= 9)
return '0' + k;
else
return 'A' + k - 10;
}
char* MyMessage::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* MyMessage::getStream(char *buffer) const {
uint8_t cmd = miGetCommand();
if ((cmd == C_STREAM) && (buffer != NULL)) {
return getCustomString(buffer);
} else {
return NULL;
}
}
char* MyMessage::getString(char *buffer) const {
uint8_t payloadType = miGetPayloadType();
if (payloadType == P_STRING) {
strncpy(buffer, data, miGetLength());
buffer[miGetLength()] = 0;
return buffer;
} else if (buffer != NULL) {
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,fPrecision,buffer);
} else if (payloadType == P_CUSTOM) {
return getCustomString(buffer);
}
return buffer;
} else {
return NULL;
}
}
uint8_t MyMessage::getByte() const {
if (miGetPayloadType() == P_BYTE) {
return data[0];
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
bool MyMessage::getBool() const {
return getInt();
}
float MyMessage::getFloat() const {
if (miGetPayloadType() == P_FLOAT32) {
return fValue;
} else if (miGetPayloadType() == P_STRING) {
return atof(data);
} else {
return 0;
}
}
long MyMessage::getLong() const {
if (miGetPayloadType() == P_LONG32) {
return lValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}
unsigned long MyMessage::getULong() const {
if (miGetPayloadType() == P_ULONG32) {
return ulValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}
int MyMessage::getInt() const {
if (miGetPayloadType() == P_INT16) {
return iValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
unsigned int MyMessage::getUInt() const {
if (miGetPayloadType() == P_UINT16) {
return uiValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
MyMessage& MyMessage::setType(uint8_t _type) {
type = _type;
return *this;
}
MyMessage& MyMessage::setSensor(uint8_t _sensor) {
sensor = _sensor;
return *this;
}
MyMessage& MyMessage::setDestination(uint8_t _destination) {
destination = _destination;
return *this;
}
// Set payload
MyMessage& MyMessage::set(void* value, uint8_t length) {
miSetPayloadType(P_CUSTOM);
miSetLength(length);
memcpy(data, value, min(length, MAX_PAYLOAD));
return *this;
}
MyMessage& MyMessage::set(const char* value) {
uint8_t length = min(strlen(value), MAX_PAYLOAD);
miSetLength(length);
miSetPayloadType(P_STRING);
strncpy(data, value, length);
return *this;
}
MyMessage& MyMessage::set(uint8_t value) {
miSetLength(1);
miSetPayloadType(P_BYTE);
data[0] = value;
return *this;
}
MyMessage& MyMessage::set(float value, uint8_t decimals) {
miSetLength(5); // 32 bit float + persi
miSetPayloadType(P_FLOAT32);
fValue=value;
fPrecision = decimals;
return *this;
}
MyMessage& MyMessage::set(unsigned long value) {
miSetPayloadType(P_ULONG32);
miSetLength(4);
ulValue = value;
return *this;
}
MyMessage& MyMessage::set(long value) {
miSetPayloadType(P_LONG32);
miSetLength(4);
lValue = value;
return *this;
}
MyMessage& MyMessage::set(unsigned int value) {
miSetPayloadType(P_UINT16);
miSetLength(2);
uiValue = value;
return *this;
}
MyMessage& MyMessage::set(int value) {
miSetPayloadType(P_INT16);
miSetLength(2);
iValue = value;
return *this;
}
#ifdef __Raspberry_Pi
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char *itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char *ltoa(long value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
long tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
char *dtostrf(float f, int width, int decimals, char *result)
{
char widths[3];
char decimalss[3];
char format[100];
itoa(width,widths,10);
itoa(decimals,decimalss,10);
strcpy(format,"%");
strcat(format,widths);
strcat(format,".");
strcat(format,decimalss);
strcat(format,"f");
sprintf(result,format,f);
return result;
}
/* Copyright (c) 2012, Peter A. Bigot <[email protected]>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the software nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
char *utoa(unsigned int num, char *str, int radix)
{
unsigned int value;
char *sp = str;
char *sp2;
value = num;
/* Store sign at start of buffer for negative base-10 values */
if (10 == radix && 0 > num) {
*sp++ = '-';
value = -num;
}
sp2 = sp;
do {
char rem = value % radix;
value /= radix;
if (10 > rem) {
*sp++ = '0' + rem;
} else {
*sp++ = 'A' + rem - 10;
}
} while (0 < value);
/* Mark end of string */
*sp-- = 0;
/* Reverse string contents (excluding sign) in place */
while (sp2 < sp) {
char tmp = *sp2;
*sp2++ = *sp;
*sp-- = tmp;
}
return str;
}
char *ultoa(unsigned long num, char *str, int radix)
{
unsigned long value;
char *sp = str;
char *sp2;
value = num;
/* Store sign at start of buffer for negative base-10 values */
if (10 == radix && 0 > num) {
*sp++ = '-';
value = -num;
}
sp2 = sp;
do {
char rem = value % radix;
value /= radix;
if (10 > rem) {
*sp++ = '0' + rem;
} else {
*sp++ = 'A' + rem - 10;
}
} while (0 < value);
/* Mark end of string */
*sp-- = 0;
/* Reverse string contents (excluding sign) in place */
while (sp2 < sp) {
char tmp = *sp2;
*sp2++ = *sp;
*sp-- = tmp;
}
return str;
}
#endif