2828#include " Http.h"
2929#include < string.h>
3030
31- const char *BEARER_PROFILE_GPRS = " AT+SAPBR=3,1,\" Contype\" ,\" GPRS\"\n " ;
32- const char *BEARER_PROFILE_APN = " AT+SAPBR=3,1,\" APN\" ,\" %s\"\n " ;
33- const char *OPEN_GPRS_CONTEXT = " AT+SAPBR=1,1\n " ;
34- const char *CLOSE_GPRS_CONTEXT = " AT+SAPBR=0,1\n " ;
35- const char *HTTP_INIT = " AT+HTTPINIT\n " ;
36- const char *HTTP_CID = " AT+HTTPPARA=\" CID\" ,1\n " ;
37- const char *HTTP_PARA = " AT+HTTPPARA=\" URL\" ,\" %s\"\n " ;
38- const char *HTTP_GET = " AT+HTTPACTION=0\n " ;
39- const char *HTTP_POST = " AT+HTTPACTION=1\n " ;
40- const char *HTTP_DATA = " AT+HTTPDATA=%d,%d\n " ;
41- const char *HTTP_READ = " AT+HTTPREAD\n " ;
42- const char *HTTP_CLOSE = " AT+HTTPTERM\n " ;
43- const char *HTTP_CONTENT = " AT+HTTPPARA=\" CONTENT\" ,\" application/json\"\n " ;
44- const char *OK = " OK" ;
45- const char *DOWNLOAD = " DOWNLOAD" ;
46- const char *HTTP_200 = " ,200," ;
31+ #define BEARER_PROFILE_GPRS " AT+SAPBR=3,1,\" Contype\" ,\" GPRS\"\r\n "
32+ #define BEARER_PROFILE_APN " AT+SAPBR=3,1,\" APN\" ,\" %s\"\r\n "
33+ #define OPEN_GPRS_CONTEXT " AT+SAPBR=1,1\r\n "
34+ #define CLOSE_GPRS_CONTEXT " AT+SAPBR=0,1\r\n "
35+ #define HTTP_INIT " AT+HTTPINIT\r\n "
36+ #define HTTP_CID " AT+HTTPPARA=\" CID\" ,1\r\n "
37+ #define HTTP_PARA " AT+HTTPPARA=\" URL\" ,\" %s\"\r\n "
38+ #define HTTP_GET " AT+HTTPACTION=0\r\n "
39+ #define HTTP_POST " AT+HTTPACTION=1\n "
40+ #define HTTP_DATA " AT+HTTPDATA=%d,%d\r\n "
41+ #define HTTP_READ " AT+HTTPREAD\r\n "
42+ #define HTTP_CLOSE " AT+HTTPTERM\r\n "
43+ #define HTTP_CONTENT " AT+HTTPPARA=\" CONTENT\" ,\" application/json\"\r\n "
44+ #define NORMAL_MODE " AT+CFUN=1,1\r\n "
45+ #define REGISTRATION_STATUS " AT+CREG?\r\n "
46+
47+ #define OK " OK\r\n "
48+ #define DOWNLOAD " DOWNLOAD"
49+ #define HTTP_200 " ,200,"
50+ #define CONNECTED " +CREG: 0,1"
51+
4752
4853Result HTTP::configureBearer (const char *apn){
4954
5055 Result result = SUCCESS;
5156
52- if (preInit () == FALSE ){
53- result = ERROR_INITIALIZATION;
54- }
55- else {
56- if (sendCmdAndWaitForResp (BEARER_PROFILE_GPRS, OK, 2000 ) == FALSE )
57- result = ERROR_BEARER_PROFILE_GPRS;
58-
59- char httpApn[128 ];
60- sprintf (httpApn, BEARER_PROFILE_APN, apn);
61- if (sendCmdAndWaitForResp (httpApn, OK, 2000 ) == FALSE )
62- result = ERROR_BEARER_PROFILE_APN;
57+ int attempts = 0 ;
58+ int MAX_ATTEMPTS = 10 ;
59+ while (sendCmdAndWaitForResp (REGISTRATION_STATUS, CONNECTED, 5000 ) != TRUE && attempts < MAX_ATTEMPTS){
60+ attempts ++;
61+ if (attempts == MAX_ATTEMPTS) {
62+ attempts = 0 ;
63+ preInit ();
64+ }
6365 }
66+
67+ if (sendCmdAndWaitForResp (BEARER_PROFILE_GPRS, OK, 5000 ) == FALSE )
68+ result = ERROR_BEARER_PROFILE_GPRS;
69+
70+ char httpApn[128 ];
71+ sprintf (httpApn, BEARER_PROFILE_APN, apn);
72+ if (sendCmdAndWaitForResp (httpApn, OK, 5000 ) == FALSE )
73+ result = ERROR_BEARER_PROFILE_APN;
74+
6475 return result;
6576}
6677
6778Result HTTP::connect () {
6879
6980 Result result = SUCCESS;
70- if (sendCmdAndWaitForResp (OPEN_GPRS_CONTEXT, OK, 2000 ) == FALSE )
81+
82+ if (sendCmdAndWaitForResp (OPEN_GPRS_CONTEXT, OK, 5000 ) == FALSE )
7183 result = ERROR_OPEN_GPRS_CONTEXT;
72- if (sendCmdAndWaitForResp (HTTP_INIT, OK, 2000 ) == FALSE )
84+
85+ if (sendCmdAndWaitForResp (HTTP_INIT, OK, 5000 ) == FALSE )
7386 result = ERROR_HTTP_INIT;
7487
7588 return result;
@@ -78,10 +91,11 @@ Result HTTP::connect() {
7891Result HTTP::disconnect () {
7992
8093 Result result = SUCCESS;
81- if (sendCmdAndWaitForResp (HTTP_CLOSE, OK, 2000 ) == FALSE )
82- result = ERROR_HTTP_CLOSE;
94+
8395 if (sendCmdAndWaitForResp (CLOSE_GPRS_CONTEXT, OK, 2000 ) == FALSE )
8496 result = ERROR_CLOSE_GPRS_CONTEXT;
97+ if (sendCmdAndWaitForResp (HTTP_CLOSE, OK, 2000 ) == FALSE )
98+ result = ERROR_HTTP_CLOSE;
8599
86100 return result;
87101}
@@ -90,9 +104,6 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
90104
91105 Result result = setHTTPSession (uri);
92106
93- if (sendCmdAndWaitForResp (HTTP_CONTENT, OK, 5000 ) == FALSE )
94- result = ERROR_HTTP_CONTENT;
95-
96107 char httpData[128 ];
97108 int delayToDownload = 5000 ;
98109 sprintf (httpData, HTTP_DATA, strlen (body), delayToDownload);
@@ -101,6 +112,7 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
101112 }
102113
103114 sendCmd (body);
115+ purgeSerial ();
104116 delay (delayToDownload);
105117
106118 if (sendCmdAndWaitForResp (HTTP_POST, HTTP_200, 5000 ) == TRUE ) {
@@ -111,6 +123,8 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
111123 else {
112124 result = ERROR_HTTP_POST;
113125 }
126+
127+ return result;
114128}
115129
116130Result HTTP::get (const char *uri, char *response) {
@@ -141,6 +155,9 @@ Result HTTP::setHTTPSession(const char *uri){
141155 if (sendCmdAndWaitForResp (httpPara, OK, 2000 ) == FALSE )
142156 result = ERROR_HTTP_PARA;
143157
158+ if (sendCmdAndWaitForResp (HTTP_CONTENT, OK, 2000 ) == FALSE )
159+ result = ERROR_HTTP_CONTENT;
160+
144161 return result;
145162}
146163
0 commit comments