-
Notifications
You must be signed in to change notification settings - Fork 2
/
XBee.cpp
1351 lines (1018 loc) · 33.2 KB
/
XBee.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) 2009 Andrew Rapp. All rights reserved.
*
* This file is part of XBee-Arduino.
*
* XBee-Arduino is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XBee-Arduino 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "XBee.h"
#include "WProgram.h"
#include "HardwareSerial.h"
XBeeResponse::XBeeResponse() {
}
uint8_t XBeeResponse::getApiId() {
return _apiId;
}
void XBeeResponse::setApiId(uint8_t apiId) {
_apiId = apiId;
}
uint8_t XBeeResponse::getMsbLength() {
return _msbLength;
}
void XBeeResponse::setMsbLength(uint8_t msbLength) {
_msbLength = msbLength;
}
uint8_t XBeeResponse::getLsbLength() {
return _lsbLength;
}
void XBeeResponse::setLsbLength(uint8_t lsbLength) {
_lsbLength = lsbLength;
}
uint8_t XBeeResponse::getChecksum() {
return _checksum;
}
void XBeeResponse::setChecksum(uint8_t checksum) {
_checksum = checksum;
}
uint8_t XBeeResponse::getFrameDataLength() {
return _frameLength;
}
void XBeeResponse::setFrameLength(uint8_t frameLength) {
_frameLength = frameLength;
}
bool XBeeResponse::isAvailable() {
return _complete;
}
void XBeeResponse::setAvailable(bool complete) {
_complete = complete;
}
bool XBeeResponse::isError() {
return _errorCode > 0;
}
uint8_t XBeeResponse::getErrorCode() {
return _errorCode;
}
void XBeeResponse::setErrorCode(uint8_t errorCode) {
_errorCode = errorCode;
}
// copy common fields from xbee response to target response
void XBeeResponse::setCommon(XBeeResponse &target) {
target.setApiId(getApiId());
target.setAvailable(isAvailable());
target.setChecksum(getChecksum());
target.setErrorCode(getErrorCode());
target.setFrameLength(getFrameDataLength());
target.setMsbLength(getMsbLength());
target.setLsbLength(getLsbLength());
}
#ifdef SERIES_2
ZBTxStatusResponse::ZBTxStatusResponse() : FrameIdResponse() {
}
uint16_t ZBTxStatusResponse::getRemoteAddress() {
return (getFrameData()[1] << 8) + getFrameData()[2];
}
uint8_t ZBTxStatusResponse::getTxRetryCount() {
return getFrameData()[3];
}
uint8_t ZBTxStatusResponse::getDeliveryStatus() {
return getFrameData()[4];
}
uint8_t ZBTxStatusResponse::getDiscoveryStatus() {
return getFrameData()[5];
}
bool ZBTxStatusResponse::isSuccess() {
return getDeliveryStatus() == SUCCESS;
}
void XBeeResponse::getZBTxStatusResponse(XBeeResponse &zbXBeeResponse) {
// way off?
ZBTxStatusResponse* zb = static_cast<ZBTxStatusResponse*>(&zbXBeeResponse);
// pass pointer array to subclass
zb->setFrameData(getFrameData());
setCommon(zbXBeeResponse);
}
ZBRxResponse::ZBRxResponse(): RxDataResponse() {
_remoteAddress64 = XBeeAddress64();
}
uint16_t ZBRxResponse::getRemoteAddress16() {
return (getFrameData()[8] << 8) + getFrameData()[9];
}
uint8_t ZBRxResponse::getOption() {
return getFrameData()[10];
}
// markers to read data from packet array. this is the index, so the 12th item in the array
uint8_t ZBRxResponse::getDataOffset() {
return 11;
}
uint8_t ZBRxResponse::getDataLength() {
return getPacketLength() - getDataOffset() - 1;
}
XBeeAddress64& ZBRxResponse::getRemoteAddress64() {
return _remoteAddress64;
}
void XBeeResponse::getZBRxResponse(XBeeResponse &rxResponse) {
ZBRxResponse* zb = static_cast<ZBRxResponse*>(&rxResponse);
//TODO verify response api id matches this api for this response
// pass pointer array to subclass
zb->setFrameData(getFrameData());
setCommon(rxResponse);
zb->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
zb->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + (getFrameData()[7]));
}
ZBRxIoSampleResponse::ZBRxIoSampleResponse() : ZBRxResponse() {
}
// 64 + 16 addresses, sample size, option = 12 (index 11), so this starts at 12
uint8_t ZBRxIoSampleResponse::getDigitalMaskMsb() {
return getFrameData()[12] & 0x1c;
}
uint8_t ZBRxIoSampleResponse::getDigitalMaskLsb() {
return getFrameData()[13];
}
uint8_t ZBRxIoSampleResponse::getAnalogMask() {
return getFrameData()[14] & 0x8f;
}
bool ZBRxIoSampleResponse::containsAnalog() {
return getAnalogMask() > 0;
}
bool ZBRxIoSampleResponse::containsDigital() {
return getDigitalMaskMsb() > 0 || getDigitalMaskLsb() > 0;
}
bool ZBRxIoSampleResponse::isAnalogEnabled(uint8_t pin) {
return ((getAnalogMask() >> pin) & 1) == 1;
}
bool ZBRxIoSampleResponse::isDigitalEnabled(uint8_t pin) {
if (pin <= 7) {
// added extra parens to calm avr compiler
return ((getDigitalMaskLsb() >> pin) & 1) == 1;
} else {
return ((getDigitalMaskMsb() >> (pin - 8)) & 1) == 1;
}
}
uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) {
// analog starts 13 bytes after sample size, if no dio enabled
uint8_t start = 15;
if (containsDigital()) {
// make room for digital i/o
start+=2;
}
// std::cout << "spacing is " << static_cast<unsigned int>(spacing) << std::endl;
// start depends on how many pins before this pin are enabled
for (int i = 0; i < pin; i++) {
if (isAnalogEnabled(pin)) {
start+=2;
}
}
// std::cout << "start for analog pin ["<< static_cast<unsigned int>(pin) << "]/sample " << static_cast<unsigned int>(sample) << " is " << static_cast<unsigned int>(start) << std::endl;
// std::cout << "returning index " << static_cast<unsigned int>(getSampleOffset() + start) << " and index " << static_cast<unsigned int>(getSampleOffset() + start + 1) << ", val is " << static_cast<unsigned int>(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast<unsigned int>(getFrameData()[getSampleOffset() + start + 1]) << std::endl;
return (uint16_t)((getFrameData()[start] << 8) + getFrameData()[start + 1]);
}
bool ZBRxIoSampleResponse::isDigitalOn(uint8_t pin) {
if (pin <= 7) {
// D0-7
// DIO LSB is index 5
return ((getFrameData()[16] >> pin) & 1) == 1;
} else {
// D10-12
// DIO MSB is index 4
return ((getFrameData()[15] >> (pin - 8)) & 1) == 1;
}
}
void XBeeResponse::getZBRxIoSampleResponse(XBeeResponse &response) {
ZBRxIoSampleResponse* zb = static_cast<ZBRxIoSampleResponse*>(&response);
// pass pointer array to subclass
zb->setFrameData(getFrameData());
setCommon(response);
zb->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
zb->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + (getFrameData()[7]));
}
#endif
#ifdef SERIES_1
RxResponse::RxResponse() : RxDataResponse() {
}
uint16_t Rx16Response::getRemoteAddress16() {
return (getFrameData()[0] << 8) + getFrameData()[1];
}
XBeeAddress64& Rx64Response::getRemoteAddress64() {
return _remoteAddress;
}
Rx64Response::Rx64Response() : RxResponse() {
_remoteAddress = XBeeAddress64();
}
Rx16Response::Rx16Response() : RxResponse() {
}
RxIoSampleBaseResponse::RxIoSampleBaseResponse() : RxResponse() {
}
uint8_t RxIoSampleBaseResponse::getSampleOffset() {
// sample starts 2 bytes after rssi
return getRssiOffset() + 2;
}
uint8_t RxIoSampleBaseResponse::getSampleSize() {
return getFrameData()[getSampleOffset()];
}
bool RxIoSampleBaseResponse::containsAnalog() {
return (getFrameData()[getSampleOffset() + 1] & 0x7e) > 0;
}
bool RxIoSampleBaseResponse::containsDigital() {
return (getFrameData()[getSampleOffset() + 1] & 0x1) > 0 || getFrameData()[getSampleOffset() + 2] > 0;
}
//uint16_t RxIoSampleBaseResponse::getAnalog0(uint8_t sample) {
// return getAnalog(0, sample);
//}
bool RxIoSampleBaseResponse::isAnalogEnabled(uint8_t pin) {
return (((getFrameData()[getSampleOffset() + 1] >> (pin + 1)) & 1) == 1);
}
bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) {
if (pin < 8) {
return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
} else {
return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
}
}
uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) {
// analog starts 3 bytes after sample size, if no dio enabled
uint8_t start = 3;
if (containsDigital()) {
// make room for digital i/o sample (2 bytes per sample)
start+=2*(sample + 1);
}
uint8_t spacing = 0;
// spacing between samples depends on how many are enabled. add one for each analog that's enabled
for (int i = 0; i <= 5; i++) {
if (isAnalogEnabled(i)) {
// each analog is two bytes
spacing+=2;
}
}
// std::cout << "spacing is " << static_cast<unsigned int>(spacing) << std::endl;
// start depends on how many pins before this pin are enabled
for (int i = 0; i < pin; i++) {
if (isAnalogEnabled(pin)) {
start+=2;
}
}
start+= sample * spacing;
// std::cout << "start for analog pin ["<< static_cast<unsigned int>(pin) << "]/sample " << static_cast<unsigned int>(sample) << " is " << static_cast<unsigned int>(start) << std::endl;
// std::cout << "returning index " << static_cast<unsigned int>(getSampleOffset() + start) << " and index " << static_cast<unsigned int>(getSampleOffset() + start + 1) << ", val is " << static_cast<unsigned int>(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast<unsigned int>(getFrameData()[getSampleOffset() + start + 1]) << std::endl;
return (uint16_t)((getFrameData()[getSampleOffset() + start] << 8) + getFrameData()[getSampleOffset() + start + 1]);
}
bool RxIoSampleBaseResponse::isDigitalOn(uint8_t pin, uint8_t sample) {
if (pin < 8) {
return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
} else {
return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
}
}
//bool RxIoSampleBaseResponse::isDigital0On(uint8_t sample) {
// return isDigitalOn(0, sample);
//}
Rx16IoSampleResponse::Rx16IoSampleResponse() : RxIoSampleBaseResponse() {
}
uint16_t Rx16IoSampleResponse::getRemoteAddress16() {
return (uint16_t)((getFrameData()[0] << 8) + getFrameData()[1]);
}
uint8_t Rx16IoSampleResponse::getRssiOffset() {
return 2;
}
void XBeeResponse::getRx16IoSampleResponse(XBeeResponse &response) {
Rx16IoSampleResponse* rx = static_cast<Rx16IoSampleResponse*>(&response);
rx->setFrameData(getFrameData());
setCommon(response);
}
Rx64IoSampleResponse::Rx64IoSampleResponse() : RxIoSampleBaseResponse() {
_remoteAddress = XBeeAddress64();
}
XBeeAddress64& Rx64IoSampleResponse::getRemoteAddress64() {
return _remoteAddress;
}
uint8_t Rx64IoSampleResponse::getRssiOffset() {
return 8;
}
void XBeeResponse::getRx64IoSampleResponse(XBeeResponse &response) {
Rx64IoSampleResponse* rx = static_cast<Rx64IoSampleResponse*>(&response);
rx->setFrameData(getFrameData());
setCommon(response);
rx->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
rx->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + getFrameData()[7]);
}
TxStatusResponse::TxStatusResponse() : FrameIdResponse() {
}
uint8_t TxStatusResponse::getStatus() {
return getFrameData()[1];
}
bool TxStatusResponse::isSuccess() {
return getStatus() == SUCCESS;
}
void XBeeResponse::getTxStatusResponse(XBeeResponse &txResponse) {
TxStatusResponse* txStatus = static_cast<TxStatusResponse*>(&txResponse);
// pass pointer array to subclass
txStatus->setFrameData(getFrameData());
setCommon(txResponse);
}
uint8_t RxResponse::getRssi() {
return getFrameData()[getRssiOffset()];
}
uint8_t RxResponse::getOption() {
return getFrameData()[getRssiOffset() + 1];
}
bool RxResponse::isAddressBroadcast() {
return (getOption() & 2) == 2;
}
bool RxResponse::isPanBroadcast() {
return (getOption() & 4) == 4;
}
uint8_t RxResponse::getDataLength() {
return getPacketLength() - getDataOffset() - 1;
}
uint8_t RxResponse::getDataOffset() {
return getRssiOffset() + 2;
}
uint8_t Rx16Response::getRssiOffset() {
return RX_16_RSSI_OFFSET;
}
void XBeeResponse::getRx16Response(XBeeResponse &rx16Response) {
Rx16Response* rx16 = static_cast<Rx16Response*>(&rx16Response);
// pass pointer array to subclass
rx16->setFrameData(getFrameData());
setCommon(rx16Response);
// rx16->getRemoteAddress16().setAddress((getFrameData()[0] << 8) + getFrameData()[1]);
}
uint8_t Rx64Response::getRssiOffset() {
return RX_64_RSSI_OFFSET;
}
void XBeeResponse::getRx64Response(XBeeResponse &rx64Response) {
Rx64Response* rx64 = static_cast<Rx64Response*>(&rx64Response);
// pass pointer array to subclass
rx64->setFrameData(getFrameData());
setCommon(rx64Response);
rx64->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
rx64->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + getFrameData()[7]);
}
#endif
RemoteAtCommandResponse::RemoteAtCommandResponse() : AtCommandResponse() {
}
uint8_t* RemoteAtCommandResponse::getCommand() {
return getFrameData() + 11;
}
uint8_t RemoteAtCommandResponse::getStatus() {
return getFrameData()[13];
}
bool RemoteAtCommandResponse::isOk() {
// weird c++ behavior. w/o this method, it calls AtCommandResponse::isOk(), which calls the AtCommandResponse::getStatus, not this.getStatus!!!
return getStatus() == AT_OK;
}
uint8_t RemoteAtCommandResponse::getValueLength() {
return getFrameDataLength() - 14;
}
uint8_t* RemoteAtCommandResponse::getValue() {
if (getValueLength() > 0) {
// value is only included for query commands. set commands does not return a value
return getFrameData() + 14;
}
return NULL;
}
uint16_t RemoteAtCommandResponse::getRemoteAddress16() {
return uint16_t((getFrameData()[9] << 8) + getFrameData()[10]);
}
XBeeAddress64& RemoteAtCommandResponse::getRemoteAddress64() {
return _remoteAddress64;
}
void XBeeResponse::getRemoteAtCommandResponse(XBeeResponse &response) {
// TODO no real need to cast. change arg to match expected class
RemoteAtCommandResponse* at = static_cast<RemoteAtCommandResponse*>(&response);
// pass pointer array to subclass
at->setFrameData(getFrameData());
setCommon(response);
at->getRemoteAddress64().setMsb((uint32_t(getFrameData()[1]) << 24) + (uint32_t(getFrameData()[2]) << 16) + (uint16_t(getFrameData()[3]) << 8) + getFrameData()[4]);
at->getRemoteAddress64().setLsb((uint32_t(getFrameData()[5]) << 24) + (uint32_t(getFrameData()[6]) << 16) + (uint16_t(getFrameData()[7]) << 8) + (getFrameData()[8]));
}
RxDataResponse::RxDataResponse() : XBeeResponse() {
}
uint8_t RxDataResponse::getData(int index) {
return getFrameData()[getDataOffset() + index];
}
uint8_t* RxDataResponse::getData() {
return getFrameData() + getDataOffset();
}
FrameIdResponse::FrameIdResponse() {
}
uint8_t FrameIdResponse::getFrameId() {
return getFrameData()[0];
}
ModemStatusResponse::ModemStatusResponse() {
}
uint8_t ModemStatusResponse::getStatus() {
return getFrameData()[0];
}
void XBeeResponse::getModemStatusResponse(XBeeResponse &modemStatusResponse) {
ModemStatusResponse* modem = static_cast<ModemStatusResponse*>(&modemStatusResponse);
// pass pointer array to subclass
modem->setFrameData(getFrameData());
setCommon(modemStatusResponse);
}
AtCommandResponse::AtCommandResponse() {
}
uint8_t* AtCommandResponse::getCommand() {
return getFrameData() + 1;
}
uint8_t AtCommandResponse::getStatus() {
return getFrameData()[3];
}
uint8_t AtCommandResponse::getValueLength() {
return getFrameDataLength() - 4;
}
uint8_t* AtCommandResponse::getValue() {
if (getValueLength() > 0) {
// value is only included for query commands. set commands does not return a value
return getFrameData() + 4;
}
return NULL;
}
bool AtCommandResponse::isOk() {
return getStatus() == AT_OK;
}
void XBeeResponse::getAtCommandResponse(XBeeResponse &atCommandResponse) {
AtCommandResponse* at = static_cast<AtCommandResponse*>(&atCommandResponse);
// pass pointer array to subclass
at->setFrameData(getFrameData());
setCommon(atCommandResponse);
}
uint16_t XBeeResponse::getPacketLength() {
return ((_msbLength << 8) & 0xff) + (_lsbLength & 0xff);
}
uint8_t* XBeeResponse::getFrameData() {
return _frameDataPtr;
}
void XBeeResponse::setFrameData(uint8_t* frameDataPtr) {
_frameDataPtr = frameDataPtr;
}
void XBeeResponse::init() {
_complete = false;
_errorCode = NO_ERROR;
_checksum = 0;
}
void XBeeResponse::reset() {
init();
_apiId = 0;
_msbLength = 0;
_lsbLength = 0;
_checksum = 0;
_frameLength = 0;
for (int i = 0; i < MAX_FRAME_DATA_SIZE; i++) {
getFrameData()[i] = 0;
}
}
void XBee::resetResponse() {
_pos = 0;
_escape = false;
_response.reset();
}
XBee::XBee(): _response(XBeeResponse()) {
_pos = 0;
_escape = false;
_checksumTotal = 0;
_nextFrameId = 0;
_xbeeSerial = &Serial;
_response.init();
_response.setFrameData(_responseFrameData);
}
uint8_t XBee::getNextFrameId() {
_nextFrameId++;
if (_nextFrameId == 0) {
// can't send 0 because that disables status response
_nextFrameId = 1;
}
return _nextFrameId;
}
void XBee::begin(long baud) {
_xbeeSerial->begin(baud);
}
void XBee::setSerial(HardwareSerial &serial) {
_xbeeSerial = &serial;
}
XBeeResponse& XBee::getResponse() {
return _response;
}
// TODO how to convert response to proper subclass?
void XBee::getResponse(XBeeResponse &response) {
response.setMsbLength(_response.getMsbLength());
response.setLsbLength(_response.getLsbLength());
response.setApiId(_response.getApiId());
response.setFrameLength(_response.getFrameDataLength());
response.setFrameData(_response.getFrameData());
}
void XBee::readPacketUntilAvailable() {
while (!(getResponse().isAvailable() || getResponse().isError())) {
// read some more
readPacket();
}
}
bool XBee::readPacket(int timeout) {
if (timeout < 0) {
return false;
}
unsigned long start = millis();
while (int((millis() - start)) < timeout) {
readPacket();
if (getResponse().isAvailable()) {
return true;
} else if (getResponse().isError()) {
return false;
}
}
// timed out
return false;
}
void XBee::readPacket() {
// reset previous response
if (_response.isAvailable() || _response.isError()) {
// discard previous packet and start over
resetResponse();
}
while (_xbeeSerial->available()) {
b = _xbeeSerial->read();
if (_pos > 0 && b == START_BYTE && ATAP == 2) {
// new packet start before previous packeted completed -- discard previous packet and start over
_response.setErrorCode(UNEXPECTED_START_BYTE);
return;
}
if (_pos > 0 && b == ESCAPE) {
if (_xbeeSerial->available()) {
b = _xbeeSerial->read();
b = 0x20 ^ b;
} else {
// escape byte. next byte will be
_escape = true;
continue;
}
}
if (_escape == true) {
b = 0x20 ^ b;
_escape = false;
}
// checksum includes all bytes starting with api id
if (_pos >= API_ID_INDEX) {
_checksumTotal+= b;
}
switch(_pos) {
case 0:
if (b == START_BYTE) {
_pos++;
}
break;
case 1:
// length msb
_response.setMsbLength(b);
_pos++;
break;
case 2:
// length lsb
_response.setLsbLength(b);
_pos++;
break;
case 3:
_response.setApiId(b);
_pos++;
break;
default:
// starts at fifth byte
if (_pos > MAX_FRAME_DATA_SIZE) {
// exceed max size. should never occur
_response.setErrorCode(PACKET_EXCEEDS_BYTE_ARRAY_LENGTH);
return;
}
// check if we're at the end of the packet
// packet length does not include start, length, or checksum bytes, so add 3
if (_pos == (_response.getPacketLength() + 3)) {
// verify checksum
//std::cout << "read checksum " << static_cast<unsigned int>(b) << " at pos " << static_cast<unsigned int>(_pos) << std::endl;
if ((_checksumTotal & 0xff) == 0xff) {
_response.setChecksum(b);
_response.setAvailable(true);
_response.setErrorCode(NO_ERROR);
} else {
// checksum failed
_response.setErrorCode(CHECKSUM_FAILURE);
}
// minus 4 because we start after start,msb,lsb,api and up to but not including checksum
// e.g. if frame was one byte, _pos=4 would be the byte, pos=5 is the checksum, where end stop reading
_response.setFrameLength(_pos - 4);
// reset state vars
_pos = 0;
_checksumTotal = 0;
return;
} else {
// add to packet array, starting with the fourth byte of the apiFrame
_response.getFrameData()[_pos - 4] = b;
_pos++;
}
}
}
}
// it's peanut butter jelly time!!
XBeeRequest::XBeeRequest(uint8_t apiId, uint8_t frameId) {
_apiId = apiId;
_frameId = frameId;
}
void XBeeRequest::setFrameId(uint8_t frameId) {
_frameId = frameId;
}
uint8_t XBeeRequest::getFrameId() {
return _frameId;
}
uint8_t XBeeRequest::getApiId() {
return _apiId;
}
void XBeeRequest::setApiId(uint8_t apiId) {
_apiId = apiId;
}
//void XBeeRequest::reset() {
// _frameId = DEFAULT_FRAME_ID;
//}
//uint8_t XBeeRequest::getPayloadOffset() {
// return _payloadOffset;
//}
//
//uint8_t XBeeRequest::setPayloadOffset(uint8_t payloadOffset) {
// _payloadOffset = payloadOffset;
//}
PayloadRequest::PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, uint8_t payloadLength) : XBeeRequest(apiId, frameId) {
_payloadPtr = payload;
_payloadLength = payloadLength;
}
uint8_t* PayloadRequest::getPayload() {
return _payloadPtr;
}
void PayloadRequest::setPayload(uint8_t* payload) {
_payloadPtr = payload;
}
uint8_t PayloadRequest::getPayloadLength() {
return _payloadLength;
}
void PayloadRequest::setPayloadLength(uint8_t payloadLength) {
_payloadLength = payloadLength;
}
XBeeAddress::XBeeAddress() {
}
XBeeAddress64::XBeeAddress64() : XBeeAddress() {
}
XBeeAddress64::XBeeAddress64(uint32_t msb, uint32_t lsb) : XBeeAddress() {
_msb = msb;
_lsb = lsb;
}
uint32_t XBeeAddress64::getMsb() {
return _msb;
}
void XBeeAddress64::setMsb(uint32_t msb) {
_msb = msb;
}
uint32_t XBeeAddress64::getLsb() {
return _lsb;
}
void XBeeAddress64::setLsb(uint32_t lsb) {
_lsb = lsb;
}
#ifdef SERIES_2
ZBTxRequest::ZBTxRequest() : PayloadRequest(ZB_TX_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
}
ZBTxRequest::ZBTxRequest(XBeeAddress64 &addr64, uint16_t addr16, uint8_t broadcastRadius, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId): PayloadRequest(ZB_TX_REQUEST, frameId, data, dataLength) {
_addr64 = addr64;
_addr16 = addr16;
_broadcastRadius = broadcastRadius;
_option = option;
}
ZBTxRequest::ZBTxRequest(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLength): PayloadRequest(ZB_TX_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
_addr64 = addr64;
_addr16 = ZB_BROADCAST_ADDRESS;
_broadcastRadius = ZB_BROADCAST_RADIUS_MAX_HOPS;
_option = ZB_TX_UNICAST;
}
uint8_t ZBTxRequest::getFrameData(uint8_t pos) {
if (pos == 0) {
return (_addr64.getMsb() >> 24) & 0xff;
} else if (pos == 1) {
return (_addr64.getMsb() >> 16) & 0xff;
} else if (pos == 2) {
return (_addr64.getMsb() >> 8) & 0xff;
} else if (pos == 3) {
return _addr64.getMsb() & 0xff;
} else if (pos == 4) {
return (_addr64.getLsb() >> 24) & 0xff;
} else if (pos == 5) {
return (_addr64.getLsb() >> 16) & 0xff;
} else if (pos == 6) {
return (_addr64.getLsb() >> 8) & 0xff;
} else if (pos == 7) {
return _addr64.getLsb() & 0xff;
} else if (pos == 8) {
return (_addr16 >> 8) & 0xff;
} else if (pos == 9) {
return _addr16 & 0xff;
} else if (pos == 10) {
return _broadcastRadius;
} else if (pos == 11) {
return _option;
} else {
return getPayload()[pos - ZB_TX_API_LENGTH];
}
}
uint8_t ZBTxRequest::getFrameDataLength() {
return ZB_TX_API_LENGTH + getPayloadLength();
}
XBeeAddress64& ZBTxRequest::getAddress64() {
return _addr64;
}
uint16_t ZBTxRequest::getAddress16() {
return _addr16;
}
uint8_t ZBTxRequest::getBroadcastRadius() {
return _broadcastRadius;
}
uint8_t ZBTxRequest::getOption() {
return _option;
}