My favorites | Sign in
Project Home Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 20 attachment: XBee.cpp (37.8 KB)

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
/**
* 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 "XBee.h"

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#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(i)) {
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_DIGIMESH

DMTxStatusResponse::DMTxStatusResponse() : FrameIdResponse() {

}

uint8_t DMTxStatusResponse::getTxRetryCount() {
return getFrameData()[3];
}

uint8_t DMTxStatusResponse::getDeliveryStatus() {
return getFrameData()[4];
}

uint8_t DMTxStatusResponse::getDiscoveryStatus() {
return getFrameData()[5];
}

bool DMTxStatusResponse::isSuccess() {
return getDeliveryStatus() == SUCCESS;
}

void XBeeResponse::getDMTxStatusResponse(XBeeResponse &dmXBeeResponse) {

// way off?
DMTxStatusResponse* dm = static_cast<DMTxStatusResponse*>(&dmXBeeResponse);
// pass pointer array to subclass
dm->setFrameData(getFrameData());
setCommon(dmXBeeResponse);
}


DMRxResponse::DMRxResponse() : RxDataResponse(){
_remoteAddress64 = XBeeAddress64();
}

uint8_t DMRxResponse::getOption() {
return getFrameData()[9];
}

// markers to read data from packet array. this is the index, so the 12th item in the array
uint8_t DMRxResponse::getDataOffset() {
return 1;
}

uint8_t DMRxResponse::getDataLength() {
return getPacketLength() - getDataOffset() - 1;
}

XBeeAddress64& DMRxResponse::getRemoteAddress64() {
return _remoteAddress64;
}

void XBeeResponse::getDMRxResponse(XBeeResponse &rxResponse) {

DMRxResponse* dm = static_cast<DMRxResponse*>(&rxResponse);

//TODO verify response api id matches this api for this response

// pass pointer array to subclass
dm->setFrameData(getFrameData());
setCommon(rxResponse);

dm->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
dm->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(i)) {
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;

_errorCode = NO_ERROR;

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;

_response.init();
_response.setFrameData(_responseFrameData);
// default
_serial = &Serial;
}

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) {
_serial->begin(baud);
}

void XBee::setSerial(HardwareSerial &serial) {
_serial = &serial;
}

bool XBee::available() {
return _serial->available();
}

uint8_t XBee::read() {
return _serial->read();
}

void XBee::flush() {
_serial->flush();
}

void XBee::write(uint8_t val) {
_serial->write(val);
}

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 (available()) {

b = 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 (available()) {
b = 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;
}

void ZBTxRequest::setAddress64(XBeeAddress64& addr64) {
_addr64 = addr64;
}

void ZBTxRequest::setAddress16(uint16_t addr16) {
_addr16 = addr16;
}

void ZBTxRequest::setBroadcastRadius(uint8_t broadcastRadius) {
_broadcastRadius = broadcastRadius;
}

void ZBTxRequest::setOption(uint8_t option) {
_option = option;
}

#endif


#ifdef SERIES_DIGIMESH

DMTxRequest::DMTxRequest() : PayloadRequest(DM_TX_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {

}

DMTxRequest::DMTxRequest(XBeeAddress64 &addr64, uint8_t broadcastRadius, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId): PayloadRequest(DM_TX_REQUEST, frameId, data, dataLength) {
_addr64 = addr64;
_broadcastRadius = broadcastRadius;
_option = option;
}

DMTxRequest::DMTxRequest(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLength): PayloadRequest(DM_TX_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
_addr64 = addr64;
_broadcastRadius = DM_BROADCAST_RADIUS_MAX_HOPS;
_option = 0X00;
}

uint8_t DMTxRequest::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 0xff;
} else if (pos == 9) {
return 0xfe;
} else if (pos == 10) {
return _broadcastRadius;
} else if (pos == 11) {
return _option;
} else {
return getPayload()[pos - DM_TX_API_LENGTH];
}
}

uint8_t DMTxRequest::getFrameDataLength() {
return DM_TX_API_LENGTH + getPayloadLength();
}

XBeeAddress64& DMTxRequest::getAddress64() {
return _addr64;
}

uint8_t DMTxRequest::getBroadcastRadius() {
return _broadcastRadius;
}

uint8_t DMTxRequest::getOption() {
return _option;
}

void DMTxRequest::setAddress64(XBeeAddress64& addr64) {
_addr64 = addr64;
}

void DMTxRequest::setBroadcastRadius(uint8_t broadcastRadius) {
_broadcastRadius = broadcastRadius;
}

void DMTxRequest::setOption(uint8_t option) {
_option = option;
}

#endif

#ifdef SERIES_1

Tx16Request::Tx16Request() : PayloadRequest(TX_16_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {

}

Tx16Request::Tx16Request(uint16_t addr16, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId) : PayloadRequest(TX_16_REQUEST, frameId, data, dataLength) {
_addr16 = addr16;
_option = option;
}

Tx16Request::Tx16Request(uint16_t addr16, uint8_t *data, uint8_t dataLength) : PayloadRequest(TX_16_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
_addr16 = addr16;
_option = ACK_OPTION;
}

uint8_t Tx16Request::getFrameData(uint8_t pos) {

if (pos == 0) {
return (_addr16 >> 8) & 0xff;
} else if (pos == 1) {
return _addr16 & 0xff;
} else if (pos == 2) {
return _option;
} else {
return getPayload()[pos - TX_16_API_LENGTH];
}
}

uint8_t Tx16Request::getFrameDataLength() {
return TX_16_API_LENGTH + getPayloadLength();
}

uint16_t Tx16Request::getAddress16() {
return _addr16;
}

void Tx16Request::setAddress16(uint16_t addr16) {
_addr16 = addr16;
}

uint8_t Tx16Request::getOption() {
return _option;
}

void Tx16Request::setOption(uint8_t option) {
_option = option;
}

Tx64Request::Tx64Request() : PayloadRequest(TX_64_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {

}

Tx64Request::Tx64Request(XBeeAddress64 &addr64, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId) : PayloadRequest(TX_64_REQUEST, frameId, data, dataLength) {
_addr64 = addr64;
_option = option;
}

Tx64Request::Tx64Request(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLength) : PayloadRequest(TX_64_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
_addr64 = addr64;
_option = ACK_OPTION;
}

uint8_t Tx64Request::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 _option;
} else {
return getPayload()[pos - TX_64_API_LENGTH];
}
}

uint8_t Tx64Request::getFrameDataLength() {
return TX_64_API_LENGTH + getPayloadLength();
}

XBeeAddress64& Tx64Request::getAddress64() {
return _addr64;
}

void Tx64Request::setAddress64(XBeeAddress64& addr64) {
_addr64 = addr64;
}

uint8_t Tx64Request::getOption() {
return _option;
}

void Tx64Request::setOption(uint8_t option) {
_option = option;
}

#endif

AtCommandRequest::AtCommandRequest() : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
_command = NULL;
clearCommandValue();
}

AtCommandRequest::AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
_command = command;
_commandValue = commandValue;
_commandValueLength = commandValueLength;
}

AtCommandRequest::AtCommandRequest(uint8_t *command) : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
_command = command;
clearCommandValue();
}

uint8_t* AtCommandRequest::getCommand() {
return _command;
}

uint8_t* AtCommandRequest::getCommandValue() {
return _commandValue;
}

uint8_t AtCommandRequest::getCommandValueLength() {
return _commandValueLength;
}

void AtCommandRequest::setCommand(uint8_t* command) {
_command = command;
}

void AtCommandRequest::setCommandValue(uint8_t* value) {
_commandValue = value;
}

void AtCommandRequest::setCommandValueLength(uint8_t length) {
_commandValueLength = length;
}

uint8_t AtCommandRequest::getFrameData(uint8_t pos) {

if (pos == 0) {
return _command[0];
} else if (pos == 1) {
return _command[1];
} else {
return _commandValue[pos - AT_COMMAND_API_LENGTH];
}
}

void AtCommandRequest::clearCommandValue() {
_commandValue = NULL;
_commandValueLength = 0;
}

//void AtCommandRequest::reset() {
// XBeeRequest::reset();
//}

uint8_t AtCommandRequest::getFrameDataLength() {
// command is 2 byte + length of value
return AT_COMMAND_API_LENGTH + _commandValueLength;
}

XBeeAddress64 RemoteAtCommandRequest::broadcastAddress64 = XBeeAddress64(0x0, BROADCAST_ADDRESS);

RemoteAtCommandRequest::RemoteAtCommandRequest() : AtCommandRequest(NULL, NULL, 0) {
_remoteAddress16 = 0;
_applyChanges = false;
setApiId(REMOTE_AT_REQUEST);
}

RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : AtCommandRequest(command, commandValue, commandValueLength) {
_remoteAddress64 = broadcastAddress64;
_remoteAddress16 = remoteAddress16;
_applyChanges = true;
setApiId(REMOTE_AT_REQUEST);
}

RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command) : AtCommandRequest(command, NULL, 0) {
_remoteAddress64 = broadcastAddress64;
_remoteAddress16 = remoteAddress16;
_applyChanges = false;
setApiId(REMOTE_AT_REQUEST);
}

RemoteAtCommandRequest::RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : AtCommandRequest(command, commandValue, commandValueLength) {
_remoteAddress64 = remoteAddress64;
// don't worry.. works for series 1 too!
_remoteAddress16 = ZB_BROADCAST_ADDRESS;
_applyChanges = true;
setApiId(REMOTE_AT_REQUEST);
}

RemoteAtCommandRequest::RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command) : AtCommandRequest(command, NULL, 0) {
_remoteAddress64 = remoteAddress64;
_remoteAddress16 = ZB_BROADCAST_ADDRESS;
_applyChanges = false;
setApiId(REMOTE_AT_REQUEST);
}

uint16_t RemoteAtCommandRequest::getRemoteAddress16() {
return _remoteAddress16;
}

void RemoteAtCommandRequest::setRemoteAddress16(uint16_t remoteAddress16) {
_remoteAddress16 = remoteAddress16;
}

XBeeAddress64& RemoteAtCommandRequest::getRemoteAddress64() {
return _remoteAddress64;
}

void RemoteAtCommandRequest::setRemoteAddress64(XBeeAddress64 &remoteAddress64) {
_remoteAddress64 = remoteAddress64;
}

bool RemoteAtCommandRequest::getApplyChanges() {
return _applyChanges;
}

void RemoteAtCommandRequest::setApplyChanges(bool applyChanges) {
_applyChanges = applyChanges;
}


uint8_t RemoteAtCommandRequest::getFrameData(uint8_t pos) {
if (pos == 0) {
return (_remoteAddress64.getMsb() >> 24) & 0xff;
} else if (pos == 1) {
return (_remoteAddress64.getMsb() >> 16) & 0xff;
} else if (pos == 2) {
return (_remoteAddress64.getMsb() >> 8) & 0xff;
} else if (pos == 3) {
return _remoteAddress64.getMsb() & 0xff;
} else if (pos == 4) {
return (_remoteAddress64.getLsb() >> 24) & 0xff;
} else if (pos == 5) {
return (_remoteAddress64.getLsb() >> 16) & 0xff;
} else if (pos == 6) {
return(_remoteAddress64.getLsb() >> 8) & 0xff;
} else if (pos == 7) {
return _remoteAddress64.getLsb() & 0xff;
} else if (pos == 8) {
return (_remoteAddress16 >> 8) & 0xff;
} else if (pos == 9) {
return _remoteAddress16 & 0xff;
} else if (pos == 10) {
return _applyChanges ? 2: 0;
} else if (pos == 11) {
return getCommand()[0];
} else if (pos == 12) {
return getCommand()[1];
} else {
return getCommandValue()[pos - REMOTE_AT_COMMAND_API_LENGTH];
}
}

uint8_t RemoteAtCommandRequest::getFrameDataLength() {
return REMOTE_AT_COMMAND_API_LENGTH + getCommandValueLength();
}


// TODO
//GenericRequest::GenericRequest(uint8_t* frame, uint8_t len, uint8_t apiId): XBeeRequest(apiId, *(frame), len) {
// _frame = frame;
//}

void XBee::send(XBeeRequest &request) {
// the new new deal

sendByte(START_BYTE, false);
//sendByte(0xaa, false);
// send length
uint8_t msbLen = ((request.getFrameDataLength() + 2) >> 8) & 0xff;
uint8_t lsbLen = (request.getFrameDataLength() + 2) & 0xff;

sendByte(msbLen, true);
sendByte(lsbLen, true);

// api id
sendByte(request.getApiId(), true);
sendByte(request.getFrameId(), true);

uint8_t checksum = 0;

// compute checksum, start at api id
checksum+= request.getApiId();
checksum+= request.getFrameId();

//std::cout << "frame length is " << static_cast<unsigned int>(request.getFrameDataLength()) << std::endl;

for (int i = 0; i < request.getFrameDataLength(); i++) {
// std::cout << "sending byte [" << static_cast<unsigned int>(i) << "] " << std::endl;
sendByte(request.getFrameData(i), true);
checksum+= request.getFrameData(i);
}

// perform 2s complement
checksum = 0xff - checksum;

// std::cout << "checksum is " << static_cast<unsigned int>(checksum) << std::endl;

// send checksum
sendByte(checksum, true);

// send packet (Note: prior to Arduino 1.0 this flushed the incoming buffer, which of course was not so great)
flush();
}

void XBee::sendByte(uint8_t b, bool escape) {

if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF)) {
// std::cout << "escaping byte [" << toHexString(b) << "] " << std::endl;
write(ESCAPE);
write(b ^ 0x20);
} else {
write(b);
}
}
Powered by Google Project Hosting