-
Notifications
You must be signed in to change notification settings - Fork 68
/
gsm.ino
1397 lines (1126 loc) · 32.2 KB
/
gsm.ino
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
//gsm functions
#if MODEM_UG96
#define AT_CONTEXT "AT+QICSGP=1,1,"
#define AT_ACTIVATE "AT+QIACT=1\r"
#define AT_DEACTIVATE "AT+QIDEACT=1\r"
#define AT_CONFIGDNS "AT+QIDNSCFG=1,"
#define AT_LOCALIP "AT+QIACT?\r"
#define AT_OPEN "AT+QIOPEN=1,0,"
#define AT_CLOSE "AT+QICLOSE=0\r"
#define AT_SEND "AT+QISEND=0,"
#define AT_RECEIVE "AT+QIRD=0,"
#define AT_STAT "AT+QISTATE=1,0\r"
#define AT_QUERYACK "AT+QISEND=0,0\r"
#define AT_ACKRESP "+QISEND: "
#define AT_NTP "AT+QNTP=1,"
#else
#define AT_CONTEXT "AT+QIREGAPP="
#define AT_ACTIVATE "AT+QIACT\r"
#define AT_DEACTIVATE "AT+QIDEACT\r"
#define AT_CONFIGDNS "AT+QIDNSCFG="
#define AT_LOCALIP "AT+QILOCIP\r"
#define AT_OPEN "AT+QIOPEN=0,"
#define AT_CLOSE "AT+QICLOSE=0\r"
#define AT_SEND "AT+QISEND=0,"
#define AT_RECEIVE "AT+QIRD=0,1,0,"
#define AT_STAT "AT+QISTATE\r"
#define AT_QUERYACK "AT+QISACK=0\r"
#define AT_ACKRESP "+QISACK: "
#define AT_NTP "AT+QNTP="
#endif
void gsm_init() {
//setup modem pins
debug_print(F("gsm_init() started"));
pinMode(PIN_C_PWR_GSM, OUTPUT);
digitalWrite(PIN_C_PWR_GSM, LOW);
pinMode(PIN_C_KILL_GSM, OUTPUT);
digitalWrite(PIN_C_KILL_GSM, LOW);
pinMode(PIN_STATUS_GSM, INPUT);
pinMode(PIN_RING_GSM, INPUT);
pinMode(PIN_WAKE_GSM, OUTPUT);
digitalWrite(PIN_WAKE_GSM, HIGH);
gsm_open();
debug_print(F("gsm_init() finished"));
}
void gsm_open() {
gsm_port.begin(115200);
}
void gsm_close() {
gsm_port.end();
}
bool gsm_power_status() {
#if MODEM_UG96
// inverted status signal
return digitalRead(PIN_STATUS_GSM) != HIGH;
#else
// help discharge floating pin, by temporarily setting as output low
PIO_Configure(
g_APinDescription[PIN_STATUS_GSM].pPort,
PIO_OUTPUT_0,
g_APinDescription[PIN_STATUS_GSM].ulPin,
g_APinDescription[PIN_STATUS_GSM].ulPinConfiguration);
pinMode(PIN_STATUS_GSM, INPUT);
delay(1);
// read modem power status
return digitalRead(PIN_STATUS_GSM) != LOW;
#endif
}
void gsm_on() {
//turn on the modem
debug_print(F("gsm_on() started"));
int k=0;
for (;;) {
unsigned long t = millis();
if(!gsm_power_status()) { // now off, turn on
digitalWrite(PIN_C_PWR_GSM, HIGH);
while (!gsm_power_status() && (millis() - t < 5000))
delay(100);
digitalWrite(PIN_C_PWR_GSM, LOW);
status_delay(1000);
}
// auto-baudrate
if (gsm_send_at())
break;
debug_print(F("gsm_on(): failed auto-baudrate"));
if (++k >= 5) // max attempts
break;
gsm_off(0);
gsm_off(1);
status_delay(1000);
debug_print(F("gsm_on(): try again"));
debug_print(k);
}
// make sure it's not sleeping
gsm_wakeup();
debug_print(F("gsm_on() finished"));
}
void gsm_off(int emergency) {
//turn off the modem
debug_print(F("gsm_off() started"));
unsigned long t = millis();
if(emergency) {
digitalWrite(PIN_C_KILL_GSM, HIGH);
while (gsm_power_status() && (millis() - t < 5000))
delay(100);
digitalWrite(PIN_C_KILL_GSM, LOW);
status_delay(1000);
}
else
if(gsm_power_status()) { // now on, turn off
#if MODEM_UG96
// 3G modem, normal power down
gsm_port.print("AT+QPOWD=1\r");
gsm_wait_for_reply(1,0);
#else
digitalWrite(PIN_C_PWR_GSM, HIGH);
while (gsm_power_status() && (millis() - t < 5000))
delay(100);
digitalWrite(PIN_C_PWR_GSM, LOW);
#endif
status_delay(1000);
}
gsm_get_reply(1);
debug_print(F("gsm_off() finished"));
}
void gsm_standby() {
// clear wake signal
digitalWrite(PIN_WAKE_GSM, HIGH);
// standby GSM
gsm_port.print("AT+CFUN=4\r");
gsm_wait_for_reply(1,0);
gsm_port.print("AT+QSCLK=1\r");
gsm_wait_for_reply(1,0);
}
void gsm_wakeup() {
// wake GSM
digitalWrite(PIN_WAKE_GSM, LOW);
delay(1000);
gsm_port.print("AT+QSCLK=0\r");
gsm_wait_for_reply(1,0);
gsm_port.print("AT+CFUN=1\r");
gsm_wait_for_reply(1,0);
}
void gsm_setup() {
debug_print(F("gsm_setup() started"));
//turn off modem
gsm_off(1);
//blink modem restart
blink_start();
//turn on modem
gsm_on();
//configure
gsm_config();
debug_print(F("gsm_setup() completed"));
}
void gsm_config() {
//supply PIN code if needed
gsm_set_pin();
// wait up to 1 minute
gsm_wait_modem_ready(60000);
//get GSM IMEI
gsm_get_imei();
//misc GSM startup commands (disable echo)
gsm_startup_cmd();
//set GSM APN
gsm_set_apn();
}
void gsm_wait_modem_ready(int timeout) {
// wait for modem ready (attached to network)
unsigned long t = millis();
do {
int pas = gsm_get_modem_status();
if(pas==0 || pas==3 || pas==4) break;
status_delay(3000);
}
while (millis() - t < timeout);
}
bool gsm_clock_was_set = false;
void gsm_set_time() {
debug_print(F("gsm_set_time() started"));
//setting modems clock from current time var
gsm_port.print("AT+CCLK=\"");
gsm_port.print(time_char);
gsm_port.print("\"\r");
gsm_wait_for_reply(1,0);
gsm_clock_was_set = true;
debug_print(F("gsm_set_time() completed"));
}
void gsm_set_pin() {
debug_print(F("gsm_set_pin() started"));
for (int k=0; k<5; ++k) {
//checking if PIN is set
gsm_port.print("AT+CPIN?");
gsm_port.print("\r");
gsm_wait_for_reply(1,1);
char *tmp = strstr(modem_reply, "SIM PIN");
if(tmp!=NULL) {
debug_print(F("gsm_set_pin(): PIN is required"));
//checking if pin is valid one
if(config.sim_pin[0] == 255) {
debug_print(F("gsm_set_pin(): PIN is not supplied."));
} else {
if(strlen(config.sim_pin) == 4) {
debug_print(F("gsm_set_pin(): PIN supplied, sending to modem."));
gsm_port.print("AT+CPIN=");
gsm_port.print(config.sim_pin);
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
tmp = strstr(modem_reply, "OK");
if(tmp!=NULL) {
debug_print(F("gsm_set_pin(): PIN is accepted"));
} else {
debug_print(F("gsm_set_pin(): PIN is not accepted"));
}
break;
} else {
debug_print(F("gsm_set_pin(): PIN supplied, but has invalid length. Not sending to modem."));
break;
}
}
}
tmp = strstr(modem_reply, "READY");
if(tmp!=NULL) {
debug_print(F("gsm_set_pin(): PIN is not required"));
break;
}
status_delay(2000);
}
debug_print(F("gsm_set_pin() completed"));
}
void gsm_get_time() {
debug_print(F("gsm_get_time() started"));
//clean any serial data
gsm_get_reply(0);
//get time from modem
gsm_port.print("AT+CCLK?");
gsm_port.print("\r");
gsm_wait_for_reply(1,1);
char *tmp = strstr(modem_reply, "+CCLK: \"");
tmp += strlen("+CCLK: \"");
char *tmpval = strtok(tmp, "\"");
//copy data to main time var
if (gsm_clock_was_set)
strlcpy(time_char, tmpval, sizeof(time_char));
debug_print(F("gsm_get_time() result:"));
debug_print(time_char);
debug_print(F("gsm_get_time() completed"));
}
void gsm_startup_cmd() {
debug_print(F("gsm_startup_cmd() started"));
//disable echo for TCP data
gsm_port.print("AT+QISDE=0");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#if MODEM_M95
//set receiving TCP data by command
gsm_port.print("AT+QINDI=1");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
//set multiple socket support
gsm_port.print("AT+QIMUX=1");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#endif
//set SMS as text format
gsm_port.print("AT+CMGF=1");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#if GSM_USE_QUECLOCATOR_TIMEOUT > 0
//set QuectLocator timeout
gsm_port.print("AT+QLOCCFG=\"timeout\",");
gsm_port.print(GSM_USE_QUECLOCATOR_TIMEOUT);
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#endif
debug_print(F("gsm_startup_cmd() completed"));
}
void gsm_get_imei() {
debug_print(F("gsm_get_imei() started"));
//get modem's imei
gsm_port.print("AT+GSN");
gsm_port.print("\r");
status_delay(1000);
gsm_get_reply(1);
//reply data stored to modem_reply
char *tmp = strstr(modem_reply, "AT+GSN\r\r\n");
tmp += strlen("AT+GSN\r\r\n");
char *tmpval = strtok(tmp, "\r");
//copy data to main IMEI var
strlcpy(config.imei, tmpval, sizeof(config.imei));
debug_print(F("gsm_get_imei() result:"));
debug_print(config.imei);
debug_print(F("gsm_get_imei() completed"));
}
int gsm_send_at() {
debug_print(F("gsm_send_at() started"));
int ret = 0;
for (int k=0; k<5; ++k) {
gsm_port.print("ATE1\r");
status_delay(50);
gsm_get_reply(1);
ret = (strstr(modem_reply, "ATE1") != NULL)
&& (strstr(modem_reply, "OK") != NULL);
if (ret) break;
status_delay(1000);
}
debug_print(F("gsm_send_at() completed"));
debug_print(ret);
return ret;
}
int gsm_get_modem_status() {
debug_print(F("gsm_get_modem_status() started"));
gsm_port.print("AT+CPAS");
gsm_port.print("\r");
int pas = -1; // unexpected reply
for (int k=0; k<10; ++k) {
status_delay(50);
gsm_get_reply(0);
char *tmp = strstr(modem_reply, "+CPAS:");
if(tmp!=NULL) {
pas = atoi(tmp+6);
break;
}
}
gsm_wait_for_reply(1,0);
debug_print(F("gsm_get_modem_status() returned: "));
debug_print(pas);
return pas;
}
int gsm_disconnect() {
int ret = 0;
debug_print(F("gsm_disconnect() started"));
#if GSM_DISCONNECT_AFTER_SEND
// option to close data session after each server connection
ret = gsm_deactivate();
#else
//close connection, if previous attempts left it open
gsm_port.print(AT_CLOSE);
gsm_wait_for_reply(MODEM_UG96,0);
//ignore errors (will be taken care during connect)
ret = 1;
#endif
debug_print(F("gsm_disconnect() completed"));
return ret;
}
int gsm_deactivate() {
// disable data session
int ret = 0;
debug_print(F("gsm_deactivate() started"));
//disconnect GSM
gsm_port.print(AT_DEACTIVATE);
gsm_wait_for_reply(MODEM_UG96,0);
#if MODEM_UG96
//check if result contains OK
char *tmp = strstr(modem_reply, "OK");
#else
//check if result contains DEACT OK
char *tmp = strstr(modem_reply, "DEACT OK");
#endif
if(tmp!=NULL)
ret = 1;
debug_print(F("gsm_deactivate() completed"));
return ret;
}
int gsm_set_apn() {
debug_print(F("gsm_set_apn() started"));
//disconnect GSM
gsm_port.print(AT_DEACTIVATE);
gsm_wait_for_reply(MODEM_UG96,0);
addon_event(ON_MODEM_ACTIVATION);
//set all APN data, dns, etc
gsm_port.print(AT_CONTEXT "\"");
gsm_port.print(config.apn);
gsm_port.print("\",\"");
gsm_port.print(config.user);
gsm_port.print("\",\"");
gsm_port.print(config.pwd);
gsm_port.print("\"");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#if MODEM_M95
gsm_port.print("AT+QIDNSIP=1");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#endif
gsm_port.print(AT_ACTIVATE);
// wait for GPRS contex activation (first time)
unsigned long t = millis();
do {
gsm_wait_for_reply(1,0);
if(modem_reply[0] != 0) break;
}
while (millis() - t < 60000);
gsm_port.print(AT_LOCALIP); // diagnostic only
status_delay(500);
gsm_get_reply(0);
gsm_send_at();
gsm_port.print(AT_CONFIGDNS "\"8.8.8.8\"");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
debug_print(F("gsm_set_apn() completed"));
return 1;
}
int gsm_get_connection_status() {
debug_print(F("gsm_get_connection_status() started"));
int ret = -1; //unknown
gsm_get_reply(1); //flush buffer
gsm_port.print(AT_STAT);
gsm_wait_for_reply(1,0);
#if MODEM_UG96
char *tmp = strtok(modem_reply, ",");
if (tmp != NULL && strstr(modem_reply, "+QISTATE:") != NULL) {
for (int k=0; k<5; ++k) {
tmp = strtok(NULL, ",");
}
if (tmp != NULL) {
ret = atoi(tmp);
debug_print(ret);
if (ret == 3)
ret = 1; // already connected
else if (ret > 0)
ret = 2; // previous connection failed, should close
}
gsm_wait_for_reply(1,0); // catch OK
}
else if (strstr(modem_reply, "OK") != NULL)
ret = 0; // ready to connect
// check also data packet connection is active
gsm_get_reply(1); //flush buffer
gsm_port.print("AT+CGACT?\r");
gsm_wait_for_reply(1,1);
tmp = strstr(modem_reply, "+CGACT:");
if(tmp!=NULL) {
tmp = strtok(tmp + 7, ",");
if(tmp!=NULL) {
tmp = strtok(NULL, ",");
if(tmp!=NULL) {
if (atoi(tmp) != 1)
ret = -2; // force deactivation
}
}
}
#else
if (strstr(modem_reply, "OK\r\n") != NULL) {
gsm_wait_for_reply(0,0);
if (strstr(modem_reply, "IP IND") != NULL ||
strstr(modem_reply, "PDP DEACT") != NULL) {
ret = -2; // force deactivation
}
// find socket status
for (int i=0; i<6; ++i) {
gsm_wait_for_reply(0,0);
if (ret == -1 && strstr(modem_reply, "+QISTATE: 0,") != NULL) {
if (strstr(modem_reply, "INITIAL") != NULL ||
strstr(modem_reply, "CLOSE") != NULL)
ret = 0; // ready to connect
if (strstr(modem_reply, "CONNECTED") != NULL)
ret = 1; // already connected
if (strstr(modem_reply, "CONNECTING") != NULL)
ret = 2; // previous connection failed, should close
}
}
gsm_wait_for_reply(1,0); // catch final OK
}
#endif
debug_print(F("gsm_get_connection_status() returned:"));
debug_print(ret);
return ret;
}
int gsm_connect() {
int ret = 0;
debug_print(F("gsm_connect() started"));
//try to connect multiple times
for(int i=0;i<CONNECT_RETRY;i++) {
// connect only when modem is ready
if (gsm_get_modem_status() == 0) {
// check if connected from previous attempts
int ipstat = gsm_get_connection_status();
if (ipstat > 1) {
//close connection, if previous attempts failed
gsm_port.print(AT_CLOSE);
gsm_wait_for_reply(MODEM_UG96,0);
ipstat = 0;
}
if (ipstat < 0) {
//deactivate required
gsm_port.print(AT_DEACTIVATE);
gsm_wait_for_reply(MODEM_UG96,0);
ipstat = 0;
#if MODEM_UG96
gsm_port.print(AT_ACTIVATE);
gsm_wait_for_reply(1,0);
gsm_port.print(AT_CONFIGDNS "\"8.8.8.8\"");
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
#endif
}
if (ipstat == 0) {
debug_print(F("Connecting to remote server..."));
debug_print(i);
//open socket connection to remote host
//opening connection
gsm_port.print(AT_OPEN "\"");
gsm_port.print(PROTO);
gsm_port.print("\",\"");
gsm_port.print(HOSTNAME);
gsm_port.print("\",");
#if MODEM_M95
gsm_port.print("\"");
#endif
gsm_port.print(HTTP_PORT);
#if MODEM_M95
gsm_port.print("\"");
#endif
gsm_port.print("\r");
gsm_wait_for_reply(1, 0); // OK sent first
long timer = millis();
if(strstr(modem_reply, "OK")==NULL)
ipstat = 0;
else
do {
gsm_get_reply(1);
#if MODEM_UG96
char *tmp = strstr(modem_reply, "+QIOPEN: 0,");
if(tmp!=NULL) {
tmp += strlen("+QIOPEN: 0,");
if (atoi(tmp)==0)
ipstat = 1;
else
ipstat = 0;
break;
}
#else
if(strstr(modem_reply, "CONNECT OK")!=NULL) {
ipstat = 1;
break;
}
if(strstr(modem_reply, "CONNECT FAIL")!=NULL ||
strstr(modem_reply, "ERROR")!=NULL) {
ipstat = 0;
break;
}
#endif
addon_delay(100);
} while (millis() - timer < CONNECT_TIMEOUT);
}
if(ipstat == 1) {
debug_print(F("Connected to remote server: "));
debug_print(HOSTNAME);
ret = 1;
break;
} else {
debug_print(F("Can not connect to remote server: "));
debug_print(HOSTNAME);
// debug only:
gsm_port.print("AT+CEER\r");
gsm_wait_for_reply(1,0);
}
}
addon_delay(2000); // wait 2s before retrying
}
debug_print(F("gsm_connect() completed"));
return ret;
}
int gsm_validate_tcp() {
int nonacked = 0;
int ret = 0;
debug_print(F("gsm_validate_tcp() started."));
//todo check in the loop if everything delivered
for(int k=0;k<10;k++) {
gsm_port.print(AT_QUERYACK);
gsm_wait_for_reply(1,1);
//todo check if everything is delivered
char *tmp = strstr(modem_reply, AT_ACKRESP);
tmp += strlen(AT_ACKRESP);
//checking how many bytes NON-acked
tmp = strtok(tmp, ", \r\n");
tmp = strtok(NULL, ", \r\n");
tmp = strtok(NULL, ", \r\n");
//non-acked value
nonacked = atoi(tmp);
if(nonacked <= PACKET_SIZE_DELIVERY) {
//all data has been delivered to the server , if not wait and check again
debug_print(F("gsm_validate_tcp() data delivered."));
ret = 1;
break;
} else {
debug_print(F("gsm_validate_tcp() data not yet delivered."));
}
}
debug_print(F("gsm_validate_tcp() completed."));
return ret;
}
int gsm_send_begin(int data_len) {
//sending header packet to remote host
gsm_port.print(AT_SEND);
gsm_port.print(data_len);
gsm_port.print("\r");
gsm_wait_for_reply(1,0);
if (strncmp(modem_reply, "> ", 2) == 0)
return 1; // accepted, can send data
return 0; // error, cannot send data
}
int gsm_send_done() {
gsm_wait_for_reply(1,0);
if (strncmp(modem_reply, "SEND OK", 7) == 0)
return 1; // send successful
return 0; // error
}
#ifdef HTTP_USE_GET
const char HTTP_HEADER0[ ] = //HTTP header line before GET params
"GET " URL "?";
const char HTTP_HEADER1[ ] = //HTTP header line before length
" HTTP/1.0\r\n"
"Host: " HOSTNAME "\r\n"
"Content-length: 0";
#else
const char HTTP_HEADER1[ ] = //HTTP header line before length
"POST " URL " HTTP/1.0\r\n"
"Host: " HOSTNAME "\r\n"
"Content-type: application/x-www-form-urlencoded\r\n"
"Content-length: ";
#endif
const char HTTP_HEADER2[ ] = //HTTP header line after length
"\r\n"
"User-Agent: " HTTP_USER_AGENT "\r\n"
"Connection: close\r\n"
"\r\n";
int gsm_send_http_current() {
//send HTTP request, after connection if fully opened
//this will send Current data
debug_print(F("gsm_send_http(): sending current data."));
debug_print(data_current);
//getting length of data full package
#ifdef HTTP_USE_GET
int http_len = strlen(config.imei)+strlen(config.key);
#else
int http_len = strlen(config.imei)+strlen(config.key)+url_encoded_strlen(data_current);
#endif
http_len += strlen(HTTP_PARAM_IMEI) + strlen(HTTP_PARAM_KEY) + strlen(HTTP_PARAM_DATA) + 5; //imei= &key= &d=
debug_print(F("gsm_send_http(): Length of data packet:"));
debug_print(http_len);
#ifdef HTTP_USE_GET
int tmp_len = strlen(HTTP_HEADER0)+http_len;
#else
//length of header package
char tmp_http_len[7];
itoa(http_len, tmp_http_len, 10);
int tmp_len = strlen(HTTP_HEADER1)+strlen(tmp_http_len)+strlen(HTTP_HEADER2);
#endif
addon_event(ON_SEND_DATA);
if (gsm_get_modem_status() == 4) {
debug_print(F("gsm_send_http(): call interrupted"));
return 0; // abort
}
debug_print(F("gsm_send_http(): Length of header packet:"));
debug_print(tmp_len);
//sending header packet to remote host
if (!gsm_send_begin(tmp_len)) {
debug_print(F("gsm_send_http(): send refused"));
return 0; // abort
}
//sending header
#ifdef HTTP_USE_GET
gsm_port.print(HTTP_HEADER0);
debug_print(F("gsm_send_http(): Sending GET params"));
debug_print(F("gsm_send_http(): Sending IMEI and Key"));
debug_print(config.imei);
// don't disclose the key
#else
gsm_port.print(HTTP_HEADER1);
gsm_port.print(tmp_http_len);
gsm_port.print(HTTP_HEADER2);
if (!gsm_send_done()) {
debug_print(F("gsm_send_http(): send error"));
return 0; // abort
}
//validate header delivery
gsm_validate_tcp();
addon_event(ON_SEND_DATA);
if (gsm_get_modem_status() == 4) {
debug_print(F("gsm_send_http(): call interrupted"));
return 0; // abort
}
debug_print(F("gsm_send_http(): Sending IMEI and Key"));
debug_print(config.imei);
// don't disclose the key
//sending imei and key first
if (!gsm_send_begin(13+strlen(config.imei)+strlen(config.key))) {
debug_print(F("gsm_send_http(): send refused"));
return 0; // abort
}
#endif
gsm_port.print(HTTP_PARAM_IMEI "=");
gsm_port.print(config.imei);
gsm_port.print("&" HTTP_PARAM_KEY "=");
gsm_port.print(config.key);
gsm_port.print("&" HTTP_PARAM_DATA "=");
if (!gsm_send_done()) {
debug_print(F("gsm_send_http(): send error"));
return 0; // abort
}
//validate header delivery
gsm_validate_tcp();
debug_print(F("gsm_send_http(): Sending body"));
int tmp_ret = gsm_send_data_current();
#ifdef HTTP_USE_GET
if (tmp_ret) {
gsm_validate_tcp();
// finish sending headers
tmp_len = strlen(HTTP_HEADER1)+strlen(HTTP_HEADER2);
addon_event(ON_SEND_DATA);
if (gsm_get_modem_status() == 4) {
debug_print(F("gsm_send_http(): call interrupted"));
return 0; // abort
}
debug_print(F("gsm_send_http(): Length of header packet:"));
debug_print(tmp_len);
//sending header packet to remote host
if (!gsm_send_begin(tmp_len)) {
debug_print(F("gsm_send_http(): send refused"));
return 0; // abort
}
gsm_port.print(HTTP_HEADER1);
gsm_port.print(HTTP_HEADER2);
if (!gsm_send_done()) {
debug_print(F("gsm_send_http(): send error"));
return 0; // abort
}
}
#endif
debug_print(F("gsm_send_http(): data sent."));
return tmp_ret;
}
int gsm_send_data_current() {
// avoid large buffer on the stack (not reentrant)
static char buf[PACKET_SIZE];
debug_print(F("gsm_send_data_current(): sending data."));
debug_print(data_current);
int data_len = strlen(data_current);
int chunk_len = 0;
int chunk_pos = 0;
debug_print(F("gsm_send_data_current(): Body packet size:"));
debug_print(data_len);
for(int i=0; i<data_len; ) {
int done = url_encoded_strlcpy(buf, sizeof(buf), &data_current[i]);
i += done;
chunk_len = strlen(buf);
addon_event(ON_SEND_DATA);
if (gsm_get_modem_status() == 4) {
debug_print(F("gsm_send_data_current(): call interrupted"));
return 0; // abort
}
// start chunk
debug_print(F("gsm_send_data_current(): Sending data chunk:"));
debug_print(chunk_pos);
debug_print(F("gsm_send_data_current(): chunk length:"));
debug_print(chunk_len);
//sending chunk
if (!gsm_send_begin(chunk_len)) {
debug_print(F("gsm_send_data_current(): send refused"));
return 0; // abort
}
//sending data
gsm_port.print(buf);
debug_print(buf);
// end chunk
if (!gsm_send_done()) {
debug_print(F("gsm_send_data_current(): send error"));
return 0; // abort
}
chunk_pos += chunk_len;
//validate previous transmission
gsm_validate_tcp();
}
debug_print(F("gsm_send_data_current(): completed"));
return 1;
}
int gsm_send_data() {
int ret_tmp = 0;
//send 2 ATs
gsm_send_at();
//make sure there is no connection
gsm_disconnect();
addon_event(ON_SEND_STARTED);
//opening connection
ret_tmp = gsm_connect();
if(ret_tmp == 1) {
//connection opened, just send data
#if SEND_RAW
ret_tmp = gsm_send_data_current();
#else
// send data, if ok then parse reply
ret_tmp = gsm_send_http_current() && parse_receive_reply();
#endif
}
gsm_disconnect(); // always
if(ret_tmp) {
gsm_send_failures = 0;