forked from bderaeve/waspmote-api-v.030
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaspXBeeDM.cpp
533 lines (485 loc) · 13.8 KB
/
WaspXBeeDM.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 0.3
* Design: David Gascón
* Implementation: Alberto Bielsa, Yuri Carmona
*/
#ifdef USE_WASP_XBEE_DM
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
void WaspXBeeDM::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used)
{
init(protocol_used, frequency, model_used, 0);
}
void WaspXBeeDM::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used, uint8_t uart_used)
{
protocol=protocol_used;
freq=frequency;
model=model_used;
uart=uart_used;
data_length=0;
it=0;
start=0;
finish=0;
add_type=0;
mode=0;
frag_length=0;
TIME1=0;
totalFragmentsReceived=0;
pendingPackets=0;
pos=0;
discoveryOptions=0x00;
scanTime[0]=SCAN_TIME_DIGIMESH_H;
scanTime[1]=SCAN_TIME_DIGIMESH_L;
encryptMode=ENCRYPT_MODE_DIGIMESH;
timeRSSI=TIME_RSSI_DIGIMESH;
if( protocol!=XBEE_900 )
{
awakeTime[0]=AWAKE_TIME_DIGIMESH_H;
awakeTime[1]=AWAKE_TIME_DIGIMESH_M;
awakeTime[2]=AWAKE_TIME_DIGIMESH_L;
sleepTime[0]=SLEEP_TIME_DIGIMESH_H;
sleepTime[1]=SLEEP_TIME_DIGIMESH_M;
sleepTime[2]=SLEEP_TIME_DIGIMESH_L;
sleepOptions=SLEEP_OPTIONS_DIGIMESH;
powerLevel=POWER_LEVEL_DIGIMESH;
networkHops=7;
netDelaySlots=3;
netRouteRequest=3;
meshNetRetries=1;
}
nextIndex1=0;
frameNext=0;
replacementPolicy=XBEE_OUT;
indexNotModified=1;
error_AT=2;
error_RX=2;
error_TX=2;
clearFinishArray();
clearCommand();
}
/*
Function: Read the number of times the RF receiver detected a CRC or length error
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "errorsRF" variable number of times CRC or length error
*/
uint8_t WaspXBeeDM::getRFerrors()
{
int8_t error=2;
error_AT=2;
gen_data(get_RF_errors_DM);
error=gen_send(get_RF_errors_DM);
if(!error)
{
errorsRF[0]=data[0];
delay(20);
errorsRF[1]=data[1];
delay(20);
}
return error;
}
/*
Function: Read the number of good frames with valid MAC headers that are received on the RF interface
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "goodPackets" variable the number of good frames received
*/
uint8_t WaspXBeeDM::getGoodPackets()
{
int8_t error=2;
error_AT=2;
gen_data(get_good_pack_DM);
error=gen_send(get_good_pack_DM);
if(!error)
{
goodPackets[0]=data[0];
delay(20);
goodPackets[1]=data[1];
delay(20);
}
return error;
}
/*
Function: Reads the DBM level of the designated channel
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "channelRSSI" variable the DBM level of the designated channel
Parameters:
channel --> The channel to get the DBM value
*/
uint8_t WaspXBeeDM::getChannelRSSI(uint8_t channel)
{
int8_t error=2;
error_AT=2;
gen_data(get_channel_RSSI_DM,channel);
gen_checksum(get_channel_RSSI_DM);
error=gen_send(get_channel_RSSI_DM);
if(!error)
{
channelRSSI=data[1];
delay(20);
}
return error;
}
/*
Function: Read the number of MAC frames that exhaust MAC retries without ever receiving a MAC acknowledgement message from the adjacent node
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "transmisionErrors" variable the number of MAC frames that exhaust
MAC retries
*/
uint8_t WaspXBeeDM::getTransmisionErrors()
{
int8_t error=2;
error_AT=2;
gen_data(get_trans_errors_DM);
error=gen_send(get_trans_errors_DM);
if(!error)
{
transmisionErrors[0]=data[0];
delay(20);
transmisionErrors[1]=data[1];
delay(20);
}
return error;
}
/*
Function: Set the maximum number of hops expected to be seen in a network route
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Change the NH command
Parameters:
nhops: maximum number of hops (1-0xFF)
*/
uint8_t WaspXBeeDM::setNetworkHops(uint8_t nhops)
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(set_network_hops_DM,nhops);
gen_checksum(set_network_hops_DM);
error=gen_send(set_network_hops_DM);
}
else {
error=-1;
error_AT=-1;
}
if(!error)
{
networkHops=nhops;
}
return error;
}
/*
Function: Read the maximum number of hops expected to be seen in a network route
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "networkHops" variable max number of hops
*/
uint8_t WaspXBeeDM::getNetworkHops()
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(get_network_hops_DM);
error=gen_send(get_network_hops_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
networkHops=data[0];
delay(20);
}
return error;
}
/*
Function: Set the maximum random number of network delay slots before rebroadcasting a network packet
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Change the NN command
Parameters:
dslots: maximum number of delay slots (0-0x0A)
*/
uint8_t WaspXBeeDM::setNetworkDelaySlots(uint8_t dslots)
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(set_network_delay_DM,dslots);
gen_checksum(set_network_delay_DM);
error=gen_send(set_network_delay_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
netDelaySlots=dslots;
}
return error;
}
/*
Function: Read the maximum random number of network delay slots before rebroadcasting a network packet
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "netDelaySlots" variable max random number of net delay slots
*/
uint8_t WaspXBeeDM::getNetworkDelaySlots()
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(get_network_delay_DM);
error=gen_send(get_network_delay_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
netDelaySlots=data[0];
delay(20);
}
return error;
}
/*
Function: Set the maximum number of route discovery retries allowed to find a path to the destination node
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Change the NQ command
Parameters:
route: maximum number of route discovery retries (0-0x0A)
*/
uint8_t WaspXBeeDM::setNetworkRouteRequests(uint8_t route)
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(set_network_route_DM,route);
gen_checksum(set_network_route_DM);
error=gen_send(set_network_route_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
netRouteRequest=route;
}
return error;
}
/*
Function: Read the maximum number of route discovery retries allowed to find a path to the destination node
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "netRouteRequest" variable max number of route discovery retries
*/
uint8_t WaspXBeeDM::getNetworkRouteRequests()
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(get_network_route_DM);
error=gen_send(get_network_route_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
netRouteRequest=data[0];
delay(20);
}
return error;
}
/*
Function: Set the maximum number of network packet delivery attempts
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Change the MR command
Parameters:
mesh: maximum number of attempts (0-7)
*/
uint8_t WaspXBeeDM::setMeshNetworkRetries(uint8_t mesh)
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(set_network_retries_DM,mesh);
gen_checksum(set_network_retries_DM);
error=gen_send(set_network_retries_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
meshNetRetries=mesh;
}
return error;
}
/*
Function: Read the maximum number of network packet delivery attempts
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Stores in global "meshNetRetries" variable max number of net packet delivery attempts
*/
uint8_t WaspXBeeDM::getMeshNetworkRetries()
{
int8_t error=2;
if( protocol!=XBEE_900 )
{
error_AT=2;
gen_data(get_network_retries_DM);
error=gen_send(get_network_retries_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
meshNetRetries=data[0];
delay(20);
}
return error;
}
/*
Function: Read module temperature in Celsius. Negatives temperatures can be returned
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Executes the TP command. Stores in global "temperature" variable the readed temp in Celsius
*/
uint8_t WaspXBeeDM::getTemperature()
{
int8_t error=2;
if( (protocol==XBEE_900) || ( (protocol==DIGIMESH) && (freq==FREQ900M) ) )
{
error_AT=2;
gen_data(get_temperature_DM);
error=gen_send(get_temperature_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
temperature[0]=data[0];
delay(20);
temperature[1]=data[1];
delay(20);
}
return error;
}
/*
Function: Reads the voltage on the Vcc pin
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Executes the %V command. Stores in global "supplyVoltage" variable the VCC
*/
uint8_t WaspXBeeDM::getSupplyVoltage()
{
int8_t error=2;
if( (protocol==XBEE_900) || ( (protocol==DIGIMESH) && (freq==FREQ900M) ) )
{
error_AT=2;
gen_data(get_supply_Volt_DM);
error=gen_send(get_supply_Volt_DM);
}
else {
error=-1;
error_AT=-1;
}
if(error==0)
{
supplyVoltage[0]=data[0];
delay(20);
supplyVoltage[1]=data[1];
delay(20);
}
return error;
}
/*
Function: Restore module parameters to compiled defaults
Returns: Integer that determines if there has been any error
error=2 --> The command has not been executed
error=1 --> There has been an error while executing the command
error=0 --> The command has been executed with no errors
Values: Executes the R1 command.
*/
uint8_t WaspXBeeDM::restoreCompiled()
{
int8_t error=2;
if( (protocol==XBEE_900) || ( (protocol==DIGIMESH) && (freq==FREQ900M) ) )
{
error_AT=2;
gen_data(restore_compiled_DM);
error=gen_send(restore_compiled_DM);
}
else {
error=-1;
error_AT=-1;
}
return error;
}
WaspXBeeDM xbeeDM = WaspXBeeDM();
#endif