-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.cpp
413 lines (363 loc) · 9.14 KB
/
Message.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
/**
* Message.cpp - The official Lithne library.
*
* Copyright (c) 2011-2012
* Serge Offermans, Remco Magielse
* {s.a.m.offermans, r.magielse}@tue.nl
* Intelligent Lighting Institute (ILI), TU/e.
*
* All rights reserved. LAST UPDATE: 01-04-2012
**/
/* The following code makes the Library compatible with Arduino 1.0 */
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Message.h"
#include "LithneXBee.h"
#include "Node.h"
#include "LithneDefinitions.h"
/** Default Message constructor */
Message::Message()
{
payloadSize = MSG_HEADER_SIZE; //Contains the number of bytes in the payload including scope and function
sender16 = 0xFFFE; //Contains the 16 bit address of the Sender
recipient16 = 0xFFFE; //Contains the 16 bit address of the Recipient
sender64 = XBeeAddress64( 0x0, 0xFFFF); // Contains the 64 bit address of the Sender
recipient64 = XBeeAddress64( 0x0, 0xFFFF); //Contains the 64 bit address of the Recipient
for(int i=MSG_HEADER_SIZE;i<MAXIMUM_PAYLOAD_BYTES;i++)
{
payload[i] = 0;
}
}
Message::~Message()
{
}
/*
__ _____ ___ ___
\ \ / / _ \_ _| \
\ V / (_) | || |) |
\_/ \___/___|___/
*/
/** Set the recipient for the message using 64 bit Address **/
XBeeAddress64 Message::toXBeeAddress64( XBeeAddress64 _addr64 )
{
return recipient64 = _addr64;
}
XBeeAddress64 Message::toXBeeAddress64( )
{
return recipient64;
}
/** DEPRECATED **/
void Message::setRecipient64( XBeeAddress64 _addr64 )
{
toXBeeAddress64( _addr64 );
}
/** Return the receiving XBee address **/
XBeeAddress64 Message::getRecipient64()
{
return toXBeeAddress64();
}
/** Set the recipient for the message using 16 bit Address, as this is varying, you wont use this in your code **/
uint16_t Message::toXBeeAddress16( uint16_t _addr16 )
{
return recipient16 = _addr16;
}
/** return the 16 bit address of the recipient **/
uint16_t Message::toXBeeAddress16( )
{
return recipient16;
}
/** DEPRECATED **/
void Message::setRecipient16( uint16_t _addr16 )
{
toXBeeAddress16( _addr16 );
}
/** DEPRECATED **/
uint16_t Message::getRecipient16()
{
return recipient16;
}
/** Set the recipient for the message using a Node pointer **/
void Message::setRecipient( Node * node )
{
if (node != NULL)
{
recipient16 = node->getAddress16();
recipient64 = node->getAddress64();
}
}
/** Store the 64 bit address of the sender of the message **/
/** Return the transmitting XBee address **/
XBeeAddress64 Message::fromXBeeAddress64( XBeeAddress64 _addr64 )
{
return sender64 = _addr64;
}
/** Return the transmitting XBee address **/
XBeeAddress64 Message::fromXBeeAddress64( )
{
return sender64;
}
/** DEPRECATED **/
void Message::setSender64( XBeeAddress64 _addr64 )
{
sender64 = _addr64;
}
/** DEPRECATED **/
XBeeAddress64 Message::getSender64( )
{
return sender64;
}
/** Returns the 16 bit address of the sender **/
/** Store the 16-bit address of the sender **/
uint16_t Message::fromXBeeAddress16( uint16_t _addr16 )
{
return sender16 = _addr16;
}
/** Store the 16-bit address of the sender **/
uint16_t Message::fromXBeeAddress16()
{
return sender16;
}
/** DEPRECATED **/
uint16_t Message::getSender16()
{
return fromXBeeAddress16();
}
/** DEPRECATED **/
void Message::setSender16( uint16_t _addr16 )
{
sender16 = _addr16;
}
/*
void Message::setRecipient( uint8_t _id )
{
Node * node = getNodeByID(_id);
if (node != NULL)
{
recipient16 = node->getAddress16();
recipient64 = node->getAddress64();
}
} */
/** Clear all message arguments (set them to 0) **/
void Message::clearArguments()
{
for(int i=MSG_HEADER_SIZE; i<MAXIMUM_PAYLOAD_BYTES; i++)
{
payload[i] = 0;
}
payloadSize = MSG_HEADER_SIZE;
}
/** Set the function the message should call **/
void Message::setFunction( String _func )
{
setFunction( hash(_func) );
}
/** Set the function the message should call **/
void Message::setFunction( uint16_t _func )
{
payload[FUNCTION_MSB] = ( _func >> 8 ) & 0xFF;
payload[FUNCTION_LSB] = _func & 0xFF;
/*Serial.print("FUNC:");
Serial.print(getFunction());
Serial.print(" from: ");
Serial.println(_func);*/
}
/** Set both the 16-bit and 64-bit address of the sender in one go **/
void Message::setSender( uint16_t _addr16, XBeeAddress64 _addr64 )
{
fromXBeeAddress16( _addr16 );
fromXBeeAddress64( _addr64 );
}
/** Sets the scope for this message as a string (this is hashed into an int) **/
void Message::setScope( String _scope )
{
setScope( hash( _scope ) );
}
/** Sets the scope for this message as an int **/
void Message::setScope( uint16_t _scope )
{
payload[SCOPE_MSB] = ( _scope >> 8 ) & 0xFF;
payload[SCOPE_LSB] = _scope & 0xFF;
/* Here we can add a line that sets the recipient
to the BROADCAST address if no address has been
set yet. This will make sure the message can
be delivered. */
}
/*
___ ___ ___ _ ___ _ _ _
| _ )/ _ \ / _ \| | | __| /_\ | \| |
| _ \ (_) | (_) | |__| _| / _ \| .` |
|___/\___/ \___/|____|___/_/ \_\_|\_|
*/
/** Add a byte argument to the list of arguments **/
bool Message::addByte( uint8_t arg )
{
if( payloadSize+1 < MAXIMUM_PAYLOAD_BYTES )
{
payload[payloadSize] = arg;
payloadSize++;
return true;
}
else
{
return false;
}
}
/** Add an argument to the list of arguments **/
bool Message::addArgument( uint16_t arg )
{
if( payloadSize+2 < MAXIMUM_PAYLOAD_BYTES )
{
payload[payloadSize] = ( arg >> 8 ) & 0xFF;
payload[payloadSize+1] = arg & 0xFF;
payloadSize+=2;
return true;
}
else
{
return false;
}
}
/** Set a String argument. All existing arguments will be overwritten. The string can be retrieved on the other end using getStringArgument() **/
bool Message::setStringArgument( String arg )
{
//First we clear the arguments
clearArguments();
for( uint8_t i=0; i < arg.length(); i++ ) //Loop through the entire argument
{
addByte(arg[i]);
if ( payloadSize >= MAXIMUM_PAYLOAD_BYTES)
{
break;
}
}
if( MSG_HEADER_SIZE+sizeof(arg) <= MAXIMUM_PAYLOAD_BYTES )
{
return true;
}
else
{
return false;
}
}
/* How to add multiple arguments to the array? Discuss this with Elco
boolean addArg( uint16_t* args ) //Add an array of arguments to the array
{
}
*/
/** FunctionIs returns true if the function of the message is the same as the function that is provided as an argument to this function.
Internally, this function compares a hash() of the provided argument with the internally stored function ID
*/
boolean Message::functionIs( String _func )
{
if ( hash( _func ) == getFunction() )
{
return true;
}
else
{
return false;
}
}
/*
___ _ _ _____ ___ ___ ___ ___
|_ _| \| |_ _| __/ __| __| _ \
| || .` | | | | _| (_ | _|| /
|___|_|\_| |_| |___\___|___|_|_\
*/
/** Return the number of bytes in the payload **/
uint8_t Message::getNumberOfBytes()
{
return payloadSize-MSG_HEADER_SIZE;
}
/** Return the number of arguments **/
uint8_t Message::getNumberOfArguments()
{
return (payloadSize-MSG_HEADER_SIZE)/2;
}
/** Return the payload size **/
uint8_t Message::getPayloadSize()
{
return payloadSize;
}
/** Returns the byte at a position in the data, excluding scope and function **/
uint8_t Message::getByte( uint8_t _pos )
{
if (_pos >= 0 && _pos < payloadSize - MSG_HEADER_SIZE )
{
return payload[_pos+MSG_HEADER_SIZE];
}
else
{
return 0;
}
}
uint8_t * Message::getPayload()
{
return payload;
}
/** Returns the byte at a position in the total payload - including scope and function **/
uint8_t Message::getPayloadByte( uint8_t _pos )
{
if (_pos >= 0 && _pos < payloadSize )
{
return payload[_pos];
}
else
{
return 0;
}
}
/** Returns a hash code based on the name of a group **/
/* -- This function is copied into the Message class; in case of alternations; do it in both classes */
uint16_t Message::hash( String _group )
{
uint16_t wordValue = 0;
for(uint16_t i=0; i < _group.length(); i++ )
{
wordValue += _group.charAt(i);
}
wordValue += _group.length();
wordValue += _group.charAt(0);
wordValue += _group.charAt( _group.length() - 1 );
return wordValue;
}
/** return the argument at the specified position **/
uint16_t Message::getArgument( uint8_t position )
{
position = constrain( position, 0, MAXIMUM_PAYLOAD_BYTES );
uint16_t arg = NO_ARGUMENT;
if ( position < getNumberOfArguments() )
{
uint16_t argMSB = payload[ MSG_HEADER_SIZE+(position*2) ] << 8;
uint8_t argLSB = payload[ MSG_HEADER_SIZE+(position*2)+1];
arg = argMSB + argLSB;
}
return arg;
}
/** return the argument at the specified position **/
String Message::getStringArgument( )
{
char characterArr[ getNumberOfBytes()+1 ]; // +1 is necessary for the '/0' terminator in Arduino strings, this is automatically added
for (uint8_t i = 0; i < sizeof(characterArr); i++ )
{
characterArr[i] = (char)(payload[MSG_HEADER_SIZE+i]);
}
return characterArr;
}
/** Return the functionID of the message **/
uint16_t Message::getFunction()
{
uint16_t funcMSB = payload[ FUNCTION_MSB ] << 8;
uint16_t funcLSB = payload[ FUNCTION_LSB ];
return funcMSB + funcLSB;
}
/** Returns the scope of the message **/
uint16_t Message::getScope()
{
uint16_t scopeMSB = payload[ SCOPE_MSB ] << 8;
uint16_t scopeLSB = payload[ SCOPE_LSB ];
return scopeMSB + scopeLSB;
}