This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspXBeeCore.cpp
7529 lines (6492 loc) · 191 KB
/
WaspXBeeCore.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 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: 3.2
* Design: David Gascón
* Implementation: Alberto Bielsa, Yuri Carmona
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
/******************************************************************************
* AT COMMANDS (FLASH Definitions)
******************************************************************************/
/// table_CORE /////////////////////////////////////////////////////////////////
const char get_own_mac_low[] PROGMEM = "7E00040852534C06"; // AT+SL
const char get_own_mac_high[] PROGMEM = "7E0004085253480A"; // AT+SH
const char set_own_net_address[] PROGMEM = "7E000608524D59000000"; // AT+MY
const char get_own_net_address[] PROGMEM = "7E000408524D59FF"; // AT+MY
const char set_baudrate[] PROGMEM = "7E0005085242440000"; // AT+BD
const char set_api_mode[] PROGMEM = "7E0005085241500000"; // AT+AP
const char set_api_options[] PROGMEM = "7E00050852414F0000"; // AT+AO
const char set_pan[] PROGMEM = "7E000608524944000000"; // AT+ID
const char set_pan_zb[] PROGMEM = "7E000C08524944000000000000000000"; // AT+ID
const char get_pan[] PROGMEM = "7E00040852494418"; // AT+ID
const char set_sleep_mode_xbee[] PROGMEM = "7E00050852534D0000"; // AT+SM
const char get_sleep_mode_xbee[] PROGMEM = "7E00040852534D05"; // AT+SM
const char set_awake_time[] PROGMEM = "7E000608525354000000"; // AT+ST
const char set_awake_time_DM[] PROGMEM = "7E00070852535400000000"; // AT+ST
const char set_sleep_time[] PROGMEM = "7E000608525350000000"; // AT+SP
const char set_sleep_time_DM[] PROGMEM = "7E00070852535000000000"; // AT+SP
const char set_channel[] PROGMEM = "7E0005085243480000"; // AT+CH
const char get_channel[] PROGMEM = "7E0004085243481A"; // AT+CH
const char get_NI[] PROGMEM = "7E000408524E490E"; // AT+NI
const char set_scanning_time[] PROGMEM = "7E000508524E540000"; // AT+NT
const char set_scanning_time_DM[] PROGMEM = "7E000608524E54000000"; // AT+NT
const char get_scanning_time[] PROGMEM = "7E000408524E5403"; // AT+NT
const char set_discov_options[] PROGMEM = "7E000508524E4F0000"; // AT+NO
const char get_discov_options[] PROGMEM = "7E000408524E4F08"; // AT+NO
const char write_values[] PROGMEM = "7E000408525752FC"; // AT+WR
const char set_scanning_channel[] PROGMEM = "7E000608525343000000"; // AT+SC
const char get_scanning_channel[] PROGMEM = "7E0004085253430F"; // AT+SC
const char get_duration_energy[] PROGMEM = "7E0004085253440E"; // AT+SD
const char set_link_key[] PROGMEM = "7E001408524B590000000000000000000000000000000000"; // AT+KY
const char set_encryption[] PROGMEM = "7E0005085245450000"; // AT+EE
const char get_encryption[] PROGMEM = "7E0004085245451B"; // AT+EE
const char set_power_level[] PROGMEM = "7E00050852504C0000"; // AT+PL
const char get_power_level[] PROGMEM = "7E00040852504C09"; // AT+PL
const char get_RSSI[] PROGMEM = "7E0004085244421F"; // AT+DB
const char get_hard_version[] PROGMEM = "7E00040852485607"; // AT+HV
const char get_soft_version[] PROGMEM = "7E000408525652FD"; // AT+VR
const char set_RSSI_time[] PROGMEM = "7E0005085252500000"; // AT+RP
const char get_RSSI_time[] PROGMEM = "7E00040852525003"; // AT+RP
const char apply_changes[] PROGMEM = "7E00040852414321"; // AT+AC
const char reset_xbee[] PROGMEM = "7E0004085246520D"; // AT+FR
const char reset_defaults_xbee[] PROGMEM = "7E0004085252450E"; // AT+RE
const char set_sleep_options_xbee[] PROGMEM = "7E00050852534F0000"; // AT+SO
const char get_sleep_options_xbee[] PROGMEM = "7E00040852534F03"; // AT+SO
const char scan_network [] PROGMEM = "7E000408524E4413"; // AT+ND
const char set_duration_energy[] PROGMEM = "7E0005085245440000"; // AT+ED
const char set_duration_energy_ZB[] PROGMEM="7E0005085253440000"; // AT+SD
const char get_low_dest_address[] PROGMEM = "7E00040852444C15"; // AT+DL
const char timestamp_packet[] PROGMEM = "%2u%2u%2u%2u%2u%2u%2u%c%2u%2u";
const char* const table_CORE[] PROGMEM=
{
get_own_mac_low, // 0
get_own_mac_high, // 1
set_own_net_address, // 2
get_own_net_address, // 3
set_baudrate, // 4
set_api_mode, // 5
set_api_options, // 6
set_pan, // 7
set_pan_zb, // 8
get_pan, // 9
set_sleep_mode_xbee, // 10
get_sleep_mode_xbee, // 11
set_awake_time, // 12
set_awake_time_DM, // 13
set_sleep_time, // 14
set_sleep_time_DM, // 15
set_channel, // 16
get_channel, // 17
get_NI, // 18
set_scanning_time, // 19
set_scanning_time_DM, // 20
get_scanning_time, // 21
set_discov_options, // 22
get_discov_options, // 23
write_values, // 24
set_scanning_channel, // 25
get_scanning_channel, // 26
get_duration_energy, // 27
set_link_key, // 28
set_encryption, // 29
get_encryption, // 30
set_power_level, // 31
get_power_level, // 32
get_RSSI, // 33
get_hard_version, // 34
get_soft_version, // 35
set_RSSI_time, // 36
get_RSSI_time, // 37
apply_changes, // 38
reset_xbee, // 39
reset_defaults_xbee, // 40
set_sleep_options_xbee, // 41
get_sleep_options_xbee, // 42
scan_network, // 43
set_duration_energy, // 44
set_duration_energy_ZB, // 45
get_low_dest_address, // 46
timestamp_packet, // 47
};
/// table_OTA //////////////////////////////////////////////////////////////////
const char NEW_FIRMWARE_MESSAGE_OK[] PROGMEM ="PROGRAM RECEIVED OK$$$$$$$$$$$$$";
const char NEW_FIRMWARE_MESSAGE_ERROR[] PROGMEM ="PROGRAM RECEIVED ERROR$$$$$$$$$$";
const char UPLOAD_FIRWARE_MESSAGE_OK[] PROGMEM ="START WITH FIRMWARE OK$$$$$$$$$$$$$$$$$$$$$$$$$$";
const char UPLOAD_FIRWARE_MESSAGE_ERROR[] PROGMEM ="START WITH FIRMWARE ERROR$$$$$$$$$$$$$$$$$$$$$$$";
const char REQUEST_BOOTLIST_MESSAGE[] PROGMEM ="READ BOOTLIST$$$$$$$$$$$$$$$$$";
const char ANSWER_START_WITH_FIRMWARE_OK[] PROGMEM ="NEW PROGRAM RUNNING$$$$$$$$$$$$$";
const char ANSWER_START_WITH_FIRMWARE_ERR[] PROGMEM ="PREVIOUS PROGRAM RUNNING$$$$$$$$";
const char RESET_MESSAGE[] PROGMEM ="RESTARTING$$$$$$$$$$$$$$$$$$$$$$";
const char DELETE_MESSAGE_OK[] PROGMEM ="FIRMWARE DELETED$$$$$$$$$$$$$$$$";
const char DELETE_MESSAGE_ERROR[] PROGMEM ="FIRMWARE NOT DELETED$$$$$$$$$$$$";
const char START_SECTOR[] PROGMEM ="FIRMWARE_FILE_FOR_WASPMOTE######";
const char FILEAUX[] PROGMEM ="FILEAUX";
const char* const table_OTA[] PROGMEM=
{
NEW_FIRMWARE_MESSAGE_OK, // 0
NEW_FIRMWARE_MESSAGE_ERROR, // 1
UPLOAD_FIRWARE_MESSAGE_OK, // 2
UPLOAD_FIRWARE_MESSAGE_ERROR, // 3
REQUEST_BOOTLIST_MESSAGE, // 4
ANSWER_START_WITH_FIRMWARE_OK, // 5
ANSWER_START_WITH_FIRMWARE_ERR, // 6
RESET_MESSAGE, // 7
DELETE_MESSAGE_OK, // 8
DELETE_MESSAGE_ERROR, // 9
START_SECTOR, // 10
FILEAUX, // 11
};
/******************************************************************************
* Class methods
******************************************************************************/
/*
* Function: Get the 32 lower bits of my MAC address
*
* 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: When it is executed stores the returned value by SL command in the
* global "sourceMacLow[4]" variable
*/
uint8_t WaspXBeeCore::getOwnMacLow()
{
int8_t error=2;
error_AT=2;
char buffer[17];
// get_own_mac_low
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[0])));
if(buffer==NULL)return 1;
gen_data(buffer);
error=gen_send(buffer);
if(error==0)
{
sourceMacLow[0]=data[0];
sourceMacLow[1]=data[1];
sourceMacLow[2]=data[2];
sourceMacLow[3]=data[3];
}
return error;
}
/*
Function: Get the 32 higher bits of my MAC address
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: When it is executed stores the returned value by SH in the global
"sourceMacHigh[4]" variable
*/
uint8_t WaspXBeeCore::getOwnMacHigh()
{
int8_t error=2;
error_AT=2;
char buffer[17];
// get_own_mac_high
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[1])));
if(buffer==NULL) return 1;
gen_data(buffer);
error=gen_send(buffer);
if(error==0)
{
sourceMacHigh[0]=data[0];
sourceMacHigh[1]=data[1];
sourceMacHigh[2]=data[2];
sourceMacHigh[3]=data[3];
}
return error;
}
/*
Function: Get the 64 bits of my MAC address
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 functions getOwnMacLow() and getOwnMacHigh()
*/
uint8_t WaspXBeeCore::getOwnMac()
{
int8_t error=2;
error=getOwnMacLow();
if(error==0)
{
error=getOwnMacHigh();
}
return error;
}
/*
Function: Set the 16b network address
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
error=-1 --> Forbidden command in this protocol
Parameters:
NA_H : Higher byte of Network Address (0x00-0xFF)
NA_L : Lower byte of Network Address (0x00-0xFF)
Values: Stores in global "sourceNA[2]" variable the 16b address set by the user
*/
uint8_t WaspXBeeCore::setOwnNetAddress(char* NA)
{
uint8_t networkAddress[2];
Utils.str2hex( (char*)NA, (uint8_t*)networkAddress );
return setOwnNetAddress( networkAddress[0], networkAddress[1]);
}
/*
Function: Set the 16b network address
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
error=-1 --> Forbidden command in this protocol
Parameters:
NA_H : Higher byte of Network Address (0x00-0xFF)
NA_L : Lower byte of Network Address (0x00-0xFF)
Values: Stores in global "sourceNA[2]" variable the 16b address set by the user
*/
uint8_t WaspXBeeCore::setOwnNetAddress(uint8_t* NA)
{
return setOwnNetAddress( NA[0], NA[1] );
}
/*
Function: Set the 16b network address
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
error=-1 --> Forbidden command in this protocol
Parameters:
NA_H : Higher byte of Network Address (0x00-0xFF)
NA_L : Lower byte of Network Address (0x00-0xFF)
Values: Stores in global "sourceNA[2]" variable the 16b address set by the user
*/
uint8_t WaspXBeeCore::setOwnNetAddress(uint8_t NA_H, uint8_t NA_L)
{
int8_t error=2;
char buffer[21];
if(protocol==XBEE_802_15_4)
{
error_AT=2;
// set_own_net_address
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[2])));
if(buffer==NULL) return 1;
gen_data(buffer,NA_H,NA_L);
gen_checksum(buffer);
error=gen_send(buffer);
}
else
{
error_AT=-1;
error=-1;
}
if(!error)
{
sourceNA[0]=NA_H;
sourceNA[1]=NA_L;
}
return error;
}
/*
* Function: Get the 16-bit network address
*
* 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
* error=-1 --> Forbidden command in this protocol
*
* Values: Stores in global "sourceNA[2]" variable the returned 16b network
* address by MY command
*/
uint8_t WaspXBeeCore::getOwnNetAddress()
{
int8_t error=2;
char buffer[17];
if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
{
error_AT=2;
// get_own_net_address
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[3])));
if(buffer==NULL) return 1;
gen_data(buffer);
error=gen_send(buffer);
}
else
{
error_AT=-1;
error=-1;
}
if(!error)
{
sourceNA[0]=data[0];
sourceNA[1]=data[1];
}
return error;
}
/*
Function: Set Baudrate to use
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
error=-1 --> Forbidden command in this protocol
Parameters:
baud_rate: integer that contains the baudrate
Values: Stores in global "baudrate" variable the baudrate
*/
uint8_t WaspXBeeCore::setBaudrate(uint8_t baud_rate)
{
int8_t error=2;
error_AT=2;
char buffer[19];
// set_baudrate
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[4])));
if(buffer==NULL) return 1;
gen_data(buffer,baud_rate);
gen_checksum(buffer);
error=gen_send(buffer);
if(!error)
{
baudrate=baud_rate;
}
return error;
}
/*
Function: Set API values
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
error=-1 --> Forbidden command in this protocol
Parameters:
api_value: integer that contains the api value
Values: Stores in global "apiValue" variable the baudrate
*/
uint8_t WaspXBeeCore::setAPI(uint8_t api_value)
{
int8_t error=2;
error_AT=2;
char buffer[19];
// set_api_mode
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[5])));
if(buffer==NULL) return 1;
gen_data(buffer,api_value);
gen_checksum(buffer);
error=gen_send(buffer);
if(!error)
{
apiValue=api_value;
}
return error;
}
/*
Function: Set API options. Enable ZIgBee Application Layer Addressing
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
error=-1 --> Forbidden command in this protocol
Parameters:
api_options: integer that contains the baudrate
*/
uint8_t WaspXBeeCore::setAPIoptions(uint8_t api_options)
{
int8_t error=2;
char buffer[19];
if( (protocol!=XBEE_802_15_4) )
{
error_AT=2;
// set_api_options
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[6])));
if(buffer==NULL) return 1;
gen_data(buffer,api_options);
gen_checksum(buffer);
error=gen_send(buffer);
}
else
{
error_AT=-1;
error=-1;
}
return error;
}
/*
Function: Set the network identifier
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
Parameters:
PANID: Array of integers than contains the 16b or 64b PAN ID
Values: Stores in global "PAN_ID" variable the recent set PAN ID value
*/
uint8_t WaspXBeeCore::setPAN(uint8_t* PANID)
{
int8_t error=2;
char buffer[33];
if( (protocol == XBEE_802_15_4)
|| (protocol == DIGIMESH)
|| (protocol == XBEE_900)
|| (protocol == XBEE_868)
|| (protocol == XBEE_868LP)
|| (protocol == XBEE_900HP) )
{
error_AT=2;
// set_pan
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[7])));
if(buffer==NULL) return 1;
gen_data(buffer,PANID);
gen_checksum(buffer);
error=gen_send(buffer);
}
if(protocol==ZIGBEE)
{
error_AT=2;
flush();
// set_pan_zb
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[8])));
if(buffer==NULL) return 1;
gen_data(buffer,PANID);
gen_checksum(buffer);
error=gen_send(buffer);
}
if(!error)
{
if( (protocol == XBEE_802_15_4)
|| (protocol == DIGIMESH)
|| (protocol == XBEE_900)
|| (protocol == XBEE_868)
|| (protocol == XBEE_868LP)
|| (protocol == XBEE_900HP) )
{
PAN_ID[0] = PANID[0];
PAN_ID[1] = PANID[1];
}
if( protocol == ZIGBEE )
{
PAN_ID[0] = PANID[0];
PAN_ID[1] = PANID[1];
PAN_ID[2] = PANID[2];
PAN_ID[3] = PANID[3];
PAN_ID[4] = PANID[4];
PAN_ID[5] = PANID[5];
PAN_ID[6] = PANID[6];
PAN_ID[7] = PANID[7];
}
}
return error;
}
/*
Function: Get Network ID
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 "error" variable any error happened while execution
Stores in global "PAN_ID" variable the 16b or 64b network PAN ID
*/
uint8_t WaspXBeeCore::getPAN()
{
int8_t error=2;
error_AT=2;
char buffer[20];
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[9]))); // get_pan
if(buffer==NULL) return 1;
gen_data(buffer);
if( protocol==ZIGBEE ) error=gen_send(buffer);
else error=gen_send(buffer);
if(!error)
{
if( (protocol == XBEE_802_15_4)
|| (protocol == DIGIMESH)
|| (protocol == XBEE_900)
|| (protocol == XBEE_868)
|| (protocol == XBEE_868LP)
|| (protocol == XBEE_900HP) )
{
PAN_ID[0] = data[0];
PAN_ID[1] = data[1];
}
if(protocol==ZIGBEE)
{
PAN_ID[0] = data[0];
PAN_ID[1] = data[1];
PAN_ID[2] = data[2];
PAN_ID[3] = data[3];
PAN_ID[4] = data[4];
PAN_ID[5] = data[5];
PAN_ID[6] = data[6];
PAN_ID[7] = data[7];
}
}
return error;
}
/*
Function: Set the module to the sleep mode specified.
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 the returned value by SM command in the global "sleepMode" variable
Parameters:
sleep: Defines the sleep mode to use by the XBee (0-5)
*/
uint8_t WaspXBeeCore::setSleepMode(uint8_t sleep)
{
int8_t error=2;
char buffer[20];
error_AT=2;
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[10]))); // set_sleep_mode_xbee
if(buffer==NULL) return 1;
gen_data(buffer,sleep);
gen_checksum(buffer);
error=gen_send(buffer);
if(!error)
{
sleepMode=sleep;
}
return error;
}
/*
Function: Get the XBee mode
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 the XBee mode in the global "sleepMode" variable
*/
uint8_t WaspXBeeCore::getSleepMode()
{
int8_t error=2;
char buffer[20];
error_AT=2;
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[11]))); // get_sleep_mode_xbee
if(buffer==NULL) return 1;
gen_data(buffer);
error=gen_send(buffer);
if(error==0)
{
sleepMode=data[0];
}
return error;
}
/*
Function: Set the time the module has to be idle before start sleeping
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 ST parameter in XBee module
Stores in global "awakeTime" the value of this time
Parameters:
awake: Array of integers that specifies the time to be awake before sleep
*/
uint8_t WaspXBeeCore::setAwakeTime(uint8_t* awake)
{
int8_t error=2;
char buffer[23];
if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
{
error_AT=2;
// set_awake_time
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[12])));
if(buffer==NULL) return 1;
gen_data(buffer,awake);
gen_checksum(buffer);
error=gen_send(buffer);
}
if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_900HP))
{
error_AT=2;
// set_awake_time_DM
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[13])));
if(buffer==NULL) return 1;
gen_data(buffer,awake);
gen_checksum(buffer);
error=gen_send(buffer);
}
if(!error)
{
if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
{
awakeTime[0]=awake[0];
awakeTime[1]=awake[1];
}
if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_900HP) )
{
awakeTime[0]=awake[0];
awakeTime[1]=awake[1];
awakeTime[2]=awake[2];
}
}
return error;
}
/*
Function: Set the cyclic sleeping time of the 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 SP parameter in the XBee module
Stores in global "sleepTime" the value of this time
Parameters:
sleep: Array of Integers that specifies the amount of time the module spends sleeping
*/
uint8_t WaspXBeeCore::setSleepTime(uint8_t* sleep)
{
int8_t error=2;
char buffer[23];
if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
{
error_AT=2;
// set_sleep_time
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[14])));
if(buffer==NULL) return 1;
gen_data(buffer,sleep);
gen_checksum(buffer);
error=gen_send(buffer);
}
if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_900HP) )
{
error_AT=2;
// set_sleep_time_DM
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[15])));
if(buffer==NULL) return 1;
gen_data(buffer,sleep);
gen_checksum(buffer);
error=gen_send(buffer);
}
if(!error)
{
if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
{
sleepTime[0]=sleep[0];
sleepTime[1]=sleep[1];
}
if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_900HP) )
{
sleepTime[0]=sleep[0];
sleepTime[1]=sleep[1];
sleepTime[2]=sleep[2];
}
}
return error;
}
/*
Function: Set the channel frequency where the module is going to work
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
error=-1 --> Forbidden command for this protocol
Values: Stores the selected channel in the global "channel" variable
Parameters:
_channel: Channel used to transmit (0x0B-0x1A)
*/
uint8_t WaspXBeeCore::setChannel(uint8_t _channel)
{
int8_t error=2;
char buffer[20];
if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) )
{
error_AT=2;
// set_channel
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[16])));
if(buffer==NULL) return 1;
gen_data(buffer,_channel);
gen_checksum(buffer);
error=gen_send(buffer);
}
else
{
error_AT=-1;
error=-1;
}
if(!error)
{
channel=_channel;
}
return error;
}
/*
Function: Get the actual frequency 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 the frequency channel in the global "channel" variable
*/
uint8_t WaspXBeeCore::getChannel()
{
int8_t error=2;
char buffer[20];
error_AT=2;
// get_channel
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[17])));
if(buffer==NULL) return 1;
gen_data(buffer);
error=gen_send(buffer);
if(!error)
{
channel=data[0];
}
return error;
}
/*
Function: Set the Node Indentifier
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 NI to the selected in the function
The NI must be a 20 character max string
Stores the given NI in the global "nodeID" variable
Parameters:
node: string that specifies the node indentifier
*/
uint8_t WaspXBeeCore::setNodeIdentifier(const char* node)
{
uint8_t NI[30];
NI[0]=0x7E;
NI[1]=0x00;
NI[3]=0x08;
NI[4]=0x52;
NI[5]=0x4E;
NI[6]=0x49;
int8_t error=2;
uint8_t counter=0;
uint8_t checksum=0;
int index = 0;
// init flag
error_AT = 2;
while( (node[index]!='\0') )
{
NI[index+7] = uint8_t(node[index]);
index++;
}
NI[2] = 4+index;
for(int i=3; i<(7+(NI[2]-4));i++)
{
checksum=checksum+NI[i];
}
while( (checksum>255))
{
checksum=checksum-256;
}
checksum=255-checksum;
NI[7+NI[2]-4]=checksum;
while(counter<(8+NI[2]-4))
{
// print byte through correspondent UART
printByte( NI[counter], uart);
counter++;
}
counter=0;
clearCommand();
command[5]=0x4E;
command[6]=0x49;
error=parse_message(command);
if(error==0)
{
for(int i=0 ; i<NI[2]-4 ; i++)
{
nodeID[i] = node[i];
}
}
return error;
}
/*
Function: Get the Node Identifier
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 the NI in the global "nodeID" variable
*/
uint8_t WaspXBeeCore::getNodeIdentifier()
{
int8_t error=2;
char buffer[20];
error_AT=2;
// get_NI
strcpy_P(buffer, (char*)pgm_read_word(&(table_CORE[18])));
if(buffer==NULL) return 1;
gen_data(buffer);
error=gen_send(buffer);
if(!error)
{
for( uint16_t i=0 ; i<data_length ; i++)
{
nodeID[i] = char(data[i]);
}
}
return error;
}
/*
Function: Scans for brothers in the same channel and same PAN ID
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 given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable
Stores in global "totalScannedBrothers" the number of founded brothers
Parameters:
node: 20-byte max string containing NI of the node to search
*/
uint8_t WaspXBeeCore::scanNetwork(const char* node)
{
uint8_t ND[30]; //{0x7E, 0x00, 0x04, 0x08, 0x52, 0x4E, 0x44, 0x13};
ND[0]=0x7E;
ND[1]=0x00;
ND[3]=0x08;
ND[4]=0x52;
ND[5]=0x4E;
ND[6]=0x44;
int8_t error = 2;
uint8_t counter = 0;
uint16_t checksum = 0;
int index = 0;
// init flag
error_AT = 2;
totalScannedBrothers = 0;
while( (node[index]!='\0') )
{
ND[index+7] = uint8_t(node[index]);
index++;
}
ND[2]=4+index;
for(int i=3 ; i<(7+(ND[2]-4)) ; i++)
{
checksum=checksum+ND[i];
}
while( (checksum>255))
{
checksum=checksum-256;
}
checksum=255-checksum;
ND[7+ND[2]-4]=checksum;
while(counter<(8+ND[2]-4))
{
// print byte through correspondent UART