This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Adafruit_FONA.cpp
902 lines (706 loc) · 23.4 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
/***************************************************
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!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <avr/pgmspace.h>
// next line per http://postwarrior.com/arduino-ethershield-error-prog_char-does-not-name-a-type/
#define prog_char char PROGMEM
#if (ARDUINO >= 100)
#include "Arduino.h"
#include <SoftwareSerial.h>
#else
#include "WProgram.h"
#include <NewSoftSerial.h>
#endif
#include "Adafruit_FONA.h"
Adafruit_FONA::Adafruit_FONA(int8_t rst) {
_rstpin = rst;
apn = F("FONAnet");
apnusername = 0;
apnpassword = 0;
mySerial = 0;
httpsredirect = false;
useragent = F("FONA");
}
boolean Adafruit_FONA::begin(Stream &port) {
mySerial = &port;
pinMode(_rstpin, OUTPUT);
digitalWrite(_rstpin, HIGH);
delay(10);
digitalWrite(_rstpin, LOW);
delay(100);
digitalWrite(_rstpin, HIGH);
// give 3 seconds to reboot
delay(3000);
while (mySerial->available()) mySerial->read();
sendCheckReply(F("AT"), F("OK"));
delay(100);
sendCheckReply(F("AT"), F("OK"));
delay(100);
sendCheckReply(F("AT"), F("OK"));
delay(100);
// turn off Echo!
sendCheckReply(F("ATE0"), F("OK"));
delay(100);
if (! sendCheckReply(F("ATE0"), F("OK"))) {
return false;
}
return true;
}
/********* Real Time Clock ********************************************/
boolean Adafruit_FONA::readRTC(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *hr, uint8_t *min, uint8_t *sec) {
uint16_t v;
sendParseReply(F("AT+CCLK?"), F("+CCLK: "), &v, '/', 0);
*year = v;
Serial.println(*year);
}
boolean Adafruit_FONA::enableRTC(uint8_t i) {
if (!sendCheckReply(F("AT+CLTS="), i, F("OK")))
return false;
return sendCheckReply(F("AT&W"), F("OK"));
}
/********* BATTERY & ADC ********************************************/
/* returns value in mV (uint16_t) */
boolean Adafruit_FONA::getBattVoltage(uint16_t *v) {
return sendParseReply(F("AT+CBC"), F("+CBC: "), v, ',', 2);
}
/* returns the percentage charge of battery as reported by sim800 */
boolean Adafruit_FONA::getBattPercent(uint16_t *p) {
return sendParseReply(F("AT+CBC"), F("+CBC: "), p, ',', 1);
}
boolean Adafruit_FONA::getADCVoltage(uint16_t *v) {
return sendParseReply(F("AT+CADC?"), F("+CADC: 1,"), v);
}
/********* SIM ***********************************************************/
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] = NULL;
return sendCheckReply(sendbuff, "OK");
}
/********* IMEI **********************************************************/
uint8_t Adafruit_FONA::getIMEI(char *imei) {
getReply("AT+GSN");
// up to 15 chars
strncpy(imei, replybuffer, 15);
imei[15] = 0;
readline(); // eat 'OK'
return strlen(imei);
}
/********* NETWORK *******************************************************/
uint8_t Adafruit_FONA::getNetworkStatus(void) {
uint16_t status;
if (!sendParseReply(F("AT+CREG?"), F("+CREG: "), &status, ',', 1)) return 0;
return status;
}
uint8_t Adafruit_FONA::getRSSI(void) {
uint16_t reply;
if (!sendParseReply(F("AT+CSQ"), F("+CSQ: "), &reply) ) return 0;
return reply;
}
/********* SMS **********************************************************/
uint8_t Adafruit_FONA::getSMSInterrupt(void) {
uint16_t reply;
if (! sendParseReply(F("AT+CFGRI?"), F("+CFGRI: "), &reply) ) return 0;
return reply;
}
boolean Adafruit_FONA::setSMSInterrupt(uint8_t i) {
return sendCheckReply(F("AT+CFGRI="), i, F("OK"));
}
int8_t Adafruit_FONA::getNumSMS(void) {
uint16_t numsms;
if (! sendCheckReply(F("AT+CMGF=1"), F("OK"))) return -1;
// ask how many sms are stored
if (! sendParseReply(F("AT+CPMS?"), F("+CPMS: \"SM_P\","), &numsms) ) return -1;
return numsms;
}
// Reading SMS's is a bit involved so we don't use helpers that may cause delays or debug
// printouts!
boolean Adafruit_FONA::readSMS(uint8_t i, char *smsbuff,
uint16_t maxlen, uint16_t *readlen) {
// text mode
if (! sendCheckReply(F("AT+CMGF=1"), F("OK"))) return false;
// show all text mode parameters
if (! sendCheckReply(F("AT+CSDH=1"), F("OK"))) return false;
// parse out the SMS len
uint16_t thesmslen = 0;
//getReply(F("AT+CMGR="), i, 1000); // do not print debug!
mySerial->print(F("AT+CMGR="));
mySerial->println(i);
readline(1000); // timeout
// parse it out...
parseReply(F("+CMGR:"), &thesmslen, ',', 11);
readRaw(thesmslen);
flushInput();
uint16_t thelen = min(maxlen, strlen(replybuffer));
strncpy(smsbuff, replybuffer, thelen);
smsbuff[thelen] = 0; // end the string
#ifdef ADAFRUIT_FONA_DEBUG
Serial.println(replybuffer);
#endif
*readlen = thelen;
return true;
}
// 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 charactesr are
// copied to the result. Returns true if a result was successfully retrieved,
// otherwise false.
boolean Adafruit_FONA::getSMSSender(uint8_t i, char *sender, int senderlen) {
// Ensure text mode and all text mode parameters are sent.
if (! sendCheckReply(F("AT+CMGF=1"), F("OK"))) return false;
if (! sendCheckReply(F("AT+CSDH=1"), F("OK"))) return false;
// Send command to retrieve SMS message and parse a line of response.
mySerial->print(F("AT+CMGR="));
mySerial->println(i);
readline(1000);
// Parse the second field in the response.
boolean result = parseReplyQuoted(F("+CMGR:"), sender, senderlen, ',', 1);
// Drop any remaining data from the response.
flushInput();
return result;
}
boolean Adafruit_FONA::sendSMS(char *smsaddr, char *smsmsg) {
if (! sendCheckReply("AT+CMGF=1", "OK")) return -1;
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, "> ")) return false;
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("> "); Serial.println(smsmsg);
#endif
mySerial->print(smsmsg);
mySerial->write(0x1A);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.println("^Z");
#endif
readline(10000); // read the +CMGS reply, wait up to 10 seconds!!!
//Serial.print("* "); Serial.println(replybuffer);
if (strstr(replybuffer, "+CMGS") == 0) {
return false;
}
readline(1000); // read OK
//Serial.print("* "); Serial.println(replybuffer);
if (strcmp(replybuffer, "OK") != 0) {
return false;
}
return true;
}
boolean Adafruit_FONA::deleteSMS(uint8_t i) {
if (! sendCheckReply("AT+CMGF=1", "OK")) return -1;
// read an sms
char sendbuff[12] = "AT+CMGD=000";
sendbuff[8] = (i / 100) + '0';
i %= 100;
sendbuff[9] = (i / 10) + '0';
i %= 10;
sendbuff[10] = i + '0';
return sendCheckReply(sendbuff, "OK", 2000);
}
/********* TIME **********************************************************/
boolean Adafruit_FONA::enableNetworkTimeSync(boolean onoff) {
if (onoff) {
if (! sendCheckReply(F("AT+CLTS=1"), F("OK")))
return false;
} else {
if (! sendCheckReply(F("AT+CLTS=0"), F("OK")))
return false;
}
flushInput(); // eat any 'Unsolicted Result Code'
return true;
}
boolean Adafruit_FONA::enableNTPTimeSync(boolean onoff,
const __FlashStringHelper *ntpserver) {
if (onoff) {
if (! sendCheckReply(F("AT+CNTPCID=1"), F("OK")))
return false;
mySerial->print(F("AT+CNTP=\""));
if (ntpserver != 0) {
mySerial->print(ntpserver);
} else {
mySerial->print(F("pool.ntp.org"));
}
mySerial->println(F("\",0"));
readline(FONA_DEFAULT_TIMEOUT_MS);
if (strcmp(replybuffer, "OK") != 0)
return false;
if (! sendCheckReply(F("AT+CNTP"), F("OK"), 10000))
return false;
uint16_t status;
readline(10000);
if (! parseReply(F("+CNTP:"), &status))
return false;
} else {
if (! sendCheckReply(F("AT+CNTPCID=0"), F("OK")))
return false;
}
return true;
}
boolean Adafruit_FONA::getTime(char *buff, uint16_t maxlen) {
getReply(F("AT+CCLK?"), (uint16_t) 10000);
if (strncmp(replybuffer, "+CCLK: ", 7) != 0)
return false;
char *p = replybuffer+7;
uint16_t lentocopy = min(maxlen-1, strlen(p));
strncpy(buff, p, lentocopy+1);
buff[lentocopy] = 0;
readline(); // eat OK
return true;
}
/********* GPRS **********************************************************/
boolean Adafruit_FONA::enableGPRS(boolean onoff) {
if (onoff) {
if (!sendCheckReply(F("AT+CGATT=1"), F("OK"), 10000))
{
Serial.println("CGATT=1 failed");
return false;
}
// set bearer profile! connection type GPRS
if (!sendCheckReply(F("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""),
F("OK"), 10000))
{
Serial.println("SAPBR CONTYPE failed");
return false;
}
// set bearer profile access point name
if (apn) {
// Send command AT+SAPBR=3,1,"APN","<apn value>" where <apn value> is the configured APN value.
if (!sendCheckReplyQuoted(F("AT+SAPBR=3,1,\"APN\","), apn, F("OK"), 10000))
{
Serial.println("SAPBR APN failed");
return false;
}
// set username/password
if (apnusername) {
// Send command AT+SAPBR=3,1,"USER","<user>" where <user> is the configured APN username.
if (!sendCheckReplyQuoted(F("AT+SAPBR=3,1,\"USER\","), apnusername, F("OK"), 10000))
{
Serial.println("SAPBR USER failed");
return false;
}
}
if (apnpassword) {
// Send command AT+SAPBR=3,1,"PWD","<password>" where <password> is the configured APN password.
if (!sendCheckReplyQuoted(F("AT+SAPBR=3,1,\"PWD\","), apnpassword, F("OK"), 10000))
{
Serial.println("SAPBR PWD failed");
return false;
}
}
}
// open GPRS context
if (!sendCheckReply(F("AT+SAPBR=1,1"), F("OK"), 10000))
{
Serial.println("SAPBR OK failed");
return false;
}
} else {
// close GPRS context
if (!sendCheckReply(F("AT+SAPBR=0,1"), F("OK"), 10000))
return false;
if (!sendCheckReply(F("AT+CGATT=0"), F("OK"), 10000))
return false;
}
return true;
}
uint8_t Adafruit_FONA::GPRSstate(void) {
uint16_t state;
if (!sendParseReply(F("AT+CGATT?"), F("+AT+CGATT: "), &state) )
return -1;
return state;
}
void Adafruit_FONA::setGPRSNetworkSettings(const __FlashStringHelper *apn,
const __FlashStringHelper *username,
const __FlashStringHelper *password) {
this->apn = apn;
this->apnusername = username;
this->apnpassword = password;
}
boolean Adafruit_FONA::getGSMLoc(uint16_t *errorcode, char *buff, uint16_t maxlen) {
getReply(F("AT+CIPGSMLOC=1,1"), (uint16_t)10000);
if (!parseReply(F("+CIPGSMLOC: "), errorcode))
return false;
char *p = replybuffer+14;
uint16_t lentocopy = min(maxlen-1, strlen(p));
strncpy(buff, p, lentocopy+1);
readline(); // eat OK
return true;
}
boolean Adafruit_FONA::HTTP_GET_start(char *url,
uint16_t *status, uint16_t *datalen){
if (!HTTP_initialize(url))
return false;
// HTTP GET
if (!sendCheckReply(F("AT+HTTPACTION=0"), F("OK")))
return false;
// HTTP response
if (!HTTP_response(status, datalen))
return false;
return true;
}
void Adafruit_FONA::HTTP_GET_end(void) {
HTTP_terminate();
}
boolean Adafruit_FONA::HTTP_POST_start(char *url,
const __FlashStringHelper *contenttype,
const uint8_t *postdata, uint16_t postdatalen,
uint16_t *status, uint16_t *datalen){
if (!HTTP_initialize(url))
return false;
if (!sendCheckReplyQuoted(F("AT+HTTPPARA=\"CONTENT\","), contenttype, F("OK"), 10000))
return false;
// HTTP POST data
flushInput();
mySerial->print(F("AT+HTTPDATA="));
mySerial->print(postdatalen);
mySerial->println(",10000");
readline(FONA_DEFAULT_TIMEOUT_MS);
if (strcmp(replybuffer, "DOWNLOAD") != 0)
return false;
mySerial->write(postdata, postdatalen);
readline(10000);
if (strcmp(replybuffer, "OK") != 0)
return false;
// HTTP POST
if (!sendCheckReply(F("AT+HTTPACTION=1"), F("OK")))
return false;
if (!HTTP_response(status, datalen))
return false;
return true;
}
void Adafruit_FONA::HTTP_POST_end(void) {
HTTP_terminate();
}
void Adafruit_FONA::setUserAgent(const __FlashStringHelper *useragent) {
this->useragent = useragent;
}
void Adafruit_FONA::setHTTPSRedirect(boolean onoff) {
httpsredirect = onoff;
}
/********* HTTP HELPERS ****************************************/
boolean Adafruit_FONA::HTTP_initialize(char *url) {
// Handle any pending
HTTP_terminate();
// Initialize and set parameters
if (!sendCheckReply(F("AT+HTTPINIT"), F("OK")))
return false;
if (!sendCheckReply(F("AT+HTTPPARA=\"CID\",1"), F("OK")))
return false;
if (!sendCheckReplyQuoted(F("AT+HTTPPARA=\"UA\","), useragent, F("OK"), 10000))
return false;
flushInput();
mySerial->print(F("AT+HTTPPARA=\"URL\",\""));
mySerial->print(url);
mySerial->println("\"");
readline(FONA_DEFAULT_TIMEOUT_MS);
if (strcmp(replybuffer, "OK") != 0)
return false;
// HTTPS redirect
if (httpsredirect) {
if (!sendCheckReply(F("AT+HTTPPARA=\"REDIR\",1"), F("OK")))
return false;
if (!sendCheckReply(F("AT+HTTPSSL=1"), F("OK")))
return false;
}
return true;
}
boolean Adafruit_FONA::HTTP_response(uint16_t *status, uint16_t *datalen) {
// Read response status
readline(10000);
if (!parseReply(F("+HTTPACTION:"), status, ',', 1))
return false;
if (!parseReply(F("+HTTPACTION:"), datalen, ',', 2))
return false;
Serial.print("Status: "); Serial.println(*status);
Serial.print("Len: "); Serial.println(*datalen);
// Read response
getReply(F("AT+HTTPREAD"));
return true;
}
void Adafruit_FONA::HTTP_terminate(void) {
flushInput();
sendCheckReply(F("AT+HTTPTERM"), F("OK"));
}
/********* LOW LEVEL *******************************************/
inline int Adafruit_FONA::available(void) {
return mySerial->available();
}
inline size_t Adafruit_FONA::write(uint8_t x) {
return mySerial->write(x);
}
inline int Adafruit_FONA::read(void) {
return mySerial->read();
}
inline int Adafruit_FONA::peek(void) {
return mySerial->peek();
}
inline void Adafruit_FONA::flush() {
mySerial->flush();
}
void Adafruit_FONA::flushInput() {
// Read all available serial input to flush pending data.
uint16_t timeoutloop = 0;
while (timeoutloop++ < 40) {
while(available()) {
read();
timeoutloop = 0; // If char was received reset the timer
}
delay(1);
}
}
uint16_t Adafruit_FONA::readRaw(uint16_t b) {
uint16_t idx = 0;
while (b && (idx < sizeof(replybuffer)-1)) {
if (mySerial->available()) {
replybuffer[idx] = mySerial->read();
idx++;
b--;
}
}
replybuffer[idx] = 0;
return idx;
}
uint8_t Adafruit_FONA::readline(uint16_t timeout, boolean multiline) {
uint16_t replyidx = 0;
while (timeout--) {
if (replyidx >= 254) {
//Serial.println(F("SPACE"));
break;
}
while(mySerial->available()) {
char c = mySerial->read();
if (c == '\r') continue;
if (c == 0xA) {
if (replyidx == 0) // the first 0x0A is ignored
continue;
if (!multiline) {
timeout = 0; // the second 0x0A is the end of the line
break;
}
}
replybuffer[replyidx] = c;
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
replyidx++;
}
if (timeout == 0) {
//Serial.println(F("TIMEOUT"));
break;
}
delay(1);
}
replybuffer[replyidx] = 0; // null term
return replyidx;
}
uint8_t Adafruit_FONA::getReply(char *send, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.println(send);
#endif
mySerial->println(send);
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
uint8_t Adafruit_FONA::getReply(const __FlashStringHelper *send, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.println(send);
#endif
mySerial->println(send);
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
// Send prefix, suffix, and newline. Return response (and also set replybuffer with response).
uint8_t Adafruit_FONA::getReply(const __FlashStringHelper *prefix, char *suffix, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.print(prefix); Serial.println(suffix);
#endif
mySerial->print(prefix);
mySerial->println(suffix);
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
// Send prefix, suffix, and newline. Return response (and also set replybuffer with response).
uint8_t Adafruit_FONA::getReply(const __FlashStringHelper *prefix, int32_t suffix, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.print(prefix); Serial.println(suffix, DEC);
#endif
mySerial->print(prefix);
mySerial->println(suffix, DEC);
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
// Send prefix, suffix, suffix2, and newline. Return response (and also set replybuffer with response).
uint8_t Adafruit_FONA::getReply(const __FlashStringHelper *prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.print(prefix);
Serial.print(suffix1, DEC); Serial.print(","); Serial.println(suffix2, DEC);
#endif
mySerial->print(prefix);
mySerial->print(suffix1);
mySerial->print(',');
mySerial->println(suffix2, DEC);
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
// Send prefix, ", suffix, ", and newline. Return response (and also set replybuffer with response).
uint8_t Adafruit_FONA::getReplyQuoted(const __FlashStringHelper *prefix, const __FlashStringHelper *suffix, uint16_t timeout) {
flushInput();
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print("\t---> "); Serial.print(prefix);
Serial.print('"'); Serial.print(suffix); Serial.println('"');
#endif
mySerial->print(prefix);
mySerial->print('"');
mySerial->print(suffix);
mySerial->println('"');
uint8_t l = readline(timeout);
#ifdef ADAFRUIT_FONA_DEBUG
Serial.print ("\t<--- "); Serial.println(replybuffer);
#endif
return l;
}
boolean Adafruit_FONA::sendCheckReply(char *send, char *reply, uint16_t timeout) {
getReply(send, timeout);
/*
for (uint8_t i=0; i<strlen(replybuffer); i++) {
Serial.print(replybuffer[i], HEX); Serial.print(" ");
}
Serial.println();
for (uint8_t i=0; i<strlen(reply); i++) {
Serial.print(reply[i], HEX); Serial.print(" ");
}
Serial.println();
*/
return (strcmp(replybuffer, reply) == 0);
}
boolean Adafruit_FONA::sendCheckReply(const __FlashStringHelper *send, const __FlashStringHelper *reply, uint16_t timeout) {
getReply(send, timeout);
return (strcmp_P(replybuffer, (prog_char*)reply) == 0);
}
// Send prefix, suffix, and newline. Verify FONA response matches reply parameter.
boolean Adafruit_FONA::sendCheckReply(const __FlashStringHelper *prefix, char *suffix, const __FlashStringHelper *reply, uint16_t timeout) {
getReply(prefix, suffix, timeout);
return (strcmp_P(replybuffer, (prog_char*)reply) == 0);
}
// Send prefix, suffix, and newline. Verify FONA response matches reply parameter.
boolean Adafruit_FONA::sendCheckReply(const __FlashStringHelper *prefix, int32_t suffix, const __FlashStringHelper *reply, uint16_t timeout) {
getReply(prefix, suffix, timeout);
return (strcmp_P(replybuffer, (prog_char*)reply) == 0);
}
// Send prefix, suffix, suffix2, and newline. Verify FONA response matches reply parameter.
boolean Adafruit_FONA::sendCheckReply(const __FlashStringHelper *prefix, int32_t suffix1, int32_t suffix2, const __FlashStringHelper *reply, uint16_t timeout) {
getReply(prefix, suffix1, suffix2, timeout);
return (strcmp_P(replybuffer, (prog_char*)reply) == 0);
}
// Send prefix, ", suffix, ", and newline. Verify FONA response matches reply parameter.
boolean Adafruit_FONA::sendCheckReplyQuoted(const __FlashStringHelper *prefix, const __FlashStringHelper *suffix, const __FlashStringHelper *reply, uint16_t timeout) {
getReplyQuoted(prefix, suffix, timeout);
return (strcmp_P(replybuffer, (prog_char*)reply) == 0);
}
boolean Adafruit_FONA::parseReply(const __FlashStringHelper *toreply,
uint16_t *v, char divider, uint8_t index) {
char *p = strstr_P(replybuffer, (prog_char*)toreply); // get the pointer to the voltage
if (p == 0) return false;
p+=strlen_P((prog_char*)toreply);
//Serial.println(p);
for (uint8_t i=0; i<index;i++) {
// increment dividers
p = strchr(p, divider);
if (!p) return false;
p++;
//Serial.println(p);
}
*v = atoi(p);
return true;
}
boolean Adafruit_FONA::parseReply(const __FlashStringHelper *toreply,
char *v, char divider, uint8_t index) {
uint8_t i=0;
char *p = strstr_P(replybuffer, (prog_char*)toreply);
if (p == 0) return false;
p+=strlen_P((prog_char*)toreply);
for (i=0; i<index;i++) {
// increment dividers
p = strchr(p, divider);
if (!p) return false;
p++;
}
for(i=0; i<strlen(p);i++) {
if(p[i] == divider)
break;
v[i] = p[i];
}
v[i] = '\0';
return true;
}
// Parse a quoted string in the response fields and copy its value (without quotes)
// to the specified character array (v). Only up to maxlen characters are copied
// into the result buffer, so make sure to pass a large enough buffer to handle the
// response.
boolean Adafruit_FONA::parseReplyQuoted(const __FlashStringHelper *toreply,
char *v, int maxlen, char divider, uint8_t index) {
uint8_t i=0, j;
// Verify response starts with toreply.
char *p = strstr_P(replybuffer, (prog_char*)toreply);
if (p == 0) return false;
p+=strlen_P((prog_char*)toreply);
// Find location of desired response field.
for (i=0; i<index;i++) {
// increment dividers
p = strchr(p, divider);
if (!p) return false;
p++;
}
// Copy characters from response field into result string.
for(i=0, j=0; j<maxlen && i<strlen(p); ++i) {
// Stop if a divier is found.
if(p[i] == divider)
break;
// Skip any quotation marks.
else if(p[i] == '"')
continue;
v[j++] = p[i];
}
// Add a null terminator if result string buffer was not filled.
if (j < maxlen)
v[j] = '\0';
return true;
}
boolean Adafruit_FONA::sendParseReply(const __FlashStringHelper *tosend,
const __FlashStringHelper *toreply,
uint16_t *v, char divider, uint8_t index) {
getReply(tosend);
if (! parseReply(toreply, v, divider, index)) return false;
readline(); // eat 'OK'
return true;
}