4242#define HTTP_READ " AT+HTTPREAD\r\n "
4343#define HTTP_CLOSE " AT+HTTPTERM\r\n "
4444#define HTTP_CONTENT " AT+HTTPPARA=\" CONTENT\" ,\" application/json\"\r\n "
45+ #define HTTPS_ENABLE " AT+HTTPSSL=1\r\n "
46+ #define HTTPS_DISABLE " AT+HTTPSSL=0\r\n "
4547#define NORMAL_MODE " AT+CFUN=1,1\r\n "
4648#define REGISTRATION_STATUS " AT+CREG?\r\n "
4749#define SIGNAL_QUALITY " AT+CSQ\r\n "
5052#define OK " OK\r\n "
5153#define DOWNLOAD " DOWNLOAD"
5254#define HTTP_200 " ,200,"
55+ #define HTTPS_PREFIX " https://"
5356#define CONNECTED " +CREG: 0,1"
5457#define BEARER_OPEN " +SAPBR: 1,1"
5558
56-
5759Result HTTP::configureBearer (const char *apn){
5860
5961 Result result = SUCCESS;
@@ -62,7 +64,7 @@ Result HTTP::configureBearer(const char *apn){
6264 unsigned int MAX_ATTEMPTS = 10 ;
6365
6466 sendATTest ();
65-
67+
6668 while (sendCmdAndWaitForResp (REGISTRATION_STATUS, CONNECTED, 2000 ) != TRUE && attempts < MAX_ATTEMPTS){
6769 sendCmdAndWaitForResp (SIGNAL_QUALITY, CONNECTED, 1000 );
6870 attempts ++;
@@ -75,12 +77,12 @@ Result HTTP::configureBearer(const char *apn){
7577
7678 if (sendCmdAndWaitForResp (BEARER_PROFILE_GPRS, OK, 2000 ) == FALSE )
7779 result = ERROR_BEARER_PROFILE_GPRS;
78-
80+
7981 char httpApn[64 ];
8082 sprintf (httpApn, BEARER_PROFILE_APN, apn);
8183 if (sendCmdAndWaitForResp (httpApn, OK, 2000 ) == FALSE )
8284 result = ERROR_BEARER_PROFILE_APN;
83-
85+
8486 return result;
8587}
8688
@@ -143,7 +145,7 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
143145Result HTTP::get (const char *uri, char *response) {
144146
145147 Result result = setHTTPSession (uri);
146-
148+
147149 if (sendCmdAndWaitForResp (HTTP_GET, HTTP_200, 2000 ) == TRUE ) {
148150 sendCmd (HTTP_READ);
149151 result = SUCCESS;
@@ -176,15 +178,20 @@ Result HTTP::setHTTPSession(const char *uri){
176178 if (sendCmdAndWaitForResp (httpPara, OK, 2000 ) == FALSE )
177179 result = ERROR_HTTP_PARA;
178180
181+ bool https = strncmp (HTTPS_PREFIX, uri, strlen (HTTPS_PREFIX)) == 0 ;
182+ if (sendCmdAndWaitForResp (https ? HTTPS_ENABLE : HTTPS_DISABLE, OK, 2000 ) == FALSE ) {
183+ result = https ? ERROR_HTTPS_ENABLE : ERROR_HTTPS_DISABLE;
184+ }
185+
179186 if (sendCmdAndWaitForResp (HTTP_CONTENT, OK, 2000 ) == FALSE )
180187 result = ERROR_HTTP_CONTENT;
181188
182189 return result;
183190}
184191
185192void HTTP::readResponse (char *response){
186-
187- char buffer[128 ];
193+
194+ char buffer[128 ];
188195 cleanBuffer (buffer, sizeof (buffer));
189196 cleanBuffer (response, sizeof (response));
190197
@@ -214,7 +221,7 @@ void HTTP::parseJSONResponse(const char *buffer, unsigned int bufferSize, char *
214221 }
215222 --j;
216223 }
217-
224+
218225 for (int k = 0 ; k < (end_index - start_index) + 2 ; ++k){
219226 response[k] = buffer[start_index + k];
220227 response[k + 1 ] = ' \0 ' ;
0 commit comments