forked from vshymanskyy/TinyGSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyGsmClientXBee.h
1655 lines (1446 loc) · 49 KB
/
TinyGsmClientXBee.h
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
/**
* @file TinyGsmClientXBee.h
* @author Volodymyr Shymanskyy
* @license LGPL-3.0
* @copyright Copyright (c) 2016 Volodymyr Shymanskyy, XBee module by Sara
* Damiano
* @date Nov 2016
*/
#ifndef SRC_TINYGSMCLIENTXBEE_H_
#define SRC_TINYGSMCLIENTXBEE_H_
// #pragma message("TinyGSM: TinyGsmClientXBee")
// #define TINY_GSM_DEBUG Serial
// XBee's do not support multi-plexing in transparent/command mode
// The much more complicated API mode is needed for multi-plexing
#define TINY_GSM_MUX_COUNT 1
#define TINY_GSM_NO_MODEM_BUFFER
// XBee's have a default guard time of 1 second (1000ms, 10 extra for safety
// here)
#define TINY_GSM_XBEE_GUARD_TIME 1010
#ifdef AT_NL
#undef AT_NL
#endif
#define AT_NL "\r"
#ifdef MODEM_MANUFACTURER
#undef MODEM_MANUFACTURER
#endif
#define MODEM_MANUFACTURER "Digi"
#ifdef MODEM_MODEL
#undef MODEM_MODEL
#endif
#define MODEM_MODEL "XBee"
#include "TinyGsmModem.tpp"
#include "TinyGsmWifi.tpp"
#include "TinyGsmGPRS.tpp"
#include "TinyGsmTCP.tpp"
#include "TinyGsmSSL.tpp"
#include "TinyGsmSMS.tpp"
#include "TinyGsmTemperature.tpp"
#include "TinyGsmBattery.tpp"
// Use this to avoid too many entrances and exits from command mode.
// The cellular Bee's often freeze up and won't respond when attempting
// to enter command mode too many times.
#define XBEE_COMMAND_START_DECORATOR(nAttempts, failureReturn) \
bool wasInCommandMode = inCommandMode; \
if (!wasInCommandMode) { /* don't re-enter command mode if already in it */ \
if (!commandMode(nAttempts)) \
return failureReturn; /* Return immediately if fails */ \
}
#define XBEE_COMMAND_END_DECORATOR \
if (!wasInCommandMode) { /* only exit if we weren't in command mode */ \
exitCommand(); \
}
enum XBeeRegStatus {
REG_OK = 0,
REG_UNREGISTERED = 1,
REG_SEARCHING = 2,
REG_DENIED = 3,
REG_UNKNOWN = 4,
};
// These are responses to the HS command to get "hardware series"
enum XBeeType {
XBEE_UNKNOWN = 0,
XBEE_S6B_WIFI = 0x601, // Digi XBee Wi-Fi
XBEE_LTE1_VZN = 0xB01, // Digi XBee Cellular LTE Cat 1
XBEE_3G = 0xB02, // Digi XBee Cellular 3G
XBEE3_LTE1_ATT = 0xB06, // Digi XBee3 Cellular LTE CAT 1
XBEE3_LTEM_ATT = 0xB08, // Digi XBee3 Cellular LTE-M
};
class TinyGsmXBee : public TinyGsmModem<TinyGsmXBee>,
public TinyGsmGPRS<TinyGsmXBee>,
public TinyGsmWifi<TinyGsmXBee>,
public TinyGsmTCP<TinyGsmXBee, TINY_GSM_MUX_COUNT>,
public TinyGsmSSL<TinyGsmXBee, TINY_GSM_MUX_COUNT>,
public TinyGsmSMS<TinyGsmXBee>,
public TinyGsmBattery<TinyGsmXBee>,
public TinyGsmTemperature<TinyGsmXBee> {
friend class TinyGsmModem<TinyGsmXBee>;
friend class TinyGsmGPRS<TinyGsmXBee>;
friend class TinyGsmWifi<TinyGsmXBee>;
friend class TinyGsmTCP<TinyGsmXBee, TINY_GSM_MUX_COUNT>;
friend class TinyGsmSSL<TinyGsmXBee, TINY_GSM_MUX_COUNT>;
friend class TinyGsmSMS<TinyGsmXBee>;
friend class TinyGsmBattery<TinyGsmXBee>;
friend class TinyGsmTemperature<TinyGsmXBee>;
/*
* Inner Client
*/
public:
class GsmClientXBee : public GsmClient {
friend class TinyGsmXBee;
public:
GsmClientXBee() {}
explicit GsmClientXBee(TinyGsmXBee& modem, uint8_t mux = 0) {
init(&modem, mux);
}
bool init(TinyGsmXBee* modem, uint8_t = 0) {
this->at = modem;
this->mux = 0;
sock_connected = false;
at->sockets[0] = this;
return true;
}
public:
// NOTE: The XBee saves all connection information (ssid/pwd etc) except
// IP address and port number, in flash (NVM).
// The NVM is be updated only when it is initialized.
// The TCP connection itself is not opened until you attempt to send data.
// Because all settings are saved to flash, it is possible (or likely) that
// you could send data even if you haven't "made" any connection.
virtual int connect(const char* host, uint16_t port, int timeout_s) {
// NOTE: Not caling stop() or yeild() here
at->streamClear(); // Empty anything in the buffer before starting
sock_connected = at->modemConnect(host, port, mux, false, timeout_s);
return sock_connected;
}
int connect(const char* host, uint16_t port) override {
return connect(host, port, 75);
}
virtual int connect(IPAddress ip, uint16_t port, int timeout_s) {
if (timeout_s != 0) {
DBG("Timeout [", timeout_s, "] doesn't apply here.");
}
// NOTE: Not caling stop() or yeild() here
at->streamClear(); // Empty anything in the buffer before starting
sock_connected = at->modemConnect(ip, port, mux, false);
return sock_connected;
}
int connect(IPAddress ip, uint16_t port) override {
return connect(ip, port, 0);
}
void stop(uint32_t maxWaitMs) {
at->streamClear(); // Empty anything in the buffer
// empty the saved currently-in-use destination address
at->modemStop(maxWaitMs);
at->streamClear(); // Empty anything in the buffer
sock_connected = false;
// Note: because settings are saved in flash, the XBEE will attempt to
// reconnect to the previous socket if it receives any outgoing data.
// Setting sock_connected to false after the stop ensures that connected()
// will return false after a stop has been ordered. This makes it play
// much more nicely with libraries like PubSubClient.
}
void stop() override {
stop(5000L);
}
size_t write(const uint8_t* buf, size_t size) override {
TINY_GSM_YIELD();
return at->modemSend(buf, size, mux);
}
size_t write(uint8_t c) override {
return write(&c, 1);
}
size_t write(const char* str) {
if (str == nullptr) return 0;
return write((const uint8_t*)str, strlen(str));
}
int available() override {
TINY_GSM_YIELD();
return at->stream.available();
/*
if (!rx.size() || at->stream.available()) {
at->maintain();
}
return at->stream.available() + rx.size();
*/
}
int read(uint8_t* buf, size_t size) override {
TINY_GSM_YIELD();
return at->stream.readBytes(reinterpret_cast<char*>(buf), size);
/*
size_t cnt = 0;
uint32_t _startMillis = millis();
while (cnt < size && millis() - _startMillis < _timeout) {
size_t chunk = TinyGsmMin(size-cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
}
// TODO(vshymanskyy): Read directly into user buffer?
if (!rx.size() || at->stream.available()) {
at->maintain();
}
}
return cnt;
*/
}
int read() override {
TINY_GSM_YIELD();
return at->stream.read();
/*
uint8_t c;
if (read(&c, 1) == 1) {
return c;
}
return -1;
*/
}
int peek() override {
return at->stream.peek();
}
void flush() override {
at->stream.flush();
}
uint8_t connected() override {
if (available()) {
return true;
// if we never got an IP, it can't be connected
} else if (at->savedIP == IPAddress(0, 0, 0, 0)) {
return false;
}
return sock_connected;
// NOTE: We don't check or return
// modemGetConnected() because we don't
// want to go into command mode.
// return at->modemGetConnected();
}
operator bool() override {
return connected();
}
/*
* Extended API
*/
String remoteIP() {
IPAddress savedIP = at->savedIP;
return TinyGsmStringFromIp(savedIP);
}
};
/*
* Inner Secure Client
*/
public:
class GsmClientSecureXBee : public GsmClientXBee {
public:
GsmClientSecureXBee() {}
explicit GsmClientSecureXBee(TinyGsmXBee& modem, uint8_t mux = 0)
: GsmClientXBee(modem, mux) {}
public:
int connect(const char* host, uint16_t port, int timeout_s) override {
// NOTE: Not caling stop() or yeild() here
at->streamClear(); // Empty anything in the buffer before starting
sock_connected = at->modemConnect(host, port, mux, true, timeout_s);
return sock_connected;
}
int connect(const char* host, uint16_t port) override {
return connect(host, port, 75);
}
int connect(IPAddress ip, uint16_t port, int timeout_s) override {
if (timeout_s != 0) {
DBG("Timeout [", timeout_s, "] doesn't apply here.");
}
// NOTE: Not caling stop() or yeild() here
at->streamClear(); // Empty anything in the buffer before starting
sock_connected = at->modemConnect(ip, port, mux, true);
return sock_connected;
}
int connect(IPAddress ip, uint16_t port) override {
return connect(ip, port, 0);
}
};
/*
* Constructor
*/
public:
explicit TinyGsmXBee(Stream& stream)
: stream(stream),
guardTime(TINY_GSM_XBEE_GUARD_TIME),
beeType(XBEE_UNKNOWN),
resetPin(-1),
savedIP(IPAddress(0, 0, 0, 0)),
savedHost(""),
savedHostIP(IPAddress(0, 0, 0, 0)),
savedOperatingIP(IPAddress(0, 0, 0, 0)),
inCommandMode(false),
lastCommandModeMillis(0) {
// Start not knowing what kind of bee it is
// Start with the default guard time of 1 second
memset(sockets, 0, sizeof(sockets));
}
TinyGsmXBee(Stream& stream, int8_t resetPin)
: stream(stream),
guardTime(TINY_GSM_XBEE_GUARD_TIME),
beeType(XBEE_UNKNOWN),
resetPin(resetPin),
savedIP(IPAddress(0, 0, 0, 0)),
savedHost(""),
savedHostIP(IPAddress(0, 0, 0, 0)),
savedOperatingIP(IPAddress(0, 0, 0, 0)),
inCommandMode(false),
lastCommandModeMillis(0) {
// Start not knowing what kind of bee it is
// Start with the default guard time of 1 second
memset(sockets, 0, sizeof(sockets));
}
/*
* Basic functions
*/
protected:
bool initImpl(const char* pin = nullptr) {
DBG(GF("### TinyGSM Version:"), TINYGSM_VERSION);
DBG(GF("### TinyGSM Compiled Module: TinyGsmClientXBee"));
if (resetPin >= 0) {
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
}
XBEE_COMMAND_START_DECORATOR(10, false)
bool changesMade = false;
bool ret_val = true;
// if there's a pin, we need to re-write to flash each time
if (pin && strlen(pin) > 0) {
sendAT(GF("PN"), pin);
if (waitResponse() != 1) {
ret_val = false;
} else {
changesMade = true;
}
}
// Put in transparent mode, if it isn't already
changesMade |= changeSettingIfNeeded(GF("AP"), 0x0);
// shorten the guard time to 100ms, if it was anything else
sendAT(GF("GT"));
if (readResponseInt() != 0x64) {
sendAT(GF("GT"), 64);
ret_val &= waitResponse() == 1;
if (ret_val) {
guardTime = 110;
changesMade = true;
}
} else {
guardTime = 110;
}
// Make sure the command mode drop-out time is long enough that we won't
// fall out of command mode without intentionally leaving it. This is the
// default drop out time of 0x64 x 100ms (10 seconds)
changesMade |= changeSettingIfNeeded(GF("CT"), 0x64);
if (changesMade) { ret_val &= writeChanges(); }
getSeries(); // Get the "Hardware Series";
XBEE_COMMAND_END_DECORATOR
return ret_val;
}
String getModemNameImpl() {
return getBeeName();
}
String getModemModelImpl() {
switch (beeType) {
case XBEE_S6B_WIFI: return "XBee Wi-Fi";
case XBEE_LTE1_VZN: return "XBee Cellular LTE Cat 1";
case XBEE_3G: return "XBee Cellular 3G";
case XBEE3_LTE1_ATT: return "XBee3 Cellular LTE CAT 1";
case XBEE3_LTEM_ATT: return "XBee3 Cellular LTE-M";
default: return "XBee Unknown";
}
}
// Gets the modem serial number
String getModemSerialNumberImpl() {
String xbeeSnLow =
sendATGetString(GF("SL")); // Request Module MAC/Serial Number Low
String xbeeSnHigh =
sendATGetString(GF("SH")); // Request Module MAC/Serial Number High
return xbeeSnLow + String(" ") + xbeeSnHigh;
}
// Gets the modem hardware version
String getModemHardwareVersion() {
return sendATGetString(GF("HV"));
}
// Gets the modem firmware version
String getModemFirmwareVersion() {
return sendATGetString(GF("VR"));
}
// Gets the modem combined version
String getModemRevisionImpl() {
String hw_ver = getModemHardwareVersion();
String fw_ver = getModemFirmwareVersion();
return hw_ver + String(" ") + fw_ver;
}
bool setBaudImpl(uint32_t baud) {
XBEE_COMMAND_START_DECORATOR(5, false)
bool changesMade = false;
switch (baud) {
case 2400: changesMade |= changeSettingIfNeeded(GF("BD"), 0x1); break;
case 4800: changesMade |= changeSettingIfNeeded(GF("BD"), 0x2); break;
case 9600: changesMade |= changeSettingIfNeeded(GF("BD"), 0x3); break;
case 19200: changesMade |= changeSettingIfNeeded(GF("BD"), 0x4); break;
case 38400: changesMade |= changeSettingIfNeeded(GF("BD"), 0x5); break;
case 57600: changesMade |= changeSettingIfNeeded(GF("BD"), 0x6); break;
case 115200: changesMade |= changeSettingIfNeeded(GF("BD"), 0x7); break;
case 230400: changesMade |= changeSettingIfNeeded(GF("BD"), 0x8); break;
case 460800: changesMade |= changeSettingIfNeeded(GF("BD"), 0x9); break;
case 921600: changesMade |= changeSettingIfNeeded(GF("BD"), 0xA); break;
default: {
DBG(GF("Specified baud rate is unsupported! Setting to 9600 baud."));
changesMade |= changeSettingIfNeeded(GF("BD"),
0x3); // Set to default of 9600
break;
}
}
if (changesMade) { writeChanges(); }
XBEE_COMMAND_END_DECORATOR
return true;
}
bool testATImpl(uint32_t timeout_ms = 10000L) {
uint32_t start = millis();
bool success = false;
while (!success && millis() - start < timeout_ms) {
if (!inCommandMode) {
success = commandMode();
if (success) exitCommand();
} else {
sendAT();
if (waitResponse(200) == 1) {
success = true;
} else {
// if we didn't respond to the AT, assume we're not in command mode
inCommandMode = false;
}
}
delay(250);
}
return success;
}
void maintainImpl() {
// this only happens OUTSIDE command mode, so if we're getting characters
// they should be data received from the TCP connection
// TINY_GSM_YIELD();
// if (!inCommandMode) {
// while (stream.available()) {
// char c = stream.read();
// if (c > 0) sockets[0]->rx.put(c);
// }
// }
}
bool factoryDefaultImpl() {
XBEE_COMMAND_START_DECORATOR(5, false)
sendAT(GF("RE"));
bool ret_val = waitResponse() == 1;
ret_val &= writeChanges();
XBEE_COMMAND_END_DECORATOR
// Make sure the guard time for the modem object is set back to default
// otherwise communication would fail after the reset
guardTime = 1010;
return ret_val;
}
String getModemInfoImpl() {
return sendATGetString(GF("HS"));
}
/*
bool thisHasSSL() {
if (beeType == XBEE_S6B_WIFI)
return false;
else
return true;
}
bool thisHasWifi() {
if (beeType == XBEE_S6B_WIFI)
return true;
else
return false;
}
bool thisHasGPRS() {
if (beeType == XBEE_S6B_WIFI)
return false;
else
return true;
}
*/
public:
XBeeType getBeeType() {
return beeType;
}
String getBeeName() {
switch (beeType) {
case XBEE_S6B_WIFI: return "Digi XBee Wi-Fi";
case XBEE_LTE1_VZN: return "Digi XBee Cellular LTE Cat 1";
case XBEE_3G: return "Digi XBee Cellular 3G";
case XBEE3_LTE1_ATT: return "Digi XBee3 Cellular LTE CAT 1";
case XBEE3_LTEM_ATT: return "Digi XBee3 Cellular LTE-M";
default: return "Digi XBee Unknown";
}
}
/*
* Power functions
*/
protected:
// The XBee's have a bad habit of getting into an unresponsive funk
// This uses the board's hardware reset pin to force it to reset
void pinReset() {
if (resetPin >= 0) {
DBG("### Forcing a modem reset!\r\n");
digitalWrite(resetPin, LOW);
delay(1);
digitalWrite(resetPin, HIGH);
} else {
DBG("### Attempting a modem software restart");
restartImpl();
}
}
bool restartImpl(const char* pin = nullptr) {
if (!commandMode()) {
DBG("### XBee not in command mode for restart; Exit");
return false;
} // Return immediately
if (beeType == XBEE_UNKNOWN) getSeries(); // how we restart depends on this
if (beeType != XBEE_S6B_WIFI) {
sendAT(GF("AM1")); // Digi suggests putting cellular modules into
// airplane mode before restarting This allows the
// sockets and connections to close cleanly
if (waitResponse() != 1) return exitAndFail();
if (!writeChanges()) return exitAndFail();
}
sendAT(GF("FR"));
if (waitResponse() != 1)
return exitAndFail();
else
inCommandMode = false; // Reset effectively exits command mode
if (beeType == XBEE_S6B_WIFI)
delay(2000); // Wifi module actually resets about 2 seconds later
else
delay(100); // cellular modules wait 100ms before reset happens
// Wait until reboot completes and XBee responds to command mode call again
for (uint32_t start = millis(); millis() - start < 60000L;) {
if (commandMode(1)) break;
delay(250); // wait a litle before trying again
}
if (beeType != XBEE_S6B_WIFI) {
sendAT(GF("AM0")); // Turn off airplane mode
if (waitResponse() != 1) return exitAndFail();
if (!writeChanges()) return exitAndFail();
}
exitCommand();
return init(pin);
}
void setupPinSleep(bool maintainAssociation = false) {
XBEE_COMMAND_START_DECORATOR(5, )
if (beeType == XBEE_UNKNOWN) getSeries(); // Command depends on series
bool changesMade = false;
// Pin sleep
changesMade |= changeSettingIfNeeded(GF("SM"), 0x1);
if (beeType == XBEE_S6B_WIFI && !maintainAssociation) {
// For lowest power, dissassociated deep sleep
changesMade |= changeSettingIfNeeded(GF("SO"), 0x200);
} else if (!maintainAssociation) {
// For supported cellular modules, maintain association
// Not supported by all modules, will return "ERROR"
changesMade |= changeSettingIfNeeded(GF("SO"), 0x1);
}
if (changesMade) { writeChanges(); }
XBEE_COMMAND_END_DECORATOR
}
bool
powerOffImpl() { // NOTE: Not supported for WiFi or older cellular firmware
XBEE_COMMAND_START_DECORATOR(5, false)
sendAT(GF("SD"));
bool ret_val = waitResponse(120000L) == 1;
// make sure we're really shut down
if (ret_val) { ret_val &= (sendATGetString(GF("AI")) == "2D"); }
XBEE_COMMAND_END_DECORATOR
return ret_val;
}
// Enable airplane mode
bool radioOffImpl() {
bool success = true;
bool changesMade = false;
XBEE_COMMAND_START_DECORATOR(5, false)
changesMade = changeSettingIfNeeded(GF("AM"), 0x1, 5000L);
if (changesMade) { success = writeChanges(); }
XBEE_COMMAND_END_DECORATOR
return success;
}
bool sleepEnableImpl(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool setPhoneFunctionalityImpl(uint8_t fun, bool reset = false)
TINY_GSM_ATTR_NOT_IMPLEMENTED;
/*
* Generic network functions
*/
public:
XBeeRegStatus getRegistrationStatus() {
XBEE_COMMAND_START_DECORATOR(5, REG_UNKNOWN)
if (!inCommandMode) return REG_UNKNOWN; // Return immediately
if (beeType == XBEE_UNKNOWN)
getSeries(); // Need to know the bee type to interpret response
sendAT(GF("AI"));
int16_t intRes = readResponseInt(10000L);
XBeeRegStatus stat = REG_UNKNOWN;
switch (beeType) {
case XBEE_S6B_WIFI: {
switch (intRes) {
case 0x00: // 0x00 Successfully joined an access point, established
// IP addresses and IP listening sockets
stat = REG_OK;
break;
case 0x01: // 0x01 Wi-Fi transceiver initialization in progress.
case 0x02: // 0x02 Wi-Fi transceiver initialized, but not yet
// scanning for access point.
case 0x40: // 0x40 Waiting for WPA or WPA2 Authentication.
case 0x41: // 0x41 Device joined a network and is waiting for IP
// configuration to complete
case 0x42: // 0x42 Device is joined, IP is configured, and listening
// sockets are being set up.
case 0xFF: // 0xFF Device is currently scanning for the configured
// SSID.
stat = REG_SEARCHING;
break;
case 0x13: // 0x13 Disconnecting from access point.
restart(); // Restart the device; the S6B tends to get stuck
// "disconnecting"
stat = REG_UNREGISTERED;
break;
case 0x23: // 0x23 SSID not configured.
stat = REG_UNREGISTERED;
break;
case 0x24: // 0x24 Encryption key invalid (either NULL or invalid
// length for WEP).
case 0x27: // 0x27 SSID was found, but join failed.
stat = REG_DENIED;
break;
default: stat = REG_UNKNOWN; break;
}
break;
}
default: { // Cellular XBee's
switch (intRes) {
case 0x00: // 0x00 Connected to the Internet.
stat = REG_OK;
break;
case 0x22: // 0x22 Registering to cellular network.
case 0x23: // 0x23 Connecting to the Internet.
case 0xFF: // 0xFF Initializing.
stat = REG_SEARCHING;
break;
case 0x24: // 0x24 The cellular component is missing, corrupt, or
// otherwise in error.
case 0x2B: // 0x2B USB Direct active.
case 0x2C: // 0x2C Cellular component is in PSM (power save mode).
stat = REG_UNKNOWN;
break;
case 0x25: // 0x25 Cellular network registration denied.
stat = REG_DENIED;
break;
case 0x2A: // 0x2A Airplane mode.
sendAT(GF("AM0")); // Turn off airplane mode
waitResponse();
writeChanges();
stat = REG_UNKNOWN;
break;
case 0x2F: // 0x2F Bypass mode active.
sendAT(GF("AP0")); // Set back to transparent mode
waitResponse();
writeChanges();
stat = REG_UNKNOWN;
break;
default: stat = REG_UNKNOWN; break;
}
break;
}
}
XBEE_COMMAND_END_DECORATOR
return stat;
}
protected:
int8_t getSignalQualityImpl() {
XBEE_COMMAND_START_DECORATOR(5, 0);
if (beeType == XBEE_UNKNOWN)
getSeries(); // Need to know what type of bee so we know how to ask
if (beeType == XBEE_S6B_WIFI)
sendAT(GF("LM")); // ask for the "link margin" - the dB above sensitivity
else
sendAT(GF("DB")); // ask for the cell strength in dBm
int16_t intRes = readResponseInt();
XBEE_COMMAND_END_DECORATOR
if (beeType == XBEE3_LTEM_ATT && intRes == 105)
intRes = 0; // tends to reply with "69" when signal is unknown
if (beeType == XBEE_S6B_WIFI) {
if (intRes == 0xFF) {
return 0; // 0xFF returned for unknown
} else {
return -93 + intRes; // the maximum sensitivity is -93dBm
}
} else {
return -1 * intRes; // need to convert to negative number
}
}
bool isNetworkConnectedImpl() {
// first check for association indicator
XBeeRegStatus s = getRegistrationStatus();
if (s == REG_OK) {
if (beeType == XBEE_S6B_WIFI) {
// For wifi bees, if the association indicator is ok, check that a both
// a local IP and DNS have been allocated
IPAddress ip = localIP();
IPAddress dns = getDNSAddress();
if (ip != IPAddress(0, 0, 0, 0) && dns != IPAddress(0, 0, 0, 0)) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
bool waitForNetworkImpl(uint32_t timeout_ms = 60000L,
bool check_signal = false) {
bool retVal = false;
XBEE_COMMAND_START_DECORATOR(5, false)
for (uint32_t start = millis(); millis() - start < timeout_ms;) {
if (check_signal) { getSignalQuality(); }
if (isNetworkConnected()) {
retVal = true;
break;
}
delay(250); // per Neil H. - more stable with delay
}
XBEE_COMMAND_END_DECORATOR
return retVal;
}
String getLocalIPImpl() {
XBEE_COMMAND_START_DECORATOR(5, "")
sendAT(GF("MY"));
String IPaddr;
IPaddr.reserve(16);
// wait for the response - this response can be very slow
IPaddr = readResponseString(30000);
XBEE_COMMAND_END_DECORATOR
IPaddr.trim();
return IPaddr;
}
String getDNS() {
XBEE_COMMAND_START_DECORATOR(5, "")
switch (beeType) {
case XBEE_S6B_WIFI: {
sendAT(GF("NS"));
break;
}
default: {
sendAT(GF("N1"));
break;
}
}
String DNSaddr;
DNSaddr.reserve(16);
// wait for the response - this response can be very slow
DNSaddr = readResponseString(30000);
XBEE_COMMAND_END_DECORATOR
DNSaddr.trim();
return DNSaddr;
}
IPAddress getDNSAddress() {
return TinyGsmIpFromString(getDNS());
}
/*
* Secure socket layer (SSL) functions
*/
// Follows functions as inherited from TinyGsmSSL.tpp
/*
* WiFi functions
*/
protected:
bool networkConnectImpl(const char* ssid, const char* pwd) {
bool changesMade = false;
bool retVal = true;
XBEE_COMMAND_START_DECORATOR(5, false)
if (ssid == nullptr) retVal = false;
changesMade |= changeSettingIfNeeded(GF("ID"), ssid);
if (pwd && strlen(pwd) > 0) {
// Set security to WPA2
changesMade |= changeSettingIfNeeded(GF("EE"), 0x2);
// set the password
// the wifi bee will NOT return the previously set password,
// so we have no way of knowing if the passwords has changed
// and must re-write to flash each time
sendAT(GF("PK"), pwd);
if (waitResponse() != 1) {
retVal = false;
} else {
changesMade = true;
}
} else {
changesMade |= changeSettingIfNeeded(GF("EE"), 0x0); // Set No security
}
if (changesMade) { retVal &= writeChanges(); }
XBEE_COMMAND_END_DECORATOR
return retVal;
}
bool networkDisconnectImpl() {
XBEE_COMMAND_START_DECORATOR(5, false)
sendAT(GF("NR0")); // Do a network reset in order to disconnect
// WARNING: On wifi modules, using a network reset will not
// allow the same ssid to re-join without rebooting the module.
int8_t res = (1 == waitResponse(5000));
writeChanges();
XBEE_COMMAND_END_DECORATOR
return res;
}
/*
* GPRS functions
*/
protected:
bool gprsConnectImpl(const char* apn, const char* user = nullptr,
const char* pwd = nullptr) {
bool success = true;
bool changesMade = false;
XBEE_COMMAND_START_DECORATOR(5, false)
// the cellular bees will NOT return the previously set username or
// password, so we have no way of knowing if they have changed
// and must re-write to flash each time
if (user && strlen(user) > 0) {
sendAT(GF("CU"), user); // Set the user for the APN
if (waitResponse() != 1) {
success = false;
} else {
changesMade = true;
}
}
if (pwd && strlen(pwd) > 0) {
sendAT(GF("CW"), pwd); // Set the password for the APN
if (waitResponse() != 1) {
success = false;
} else {
changesMade = true;
}
}
changesMade |= changeSettingIfNeeded(GF("AN"), String(apn)); // Set the APN
changesMade |= changeSettingIfNeeded(GF("AM"), 0x0,
5000L); // Airplane mode off
if (changesMade) { success = writeChanges(); }
XBEE_COMMAND_END_DECORATOR
return success;
}
bool gprsDisconnectImpl() {
bool success = true;
XBEE_COMMAND_START_DECORATOR(5, false)
// Cheating and disconnecting by turning on airplane mode
bool changesMade = changeSettingIfNeeded(GF("AM"), 0x1, 5000L);
if (changesMade) { success = writeChanges(); }
XBEE_COMMAND_END_DECORATOR
return success;
}
bool isGprsConnectedImpl() {
return isNetworkConnected();
}
String getOperatorImpl() {
return sendATGetString(GF("MN"));
}
/*
* SIM card functions
*/
protected:
bool simUnlockImpl(const char* pin) {
if (pin && strlen(pin) > 0) {
sendAT(GF("PN"), pin);
return waitResponse() == 1;
}
return false;
}
String getSimCCIDImpl() {
return sendATGetString(GF("S#"));
}
String getIMEIImpl() {
return sendATGetString(GF("IM"));
}
String getIMSIImpl() {
return sendATGetString(GF("II"));
}
SimStatus getSimStatusImpl(uint32_t) {
return SIM_READY; // unsupported
}
/*
* Phone Call functions
*/
// No functions of this type supported
/*