-
Notifications
You must be signed in to change notification settings - Fork 30
/
inetGSM.cpp
586 lines (508 loc) · 13.3 KB
/
inetGSM.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
#include "inetGSM.h"
#define _GSM_CONNECTION_TOUT_ 5
#define _TCP_CONNECTION_TOUT_ 20
#define _GSM_DATA_TOUT_ 10
int InetGSM::httpGET(const char* server, int port, const char* path, char* result, int resultlength)
{
boolean connected=false;
int n_of_at=0;
int length_write;
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';
/*
Status = ATTACHED.
if(gsm.getStatus()!=GSM::ATTACHED)
return 0;
*/
while(n_of_at<3) {
if(!connectTCP(server, port)) {
#ifdef DEBUG_ON
Serial.println("DB:NOT CONN");
#endif
n_of_at++;
} else {
connected=true;
n_of_at=3;
}
}
if(!connected) return 0;
gsm.SimpleWrite("GET ");
gsm.SimpleWrite(path);
gsm.SimpleWrite(" HTTP/1.0\r\nHost: ");
gsm.SimpleWrite(server);
gsm.SimpleWrite("\r\n");
gsm.SimpleWrite("User-Agent: Arduino");
gsm.SimpleWrite("\r\n\r\n");
gsm.SimpleWrite(end_c);
switch(gsm.WaitResp(10000, 10, "SEND OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
delay(50);
#ifdef DEBUG_ON
Serial.println("DB:SENT");
#endif
int res = gsm.read(result, resultlength);
//gsm.disconnectTCP();
//int res=1;
return res;
}
int InetGSM::httpPOST(const char* server, int port, const char* path, const char* parameters, char* result, int resultlength)
{
boolean connected=false;
int n_of_at=0;
char itoaBuffer[8];
int num_char;
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';
while(n_of_at<3) {
if(!connectTCP(server, port)) {
#ifdef DEBUG_ON
Serial.println("DB:NOT CONN");
#endif
n_of_at++;
} else {
connected=true;
n_of_at=3;
}
}
if(!connected) return 0;
gsm.SimpleWrite("POST ");
gsm.SimpleWrite(path);
gsm.SimpleWrite(" HTTP/1.1\r\nHost: ");
gsm.SimpleWrite(server);
gsm.SimpleWrite("\r\n");
gsm.SimpleWrite("User-Agent: Arduino\r\n");
gsm.SimpleWrite("Content-Type: application/x-www-form-urlencoded\r\n");
gsm.SimpleWrite("Content-Length: ");
itoa(strlen(parameters),itoaBuffer,10);
gsm.SimpleWrite(itoaBuffer);
gsm.SimpleWrite("\r\n\r\n");
gsm.SimpleWrite(parameters);
gsm.SimpleWrite("\r\n\r\n");
gsm.SimpleWrite(end_c);
switch(gsm.WaitResp(10000, 10, "SEND OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
delay(50);
#ifdef DEBUG_ON
Serial.println("DB:SENT");
#endif
int res= gsm.read(result, resultlength);
//gsm.disconnectTCP();
return res;
}
int InetGSM::openmail(char* server, char* loginbase64, char* passbase64, char* from, char* to, char* subj)
{
boolean connected=false;
int n_of_at=0;
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';
while(n_of_at<3) {
if(!connectTCP(server, 25)) {
#ifdef DEBUG_ON
Serial.println("DB:NOT CONN");
#endif
n_of_at++;
} else {
connected=true;
n_of_at=3;
}
}
if(!connected) return 0;
delay(100);
gsm.SimpleWrite("HELO ");
gsm.SimpleWrite(server);
gsm.SimpleWrite("\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("SEND OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100);
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite("AUTH LOGIN\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100);
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite(loginbase64);
gsm.SimpleWrite("\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100);
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite(passbase64);
gsm.SimpleWrite("\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100);
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite("MAIL From: <");
gsm.SimpleWrite(from);
gsm.SimpleWrite(">\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100, "");
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite("RCPT TO: <");
gsm.SimpleWrite(to);
gsm.SimpleWrite(">\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100, "");
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite("Data\n");
gsm.SimpleWrite(end_c);
gsm.WaitResp(5000, 100, "OK");
if(!gsm.IsStringReceived("OK"))
return 0;
delay(500);
gsm.WaitResp(5000, 100, "");
delay(100);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
gsm.SimpleWrite("Subject: ");
gsm.SimpleWrite(subj);
gsm.SimpleWrite("\n\n");
return 1;
}
int InetGSM::closemail()
{
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';
gsm.SimpleWrite("\n.\n");
gsm.SimpleWrite(end_c);
disconnectTCP();
return 1;
}
int InetGSM::attachGPRS(char* domain, char* dom1, char* dom2)
{
int i=0;
delay(5000);
//gsm._tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
gsm.WaitResp(50, 50);
gsm.SimpleWriteln("AT+CIFSR");
if(gsm.WaitResp(5000, 50, "ERROR")!=RX_FINISHED_STR_RECV) {
#ifdef DEBUG_ON
Serial.println("DB:ALREADY HAVE AN IP");
#endif
gsm.SimpleWriteln("AT+CIPCLOSE");
gsm.WaitResp(5000, 50, "ERROR");
delay(2000);
gsm.SimpleWriteln("AT+CIPSERVER=0");
gsm.WaitResp(5000, 50, "ERROR");
return 1;
} else {
#ifdef DEBUG_ON
Serial.println("DB:STARTING NEW CONNECTION");
#endif
gsm.SimpleWriteln("AT+CIPSHUT");
switch(gsm.WaitResp(500, 50, "SHUT OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println("DB:SHUTTED OK");
#endif
delay(1000);
gsm.SimpleWrite("AT+CSTT=\"");
gsm.SimpleWrite(domain);
gsm.SimpleWrite("\",\"");
gsm.SimpleWrite(dom1);
gsm.SimpleWrite("\",\"");
gsm.SimpleWrite(dom2);
gsm.SimpleWrite("\"\r");
switch(gsm.WaitResp(500, 50, "OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println("DB:APN OK");
#endif
delay(5000);
gsm.SimpleWriteln("AT+CIICR");
switch(gsm.WaitResp(10000, 50, "OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println("DB:CONNECTION OK");
#endif
delay(1000);
gsm.SimpleWriteln("AT+CIFSR");
if(gsm.WaitResp(5000, 50, "ERROR")!=RX_FINISHED_STR_RECV) {
#ifdef DEBUG_ON
Serial.println("DB:ASSIGNED AN IP");
#endif
gsm.setStatus(gsm.ATTACHED);
return 1;
}
#ifdef DEBUG_ON
Serial.println("DB:NO IP AFTER CONNECTION");
#endif
return 0;
}
}
int InetGSM::dettachGPRS()
{
if (gsm.getStatus()==gsm.IDLE) return 0;
//gsm._tf.setTimeout(_GSM_CONNECTION_TOUT_);
//_cell.flush();
//GPRS dettachment.
gsm.SimpleWriteln("AT+CGATT=0");
if(gsm.WaitResp(5000, 50, "OK")!=RX_FINISHED_STR_NOT_RECV) {
gsm.setStatus(gsm.ERROR);
return 0;
}
delay(500);
// Commented in initial trial code!!
//Stop IP stack.
//_cell << "AT+WIPCFG=0" << _DEC(cr) << endl;
// if(!gsm._tf.find("OK")) return 0;
//Close GPRS bearer.
//_cell << "AT+WIPBR=0,6" << _DEC(cr) << endl;
gsm.setStatus(gsm.READY);
return 1;
}
int InetGSM::connectTCP(const char* server, int port)
{
//gsm._tf.setTimeout(_TCP_CONNECTION_TOUT_);
//Status = ATTACHED.
//if (getStatus()!=ATTACHED)
//return 0;
//_cell.flush();
//Visit the remote TCP server.
gsm.SimpleWrite("AT+CIPSTART=\"TCP\",\"");
gsm.SimpleWrite(server);
gsm.SimpleWrite("\",");
gsm.SimpleWriteln(port);
switch(gsm.WaitResp(1000, 200, "OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println("DB:RECVD CMD");
#endif
if (!gsm.IsStringReceived("CONNECT OK")) {
switch(gsm.WaitResp(15000, 200, "OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
}
#ifdef DEBUG_ON
Serial.println("DB:OK TCP");
#endif
delay(3000);
gsm.SimpleWriteln("AT+CIPSEND");
switch(gsm.WaitResp(5000, 200, ">")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println("DB:>");
#endif
delay(4000);
return 1;
}
int InetGSM::disconnectTCP()
{
//Status = TCPCONNECTEDCLIENT or TCPCONNECTEDSERVER.
/*
if ((getStatus()!=TCPCONNECTEDCLIENT)&&(getStatus()!=TCPCONNECTEDSERVER))
return 0;
*/
//gsm._tf.setTimeout(_GSM_CONNECTION_TOUT_);
//_cell.flush();
//Switch to AT mode.
//_cell << "+++" << endl;
//delay(200);
//Close TCP client and deact.
gsm.SimpleWriteln("AT+CIPCLOSE");
//If remote server close connection AT+QICLOSE generate ERROR
/*if(gsm._tf.find("OK"))
{
if(getStatus()==TCPCONNECTEDCLIENT)
gsm.setStatus(ATTACHED);
else
gsm.setStatus(TCPSERVERWAIT);
return 1;
}
gsm.setStatus(ERROR);
return 0; */
if(gsm.getStatus()==gsm.TCPCONNECTEDCLIENT)
gsm.setStatus(gsm.ATTACHED);
else
gsm.setStatus(gsm.TCPSERVERWAIT);
return 1;
}
int InetGSM::connectTCPServer(int port)
{
/*
if (getStatus()!=ATTACHED)
return 0;
*/
//gsm._tf.setTimeout(_GSM_CONNECTION_TOUT_);
//_cell.flush();
// Set port
gsm.SimpleWrite("AT+CIPSERVER=1,");
gsm.SimpleWriteln(port);
/*
switch(gsm.WaitResp(5000, 50, "OK")){
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
switch(gsm.WaitResp(5000, 50, "SERVER")){ //Try SERVER OK
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
*/
//delay(200);
return 1;
}
boolean InetGSM::connectedClient()
{
/*
if (getStatus()!=TCPSERVERWAIT)
return 0;
*/
gsm.SimpleWriteln("AT+CIPSTATUS");
// Alternative: AT+QISTAT, although it may be necessary to call an AT
// command every second,which is not wise
/*
switch(gsm.WaitResp(1000, 200, "OK")){
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}*/
//gsm._tf.setTimeout(1);
if(gsm.WaitResp(5000, 50, "CONNECT OK")!=RX_FINISHED_STR_RECV) {
gsm.setStatus(gsm.TCPCONNECTEDSERVER);
return true;
} else
return false;
}