-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncGSM.cpp
More file actions
executable file
·868 lines (715 loc) · 23.1 KB
/
Copy pathAsyncGSM.cpp
File metadata and controls
executable file
·868 lines (715 loc) · 23.1 KB
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
/*
AsyncGSM.cpp
*/
#include "Arduino.h"
#include "AsyncGSM.h"
#define GSM_DEBUG_PRINT(...) debugStream->print(__VA_ARGS__)
#define GSM_DEBUG_PRINTLN(...) debugStream->println(__VA_ARGS__)
AsyncGSM::AsyncGSM(uint8_t reset, uint8_t pstat, uint8_t key)
{
ok_reply = F("OK");
pinMode(reset, OUTPUT);
pinMode(pstat, INPUT);
pinMode(key, OUTPUT);
digitalWrite(key, HIGH);
this->power_state = digitalRead(pstat);
this->reset = reset;
this->pstat = pstat;
this->key = key;
modem_state = STATE_IDLE;
autobauding = 0;
echo = 0;
cnmi = 0;
command_state = COMMAND_NONE;
currentconnection = -1;
command_timeout = 10000;
}
void AsyncGSM::setPower(uint8_t power) {
this->power = power;
uint8_t current_power = digitalRead(pstat);
//GSM_DEBUG_PRINTLN(F("setPower: "));
//GSM_DEBUG_PRINTLN(power);
//GSM_DEBUG_PRINTLN(current_power);
}
uint8_t AsyncGSM::handlePowerState() {
uint8_t current_power = digitalRead(pstat);
if (!current_power && power && power_state == POWER_STATE_OFF) {
// start turning on
GSM_DEBUG_PRINT(F("Turning on. Current power: "));
GSM_DEBUG_PRINTLN(current_power);
power_state = POWER_STATE_STARTING;
digitalWrite(key, LOW);
power_state_changed = millis();
return 0;
} else if (current_power && !power && power_state == POWER_STATE_ON) {
// start turning off
GSM_DEBUG_PRINT(F("Turning off. Current power: "));
GSM_DEBUG_PRINTLN(current_power);
power_state = POWER_STATE_STOPPING;
digitalWrite(key, LOW);
power_state_changed = millis();
return 0;
} else if ((power_state == POWER_STATE_STOPPING || power_state == POWER_STATE_STARTING) && millis() > (power_state_changed + 3000)) {
// stop "pressing" key and reset power_state
GSM_DEBUG_PRINTLN(F("Release key button"));
digitalWrite(key, HIGH);
power_state = digitalRead(pstat);
GSM_DEBUG_PRINT(F("current_power: "));
GSM_DEBUG_PRINTLN(power_state);
resetModemState();
return power_state;
}
return current_power;
}
void AsyncGSM::resetModemState() {
echo = 0;
cscs = 0;
clip = 0;
cmgf = 0;
cnmi = 0;
cipmux = 0;
creg = 0;
modem_state = STATE_IDLE;
autobauding = 0;
command_state = COMMAND_NONE;
gprs_state = GPRS_STATE_UNKNOWN;
currentconnection = -1;
command_timeout = 10000;
}
uint8_t AsyncGSM::initialize(Stream &serial)
{
mySerial = &serial;
}
void AsyncGSM::setDebugStream(Stream &stream)
{
debugStream = &stream;
}
uint8_t AsyncGSM::writeBuffer(CircularBuffer * buffer, char data) {
int next = buffer->head + 1;
if (next >= NELEMS(buffer->buffer))
next = 0;
// Cicular buffer is full
if (next == buffer->tail)
return -1; // quit with an error
buffer->buffer[buffer->head] = data;
buffer->head = next;
return 0;
}
uint8_t AsyncGSM::readBuffer(CircularBuffer * buffer, char * data) {
// if the head isn't ahead of the tail, we don't have any characters
if (buffer->head == buffer->tail)
return -1; // quit with an error
*data = buffer->buffer[buffer->tail];
buffer->buffer[buffer->tail] = 0; // clear the data (optional)
int next = buffer->tail + 1;
if(next >= NELEMS(buffer->buffer))
next = 0;
buffer->tail = next;
return 0;
}
uint8_t AsyncGSM::bufferSize(CircularBuffer * buffer) {
if (buffer->head > buffer->tail) {
return buffer->head - buffer->tail;
}
if (buffer->head < buffer->tail) {
return GSM_BUFFER_SIZE - buffer->tail + buffer->head;
}
return 0;
}
uint8_t AsyncGSM::dataAvailable(int connection) {
return bufferSize(&connectionState[connection].inboundCircular);
}
uint8_t AsyncGSM::outboundBufferSize(int connection) {
return bufferSize(&connectionState[connection].outboundCircular);
}
uint8_t AsyncGSM::messageAvailable() {
return messageBuffer.available;
}
ShortMessage AsyncGSM::readMessage() {
ShortMessage messageCopy = messageBuffer;
messageBuffer.available = 0;
return messageCopy;
}
void AsyncGSM::sendMessage(ShortMessage message) {
outboundMessage = message;
}
int8_t AsyncGSM::incomingCall() {
return incomingcall;
}
char * AsyncGSM::getCallerIdentification() {
return callerId;
}
void AsyncGSM::answerIncomingCall() {
if (incomingcall && !answerincomingcall && !callinprogress) {
answerincomingcall = 1;
}
}
void AsyncGSM::hangupCall() {
}
// state machine
void AsyncGSM::process() {
uint8_t current_power = handlePowerState();
if (!current_power) {
return;
}
if(mySerial->available() > 0) {
processIncomingModemByte(mySerial->read());
}
// check for timeout
if (modem_state == STATE_WAITING_REPLY && millis() > last_command + command_timeout) {
GSM_DEBUG_PRINTLN(F("TIMEOUT"));
modem_state = STATE_IDLE;
}
if (modem_state == STATE_IDLE && !autobauding) {
queueAtCommand(F("AT"), 2000);
return;
}
if (modem_state == STATE_IDLE && !echo && autobauding) {
queueAtCommand(F("ATE0"), 5000);
command_state = COMMAND_ATE;
return;
}
if (modem_state == STATE_IDLE && incomingcall && answerincomingcall) {
queueAtCommand(F("ATA"), 5000);
command_state = COMMAND_ATA;
return;
}
if (modem_state == STATE_IDLE && (millis() > last_csq_update + 90000) && autobauding) {
queueAtCommand(F("AT+CSQ"), 5000);
last_csq_update = millis();
command_state = COMMAND_CSQ;
return;
}
if (modem_state == STATE_IDLE && (millis() > last_battery_update + 60000) && autobauding) {
queueAtCommand(F("AT+CBC"), 5000);
last_battery_update = millis();
command_state = COMMAND_CBC;
return;
}
if (modem_state == STATE_IDLE && (millis() > last_creg + 60000) && autobauding && creg < 2) {
queueAtCommand(F("AT+CREG?"), 5000);
last_creg = millis();
command_state = COMMAND_TEST_CREG;
return;
} else if (creg < 2) {
return;
}
if (modem_state == STATE_IDLE && autobauding && !powersave && enable_powersave) {
queueAtCommand(F("AT+CSCLK=1"), 5000);
command_state = COMMAND_ENABLE_POWERSAVE;
return;
}
if (modem_state == STATE_IDLE && autobauding && powersave && !enable_powersave) {
queueAtCommand(F("AT+CSCLK=0"), 5000);
command_state = COMMAND_DISABLE_POWERSAVE;
return;
}
if (modem_state == STATE_IDLE && !clts && autobauding) {
queueAtCommand(F("AT+CLTS=1"), 5000);
command_state = COMMAND_SET_CLTS;
return;
}
if (modem_state == STATE_IDLE && !clip && autobauding) {
queueAtCommand(F("AT+CLIP=1"), 5000);
command_state = COMMAND_WRITE_CLIP;
return;
}
if (modem_state == STATE_IDLE && cipmux == 1 && autobauding && creg == 2) {
queueAtCommand(F("AT+CIPMUX=1"), 5000);
command_state = COMMAND_WRITE_CIPMUX;
return;
}
if (modem_state == STATE_IDLE && !cipmux && autobauding && creg == 2) {
queueAtCommand(F("AT+CIPMUX?"), 5000);
command_state = COMMAND_TEST_CIPMUX;
return;
}
if (modem_state == STATE_IDLE && gprs_state == GPRS_STATE_UNKNOWN && autobauding && creg == 2 && enable_gprs) {
queueAtCommand(F("AT+CIPSHUT"), 10000);
command_state = COMMAND_CIPSHUT;
return;
}
if (modem_state == STATE_IDLE && (gprs_state != GPRS_STATE_IP_INITIAL) && autobauding && creg == 2 && !enable_gprs) {
queueAtCommand(F("AT+CIPSHUT"), 10000);
command_state = COMMAND_CIPSHUT;
return;
}
if (modem_state == STATE_IDLE && gprs_state == GPRS_STATE_IP_INITIAL && autobauding && creg == 2 && enable_gprs) {
queueAtCommand(F("AT+CSTT=\"internet.saunalahti\",\"\",\"\""), 10000);
command_state = COMMAND_SET_CSTT;
return;
}
if (modem_state == STATE_IDLE && !cnmi && autobauding) {
queueAtCommand(F("AT+CNMI?"), 10000);
command_state = COMMAND_TEST_CNMI;
return;
}
if (modem_state == STATE_IDLE && !cmgf && autobauding) {
queueAtCommand(F("AT+CMGF?"), 10000);
command_state = COMMAND_TEST_CMGF;
return;
}
if (modem_state == STATE_IDLE && cmgf == 1 && autobauding) {
// text mode sms
queueAtCommand(F("AT+CMGF=1"), 5000);
command_state = COMMAND_WRITE_CMGF;
return;
}
if (modem_state == STATE_IDLE && !cscs && autobauding) {
// text mode sms
queueAtCommand(F("AT+CSCS=\"8859-1\""), 5000);
command_state = COMMAND_WRITE_CSCS;
return;
}
if (modem_state == STATE_IDLE && cnmi == 1 && autobauding) {
queueAtCommand(F("AT+CNMI=2,2,0,0,0"), 60000);
command_state = COMMAND_WRITE_CNMI;
return;
}
if (modem_state == STATE_IDLE && (millis() > last_time_update + 120000) && autobauding) {
queueAtCommand(F("AT+CCLK?"), 5000);
last_time_update = millis();
command_state = COMMAND_TEST_CCLK;
return;
}
if (modem_state == STATE_IDLE && gprs_state == GPRS_STATE_IP_START && autobauding && creg == 2 && enable_gprs) {
queueAtCommand(F("AT+CIICR"), 120000);
command_state = COMMAND_SET_CIICR;
return;
}
if (modem_state == STATE_IDLE && gprs_state == GPRS_STATE_IP_GPRSACT && autobauding && creg == 2 && enable_gprs) {
queueAtCommand(F("AT+CIFSR"), 120000);
command_state = COMMAND_CIFSR;
return;
}
for (int i = 0; i < NELEMS(connectionState); i++) {
if (modem_state == STATE_IDLE &&
gprs_state == GPRS_STATE_IP_STATUS &&
connectionState[i].connectionState != GPRS_STATE_CONNECT_OK &&
creg == 2 &&
strlen(connectionState[i].address) > 0 &&
connectionState[i].port != 0 &&
connectionState[i].connect &&
enable_gprs) {
char command[32];
sprintf(command, "AT+CIPSTART=%u,\"%s\",\"%s\",%u",
i,
connectionState[i].type == CONNECTION_TYPE_TCP ? "TCP" : "UDP",
connectionState[i].address,
connectionState[i].port);
queueAtCommand(command, 60000);
command_state = COMMAND_WRITE_CIPSTART;
currentconnection = i;
return;
}
}
for (int j = 0; j < NELEMS(connectionState); j++) {
if (modem_state == STATE_IDLE &&
gprs_state == GPRS_STATE_IP_STATUS &&
connectionState[j].connectionState == GPRS_STATE_CONNECT_OK &&
bufferSize(&connectionState[j].outboundCircular) > 0 && creg == 2 && enable_gprs) {
char command[16];
connectionState[j].outboundBytes = bufferSize(&connectionState[j].outboundCircular);
sprintf(command, "AT+CIPSEND=%u,%u", j, connectionState[j].outboundBytes);
queueAtCommand(command, 120000);
command_state = COMMAND_WRITE_CIPSEND;
currentconnection = j;
return;
}
}
for (int i = 0; i < NELEMS(connectionState); i++) {
if (modem_state == STATE_IDLE &&
gprs_state == GPRS_STATE_IP_STATUS &&
connectionState[i].connectionState == GPRS_STATE_CONNECT_OK &&
creg == 2 &&
!connectionState[i].connect && enable_gprs) {
char command[32];
sprintf(command, "AT+CIPCLOSE=%u,0", i);
queueAtCommand(command, 60000);
command_state = COMMAND_WRITE_CIPCLOSE;
currentconnection = i;
return;
}
}
/*
if (millis() > last_udp_send + 120000) {
char data[16];
sprintf(data, "Test%u", millis());
writeData(data, strlen(data));
last_udp_send = millis();
}
*/
if (modem_state == STATE_IDLE && strlen(outboundMessage.message) > 0 && autobauding && creg == 2) {
char command[64];
sprintf(command, "AT+CMGS=\"%s\"", outboundMessage.msisdn);
queueAtCommand(command, 10000);
command_state = COMMAND_WRITE_CMGS;
}
}
uint8_t AsyncGSM::isConnected(int connection) {
return connectionState[connection].connectionState == GPRS_STATE_CONNECT_OK;
}
uint8_t AsyncGSM::isModemIdle() {
return modem_state == STATE_IDLE;
}
uint8_t AsyncGSM::isModemError() {
return modem_state == STATE_ERROR;
}
uint8_t AsyncGSM::isModemRegistered() {
return creg == 2;
}
void AsyncGSM::enableGprs() {
enable_gprs = 1;
}
void AsyncGSM::disableGprs() {
enable_gprs = 0;
}
void AsyncGSM::enablePowerSave() {
enable_powersave = 1;
}
void AsyncGSM::disablePowerSave() {
enable_powersave = 0;
}
uint8_t AsyncGSM::isGprsEnabled() {
return gprs_state == GPRS_STATE_IP_STATUS;
}
uint8_t AsyncGSM::isGprsDisabled() {
return gprs_state == GPRS_STATE_UNKNOWN || gprs_state == GPRS_STATE_IP_INITIAL;
}
void AsyncGSM::connect(char * data, int port, int connection, int type) {
memcpy(connectionState[connection].address, data, strlen(data) + 1);
connectionState[connection].port = port;
connectionState[connection].type = type;
connectionState[connection].connect = 1;
}
void AsyncGSM::disconnect(int connection) {
connectionState[connection].address[0] = NULL;
connectionState[connection].port = 0;
connectionState[connection].connect = 0;
}
uint8_t AsyncGSM::writeData(char * data, int len, int connection) {
for (int i = 0; i < len; i++) {
writeBuffer(&connectionState[connection].outboundCircular, data[i]);
}
}
void AsyncGSM::queueAtCommand(char * command, uint32_t timeout) {
GSM_DEBUG_PRINT(F("--> ")); GSM_DEBUG_PRINTLN(command);
mySerial->println(command);
last_command = millis();
command_timeout = timeout;
modem_state = STATE_WAITING_REPLY;
GSM_DEBUG_PRINTLN(F("STATE_WAITING_REPLY"));
}
void AsyncGSM::queueAtCommand(GSMFlashStringPtr command, uint32_t timeout) {
GSM_DEBUG_PRINT(F("--> ")); GSM_DEBUG_PRINTLN(command);
mySerial->println(command);
last_command = millis();
command_timeout = timeout;
modem_state = STATE_WAITING_REPLY;
GSM_DEBUG_PRINTLN(F("STATE_WAITING_REPLY"));
}
time_t AsyncGSM::getCurrentTime() {
return last_network_time + (millis() - last_network_time_update) / 1000;
}
uint8_t AsyncGSM::parseConnectionNumber(char * data) {
data[1] = 0;
return atoi(data);
}
void AsyncGSM::processIncomingModemByte (const byte inByte) {
switch (inByte) {
case '\n': // end of text
input_modem_line[input_modem_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_modem_data (input_modem_line);
// reset buffer for next time
input_modem_pos = 0;
break;
case '\r': // discard carriage return
break;
case '>':
input_modem_line[input_modem_pos++] = inByte;
input_modem_line[input_modem_pos++] = 0;
process_modem_data(input_modem_line);
input_modem_pos = 0;
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_modem_pos < (MAX_INPUT - 1))
input_modem_line [input_modem_pos++] = inByte;
break;
}
}
#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
time_t AsyncGSM::parseTime(char * data) {
char year[3];
char month[3];
char day[3];
char hour[3];
char minute[3];
char secs[3];
memcpy(year, data, 2);
year[2] = 0;
memcpy(month, data + 3, 2);
month[2] = 0;
memcpy(day, data + 6, 2);
day[2] = 0;
memcpy(hour, data + 9, 2);
hour[2] = 0;
memcpy(minute, data + 12, 2);
minute[2] = 0;
memcpy(secs, data + 15, 2);
secs[2] = 0;
tmelements_t tm;
tm.second = atoi(secs);
tm.minute = atoi(minute);
tm.hour = atoi(hour);
tm.day = atoi(day);
tm.month = atoi(month);
tm.year = atoi(year) + 2000 - 1970;
int i;
uint32_t seconds;
// seconds from 1970 till 1 jan 00:00:00 of the given year
seconds= tm.year*(SECS_PER_DAY * 365);
for (i = 0; i < tm.year; i++) {
if (LEAP_YEAR(i)) {
seconds += SECS_PER_DAY; // add extra days for leap years
}
}
// add days for this year, months start from 1
for (i = 1; i < tm.month; i++) {
if ( (i == 2) && LEAP_YEAR(tm.year)) {
seconds += SECS_PER_DAY * 29;
} else {
seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0
}
}
seconds+= (tm.day-1) * SECS_PER_DAY;
seconds+= tm.hour * SECS_PER_HOUR;
seconds+= tm.minute * SECS_PER_MIN;
seconds+= tm.second;
return (time_t)seconds;
}
void AsyncGSM::process_modem_data (char * data) {
GSM_DEBUG_PRINT(F("<-- "));
GSM_DEBUG_PRINTLN(data);
if (strstr(data, "+CMT:") != 0) {
command_state = COMMAND_UCR_CMT;
modem_state = STATE_UCR;
}
if (command_state == COMMAND_UCR_CMT) {
int index = 0;
char * pch;
pch = strtok (data, "\"");
while (pch != NULL) {
if (index == 1) {
memcpy(messageBuffer.msisdn, pch, strlen(pch) + 1);
} else if (index == 4) {
messageBuffer.receive_time = parseTime(pch);
}
pch = strtok (NULL, "\"");
index++;
}
command_state = COMMAND_UCR_CMT_DATA;
return;
}
if (command_state == COMMAND_TEST_CCLK) {
if (strstr(data, "+CCLK:") != 0) {
last_network_time = parseTime(data + 8);
last_network_time_update = millis();
GSM_DEBUG_PRINT("current_time: ");
GSM_DEBUG_PRINTLN(last_network_time);
}
}
if (command_state == COMMAND_UCR_CMT_DATA) {
memcpy(messageBuffer.message, data, strlen(data) + 1);
messageBuffer.available = 1;
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(messageBuffer.msisdn);
GSM_DEBUG_PRINTLN(messageBuffer.receive_time);
GSM_DEBUG_PRINTLN(messageBuffer.message);
GSM_DEBUG_PRINTLN("STATE_IDLE");
}
if (command_state == COMMAND_WRITE_CMGS) {
if (strstr(data, "OK") != 0) {
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN("STATE_IDLE");
}
if (strstr(data, ">") != 0) {
GSM_DEBUG_PRINTLN(strlen(outboundMessage.message));
GSM_DEBUG_PRINT(F("--> "));
GSM_DEBUG_PRINTLN(outboundMessage.message);
mySerial->write(outboundMessage.message, strlen(outboundMessage.message));
mySerial->write("\x1A");
mySerial->flush();
outboundMessage.message[0] = 0;
outboundMessage.msisdn[0] = 0;
}
}
if (command_state == COMMAND_TEST_CREG) {
if (strstr(data, "+CREG: 0,1") != 0) {
creg = 2;
return;
} else if (strstr(data, "+CREG: 0,2") != 0) {
creg = 1;
return;
}
}
if (command_state == COMMAND_WRITE_CIPSEND) {
if (strstr(data, ">") != 0) {
// write data
int len = connectionState[currentconnection].outboundBytes;
char data[len];
for (int i = 0; i < len; i++) {
readBuffer(&connectionState[currentconnection].outboundCircular, data + i);
}
GSM_DEBUG_PRINT(F("Writing to gsm serial"));
GSM_DEBUG_PRINTLN(connectionState[currentconnection].outboundBytes);
mySerial->write(data, len);
mySerial->flush();
GSM_DEBUG_PRINTLN(F("Write ok."));
return;
}
if (strstr(data, "SEND OK") != 0) {
modem_state = STATE_IDLE;
currentconnection = -1;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
return;
}
}
if (command_state == COMMAND_WRITE_CIPSTART) {
GSM_DEBUG_PRINTLN("COMMAND_WRITE_CIPSTART2");
if (strstr(data, "CONNECT OK") != 0) {
connectionState[currentconnection].connectionState = GPRS_STATE_CONNECT_OK;
modem_state = STATE_IDLE;
currentconnection = -1;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
return;
} else if (strstr(data, "CONNECT FAIL") != 0) {
// tcp or udp connection failed
uint8_t connectionNumber = parseConnectionNumber(data);
GSM_DEBUG_PRINTLN(connectionNumber);
connectionState[0].connectionState = GPRS_STATE_IP_INITIAL;
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
}
if (strstr(data, "OK") != 0) {
return;
}
}
if (command_state == COMMAND_WRITE_CIPCLOSE) {
if (strstr(data, "CLOSE OK") != 0) {
// tcp or udp connection closed
uint8_t connectionNumber = parseConnectionNumber(data);
connectionState[connectionNumber].connectionState = GPRS_STATE_IP_INITIAL;
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
}
}
if (command_state == COMMAND_TEST_CNMI && strstr(data, "+CNMI:") != 0) {
if (strstr(data, "+CNMI:2,2,0,0,0") == 0) {
cnmi = 1;
}
}
if (command_state == COMMAND_TEST_CMGF) {
if (strstr(data, "+CMGF: 1") != 0) {
cmgf = 2;
} else if (strstr(data, "+CMGF: 0") != 0) {
cmgf = 1;
}
}
if (command_state == COMMAND_CIPSHUT && strstr(data, "SHUT OK") != 0) {
gprs_state = GPRS_STATE_IP_INITIAL;
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
}
if (command_state == COMMAND_CIFSR && strlen(data) > 0) {
gprs_state = GPRS_STATE_IP_STATUS;
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
}
if (command_state == COMMAND_TEST_CIPMUX && strstr(data, "+CIPMUX:") != 0) {
if (strstr(data, "+CIPMUX: 1") != 0) {
cipmux = 2;
} else if (strstr(data, "+CIPMUX: 0") != 0) {
cipmux = 1;
}
}
if (strcmp(data, "OK") == 0) {
if (!autobauding) {
autobauding = 1;
}
if (command_state == COMMAND_ENABLE_POWERSAVE) {
powersave = 1;
}
if (command_state == COMMAND_DISABLE_POWERSAVE) {
powersave = 0;
}
if (command_state == COMMAND_ATA) {
callinprogress = 1;
answerincomingcall = 0;
}
if (command_state == COMMAND_ATE) {
echo = 1;
}
if (command_state == COMMAND_WRITE_CIPMUX) {
cipmux = 2;
}
if (command_state == COMMAND_SET_CSTT) {
gprs_state = GPRS_STATE_IP_START;
}
if (command_state == COMMAND_WRITE_CNMI) {
cnmi = 2;
}
if (command_state == COMMAND_SET_CIICR) {
gprs_state = GPRS_STATE_IP_GPRSACT;
}
if (command_state == COMMAND_SET_CLTS) {
clts = 1;
}
if (command_state == COMMAND_WRITE_CMGF) {
cmgf = 2;
}
if (command_state == COMMAND_WRITE_CSCS) {
cscs = 1;
}
if (command_state == COMMAND_WRITE_CLIP) {
clip = 1;
}
modem_state = STATE_IDLE;
GSM_DEBUG_PRINTLN(F("STATE_IDLE"));
}
if (strstr(data, "RING") != 0) {
incomingcall = 1;
}
if (strstr(data, "NO CARRIER") != 0) {
incomingcall = 0;
callinprogress = 0;
answerincomingcall = 0;
}
if (strstr(data, "SMS Ready") != 0) {
GSM_DEBUG_PRINTLN(F("resetModemState()"));
resetModemState();
}
if (strstr(data, "ERROR") != 0) {
modem_state = STATE_ERROR;
GSM_DEBUG_PRINTLN(F("STATE_ERROR"));
// reset modem
}
if (strstr(data, "CLOSED") != 0) {
// tcp or udp connection closed
uint8_t connectionNumber = parseConnectionNumber(data);
connectionState[0].connectionState = GPRS_STATE_IP_INITIAL;
}
if (strstr(data, "+RECEIVE") != 0) {
data[strlen(data) - 1] = NULL;
data[10] = NULL;
uint8_t connectionNumber = atoi(data + 9);
uint16_t availableData = atoi(data + 11);
GSM_DEBUG_PRINTLN(connectionNumber);
GSM_DEBUG_PRINTLN(availableData);
command_state = COMMAND_UCR_RECEIVE;
GSM_DEBUG_PRINTLN(F("COMMAND_UCR_RECEIVE"));
return;
}
}