This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspGPRS_Pro_core.cpp
7173 lines (6154 loc) · 188 KB
/
WaspGPRS_Pro_core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.1
* Design: David Gascón
* Implementation: Alejandro Gállego
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
#include "WaspGPRS_Pro_core.h"
const char POWER_FULL[] PROGMEM = "+CFUN=1"; //0
const char POWER_RF_OFF[] PROGMEM = "+CFUN=4"; //1
const char POWER_MIN[] PROGMEM = "+CFUN=0"; //2
const char POWER_NO_SLEEP[] PROGMEM = "+CSCLK=0"; //3
const char POWER_SLEEP[] PROGMEM = "+CSCLK=2"; //4
const char SET_TIME[] PROGMEM = "+CCLK="; //5
const char AT_PIN[] PROGMEM = "+CPIN="; //6
const char AT_CHANGE_PASSWORD[] PROGMEM = "+CPWD="; //7
const char AT_GPRS_IMEI[] PROGMEM = "+GSN"; //8
const char AT_GPRS_IMSI[] PROGMEM = "+CIMI"; //9
const char AT_COMMAND_MODE[] PROGMEM = "+++"; //10
const char AT_DATA_MODE[] PROGMEM = "O"; //11
const char AT_DATA_MODE_R[] PROGMEM = "CONNECT"; //12
const char AT_DATA_MODE_FAIL[] PROGMEM = "NO CARRIER"; //13
const char AT_GPRS_CHECK_ATT[] PROGMEM = "+CGATT?"; //14
const char AT_GPRS_CHECK_ON[] PROGMEM = "+CGATT: 1"; //15
const char AT_GPRS_CHECK_OFF[] PROGMEM = "+CGATT: 0"; //16
const char AT_GPRS_ATT_ON[] PROGMEM = "+CGATT=1"; //17
const char AT_GPRS_ATT_OFF[] PROGMEM = "+CGATT=0"; //18
const char AT_GPRS_CELLID[] PROGMEM = "+CENG"; //19
const char AT_GPRS_RSSI[] PROGMEM = "+CSQ"; //20
//Various
const char AT_GET_OPERATOR[] PROGMEM = "+COPS?"; //21
const char AT_GET_OPERATOR_R[] PROGMEM = "+COPS:"; //22
const char AT_SET_PREF_OPERATOR[] PROGMEM = "+COPS="; //23
const char AT_OPERATOR_LIST[] PROGMEM = "+CPOL?"; //24
const char AT_OPERATOR_LIST_R[] PROGMEM = "+CPOL:"; //25
const char AT_WHO_AM_I[] PROGMEM = "+CGMM"; //26
const char AT_FIRMWARE[] PROGMEM = "+CGMR"; //27
const char AT_FIRMWARE_R[] PROGMEM = "Revision:"; //28
const char AT_IP_SET_DNS[] PROGMEM = "+CDNSCFG="; //29
// FTP_HTTP Constants
const char AT_GPRS_CFG[] PROGMEM = "+SAPBR="; //30
const char AT_GPRS[] PROGMEM = "GPRS"; //31
//Set mode and connection constants
const char NUMERIC_ERROR[] PROGMEM = "+CMEE=1"; //32
const char NO_ECHO[] PROGMEM = "E0"; //33
const char CHECK[] PROGMEM = "+CREG?"; //34
const char HOME_NETWORK[] PROGMEM = "+CREG: 0,"; //35
const char ROAMING[] PROGMEM = "+CREG: 0,5"; //36
const char CON_TYPE[] PROGMEM = "CONTYPE"; //37
const char APN[] PROGMEM = "APN"; //38
const char USER[] PROGMEM = "USER"; //39
const char PWD[] PROGMEM = "PWD"; //40
//Constants for manageIncomingData
const char INCOMING_CALL[] PROGMEM = "+CLIP"; //41
const char INCOMING_SMS[] PROGMEM = "+CMTI"; //42
const char GET_BAND[] PROGMEM = "+CBAND"; //43
const char BEARER_RES[] PROGMEM = "+SAPBR: "; //44
const char AT_GPRS_CHECK[] PROGMEM = "+CGREG?"; //45
const char AT_GPRS_CHECK_RES[] PROGMEM = "+CGREG: 0,"; //46
const char* const table_MISC[] PROGMEM =
{
//Power constants
POWER_FULL, //0
POWER_RF_OFF, //1
POWER_MIN, //2
POWER_NO_SLEEP, //3
POWER_SLEEP, //4
SET_TIME, //5
AT_PIN, //6
AT_CHANGE_PASSWORD, //7
AT_GPRS_IMEI, //8
AT_GPRS_IMSI, //9
AT_COMMAND_MODE, //10
AT_DATA_MODE, //11
AT_DATA_MODE_R, //12
AT_DATA_MODE_FAIL, //13
AT_GPRS_CHECK_ATT, //14
AT_GPRS_CHECK_ON, //15
AT_GPRS_CHECK_OFF, //16
AT_GPRS_ATT_ON, //17
AT_GPRS_ATT_OFF, //18
AT_GPRS_CELLID, //19
AT_GPRS_RSSI, //20
//Various
AT_GET_OPERATOR, //21
AT_GET_OPERATOR_R, //22
AT_SET_PREF_OPERATOR, //23
AT_OPERATOR_LIST, //24
AT_OPERATOR_LIST_R, //25
AT_WHO_AM_I, //26
AT_FIRMWARE, //27
AT_FIRMWARE_R, //28
AT_IP_SET_DNS, //29
// FTP_HTTP Constants
AT_GPRS_CFG, //30
AT_GPRS, //31
//Set mode and connection constants
NUMERIC_ERROR, //32
NO_ECHO, //33
CHECK, //34
HOME_NETWORK, //35
ROAMING, //36
CON_TYPE, //37
APN, //38
USER, //39
PWD, //40
//Constants for manageIncomingData
INCOMING_CALL, //41
INCOMING_SMS, //42
GET_BAND, //43
BEARER_RES, //44
AT_GPRS_CHECK, //45
AT_GPRS_CHECK_RES, //46
};
#if GSM_FUSE
// Calls Constants
const char AT_CALL[] PROGMEM = "D"; //0
const char AT_HANG[] PROGMEM = "H"; //1
const char AT_DTMF[] PROGMEM = "+CLDTMF="; //2
// Voice Mode Constants
const char AT_ID_INCALL[] PROGMEM = "+CLIP=1"; //3
// SMS Constants
const char AT_SMS[] PROGMEM = "+CMGS="; //4
const char AT_SMS_R[] PROGMEM = ">"; //5
const char AT_SMS_MODE[] PROGMEM = "+CMGF=1"; //6
const char AT_SMS_INFO[] PROGMEM = "+CNMI=2,1,0,0,0";//7
const char AT_SMS_READ[] PROGMEM = "+CMGR="; //8
const char AT_SMS_MEMORY[] PROGMEM = "+CPMS="; //9
const char AT_SMS_MEMORY_R[] PROGMEM = "+CPMS: "; //10
const char AT_SMS_DELETE[] PROGMEM = "+CMGD="; //11
// Sound Constants
const char AT_SOUND_INT[] PROGMEM = "#CAP=2"; //12
const char AT_SOUND_EXT[] PROGMEM = "#CAP=1"; //13
const char AT_VOLUME_SET[] PROGMEM = "+CLVL="; //14
const char AT_SPEAKER_VOLUME[] PROGMEM = "L"; //15
const char AT_SPEAKER_MODE[] PROGMEM = "M"; //16
const char AT_CLIP_MODE[] PROGMEM = "+CLIP="; //17
const char AT_CLIR_MODE[] PROGMEM = "+CLIR="; //18
const char AT_PHONE_ACTIVITY[] PROGMEM = "+CPAS"; //19
const char AT_PHONE_ACTIVITY_R[] PROGMEM = "+CPAS:"; //20
const char AT_ALERT_SOUND_MODE[] PROGMEM = "+CALM="; //21
const char AT_ALERT_SOUND_LEVEL[] PROGMEM = "+CALS="; //22
const char AT_RINGER_SOUND_LEVEL[] PROGMEM = "+CRSL="; //23
const char AT_SPEAKER_LEVEL[] PROGMEM = "+CLVL="; //24
const char AT_MUTE[] PROGMEM = "+CMUT="; //25
const char* const table_GSM[] PROGMEM =
{
AT_CALL, //0
AT_HANG, //1
AT_DTMF, //2
// Voice Mode Constants
AT_ID_INCALL, //3
// SMS Constants
AT_SMS, //4
AT_SMS_R, //5
AT_SMS_MODE, //6
AT_SMS_INFO, //7
AT_SMS_READ, //8
AT_SMS_MEMORY, //9
AT_SMS_MEMORY_R, //10
AT_SMS_DELETE, //11
// Sound Constants
AT_SOUND_INT, //12
AT_SOUND_EXT, //13
AT_VOLUME_SET, //14
AT_SPEAKER_VOLUME, //15
AT_SPEAKER_MODE, //16
AT_CLIP_MODE, //17
AT_CLIR_MODE, //18
AT_PHONE_ACTIVITY, //19
AT_PHONE_ACTIVITY_R, //20
AT_ALERT_SOUND_MODE, //21
AT_ALERT_SOUND_LEVEL, //22
AT_RINGER_SOUND_LEVEL, //23
AT_SPEAKER_LEVEL, //24
AT_MUTE, //25
};
#endif
#if FTP_FUSE
const unsigned long AT_FTP_WAIT_CONFIG = 10000;
const unsigned long AT_FTP_WAIT_CONNEC = 60000; // milliseconds
const unsigned long AT_FTP_MAX_TIME = 300000; // milliseconds
const char AT_FTP_ID[] PROGMEM = "+FTPCID="; //0
const char AT_FTP_PORT[] PROGMEM = "+FTPPORT="; //1
const char AT_FTP_MODE[] PROGMEM = "+FTPMODE"; //2
const char AT_FTP_TYPE[] PROGMEM = "+FTPTYPE="; //3
const char AT_FTP_SERVER[] PROGMEM = "+FTPSERV="; //4
const char AT_FTP_UN[] PROGMEM = "+FTPUN="; //5
const char AT_FTP_PW[] PROGMEM = "+FTPPW="; //6
const char AT_FTP_PUT[] PROGMEM = "+FTPPUT="; //7
const char AT_FTP_PUT_NAME[] PROGMEM = "+FTPPUTNAME="; //8
const char AT_FTP_PUT_PATH[] PROGMEM = "+FTPPUTPATH="; //9
const char AT_FTP_GET[] PROGMEM = "+FTPGET="; //10
const char AT_FTP_GET_NAME[] PROGMEM = "+FTPGETNAME="; //11
const char AT_FTP_GET_PATH[] PROGMEM = "+FTPGETPATH="; //12
const char AT_FTP_GET_SIZE[] PROGMEM = "+FTPSIZE"; //13
const char AT_FTP_STATUS[] PROGMEM = "+FTPSTATE"; //14
const char* const table_FTP[] PROGMEM =
{
AT_FTP_ID, //0
AT_FTP_PORT, //1
AT_FTP_MODE, //2
AT_FTP_TYPE, //3
AT_FTP_SERVER, //4
AT_FTP_UN, //5
AT_FTP_PW, //6
AT_FTP_PUT, //7
AT_FTP_PUT_NAME, //8
AT_FTP_PUT_PATH, //9
AT_FTP_GET, //10
AT_FTP_GET_NAME, //11
AT_FTP_GET_PATH, //12
AT_FTP_GET_SIZE, //13
AT_FTP_STATUS, //14
};
#endif
#if HTTP_FUSE
const unsigned long HTTP_TIMEOUT = 60000; // Timeout for HTTP and HTTPS functions in miliseconds
const unsigned long HTTP_CONF_TIMEOUT = 15000; // Timeout for HTTP and HTTPS functions in miliseconds
const char AT_HTTP_INIT[] PROGMEM = "+HTTPINIT"; //0
const char AT_HTTP_PARAM[] PROGMEM = "+HTTPPARA="; //1
const char AT_HTTP_ACTION[] PROGMEM = "+HTTPACTION="; //2
const char AT_HTTP_ACTION_R[] PROGMEM = "+HTTPACTION:"; //3
const char AT_HTTP_READ[] PROGMEM = "+HTTPREAD"; //4
const char AT_HTTP_READ_R[] PROGMEM = "+HTTPREAD:"; //5
const char AT_HTTP_TERM[] PROGMEM = "+HTTPTERM"; //6
const char AT_HTTP_CID[] PROGMEM = "CID"; //7
const char AT_HTTP_URL[] PROGMEM = "URL"; //8
const char AT_HTTP_DATA[] PROGMEM = "+HTTPDATA="; //9
const char AT_HTTP_DATA_R[] PROGMEM = "DOWNLOAD"; //10
const char AT_HTTP_PARA_CONT[] PROGMEM = "+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\""; //11
const char FRAME_HEADER_FIELD[] PROGMEM = "frame="; //12
const char* const table_HTTP[] PROGMEM =
{
AT_HTTP_INIT, //0
AT_HTTP_PARAM, //1
AT_HTTP_ACTION, //2
AT_HTTP_ACTION_R, //3
AT_HTTP_READ, //4
AT_HTTP_READ_R, //5
AT_HTTP_TERM, //6
AT_HTTP_CID, //7
AT_HTTP_URL, //8
AT_HTTP_DATA, //9
AT_HTTP_DATA_R, //10
AT_HTTP_PARA_CONT, //11
FRAME_HEADER_FIELD, //12
};
#endif
#if IP_FUSE
const unsigned long TCP_CONNECTION_TIME_1 = 30000;
const unsigned long TCP_CONNECTION_TIME_2 = 60000; // milliseconds
const char AT_IP[] PROGMEM = "IP"; //0
const char AT_SINGLE_CONN[] PROGMEM = "+CIPMUX=0"; //1
const char AT_MULTI_CONN[] PROGMEM = "+CIPMUX=1"; //2
const char AT_IP_STATUS[] PROGMEM = "+CIPSTATUS"; //3
const char AT_IP_STATUS_R[] PROGMEM = "STATE: "; //4
const char AT_IP_SET_APN[] PROGMEM = "+CSTT="; //5
const char AT_IP_BRING[] PROGMEM = "+CIICR"; //6
const char AT_IP_GET_IP[] PROGMEM = "+CIFSR"; //7
const char AT_IP_APP_MODE[] PROGMEM = "+CIPMODE="; //8
const char AT_IP_HEADER_1[] PROGMEM = "AT+CIPHEAD=1"; //9
const char AT_IP_CLIENT[] PROGMEM = "+CIPSTART="; //10
const char AT_TCP[] PROGMEM = "TCP"; //11
const char AT_UDP[] PROGMEM = "UDP"; //12
const char AT_CONNECTED_OK[] PROGMEM = "CONNECT OK"; //13
const char AT_CONNECTED_FAIL[] PROGMEM = "CONNECT FAIL"; //14
const char AT_IP_SERVER[] PROGMEM = "+CIPSERVER="; //15
const char AT_IP_SEND[] PROGMEM = "+CIPSEND"; //16
const char AT_IP_SEND_R[] PROGMEM = "SEND OK"; //17
const char AT_IP_SEND_FAIL[] PROGMEM = "SEND FAIL"; //18
const char AT_IP_CLOSE[] PROGMEM = "+CIPCLOSE="; //19
const char AT_IP_CLOSE_R[] PROGMEM = "CLOSE OK"; //20
const char AT_IP_QCLOSE[] PROGMEM = "+CIPQRCLOSE="; //21
const char AT_IP_SHUT[] PROGMEM = "+CIPSHUT"; //22
const char AT_IP_QUERY_DNS[] PROGMEM = "+CDNSGIP="; //23
const char AT_IP_QUERY_DNS_R[] PROGMEM = "+CDNSGIP:"; //24
const char AT_IP_LOCAL_PORT[] PROGMEM = "+CLPORT="; //25
const char AT_IP_SAVE_CONF[] PROGMEM = "+CIPSCONT"; //26
const char AT_IP_HEADER[] PROGMEM = "+CIPHEAD="; //27
const char AT_IP_AUTOSENDING[] PROGMEM = "+CIPATS="; //28
const char AT_IP_SHOW_REMOTE_IP[] PROGMEM = "+CIPSRIP="; //29
const char AT_IP_PROTOCOL_HEADER[] PROGMEM = "+CIPSHOWTP="; //30
const char AT_IP_DISCARD_AT_DATA[] PROGMEM = "+CIPTXISS="; //31
const char AT_IP_GET_MANUALLY[] PROGMEM = "+CIPRXGET"; //32
const char AT_IP_UDP_EXTENDED[] PROGMEM = "+CIPUDPMODE="; //33
const char AT_IP_CGATT[] PROGMEM = "+CGATT"; //34
// IP states
const char IP_INITIAL[] PROGMEM = "IP INITIAL"; //35
const char IP_START[] PROGMEM = "IP_START"; //36
const char IP_CONFIG[] PROGMEM = "IP CONFIG"; //37
const char IP_GPRSACT[] PROGMEM = "IP GPRSACT"; //38
const char IP_STATUS[] PROGMEM = "IP STATUS"; //39
const char IP_PROCESSING[] PROGMEM = "IP PROCESSING"; //40
const char TCP_CONNECTING[] PROGMEM = "TCP CONNECTING"; //41
const char UDP_CONNECTING[] PROGMEM = "UDP CONNECTING"; //42
const char SERVER_LISTENING[] PROGMEM = "SERVER LISTENING"; //43
const char CONNECT_OK[] PROGMEM = "CONNECT OK"; //44
const char TCP_CLOSING[] PROGMEM = "TCP CLOSING"; //45
const char UDP_CLOSING[] PROGMEM = "UDP CLOSING"; //46
const char TCP_CLOSED[] PROGMEM = "TCP CLOSED"; //47
const char UDP_CLOSED[] PROGMEM = "UDP CLOSED"; //48
const char PDP_DEACT[] PROGMEM = "PDP DEACT"; //49
const char GET_MANUAL[] PROGMEM = "+CIPRXGET"; //50
const char IP_DATA[] PROGMEM = "+IPD,"; //51
const char GET_DATA[] PROGMEM = "+CIPRXGET=2,"; //52
const char IP_MULTI_DATA[] PROGMEM = "+RECEIVE,"; //53
const char* const table_IP[] PROGMEM =
{
AT_IP, //0
AT_SINGLE_CONN, //1
AT_MULTI_CONN, //2
AT_IP_STATUS, //3
AT_IP_STATUS_R, //4
AT_IP_SET_APN, //5
AT_IP_BRING, //6
AT_IP_GET_IP, //7
AT_IP_APP_MODE, //8
AT_IP_HEADER_1, //9
AT_IP_CLIENT, //10
AT_TCP, //11
AT_UDP, //12
AT_CONNECTED_OK, //13
AT_CONNECTED_FAIL, //14
AT_IP_SERVER, //15
AT_IP_SEND, //16
AT_IP_SEND_R, //17
AT_IP_SEND_FAIL, //18
AT_IP_CLOSE, //19
AT_IP_CLOSE_R, //20
AT_IP_QCLOSE, //21
AT_IP_SHUT, //22
AT_IP_QUERY_DNS, //23
AT_IP_QUERY_DNS_R, //24
AT_IP_LOCAL_PORT, //25
AT_IP_SAVE_CONF, //26
AT_IP_HEADER, //27
AT_IP_AUTOSENDING, //28
AT_IP_SHOW_REMOTE_IP, //29
AT_IP_PROTOCOL_HEADER, //30
AT_IP_DISCARD_AT_DATA, //31
AT_IP_GET_MANUALLY, //32
AT_IP_UDP_EXTENDED, //33
AT_IP_CGATT, //34
// IP states
IP_INITIAL, //35
IP_START, //36
IP_CONFIG, //37
IP_GPRSACT, //38
IP_STATUS, //39
IP_PROCESSING, //40
TCP_CONNECTING, //41
UDP_CONNECTING, //42
SERVER_LISTENING, //43
CONNECT_OK, //44
TCP_CLOSING, //45
UDP_CLOSING, //46
TCP_CLOSED, //47
UDP_CLOSED, //48
PDP_DEACT, //49
GET_MANUAL, //50
IP_DATA, //51
GET_DATA, //52
IP_MULTI_DATA, //53
};
#endif
#if OTA_FUSE
const char OTA_ver_file[] PROGMEM = "/UPGRADE.TXT"; //0
const char NO_OTA[] PROGMEM = "NO_FILE"; //1
const char FILE_TAG[] PROGMEM = "FILE:"; //2
const char PATH_TAG[] PROGMEM = "PATH:"; //3
const char VERSION_TAG[] PROGMEM = "VERSION:"; //4
const char* const table_OTA_GPRS[] PROGMEM =
{
OTA_ver_file, //0
NO_OTA, //1
FILE_TAG, //2
PATH_TAG, //3
VERSION_TAG, //4
};
#endif
// Constructors ////////////////////////////////////////////////////////////////
WaspGPRS_Pro_core::WaspGPRS_Pro_core(){
_baudRate=GPRS_PRO_RATE;
_pwrMode=GPRS_PRO_ON;
_socket=1;
not_ready=1;
memset(_apn, '\0', sizeof(_apn));
memset(_apn_login, '\0', sizeof(_apn_login));
memset(_apn_password, '\0', sizeof(_apn_password));
strncpy(_apn, AT_GPRS_APN, min(sizeof(_apn), strlen(AT_GPRS_APN)));
strncpy(_apn_login, AT_GPRS_LOGIN, min(sizeof(_apn_login), strlen(AT_GPRS_LOGIN)));
strncpy(_apn_password, AT_GPRS_PASSW, min(sizeof(_apn_password), strlen(AT_GPRS_PASSW)));
}
// Private Methods /////////////////////////////////////////////////////////////
#if IP_FUSE
/* getIP() - Gets IP direction when configure a TCP/UDP profiles
*
* This function gets IP direction when configure a TCP/UDP profiles and stores it in 'IP_direction'
*
* Returns '1' on success and '0' if error
*/
uint8_t WaspGPRS_Pro_core::getIP(){
int i;
unsigned long previous;
serialFlush(_socket);
//AT_IP_GET_IP
strcpy_P(str_aux1, (char*)pgm_read_word(&(table_IP[7]))); //+CIFSR
snprintf(buffer_GPRS, sizeof(buffer_GPRS), "AT%s\r\n", str_aux1);
printString(buffer_GPRS, _socket);
previous=millis();
i=0;
do{
while ((!serialAvailable(_socket)) && ((millis() - previous) < 1000))
{
// Condition to avoid an overflow (DO NOT REMOVE)
if( millis() < previous) previous = millis();
}
IP_dir[i]=serialRead(_socket);
if (((0x2F < IP_dir[i]) && (IP_dir[i] < 0x3A)) || (IP_dir[i] == '.')) // Only moves the cursor when it's a number or a dot
{
i++;
}
// Condition to avoid an overflow (DO NOT REMOVE)
if( millis() < previous) previous = millis();
}while (((!((IP_dir[i] == '\r') && (i != 0))) && (IP_dir[i] != 'E')) && ((millis() - previous) < 1000));
IP_dir[i]='\0';
if (IP_dir[0] == '\0')
{
return 0;
}
return 1;
}
/* checkIPstatus() - get the status of the IP connection
*
* This function gets the status of the IP connection
*
* Returns a number with the state
*/
uint8_t WaspGPRS_Pro_core::checkIPstatus(){
uint8_t answer = 10;
int it = 0;
memset(str_aux3, '\0', sizeof(str_aux3) );
strcpy_P(str_aux1, (char*)pgm_read_word(&(table_IP[3]))); //"+CIPSTATUS"
strcpy_P(str_aux2, (char*)pgm_read_word(&(table_IP[4]))); //"STATE: "
answer=sendCommand1(str_aux1, str_aux2);
if (answer == 1)
{
do{
str_aux3[it]=serialRead(_socket);
it++;
}while (str_aux3[it-1] != '\n');
if (strstr(str_aux3, "IP INITIAL") != NULL)
{
return 1;
}
if (strstr(str_aux3, "IP START") != NULL)
{
return 2;
}
if (strstr(str_aux3, "IP CONFIG") != NULL)
{
return 3;
}
if (strstr(str_aux3, "IP GPRSACT") != NULL)
{
return 4;
}
if ((strstr(str_aux3, "IP STATUS") != NULL) || (strstr(str_aux3, "IP PROCESSING") != NULL))
{
return 5;
}
if ((strstr(str_aux3, "TCP CONNECTING") != NULL) || (strstr(str_aux3, "UDP CONNECTING") != NULL) || (strstr(str_aux3, "SERVER LISTENING") != NULL))
{
return 6;
}
if (strstr(str_aux3, "CONNECT OK") != NULL)
{
return 7;
}
if ((strstr(str_aux3, "TCP CLOSING") != NULL) || (strstr(str_aux3, "UDP CLOSING") != NULL))
{
return 8;
}
if ((strstr(str_aux3, "TCP CLOSED") != NULL) || (strstr(str_aux3, "UDP CLOSED") != NULL))
{
return 9;
}
if (strstr(str_aux3, "PDP DEACT") != NULL)
{
return 10;
}
answer = 0;
}
if (answer == 0)
{
return 0;
}
return 11;
}
#endif
/* getIfReady() - gets if GPRS module is ready or not
*
* This function gets if GPRS module is ready or not
*
* Returns nothing. It changes the value of 'not_ready'
*/
void WaspGPRS_Pro_core::getIfReady(){
uint8_t answer=0;
// clean rx buffer
serialFlush(_socket);
printString(AT_COMMAND, _socket);
printByte('\r', _socket);
printByte('\n', _socket);
delay(10);
answer = waitForData(OK_RESPONSE, 2000, millis(), 0);
if (answer == 1)
{
not_ready=0;
}
else
{
not_ready=1;
}
}
/* checkGPRS() - checks if GPRS connection is OK
*
* This function checks if GPRS connection is OK
*
* Returns '1' on success and '0' if error
*/
uint8_t WaspGPRS_Pro_core::checkGPRS_at(unsigned long time){
uint8_t answer = 0;
unsigned long previous;
strcpy_P(str_aux1, (char*)pgm_read_word(&(table_MISC[14]))); //AT_GPRS_CHECK_ATT
strcpy_P(str_aux2, (char*)pgm_read_word(&(table_MISC[15]))); //AT_GPRS_CHECK_ON
strcpy_P(str_aux3, (char*)pgm_read_word(&(table_MISC[16]))); //AT_GPRS_CHECK_OFF
previous = millis();
do{
answer = sendCommand2(str_aux1, str_aux2, str_aux3);
}while ((answer != 1) && ((millis() - previous) < (time * 1000)));
if (answer != 1)
{
return 0;
}
return 1;
}
/* checkGPRS() - checks if GPRS connection is OK
*
* This function checks if GPRS connection is OK
*
* Returns '1' when connected,
* '0' if not registered and the module is not currently searching a new operator,
* '-2' if not registered, but the module is currently searching a new operator,
* '-3' if registration denied
* '-4' if the state is unknown
*/
int8_t WaspGPRS_Pro_core::checkGPRS(unsigned long time){
int8_t answer=0;
unsigned long previous;
// AT+CGREG?
strcpy_P(str_aux1, (char*)pgm_read_word(&(table_MISC[45]))); //"+CGREG?"
strcpy_P(str_aux2, (char*)pgm_read_word(&(table_MISC[46]))); //"+CGREG: 0,"
previous = millis();
do{
// Sends the command and waits for the answer (0,1 for home network and 0,5 for roaming)
answer = sendCommand1(str_aux1, str_aux2, 3000, 1);
// Check if pattern "+CGREG: 0," was found
if (answer == 1)
{
// read status
answer = serialRead(_socket) - 0x30;
}
delay(500);
// Condition to avoid an overflow (DO NOT REMOVE)
if( millis() < previous) previous = millis();
}while (((answer == 2) || (answer == 4) || (answer == 0)) && ((millis() - previous) < (time * 1000)));
#if GPRS_debug_mode>0
USB.print(F("GPRS status: "));
USB.println(answer, DEC);
#endif
// check correct answer
if ( (answer == 1) || (answer == 5) )
{
return 1;
}
// if error return corresponding flag
return -answer;
}
// AT Comands ///////////////////////////////////////////////////////////////////
/*
* sendCommand1
*
* It sends an AT command to the module for a maximum of 'DEFAULT_TIMEOUT'
* milliseconds and tries to seek the patterns specified in 'expectedAnswer'
*
* Returns
* '1' if 'expectedAnswer' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::sendCommand1(const char* theText,
const char* expectedAnswer)
{
return sendCommand1(theText, expectedAnswer, DEFAULT_TIMEOUT, SEND_ONCE);
}
/*
* sendCommand1
*
* It sends an AT command to the module for a maximum of 'max_timeout'
* milliseconds and tries to seek the patterns specified in 'expectedAnswer'
*
* Returns
* '1' if 'expectedAnswer1' has been detected,
* '2' if 'expectedAnswer2' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::sendCommand1(const char* theText,
const char* expectedAnswer,
unsigned long max_timeout,
int num_tries)
{
unsigned long timeout = 0;
#if GPRS_debug_mode>0
USB.print(F("Send command with 1 answer: "));
USB.print(F("AT"));
USB.println(theText);
#endif
beginSerial(_baudRate, _socket);
// try sending the command
// wait for serial response
timeout = millis();
serialFlush(_socket);
while ( (!serialAvailable(_socket)) && ((millis() - timeout) < max_timeout))
{
if (num_tries > 0)
{
printString("AT", _socket);
printString(theText, _socket);
printString("\r\n", _socket);
num_tries--;
}
delay(DELAY_ON_SEND);
}
int answer= waitForData( expectedAnswer, max_timeout, timeout, 0);
#if GPRS_debug_mode>0
USB.print(F("Answer received: "));
USB.println(answer, DEC);
#endif
return answer;
}
/*
* sendCommand2
*
* It sends an AT command to the module for a maximum of 'DEFAULT_TIMEOUT'
* milliseconds and tries to seek the patterns specified in 'expectedAnswer1'
* and 'expectedAnswer2'
*
* Returns
* '1' if 'expectedAnswer1' has been detected,
* '2' if 'expectedAnswer2' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::sendCommand2( const char* theText,
const char* expectedAnswer1,
const char* expectedAnswer2)
{
return sendCommand2( theText,
expectedAnswer1,
expectedAnswer2,
DEFAULT_TIMEOUT,
SEND_ONCE );
}
/*
* sendCommand2
*
* It sends an AT command to the module for a maximum of 'max_timeout'
* milliseconds and tries to seek the patterns specified in 'expectedAnswer1'
* and 'expectedAnswer2'
*
* Returns
* '1' if 'expectedAnswer1' has been detected,
* '2' if 'expectedAnswer2' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::sendCommand2( const char* theText,
const char* expectedAnswer1,
const char* expectedAnswer2,
unsigned long max_timeout,
int num_tries)
{
unsigned long timeout = 0;
#if GPRS_debug_mode>0
USB.print(F("Send command with 2 answers: "));
USB.print(F("AT"));
USB.println(theText);
#endif
beginSerial(_baudRate, _socket);
// try sending the command
// wait for serial response
timeout = millis();
serialFlush(_socket);
while((!serialAvailable(_socket)) && ((millis() - timeout) < max_timeout))
{
if (num_tries > 0)
{
printString("AT", _socket);
printString(theText, _socket);
printString("\r\n", _socket);
num_tries--;
}
delay(DELAY_ON_SEND);
}
int answer= waitForData( expectedAnswer1, expectedAnswer2, max_timeout, timeout, 0, 2);
#if GPRS_debug_mode>0
USB.print(F("Answer received: "));
USB.println(answer, DEC);
#endif
return answer;
}
/*
* waitForData
*
* It waits for data from the module for a maximum of max_timeout seconds and
* tries to seek the pattern specified in 'expectedAnswer'
*
* Returns
* '1' if 'expectedAnswer' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::waitForData( const char* expectedAnswer,
unsigned long max_timeout,
unsigned long timeout,
int seconds)
{
return (waitForData( expectedAnswer, expectedAnswer, max_timeout, timeout, seconds, 1));
}
/*
* waitForData
*
* It waits for data from the module for a maximum of max_timeout seconds and
* tries to seek the patterns specified in 'expectedAnswer1' and
* 'expectedAnswer2'
*
* Returns
* '1' if 'expectedAnswer1' has been detected,
* '2' if 'expectedAnswer2' has been detected,
* '0' if not detected
*/
uint8_t WaspGPRS_Pro_core::waitForData( const char* expectedAnswer1,
const char* expectedAnswer2,
unsigned long max_timeout,
unsigned long timeout,
int seconds,
int n_answers)
{
char theCommand[100];
memset(theCommand, '\0', sizeof(theCommand));
int theLength1 = 0;
int theLength2 = 0;
int minLength;
int maxLength;
int it=0;
uint8_t first=1;
char aux;
#if GPRS_debug_mode>1
USB.print(F("Excepted answer 1: "));
USB.println(expectedAnswer1);
USB.print(F("Excepted answer 2: "));
USB.println(expectedAnswer2);
#endif
// Gets the maximum length and the minimum length of the 2 strings
theLength1=strlen(expectedAnswer1);
minLength=theLength1;
maxLength=theLength1;
if (n_answers > 1)
{
theLength2=strlen(expectedAnswer2);
if(minLength>theLength2)
{
minLength=theLength2;
}
if(maxLength<theLength2)
{
maxLength=theLength2;
}
}
// if there is a heating time, then wait to see if you got
// any data from the serial port
while (seconds > 0)
{
delay(1000);
seconds--;
}
while ((millis() - timeout) < max_timeout)
{
while (!serialAvailable(_socket) && ((millis() - timeout) < max_timeout))
{
delay(10);
// Condition to avoid an overflow (DO NOT REMOVE)
if( millis() < timeout) timeout = millis();
}
while (serialAvailable(_socket))
{
if ( first == 1 ) // Gets data from serial port until 'minLength'
{
theCommand[it] = serialRead(_socket);
it++;
#if GPRS_debug_mode>1
USB.print(F("Command answer 1: "));
USB.println(theCommand);
USB.print(F("Length: "));
USB.println(strlen(theCommand), DEC);
#endif
if (it > minLength)
{
if ((n_answers > 1) && (minLength < maxLength))
{
first = 2;
}
else
{
first = 0;
}
}
}
else if (first == 2) // Gets data from serial port increasing the length of 'theCommand' from minLength to maxLength
{
if (serialAvailable(_socket))
{
it=minLength;
minLength++;
theCommand[minLength]=serialRead(_socket);
delay(20);
#if GPRS_debug_mode>1
USB.print(F("Command answer 2: "));
USB.println(theCommand);
USB.print(F("Length: "));
USB.println(strlen(theCommand), DEC);
#endif
}