-
Notifications
You must be signed in to change notification settings - Fork 238
/
Adafruit_FONA.cpp
2967 lines (2525 loc) · 73.9 KB
/
Adafruit_FONA.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
/*!
* @file Adafruit_FONA.cpp
*
* @mainpage Adafruit FONA
*
* @section intro_sec Introduction
*
* This is a library for our Adafruit FONA Cellular Module
*
* Designed specifically to work with the Adafruit FONA
* ----> http://www.adafruit.com/products/1946
* ----> http://www.adafruit.com/products/1963
*
* These displays use TTL Serial to communicate, 2 pins are required to
* interface
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
* @section license License
*
* BSD license, all text above must be included in any redistribution
*
*/
// next line per
// http://postwarrior.com/arduino-ethershield-error-prog_char-does-not-name-a-type/
#include "Adafruit_FONA.h"
#if defined(ESP8266)
// ESP8266 doesn't have the min and max functions natively available like
// AVR libc seems to provide. Include the STL algorithm library to get these.
// Unfortunately algorithm isn't available in AVR libc so this is ESP8266
// specific (and likely needed for ARM or other platforms, but they lack
// software serial and are currently incompatible with the FONA library).
#include <algorithm>
using namespace std;
#endif
/**
* @brief Construct a new Adafruit_FONA object
*
* @param rst The reset pin
*/
Adafruit_FONA::Adafruit_FONA(int8_t rst) {
_rstpin = rst;
apn = F("FONAnet");
apnusername = 0;
apnpassword = 0;
mySerial = 0;
httpsredirect = false;
useragent = F("FONA");
ok_reply = F("OK");
}
/**
* @brief Get the module type
*
* @return uint8_t The module type
*/
uint8_t Adafruit_FONA::type(void) { return _type; }
/**
* @brief Connect to the cell module
*
* @param port the serial connection to use to connect
* @return bool true on success, false if a connection cannot be made
*/
bool Adafruit_FONA::begin(Stream &port) {
mySerial = &port;
pinMode(_rstpin, OUTPUT);
digitalWrite(_rstpin, HIGH);
delay(10);
digitalWrite(_rstpin, LOW);
delay(100);
digitalWrite(_rstpin, HIGH);
DEBUG_PRINTLN(F("Attempting to open comm with ATs"));
// give 7 seconds to reboot
int16_t timeout = 7000;
while (timeout > 0) {
while (mySerial->available())
mySerial->read();
if (sendCheckReply(F("AT"), ok_reply))
break;
while (mySerial->available())
mySerial->read();
if (sendCheckReply(F("AT"), F("AT")))
break;
delay(500);
timeout -= 500;
}
if (timeout <= 0) {
#ifdef ADAFRUIT_FONA_DEBUG
DEBUG_PRINTLN(F("Timeout: No response to AT... last ditch attempt."));
#endif
sendCheckReply(F("AT"), ok_reply);
delay(100);
sendCheckReply(F("AT"), ok_reply);
delay(100);
sendCheckReply(F("AT"), ok_reply);
delay(100);
}
// turn off Echo!
sendCheckReply(F("ATE0"), ok_reply);
delay(100);
if (!sendCheckReply(F("ATE0"), ok_reply)) {
return false;
}
// turn on hangupitude
sendCheckReply(F("AT+CVHU=0"), ok_reply);
delay(100);
flushInput();
DEBUG_PRINT(F("\t---> "));
DEBUG_PRINTLN("ATI");
mySerial->println("ATI");
readline(500, true);
DEBUG_PRINT(F("\t<--- "));
DEBUG_PRINTLN(replybuffer);
if (prog_char_strstr(replybuffer, (prog_char *)F("SIM808 R14")) != 0) {
_type = FONA808_V2;
} else if (prog_char_strstr(replybuffer, (prog_char *)F("SIM808 R13")) != 0) {
_type = FONA808_V1;
} else if (prog_char_strstr(replybuffer, (prog_char *)F("SIM800 R13")) != 0) {
_type = FONA800L;
} else if (prog_char_strstr(replybuffer, (prog_char *)F("SIMCOM_SIM5320A")) !=
0) {
_type = FONA3G_A;
} else if (prog_char_strstr(replybuffer, (prog_char *)F("SIMCOM_SIM5320E")) !=
0) {
_type = FONA3G_E;
}
if (_type == FONA800L) {
// determine if L or H
DEBUG_PRINT(F("\t---> "));
DEBUG_PRINTLN("AT+GMM");
mySerial->println("AT+GMM");
readline(500, true);
DEBUG_PRINT(F("\t<--- "));
DEBUG_PRINTLN(replybuffer);
if (prog_char_strstr(replybuffer, (prog_char *)F("SIM800H")) != 0) {
_type = FONA800H;
}
}
#if defined(FONA_PREF_SMS_STORAGE)
sendCheckReply(F("AT+CPMS=" FONA_PREF_SMS_STORAGE "," FONA_PREF_SMS_STORAGE
"," FONA_PREF_SMS_STORAGE),
ok_reply);
#endif
return true;
}
/********* Serial port ********************************************/
/**
* @brief Set the baud rate that the module will use
*
* @param baud The new baud rate to set
* @return true: success, false: failure
*/
bool Adafruit_FONA::setBaudrate(uint16_t baud) {
return sendCheckReply(F("AT+IPREX="), baud, ok_reply);
}
/********* Real Time Clock ********************************************/
// This RTC setup isn't fully operational
// see https://forums.adafruit.com/viewtopic.php?f=19&t=58002&p=294235#p294235
/**
* @brief Read the Real Time Clock
*
* **NOTE** Currently this function **will only fill the 'year' variable**
*
* @param year Pointer to a uint8_t to be set with year data
* @param month Pointer to a uint8_t to be set with month data **NOT WORKING**
* @param day Pointer to a uint8_t to be set with day data **NOT WORKING**
* @param hr Pointer to a uint8_t to be set with hour data **NOT WORKING**
* @param min Pointer to a uint8_t to be set with minute data **NOT WORKING**
* @param sec Pointer to a uint8_t to be set with year data **NOT WORKING**
* @return true: success, false: failure
*/
bool Adafruit_FONA::readRTC(uint8_t *year, uint8_t *month, uint8_t *day,
uint8_t *hr, uint8_t *min, uint8_t *sec) {
uint16_t v;
sendParseReply(F("AT+CCLK?"), F("+CCLK: "), &v, '/', 0);
// TODO: https://github.com/adafruit/Adafruit_FONA/issues/111
*year = v;
// avoid non-used warning
#if 0
(void *)month;
(void *)day;
(void *)hr;
(void *)min;
(void *)sec;
#else
month = month;
day = day;
hr = hr;
min = min;
sec = sec;
#endif
DEBUG_PRINTLN(*year);
return true;
}
/**
* @brief Enable the Real Time Clock
*
* @param mode 1: Enable 0: Disable
* @return true: Success , false: failure
*/
bool Adafruit_FONA::enableRTC(uint8_t mode) {
if (!sendCheckReply(F("AT+CLTS="), mode, ok_reply))
return false;
return sendCheckReply(F("AT&W"), ok_reply);
}
/********* BATTERY & ADC ********************************************/
/**
* @brief Get the current batery voltage
*
* @param v battery value pointer to be filled with the current value in mV
* (uint16_t)
* @return true: success, false: failure
*/
bool Adafruit_FONA::getBattVoltage(uint16_t *v) {
return sendParseReply(F("AT+CBC"), F("+CBC: "), v, ',', 2);
}
/* returns value in mV (uint16_t) */
/**
* @brief Get the current batery voltage
*
* @param v battery voltage pointer to be filled with the current value in mV
* (uint16_t)
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::getBattVoltage(uint16_t *v) {
float f;
bool b = sendParseReply(F("AT+CBC"), F("+CBC: "), &f, ',', 2);
*v = f * 1000;
return b;
}
/* */
/**
* @brief Get the percentage charge of battery as reported by sim800
*
* @param p Ponter to a uint16_t to set with the battery change as a percentage
* @return true: success, false: failure
*/
bool Adafruit_FONA::getBattPercent(uint16_t *p) {
return sendParseReply(F("AT+CBC"), F("+CBC: "), p, ',', 1);
}
/**
* @brief Get the current ADC voltage
*
* @param v Ponter to a uint16_t to set with the ADC voltage
*
* @return true: success, false: failure
*/
bool Adafruit_FONA::getADCVoltage(uint16_t *v) {
return sendParseReply(F("AT+CADC?"), F("+CADC: 1,"), v);
}
/********* SIM ***********************************************************/
/**
* @brief Unlock the sim with a provided PIN
*
* @param pin Pointer to a buffer with the PIN to use to unlock the SIM
* @return uint8_t
*/
uint8_t Adafruit_FONA::unlockSIM(char *pin) {
char sendbuff[14] = "AT+CPIN=";
sendbuff[8] = pin[0];
sendbuff[9] = pin[1];
sendbuff[10] = pin[2];
sendbuff[11] = pin[3];
sendbuff[12] = '\0';
return sendCheckReply(sendbuff, ok_reply);
}
/**
* @brief Get the SIM CCID
*
* @param ccid Pointer to a buffer to be filled with the CCID
* @return uint8_t
*/
uint8_t Adafruit_FONA::getSIMCCID(char *ccid) {
getReply(F("AT+CCID"));
// up to 28 chars for reply, 20 char total ccid
if (replybuffer[0] == '+') {
// fona 3g?
strncpy(ccid, replybuffer + 8, 20);
} else {
// fona 800 or 800
strncpy(ccid, replybuffer, 20);
}
ccid[20] = 0;
readline(); // eat 'OK'
return strlen(ccid);
}
/********* IMEI **********************************************************/
/**
* @brief Get the IMEI
*
* @param imei Pointer to a buffer to be filled with the IMEI
* @return uint8_t
*/
uint8_t Adafruit_FONA::getIMEI(char *imei) {
getReply(F("AT+GSN"));
// up to 15 chars
strncpy(imei, replybuffer, 15);
imei[15] = 0;
readline(); // eat 'OK'
return strlen(imei);
}
/********* NETWORK *******************************************************/
/**
* @brief Gets the current network status
*
* @return uint8_t The nework status:
*
* * 0: Not registered, MT is not currently searching a new operator to
* register to
* * 1: Registered, home network
* * 2: Not registered, but MT is currently searching a newoperator to register
* to
* * 3: Registration denied
* * 4: Unknown
* * 5: Registered, roaming
*/
uint8_t Adafruit_FONA::getNetworkStatus(void) {
uint16_t status;
if (!sendParseReply(F("AT+CREG?"), F("+CREG: "), &status, ',', 1))
return 0;
return status;
}
/**
* @brief get the current Received Signal Strength Indication
*
* @return uint8_t The current RSSI
*/
uint8_t Adafruit_FONA::getRSSI(void) {
uint16_t reply;
if (!sendParseReply(F("AT+CSQ"), F("+CSQ: "), &reply))
return 0;
return reply;
}
/********* AUDIO *******************************************************/
/**
* @brief Set the audio ouput
*
* @param audio_output The output to set.
* * 0: headset
* * 1: external audio
* @return true: success, false: failure
*/
bool Adafruit_FONA::setAudio(uint8_t audio_output) {
if (audio_output > 1)
return false;
return sendCheckReply(F("AT+CHFA="), audio_output, ok_reply);
}
/**
* @brief Get the current volume level
*
* @return uint8_t The current volume level
*/
uint8_t Adafruit_FONA::getVolume(void) {
uint16_t reply;
if (!sendParseReply(F("AT+CLVL?"), F("+CLVL: "), &reply))
return 0;
return reply;
}
/**
* @brief Set the volume
*
* @param volume_level The new volume to set
* @return true: success, false: failure
*/
bool Adafruit_FONA::setVolume(uint8_t volume_level) {
return sendCheckReply(F("AT+CLVL="), volume_level, ok_reply);
}
/**
* @brief Play a DTMF/Key tone
*
* @param dtmf The tone to play
* @return true: success, false: failure
*/
bool Adafruit_FONA::playDTMF(char dtmf) {
char str[4];
str[0] = '\"';
str[1] = dtmf;
str[2] = '\"';
str[3] = 0;
return sendCheckReply(F("AT+CLDTMF=3,"), str, ok_reply);
}
/**
* @brief Play a toolkit tone
*
* @param t The tone to play
* @param len The tone length
* @return true: success, false: failure
*/
bool Adafruit_FONA::playToolkitTone(uint8_t t, uint16_t len) {
return sendCheckReply(F("AT+STTONE=1,"), t, len, ok_reply);
}
/**
* @brief Play a toolkit tone
*
* @param t The tone to play
* @param len The tone length
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::playToolkitTone(uint8_t t, uint16_t len) {
if (!sendCheckReply(F("AT+CPTONE="), t, ok_reply))
return false;
delay(len);
return sendCheckReply(F("AT+CPTONE=0"), ok_reply);
}
/**
* @brief Set the microphone gain
*
*
* @param a Which microphone gain to set.
* * 0: headset
* * 1: external audio
*
* @param level The new gain level
* @return true: success, false: failure
*/
bool Adafruit_FONA::setMicVolume(uint8_t a, uint8_t level) {
if (a > 1)
return false;
return sendCheckReply(F("AT+CMIC="), a, level, ok_reply);
}
/********* FM RADIO *******************************************************/
/**
* @brief Turn the FM Radio on or off
*
* @param onoff true; on false: off
* @param a The audio source to use:
* * 0: headset
* * 1: external audio
* @return true: success, false: failure
*/
bool Adafruit_FONA::FMradio(bool onoff, uint8_t a) {
if (!onoff) {
return sendCheckReply(F("AT+FMCLOSE"), ok_reply);
}
if (a > 1)
return false;
return sendCheckReply(F("AT+FMOPEN="), a, ok_reply);
}
/**
* @brief Set the FM Radio station
*
* @param station The station to set the radio to
* @return true: success, false: failure
*/
bool Adafruit_FONA::tuneFMradio(uint16_t station) {
// Fail if FM station is outside allowed range.
if ((station < 870) || (station > 1090))
return false;
return sendCheckReply(F("AT+FMFREQ="), station, ok_reply);
}
/**
* @brief Set the FM Radio Volume
*
* @param i The volume to set
* @return true: success, false: failure
*/
bool Adafruit_FONA::setFMVolume(uint8_t i) {
// Fail if volume is outside allowed range (0-6).
if (i > 6) {
return false;
}
// Send FM volume command and verify response.
return sendCheckReply(F("AT+FMVOLUME="), i, ok_reply);
}
/**
* @brief Get the current volume level
*
* @return uint8_t The current volume level
*/
int8_t Adafruit_FONA::getFMVolume(void) {
uint16_t level;
if (!sendParseReply(F("AT+FMVOLUME?"), F("+FMVOLUME: "), &level))
return 0;
return level;
}
/**
* @brief Gets the current FM signal level
*
* @param station The station to chek the signal level of
* @return int8_t The signal level of the given stations. -1 if FM station is
* outside allowed range.
*/
int8_t Adafruit_FONA::getFMSignalLevel(uint16_t station) {
if ((station < 875) || (station > 1080)) {
return -1;
}
// Send FM signal level query command.
// Note, need to explicitly send timeout so right overload is chosen.
getReply(F("AT+FMSIGNAL="), station, FONA_DEFAULT_TIMEOUT_MS);
// Check response starts with expected value.
char *p = prog_char_strstr(replybuffer, PSTR("+FMSIGNAL: "));
if (p == 0)
return -1;
p += 11;
// Find second colon to get start of signal quality.
p = strchr(p, ':');
if (p == 0)
return -1;
p += 1;
// Parse signal quality.
int8_t level = atoi(p);
readline(); // eat the "OK"
return level;
}
/********* PWM/BUZZER **************************************************/
/**
* @brief Set the PWM Period and Duty Cycle
*
* @param period The PWM period
* @param duty The PWM duty cycle
* @return true: success, false: failure
*/
bool Adafruit_FONA::setPWM(uint16_t period, uint8_t duty) {
if (period > 2000)
return false;
if (duty > 100)
return false;
return sendCheckReply(F("AT+SPWM=0,"), period, duty, ok_reply);
}
/********* CALL PHONES **************************************************/
/**
* @brief Call a phone number
*
* @param number Pointer to a buffer with the phone number to call
* @return true: success, false: failure
*/
bool Adafruit_FONA::callPhone(char *number) {
char sendbuff[35] = "ATD";
strncpy(sendbuff + 3, number, min(30, (int)strlen(number)));
uint8_t x = strlen(sendbuff);
sendbuff[x] = ';';
sendbuff[x + 1] = 0;
// DEBUG_PRINTLN(sendbuff);
return sendCheckReply(sendbuff, ok_reply);
}
/**
* @brief Get the current call status
*
* @return uint8_t The call status:
* * 0: Ready
* * 1: Failure
* * 2: Unknown
* * 3: Ringing
* * 4: Call in progress
*/
uint8_t Adafruit_FONA::getCallStatus(void) {
uint16_t phoneStatus;
if (!sendParseReply(F("AT+CPAS"), F("+CPAS: "), &phoneStatus))
return FONA_CALL_FAILED; // 1, since 0 is actually a known, good reply
return phoneStatus;
}
/**
* @brief End the current call
*
* @return true: success, false: failure
*/
bool Adafruit_FONA::hangUp(void) { return sendCheckReply(F("ATH0"), ok_reply); }
/**
* @brief End the current call
*
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::hangUp(void) {
getReply(F("ATH"));
return (prog_char_strstr(replybuffer, (prog_char *)F("VOICE CALL: END")) !=
0);
}
/**
* @brief Answer a call
*
* @return true: success, false: failure
*/
bool Adafruit_FONA::pickUp(void) { return sendCheckReply(F("ATA"), ok_reply); }
/**
* @brief Answer an incoming call
*
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::pickUp(void) {
return sendCheckReply(F("ATA"), F("VOICE CALL: BEGIN"));
}
/**
* @brief On incoming call callback
*
*/
void Adafruit_FONA::onIncomingCall() {
DEBUG_PRINT(F("> "));
DEBUG_PRINTLN(F("Incoming call..."));
Adafruit_FONA::_incomingCall = true;
}
bool Adafruit_FONA::_incomingCall = false;
/**
* @brief Enable or disable caller ID
*
* @param enable true to enable, false to disable
* @param interrupt An optional interrupt to attach
* @return true: success, false: failure
*/
bool Adafruit_FONA::callerIdNotification(bool enable, uint8_t interrupt) {
if (enable) {
attachInterrupt(interrupt, onIncomingCall, FALLING);
return sendCheckReply(F("AT+CLIP=1"), ok_reply);
}
detachInterrupt(interrupt);
return sendCheckReply(F("AT+CLIP=0"), ok_reply);
}
/**
* @brief Get the number of the incoming call
*
* @param phonenum Pointer to a buffer to hold the incoming caller's phone
* number
* @return true: success, false: failure
*/
bool Adafruit_FONA::incomingCallNumber(char *phonenum) {
//+CLIP: "<incoming phone number>",145,"",0,"",0
if (!Adafruit_FONA::_incomingCall)
return false;
readline();
while (!prog_char_strcmp(replybuffer, (prog_char *)F("RING")) == 0) {
flushInput();
readline();
}
readline(); // reads incoming phone number line
parseReply(F("+CLIP: \""), phonenum, '"');
DEBUG_PRINT(F("Phone Number: "));
DEBUG_PRINTLN(replybuffer);
Adafruit_FONA::_incomingCall = false;
return true;
}
/********* SMS **********************************************************/
/**
* @brief Get the current SMS Iterrupt
*
* @return uint8_t The SMS interrupt or 0 on failure
*/
uint8_t Adafruit_FONA::getSMSInterrupt(void) {
uint16_t reply;
if (!sendParseReply(F("AT+CFGRI?"), F("+CFGRI: "), &reply))
return 0;
return reply;
}
/**
* @brief Attach an interrupt for SMS notifications
*
* @param i The interrupt to set
* @return true: success, false: failure
*/
bool Adafruit_FONA::setSMSInterrupt(uint8_t i) {
return sendCheckReply(F("AT+CFGRI="), i, ok_reply);
}
/**
* @brief Get the number of SMS
*
* @return int8_t The SMS count. -1 on error
*/
int8_t Adafruit_FONA::getNumSMS(void) {
uint16_t numsms;
// get into text mode
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return -1;
// ask how many sms are stored
if (sendParseReply(F("AT+CPMS?"), F(FONA_PREF_SMS_STORAGE ","), &numsms))
return numsms;
if (sendParseReply(F("AT+CPMS?"), F("\"SM\","), &numsms))
return numsms;
if (sendParseReply(F("AT+CPMS?"), F("\"SM_P\","), &numsms))
return numsms;
return -1;
}
// Reading SMS's is a bit involved so we don't use helpers that may cause delays
// or debug printouts!
/**
* @brief Read an SMS message into a provided buffer
*
* @param message_index The SMS message index to retrieve
* @param smsbuff SMS message buffer
* @param maxlen The maximum read length
* @param readlen The length read
* @return true: success, false: failure
*/
bool Adafruit_FONA::readSMS(uint8_t message_index, char *smsbuff,
uint16_t maxlen, uint16_t *readlen) {
// text mode
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return false;
// show all text mode parameters
if (!sendCheckReply(F("AT+CSDH=1"), ok_reply))
return false;
// parse out the SMS len
uint16_t thesmslen = 0;
DEBUG_PRINT(F("AT+CMGR="));
DEBUG_PRINTLN(message_index);
// getReply(F("AT+CMGR="), message_index, 1000); // do not print debug!
mySerial->print(F("AT+CMGR="));
mySerial->println(message_index);
readline(1000); // timeout
// DEBUG_PRINT(F("Reply: ")); DEBUG_PRINTLN(replybuffer);
// parse it out...
DEBUG_PRINTLN(replybuffer);
if (!parseReply(F("+CMGR:"), &thesmslen, ',', 11)) {
*readlen = 0;
return false;
}
readRaw(thesmslen);
flushInput();
uint16_t thelen = min(maxlen, (uint16_t)strlen(replybuffer));
strncpy(smsbuff, replybuffer, thelen);
smsbuff[thelen] = 0; // end the string
DEBUG_PRINTLN(replybuffer);
*readlen = thelen;
return true;
}
/**
* @brief Retrieve the sender of the specified SMS message and copy it as a
* string to the sender buffer. Up to senderlen characters of the sender
* will be copied and a null terminator will be added if less than senderlen
* characters are copied to the result.
*
* @param message_index The SMS message index to retrieve the sender for
* @param sender Pointer to a buffer to fill with the sender
* @param senderlen The maximum length to read
* @return true: a result was successfully retrieved, false: failure
*/
bool Adafruit_FONA::getSMSSender(uint8_t message_index, char *sender,
int senderlen) {
// Ensure text mode and all text mode parameters are sent.
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return false;
if (!sendCheckReply(F("AT+CSDH=1"), ok_reply))
return false;
DEBUG_PRINT(F("AT+CMGR="));
DEBUG_PRINTLN(message_index);
// Send command to retrieve SMS message and parse a line of response.
mySerial->print(F("AT+CMGR="));
mySerial->println(message_index);
readline(1000);
DEBUG_PRINTLN(replybuffer);
// Parse the second field in the response.
bool result = parseReplyQuoted(F("+CMGR:"), sender, senderlen, ',', 1);
// Drop any remaining data from the response.
flushInput();
return result;
}
/**
* @brief Send an SMS Message from a buffer provided
*
* @param smsaddr The SMS address buffer
* @param smsmsg The SMS message buffer
* @return true: success, false: failure
*/
bool Adafruit_FONA::sendSMS(char *smsaddr, char *smsmsg) {
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return false;
char sendcmd[30] = "AT+CMGS=\"";
strncpy(sendcmd + 9, smsaddr,
30 - 9 - 2); // 9 bytes beginning, 2 bytes for close quote + null
sendcmd[strlen(sendcmd)] = '\"';
if (!sendCheckReply(sendcmd, F("> ")))
return false;
DEBUG_PRINT(F("> "));
DEBUG_PRINTLN(smsmsg);
mySerial->println(smsmsg);
mySerial->println();
mySerial->write(0x1A);
DEBUG_PRINTLN("^Z");
if ((_type == FONA3G_A) || (_type == FONA3G_E)) {
// Eat two sets of CRLF
readline(200);
// DEBUG_PRINT("Line 1: "); DEBUG_PRINTLN(strlen(replybuffer));
readline(200);
// DEBUG_PRINT("Line 2: "); DEBUG_PRINTLN(strlen(replybuffer));
}
readline(10000); // read the +CMGS reply, wait up to 10 seconds!!!
// DEBUG_PRINT("Line 3: "); DEBUG_PRINTLN(strlen(replybuffer));
if (strstr(replybuffer, "+CMGS") == 0) {
return false;
}
readline(1000); // read OK
// DEBUG_PRINT("* "); DEBUG_PRINTLN(replybuffer);
if (strcmp(replybuffer, "OK") != 0) {
return false;
}
return true;
}
/**
* @brief Delete an SMS Message
*
* @param message_index The message to delete
* @return true: success, false: failure
*/
bool Adafruit_FONA::deleteSMS(uint8_t message_index) {
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return false;
// read an sms
char sendbuff[12] = "AT+CMGD=000";
sendbuff[8] = (message_index / 100) + '0';
message_index %= 100;
sendbuff[9] = (message_index / 10) + '0';
message_index %= 10;
sendbuff[10] = message_index + '0';
return sendCheckReply(sendbuff, ok_reply, 2000);
}
/********* USSD *********************************************************/
/**
* @brief Send USSD
*
* @param ussdmsg The USSD message buffer
* @param ussdbuff The USSD bufer
* @param maxlen The maximum read length
* @param readlen The length actually read
* @return true: success, false: failure
*/
bool Adafruit_FONA::sendUSSD(char *ussdmsg, char *ussdbuff, uint16_t maxlen,
uint16_t *readlen) {
if (!sendCheckReply(F("AT+CUSD=1"), ok_reply))
return false;
char sendcmd[30] = "AT+CUSD=1,\"";
strncpy(sendcmd + 11, ussdmsg,
30 - 11 - 2); // 11 bytes beginning, 2 bytes for close quote + null
sendcmd[strlen(sendcmd)] = '\"';
if (!sendCheckReply(sendcmd, ok_reply)) {
*readlen = 0;
return false;
} else {
readline(10000); // read the +CUSD reply, wait up to 10 seconds!!!
// DEBUG_PRINT("* "); DEBUG_PRINTLN(replybuffer);
char *p = prog_char_strstr(replybuffer, PSTR("+CUSD: "));
if (p == 0) {
*readlen = 0;
return false;
}
p += 7; //+CUSD
// Find " to get start of ussd message.
p = strchr(p, '\"');
if (p == 0) {
*readlen = 0;
return false;
}
p += 1; //"
// Find " to get end of ussd message.
char *strend = strchr(p, '\"');
uint16_t lentocopy = min(maxlen - 1, strend - p);
strncpy(ussdbuff, p, lentocopy + 1);
ussdbuff[lentocopy] = 0;
*readlen = lentocopy;
}
return true;
}
/********* TIME **********************************************************/
/**
* @brief Enable network time sync
*
* @param onoff true: enable false: disable
* @return true: success, false: failure
*/
bool Adafruit_FONA::enableNetworkTimeSync(bool onoff) {
if (onoff) {
if (!sendCheckReply(F("AT+CLTS=1"), ok_reply))