this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Mon Jul 15 21:37:22 2019 +0000
Branch:
PassingRegression
Revision:
127:a21788227ca6
Parent:
126:9bc33f8b57d5
Child:
128:3a641aaad2d9
started coding of access token acquisition:; 1. make request with credentials - DONE; 2. get response - DONE; 3. extract Id and refresh tokens from response - DONE; 4. integrate with code - pending; 5. test - pending

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 103:7b566b522427 1 #include "debug.h"
ocomeni 78:07bb86e3ce14 2 #include "WiFiManager.h"
ocomeni 78:07bb86e3ce14 3 #include "common_config.h"
ocomeni 103:7b566b522427 4 #define FILE_CODE "wifi"
ocomeni 108:3c8fb2c6e7bf 5 #define USE_EVENTS_FOR_HTTPS_REQUESTS
ocomeni 78:07bb86e3ce14 6
ocomeni 116:2296cf274661 7 WiFiManager::WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi,
ocomeni 116:2296cf274661 8 internet_config_t *internet_config,
ocomeni 98:65c2333a38b6 9 events::EventQueue &event_queue,
ocomeni 80:e8f0e92e3ac9 10 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
ocomeni 80:e8f0e92e3ac9 11 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 81:637a87eb8170 12 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
ocomeni 81:637a87eb8170 13 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue,
ocomeni 87:99b37d26ff2a 14 MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool,
ocomeni 87:99b37d26ff2a 15 Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue,
ocomeni 87:99b37d26ff2a 16 MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool,
ocomeni 87:99b37d26ff2a 17 Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue)
ocomeni 78:07bb86e3ce14 18 :
ocomeni 81:637a87eb8170 19 wifi_config(wifi_config),
ocomeni 81:637a87eb8170 20 network(wifi),
ocomeni 116:2296cf274661 21 internet_config(internet_config),
ocomeni 98:65c2333a38b6 22 _event_queue(event_queue),
ocomeni 81:637a87eb8170 23 _aT2WiFimPool(aT2WiFimPool),
ocomeni 81:637a87eb8170 24 _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
ocomeni 81:637a87eb8170 25
ocomeni 81:637a87eb8170 26 _wiFi2ATmPool(wiFi2ATmPool),
ocomeni 81:637a87eb8170 27 _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
ocomeni 81:637a87eb8170 28
ocomeni 81:637a87eb8170 29 _aT2WiFiDatamPool(aT2WiFiDatamPool),
ocomeni 81:637a87eb8170 30 _aT2WiFiDataQueue(aT2WiFiDataQueue),
ocomeni 81:637a87eb8170 31
ocomeni 81:637a87eb8170 32 _wiFi2ATDatamPool(wiFi2ATDatamPool),
ocomeni 81:637a87eb8170 33 _wiFi2ATDataQueue(wiFi2ATDataQueue)
ocomeni 78:07bb86e3ce14 34
ocomeni 78:07bb86e3ce14 35 {
ocomeni 104:11e9605093c9 36 lastScanCount = 0;
ocomeni 104:11e9605093c9 37 wifiCmd = WIFI_CMD_NONE;
ocomeni 116:2296cf274661 38 //internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
ocomeni 104:11e9605093c9 39 is_connected = false;
ocomeni 104:11e9605093c9 40 http_response = NULL;
ocomeni 104:11e9605093c9 41 chunkNum = 0;
ocomeni 104:11e9605093c9 42 socket = NULL;
ocomeni 118:8df0e9c2ee3f 43 responseString = NULL;
ocomeni 118:8df0e9c2ee3f 44 responseBytes = NULL;
ocomeni 118:8df0e9c2ee3f 45 at_data_resp = NULL;
ocomeni 104:11e9605093c9 46 https_connection_active = false;
ocomeni 113:888e262ff0a9 47 use_full_hostname = false;
ocomeni 104:11e9605093c9 48 wifiBusy = 0;
ocomeni 104:11e9605093c9 49 wifiWatchdogTimer.start();
ocomeni 104:11e9605093c9 50 watchdogCnt = 0;
ocomeni 109:c274780ff609 51 //_event_queue.call_every(10000, this, &WiFiManager::callWifiWatchDog);
ocomeni 123:a49e9ffbaca6 52 //keep_alive_id = _event_queue.call_every(CLOUD_KEEP_ALIVE_INTERVAL, this, &WiFiManager::callInternetKeepAlive);
ocomeni 123:a49e9ffbaca6 53 keep_alive_id = 0;
ocomeni 127:a21788227ca6 54 https_request_succeeded = false;
ocomeni 122:62166886db5f 55 //watchDogTick.attach(callback(this, &WiFiManager::callWifiWatchDogIsr), 10.0); // call flip function every 10 seconds
ocomeni 78:07bb86e3ce14 56 }
ocomeni 78:07bb86e3ce14 57
ocomeni 78:07bb86e3ce14 58 WiFiManager::~WiFiManager()
ocomeni 78:07bb86e3ce14 59 {
ocomeni 114:b11bb96c09f3 60 delete network;
ocomeni 124:eae4512b131b 61 wifiWatchdogTimer.stop();
ocomeni 124:eae4512b131b 62 socket->close();
ocomeni 124:eae4512b131b 63 delete socket;
ocomeni 127:a21788227ca6 64 free(aws_id_token);
ocomeni 127:a21788227ca6 65 free(aws_refresh_token);
ocomeni 78:07bb86e3ce14 66 }
ocomeni 105:e5ce023eee93 67 //#define DISABLE_WATCHDOG
ocomeni 109:c274780ff609 68
ocomeni 109:c274780ff609 69 void WiFiManager::callWifiWatchDogIsr()
ocomeni 109:c274780ff609 70 {
ocomeni 118:8df0e9c2ee3f 71 _event_queue.call_in(10, this, &WiFiManager::callWifiWatchDog);
ocomeni 109:c274780ff609 72 }
ocomeni 104:11e9605093c9 73 void WiFiManager::callWifiWatchDog()
ocomeni 104:11e9605093c9 74 {
ocomeni 105:e5ce023eee93 75 #ifdef DISABLE_WATCHDOG
ocomeni 105:e5ce023eee93 76 return;
ocomeni 105:e5ce023eee93 77 #else
ocomeni 104:11e9605093c9 78 static int inactivity_monitor = 0;
ocomeni 104:11e9605093c9 79 watchdogCnt++;
ocomeni 118:8df0e9c2ee3f 80 if(watchdogCnt >= 6 && outputBuffersAvailable()) // every minute
ocomeni 104:11e9605093c9 81 {
ocomeni 118:8df0e9c2ee3f 82 char * respStr = (char *) malloc(120);
ocomeni 107:f1a83fd41b17 83 sprintf(responseString, "\r\n[WiFi-MAN] WiFi Manager Alive : state = %d busy = %d httpsConnActive = %d\r\n",
ocomeni 107:f1a83fd41b17 84 wifiCmd, wifiBusy, https_connection_active);
ocomeni 118:8df0e9c2ee3f 85 sendThreadATresponseString(respStr, WIFI_WATCH_DOG);
ocomeni 104:11e9605093c9 86 watchdogCnt = 0;
ocomeni 104:11e9605093c9 87 }
ocomeni 112:a0999ea4ece0 88 else if(wifiWatchdogTimer.read() > 30 && responseString==NULL)
ocomeni 104:11e9605093c9 89 {
ocomeni 104:11e9605093c9 90 if(wifiCmd == WIFI_CMD_NONE)
ocomeni 104:11e9605093c9 91 inactivity_monitor++;
ocomeni 118:8df0e9c2ee3f 92 char * respStr = (char *) malloc(120);
ocomeni 104:11e9605093c9 93 sprintf(responseString, "\r\n[WiFi-MAN] Main Loop InActive : state = %d busy = %d httpsConnActive = %d\r\n",
ocomeni 104:11e9605093c9 94 wifiCmd, wifiBusy, https_connection_active);
ocomeni 118:8df0e9c2ee3f 95 sendThreadATresponseString(respStr, WIFI_WATCH_DOG);
ocomeni 104:11e9605093c9 96 if(inactivity_monitor >= 3)
ocomeni 104:11e9605093c9 97 {
ocomeni 104:11e9605093c9 98 free_DataMsg();
ocomeni 104:11e9605093c9 99 inactivity_monitor = 0;
ocomeni 104:11e9605093c9 100 }
ocomeni 104:11e9605093c9 101 }
ocomeni 105:e5ce023eee93 102 #endif
ocomeni 104:11e9605093c9 103 }
ocomeni 118:8df0e9c2ee3f 104
ocomeni 119:8d939a902333 105 void WiFiManager::callInternetKeepAlive()
ocomeni 119:8d939a902333 106 {
ocomeni 119:8d939a902333 107 if(https_connection_active)
ocomeni 119:8d939a902333 108 {
ocomeni 119:8d939a902333 109 setNextCommand(WIFI_CMD_INTERNET_KEEP_ALIVE);
ocomeni 119:8d939a902333 110 }
ocomeni 122:62166886db5f 111 else
ocomeni 122:62166886db5f 112 {
ocomeni 122:62166886db5f 113 setNextCommand(WIFI_CMD_TLS_CONNECT);
ocomeni 122:62166886db5f 114 }
ocomeni 119:8d939a902333 115 }
ocomeni 119:8d939a902333 116
ocomeni 119:8d939a902333 117
ocomeni 119:8d939a902333 118 void WiFiManager::keepSocketAlive()
ocomeni 119:8d939a902333 119 {
ocomeni 119:8d939a902333 120 // Send data
ocomeni 122:62166886db5f 121 nsapi_size_or_error_t error;
ocomeni 119:8d939a902333 122 //serr = socket->send("GET /nudgebox/v1 HTTP/1.0\r\nHost: https://dev2.dnanudge.io\r\n\r\n", 18);
ocomeni 127:a21788227ca6 123 dbg_printf(LOG, "\n[WIFI MAN] KEEP ALIVE SERVER REQUEST\r\n");
ocomeni 122:62166886db5f 124 error = socket->send(HELLO_MSG, sizeof(HELLO_MSG));
ocomeni 119:8d939a902333 125
ocomeni 122:62166886db5f 126 if(error < 0)
ocomeni 119:8d939a902333 127 {
ocomeni 119:8d939a902333 128 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 122:62166886db5f 129 https_connection_active = false;
ocomeni 122:62166886db5f 130 socket->close();
ocomeni 122:62166886db5f 131 delete socket;
ocomeni 123:a49e9ffbaca6 132 socket = NULL;
ocomeni 122:62166886db5f 133 return;
ocomeni 119:8d939a902333 134 }
ocomeni 119:8d939a902333 135
ocomeni 119:8d939a902333 136 // Receive data
ocomeni 119:8d939a902333 137 char buf[500];
ocomeni 119:8d939a902333 138 error = socket->recv(buf, 500);
ocomeni 127:a21788227ca6 139 if(error >= 0 && strstr(buf, "HTTP/1.1 200")!= NULL) // received HTTP 200 OK status
ocomeni 119:8d939a902333 140 {
ocomeni 119:8d939a902333 141 dbg_printf(LOG, "\n[WIFI MAN] KEEP ALIVE SERVER RESPONSE: \r\n %s\r\n", buf);
ocomeni 119:8d939a902333 142 queueATresponse(AT_SOCKET_KEEP_ALIVE_OK);
ocomeni 122:62166886db5f 143 https_connection_active = true;
ocomeni 119:8d939a902333 144 }
ocomeni 119:8d939a902333 145 else
ocomeni 119:8d939a902333 146 {
ocomeni 119:8d939a902333 147 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 122:62166886db5f 148 https_connection_active = false;
ocomeni 119:8d939a902333 149 }
ocomeni 119:8d939a902333 150 }
ocomeni 119:8d939a902333 151
ocomeni 127:a21788227ca6 152 void WiFiManager::CreateTokenHttpsRequest(char * httpsReq)
ocomeni 127:a21788227ca6 153 {
ocomeni 127:a21788227ca6 154
ocomeni 127:a21788227ca6 155 sprintf(httpsReq, "%s\r\n\r\n{\r\n\"AuthParameters\" : {\r\n"
ocomeni 127:a21788227ca6 156 "\"USERNAME\" : \"%s\",\r\n\"PASSWORD\" : \"%s\"\r\n},\r\n"
ocomeni 127:a21788227ca6 157 "\"AuthFlow\" : \"%s\",\r\n"
ocomeni 127:a21788227ca6 158 "\"ClientId\" : \"%s\"\r\n}", TOKEN_REQ_HDR, UserName, Password, AuthFlow, ClientId);
ocomeni 127:a21788227ca6 159 dbg_printf(LOG, "\n[WIFI MAN] Cloud Access Token REQUEST:\r\n%s\r\n", httpsReq);
ocomeni 127:a21788227ca6 160 }
ocomeni 127:a21788227ca6 161
ocomeni 127:a21788227ca6 162 void WiFiManager::GetCloudAccessToken()
ocomeni 127:a21788227ca6 163 {
ocomeni 127:a21788227ca6 164 // Send data
ocomeni 127:a21788227ca6 165 nsapi_size_or_error_t error;
ocomeni 127:a21788227ca6 166 dbg_printf(LOG, "\n[WIFI MAN] Get Cloud Access Token REQUEST\r\n");
ocomeni 127:a21788227ca6 167 char *httpsReq = new char[500];
ocomeni 127:a21788227ca6 168 httpsReq[0] = NULL;
ocomeni 127:a21788227ca6 169 CreateTokenHttpsRequest(httpsReq);
ocomeni 127:a21788227ca6 170 if(strlen(httpsReq) == 0)
ocomeni 127:a21788227ca6 171 {
ocomeni 127:a21788227ca6 172 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 127:a21788227ca6 173 return;
ocomeni 127:a21788227ca6 174 }
ocomeni 127:a21788227ca6 175 error = socket->send(httpsReq, strlen(httpsReq)+1);
ocomeni 127:a21788227ca6 176
ocomeni 127:a21788227ca6 177 if(error < 0)
ocomeni 127:a21788227ca6 178 {
ocomeni 127:a21788227ca6 179 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 127:a21788227ca6 180 return;
ocomeni 127:a21788227ca6 181 }
ocomeni 127:a21788227ca6 182
ocomeni 127:a21788227ca6 183 // Receive data
ocomeni 127:a21788227ca6 184 // reserve large buffer from heap to hold response
ocomeni 127:a21788227ca6 185 char *respBuf = new char[TOKEN_RESPONSE_SIZE];
ocomeni 127:a21788227ca6 186 error = socket->recv(respBuf, TOKEN_RESPONSE_SIZE);
ocomeni 127:a21788227ca6 187 if(error >= 0 && strstr(respBuf, "HTTP/1.1 200")!= NULL) // received HTTP 200 OK status
ocomeni 127:a21788227ca6 188 {
ocomeni 127:a21788227ca6 189 dbg_printf(LOG, "\n[WIFI MAN] ACCESS TOKEN RESPONSE: \r\n %s\r\n", respBuf);
ocomeni 127:a21788227ca6 190 // reserve memomry on the heap for tokens
ocomeni 127:a21788227ca6 191 aws_id_token = new char[ID_TOKEN_SIZE];
ocomeni 127:a21788227ca6 192 aws_refresh_token = new char[REFRESH_TOKEN_SIZE];
ocomeni 127:a21788227ca6 193 // extract ID TOKEN
ocomeni 127:a21788227ca6 194 char *sp1 = strstr(respBuf,ID_TOKEN_STR_START);
ocomeni 127:a21788227ca6 195 char *sp2 = strstr(respBuf,ACCESS_TOKEN_STR_END);
ocomeni 127:a21788227ca6 196 int tokenLen = sp2-sp1;
ocomeni 127:a21788227ca6 197 strncpy(aws_id_token, sp1, tokenLen);
ocomeni 127:a21788227ca6 198 dbg_printf(LOG, "\n[WIFI MAN] IdToken: \r\n %s\r\n", aws_id_token);
ocomeni 127:a21788227ca6 199 // extract Refresh TOKEN
ocomeni 127:a21788227ca6 200 sp1 = strstr(respBuf,REFRESH_TOKEN_STR_START);
ocomeni 127:a21788227ca6 201 sp2 = strstr(respBuf,ACCESS_TOKEN_STR_END);
ocomeni 127:a21788227ca6 202 tokenLen = sp2-sp1;
ocomeni 127:a21788227ca6 203 strncpy(aws_refresh_token, sp1, tokenLen);
ocomeni 127:a21788227ca6 204 dbg_printf(LOG, "\n[WIFI MAN] RefreshToken: \r\n %s\r\n", aws_refresh_token);
ocomeni 127:a21788227ca6 205 queueATresponse(AT_SOCKET_KEEP_ALIVE_OK);
ocomeni 127:a21788227ca6 206 https_connection_active = true;
ocomeni 127:a21788227ca6 207 }
ocomeni 127:a21788227ca6 208 else
ocomeni 127:a21788227ca6 209 {
ocomeni 127:a21788227ca6 210 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 127:a21788227ca6 211 https_connection_active = false;
ocomeni 127:a21788227ca6 212 }
ocomeni 127:a21788227ca6 213 delete respBuf;
ocomeni 127:a21788227ca6 214 }
ocomeni 127:a21788227ca6 215
ocomeni 119:8d939a902333 216
ocomeni 118:8df0e9c2ee3f 217 void WiFiManager::sendThreadATresponseString(const char * buf, at_cmd_resp_t at_cmd)
ocomeni 118:8df0e9c2ee3f 218 {
ocomeni 118:8df0e9c2ee3f 219 if(at_data_resp != NULL) return;
ocomeni 118:8df0e9c2ee3f 220 int strLen = strlen(buf) + 1;
ocomeni 118:8df0e9c2ee3f 221 at_data_resp = new at_data_msg_t;
ocomeni 118:8df0e9c2ee3f 222 // set string length
ocomeni 118:8df0e9c2ee3f 223 at_data_resp->dataLen = strLen;
ocomeni 118:8df0e9c2ee3f 224 memcpy(at_data_resp->buffer, buf, strLen);
ocomeni 118:8df0e9c2ee3f 225 // package and send on wifi data queue
ocomeni 118:8df0e9c2ee3f 226 at_data_resp->at_resp = at_cmd;
ocomeni 118:8df0e9c2ee3f 227 bool queueResult = true;
ocomeni 118:8df0e9c2ee3f 228 int wait_count = 0;
ocomeni 118:8df0e9c2ee3f 229 do
ocomeni 118:8df0e9c2ee3f 230 {
ocomeni 118:8df0e9c2ee3f 231 if(!queueResult){
ocomeni 118:8df0e9c2ee3f 232 wait_count+=10;
ocomeni 119:8d939a902333 233 dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count);
ocomeni 118:8df0e9c2ee3f 234 wait_ms(10);
ocomeni 118:8df0e9c2ee3f 235 }
ocomeni 118:8df0e9c2ee3f 236 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 118:8df0e9c2ee3f 237 }while(queueResult == false && wait_count<QUEUE_WAIT_TIMEOUT_MS);
ocomeni 118:8df0e9c2ee3f 238 delete at_data_resp;
ocomeni 118:8df0e9c2ee3f 239 at_data_resp = NULL;
ocomeni 118:8df0e9c2ee3f 240 }
ocomeni 118:8df0e9c2ee3f 241
ocomeni 118:8df0e9c2ee3f 242
ocomeni 118:8df0e9c2ee3f 243 bool WiFiManager::outputBuffersAvailable()
ocomeni 118:8df0e9c2ee3f 244 {
ocomeni 118:8df0e9c2ee3f 245 int timeout = 0;
ocomeni 118:8df0e9c2ee3f 246 while(timeout < 100)
ocomeni 118:8df0e9c2ee3f 247 {
ocomeni 118:8df0e9c2ee3f 248 if(responseBytes==NULL && responseString==NULL && at_data_resp==NULL)
ocomeni 118:8df0e9c2ee3f 249 {
ocomeni 118:8df0e9c2ee3f 250 return true;
ocomeni 118:8df0e9c2ee3f 251 }
ocomeni 118:8df0e9c2ee3f 252 else
ocomeni 118:8df0e9c2ee3f 253 {
ocomeni 118:8df0e9c2ee3f 254 timeout += 10;
ocomeni 118:8df0e9c2ee3f 255 wait_ms(10);
ocomeni 118:8df0e9c2ee3f 256 }
ocomeni 118:8df0e9c2ee3f 257 }
ocomeni 118:8df0e9c2ee3f 258 if(responseBytes==NULL && responseString==NULL && at_data_resp==NULL)
ocomeni 118:8df0e9c2ee3f 259 {
ocomeni 118:8df0e9c2ee3f 260 return true;
ocomeni 118:8df0e9c2ee3f 261 }
ocomeni 118:8df0e9c2ee3f 262 else
ocomeni 118:8df0e9c2ee3f 263 {
ocomeni 118:8df0e9c2ee3f 264 return false;
ocomeni 118:8df0e9c2ee3f 265 }
ocomeni 118:8df0e9c2ee3f 266 }
ocomeni 118:8df0e9c2ee3f 267
ocomeni 118:8df0e9c2ee3f 268
ocomeni 81:637a87eb8170 269 bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
ocomeni 92:ec9550034276 270 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 271 at_resp_message_t *atResp = _wiFi2ATmPool->alloc();
ocomeni 92:ec9550034276 272 #else
ocomeni 92:ec9550034276 273 at_resp_message_t *atResp = (at_resp_message_t *) malloc(sizeof(at_resp_message_t));
ocomeni 92:ec9550034276 274 #endif
ocomeni 88:7ffa053be662 275 if(atResp == NULL) return false; // queue full;
ocomeni 81:637a87eb8170 276 atResp->at_resp = resp;
ocomeni 81:637a87eb8170 277 _wiFi2ATCmdQueue->put(atResp);
ocomeni 81:637a87eb8170 278 return true;
ocomeni 81:637a87eb8170 279 }
ocomeni 81:637a87eb8170 280
ocomeni 81:637a87eb8170 281
ocomeni 81:637a87eb8170 282 bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){
ocomeni 81:637a87eb8170 283 at_data_msg_t *atData = _wiFi2ATDatamPool->alloc();
ocomeni 88:7ffa053be662 284 if(atData == NULL) return false; // queue full;
ocomeni 81:637a87eb8170 285 atData->at_resp = at_resp.at_resp;
ocomeni 81:637a87eb8170 286 atData->dataLen = at_resp.dataLen;
ocomeni 81:637a87eb8170 287 memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen);
ocomeni 81:637a87eb8170 288 _wiFi2ATDataQueue->put(atData);
ocomeni 103:7b566b522427 289 dbg_printf(LOG, "[WIFI MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp);
ocomeni 81:637a87eb8170 290 return true;
ocomeni 81:637a87eb8170 291 }
ocomeni 81:637a87eb8170 292
ocomeni 114:b11bb96c09f3 293 void WiFiManager::getWiFiInstance()
ocomeni 114:b11bb96c09f3 294 {
ocomeni 114:b11bb96c09f3 295 network = WiFiInterface::get_default_instance();
ocomeni 114:b11bb96c09f3 296 if (!network) {
ocomeni 114:b11bb96c09f3 297 dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
ocomeni 114:b11bb96c09f3 298 }
ocomeni 114:b11bb96c09f3 299 }
ocomeni 79:a2187bbfa407 300
ocomeni 79:a2187bbfa407 301 void WiFiManager::runMain(){
ocomeni 81:637a87eb8170 302 nsapi_error_t error;
ocomeni 99:05398b3184f8 303 bool result;
ocomeni 103:7b566b522427 304 dbg_printf(LOG, "\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
ocomeni 79:a2187bbfa407 305 while(true){
ocomeni 79:a2187bbfa407 306 dequeueWiFiCommands();
ocomeni 81:637a87eb8170 307 dequeueATdataResponse();
ocomeni 104:11e9605093c9 308 wifiWatchdogTimer.reset();
ocomeni 79:a2187bbfa407 309 switch(wifiCmd){
ocomeni 79:a2187bbfa407 310 case WIFI_CMD_NONE:
ocomeni 79:a2187bbfa407 311 // IDLE STATE
ocomeni 79:a2187bbfa407 312 break;
ocomeni 79:a2187bbfa407 313 case WIFI_CMD_SCAN:
ocomeni 104:11e9605093c9 314 wifiBusy = 1;
ocomeni 79:a2187bbfa407 315 error = scanNetworks();
ocomeni 79:a2187bbfa407 316 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 317 queueATresponse(AT_SCAN_RESP);
ocomeni 104:11e9605093c9 318 wifiBusy = 0;
ocomeni 81:637a87eb8170 319 break;
ocomeni 81:637a87eb8170 320 case WIFI_CMD_DETAILED_SCAN:
ocomeni 98:65c2333a38b6 321 {
ocomeni 104:11e9605093c9 322 wifiBusy = 1;
ocomeni 81:637a87eb8170 323 nsapi_size_or_error_t cnt_err;
ocomeni 81:637a87eb8170 324 cnt_err = getAvailableAPs(lastScanCount);
ocomeni 81:637a87eb8170 325 wifiCmd = WIFI_CMD_NONE;
ocomeni 122:62166886db5f 326 if(cnt_err >= 0)
ocomeni 122:62166886db5f 327 {
ocomeni 122:62166886db5f 328 queueATresponse(AT_DETAILED_SCAN_RESP);
ocomeni 122:62166886db5f 329 }
ocomeni 104:11e9605093c9 330 wifiBusy = 0;
ocomeni 79:a2187bbfa407 331 break;
ocomeni 98:65c2333a38b6 332 }
ocomeni 79:a2187bbfa407 333 case WIFI_CMD_CONNECT:
ocomeni 98:65c2333a38b6 334 {
ocomeni 124:eae4512b131b 335 if(is_connected) // already connected
ocomeni 124:eae4512b131b 336 {
ocomeni 124:eae4512b131b 337 wifiCmd = WIFI_CMD_NONE;
ocomeni 124:eae4512b131b 338 break;
ocomeni 124:eae4512b131b 339 }
ocomeni 104:11e9605093c9 340 wifiBusy = 1;
ocomeni 81:637a87eb8170 341 error = connect();
ocomeni 93:06e755a80187 342 int secCount = 0;
ocomeni 118:8df0e9c2ee3f 343 while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS && is_connected==false){
ocomeni 93:06e755a80187 344 wait(1); // wait 1 sec
ocomeni 93:06e755a80187 345 }
ocomeni 126:9bc33f8b57d5 346 wifiCmd = WIFI_CMD_NONE;
ocomeni 93:06e755a80187 347 if(is_connected==false){
ocomeni 118:8df0e9c2ee3f 348 if(outputBuffersAvailable() == false) // first free it
ocomeni 118:8df0e9c2ee3f 349 {
ocomeni 118:8df0e9c2ee3f 350 free(responseString);
ocomeni 118:8df0e9c2ee3f 351 }
ocomeni 103:7b566b522427 352 dbg_printf(LOG, "[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n");
ocomeni 95:290859010c8c 353 //queueATresponse(AT_COMMAND_FAILED);
ocomeni 95:290859010c8c 354 responseString = (char *) malloc(100);
ocomeni 95:290859010c8c 355 sprintf(responseString, "\r\n+UUTIMEOUT\r\n");
ocomeni 95:290859010c8c 356 sendATresponseString(AT_COMMAND_FAILED);
ocomeni 93:06e755a80187 357 }
ocomeni 93:06e755a80187 358 else {
ocomeni 95:290859010c8c 359 sendATresponseString(AT_CONNECT_RESP);
ocomeni 93:06e755a80187 360 }
ocomeni 104:11e9605093c9 361 wifiBusy = 0;
ocomeni 79:a2187bbfa407 362 break;
ocomeni 98:65c2333a38b6 363 }
ocomeni 79:a2187bbfa407 364 case WIFI_CMD_DISCONNECT:
ocomeni 124:eae4512b131b 365 {
ocomeni 124:eae4512b131b 366 if(!is_connected) // already disconnected
ocomeni 124:eae4512b131b 367 {
ocomeni 124:eae4512b131b 368 wifiCmd = WIFI_CMD_NONE;
ocomeni 124:eae4512b131b 369 break;
ocomeni 124:eae4512b131b 370 }
ocomeni 104:11e9605093c9 371 wifiBusy = 1;
ocomeni 81:637a87eb8170 372 error = disconnect();
ocomeni 126:9bc33f8b57d5 373 wifiCmd = WIFI_CMD_NONE;
ocomeni 124:eae4512b131b 374 if(error >= 0)
ocomeni 124:eae4512b131b 375 {
ocomeni 124:eae4512b131b 376 int secCount = 0;
ocomeni 124:eae4512b131b 377 while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS && is_connected==true){
ocomeni 124:eae4512b131b 378 wait(1); // wait 1 sec
ocomeni 124:eae4512b131b 379 }
ocomeni 124:eae4512b131b 380 if(!is_connected)
ocomeni 124:eae4512b131b 381 {
ocomeni 124:eae4512b131b 382 sendATresponseString(AT_DISCONNECT_RESP);
ocomeni 124:eae4512b131b 383 }
ocomeni 124:eae4512b131b 384 else
ocomeni 124:eae4512b131b 385 {
ocomeni 124:eae4512b131b 386 dbg_printf(LOG, "[WIFI MAN] +++ WIFI DISCONNECTION TIMEOUT +++ \r\n");
ocomeni 124:eae4512b131b 387 //queueATresponse(AT_COMMAND_FAILED);
ocomeni 124:eae4512b131b 388 responseString = (char *) malloc(100);
ocomeni 124:eae4512b131b 389 sprintf(responseString, "\r\n+UUTIMEOUT\r\n");
ocomeni 124:eae4512b131b 390 sendATresponseString(AT_COMMAND_FAILED);
ocomeni 124:eae4512b131b 391 }
ocomeni 124:eae4512b131b 392 // attempt reconnection if always connected scheme is set
ocomeni 124:eae4512b131b 393 if(internet_config->connectionScheme == ALWAYS_CONNECTED)
ocomeni 124:eae4512b131b 394 {
ocomeni 124:eae4512b131b 395 setNextCommand(WIFI_CMD_CONNECT);
ocomeni 124:eae4512b131b 396 }
ocomeni 124:eae4512b131b 397 }
ocomeni 104:11e9605093c9 398 wifiBusy = 0;
ocomeni 81:637a87eb8170 399 break;
ocomeni 124:eae4512b131b 400 }
ocomeni 81:637a87eb8170 401 case WIFI_CMD_CONFIG:
ocomeni 104:11e9605093c9 402 wifiBusy = 1;
ocomeni 81:637a87eb8170 403 set_WIFI_CONFIG();
ocomeni 81:637a87eb8170 404 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 405 queueATresponse(AT_CONFIG_RESP);
ocomeni 104:11e9605093c9 406 wifiBusy = 0;
ocomeni 82:10072c1794d3 407 break;
ocomeni 81:637a87eb8170 408 case WIFI_CMD_INTERNET_CONFIG:
ocomeni 107:f1a83fd41b17 409 {
ocomeni 104:11e9605093c9 410 wifiBusy = 1;
ocomeni 107:f1a83fd41b17 411 backgroundTaskCompleted = false;
ocomeni 100:80ef4bc31b7a 412 set_internet_config();
ocomeni 111:3ab1d9644835 413 // Wait for callback semaphore
ocomeni 111:3ab1d9644835 414 #ifdef DNANUDGE_DEBUG
ocomeni 111:3ab1d9644835 415 callback_semaphore.wait();
ocomeni 111:3ab1d9644835 416 #endif
ocomeni 107:f1a83fd41b17 417 int msecCount = 0;
ocomeni 126:9bc33f8b57d5 418 while(!backgroundTaskCompleted && msecCount < 1000)
ocomeni 107:f1a83fd41b17 419 {
ocomeni 107:f1a83fd41b17 420 msecCount++;
ocomeni 107:f1a83fd41b17 421 wait_ms(10);
ocomeni 107:f1a83fd41b17 422 }
ocomeni 107:f1a83fd41b17 423 if(backgroundTaskCompleted)
ocomeni 107:f1a83fd41b17 424 {
ocomeni 107:f1a83fd41b17 425 queueATresponse(AT_INTERNET_CONFIG_RESP);
ocomeni 107:f1a83fd41b17 426 }
ocomeni 107:f1a83fd41b17 427 backgroundTaskCompleted = false;
ocomeni 81:637a87eb8170 428 wifiCmd = WIFI_CMD_NONE;
ocomeni 95:290859010c8c 429 break;
ocomeni 107:f1a83fd41b17 430 }
ocomeni 95:290859010c8c 431 case WIFI_CMD_NETWORK_STATUS:
ocomeni 104:11e9605093c9 432 wifiBusy = 1;
ocomeni 118:8df0e9c2ee3f 433 if(outputBuffersAvailable())
ocomeni 118:8df0e9c2ee3f 434 {
ocomeni 118:8df0e9c2ee3f 435 getNetworkStatus();
ocomeni 118:8df0e9c2ee3f 436 }
ocomeni 95:290859010c8c 437 sendATresponseString(AT_NETWORK_STATUS_RESP);
ocomeni 95:290859010c8c 438 wifiCmd = WIFI_CMD_NONE;
ocomeni 104:11e9605093c9 439 wifiBusy = 0;
ocomeni 95:290859010c8c 440 break;
ocomeni 95:290859010c8c 441 case WIFI_CMD_WIFI_STATUS:
ocomeni 104:11e9605093c9 442 wifiBusy = 1;
ocomeni 118:8df0e9c2ee3f 443 if(outputBuffersAvailable())
ocomeni 118:8df0e9c2ee3f 444 {
ocomeni 118:8df0e9c2ee3f 445 getWiFiStatus();
ocomeni 118:8df0e9c2ee3f 446 }
ocomeni 95:290859010c8c 447 sendATresponseString(AT_WIFI_STATUS_RESP);
ocomeni 95:290859010c8c 448 wifiCmd = WIFI_CMD_NONE;
ocomeni 104:11e9605093c9 449 wifiBusy = 0;
ocomeni 79:a2187bbfa407 450 break;
ocomeni 79:a2187bbfa407 451 case WIFI_CMD_SEND_HTTPS_REQ:
ocomeni 107:f1a83fd41b17 452 {
ocomeni 119:8d939a902333 453 // cancel keep alive event as not needed since new request has come in.
ocomeni 123:a49e9ffbaca6 454 if(keep_alive_id != 0) // only cancel if it has been activated
ocomeni 123:a49e9ffbaca6 455 {
ocomeni 123:a49e9ffbaca6 456 _event_queue.cancel(keep_alive_id);
ocomeni 123:a49e9ffbaca6 457 }
ocomeni 104:11e9605093c9 458 wifiBusy = 1;
ocomeni 109:c274780ff609 459 #ifdef SEND_DEBUG_MESSAGES
ocomeni 118:8df0e9c2ee3f 460 if(outputBuffersAvailable())
ocomeni 104:11e9605093c9 461 {
ocomeni 104:11e9605093c9 462 responseString = (char *) malloc(100);
ocomeni 104:11e9605093c9 463 sprintf(responseString, "\r\nHTTP REQUEST RECEIVED\r\n");
ocomeni 104:11e9605093c9 464 sendATresponseString(AT_EVENT);
ocomeni 104:11e9605093c9 465 }
ocomeni 109:c274780ff609 466 #endif
ocomeni 103:7b566b522427 467 dbg_printf(LOG, "before call to send http request \n");
ocomeni 104:11e9605093c9 468 dbg_printf(LOG, "\r\n[WIFI-MAN] Received HTTPS request...\r\n");
ocomeni 88:7ffa053be662 469 print_memory_info();
ocomeni 105:e5ce023eee93 470 //network->attach(NULL);
ocomeni 107:f1a83fd41b17 471 #ifdef USE_EVENTS_FOR_HTTPS_REQUESTS
ocomeni 107:f1a83fd41b17 472
ocomeni 107:f1a83fd41b17 473 // Events can be cancelled as long as they have not been dispatched. If the
ocomeni 107:f1a83fd41b17 474 // event has already expired, cancel has no side-effects.
ocomeni 107:f1a83fd41b17 475 int event_id = _event_queue.call(this, &WiFiManager::createSendHttpsRequest);
ocomeni 107:f1a83fd41b17 476 backgroundTaskCompleted = false;
ocomeni 107:f1a83fd41b17 477 int msecCount = 0;
ocomeni 109:c274780ff609 478 int oldChunkNum = chunkNum;
ocomeni 109:c274780ff609 479 while(!backgroundTaskCompleted && msecCount < 6000)
ocomeni 107:f1a83fd41b17 480 {
ocomeni 118:8df0e9c2ee3f 481 msecCount+=10;
ocomeni 107:f1a83fd41b17 482 wait_ms(10);
ocomeni 109:c274780ff609 483 if(oldChunkNum != chunkNum) // new payload received
ocomeni 109:c274780ff609 484 {
ocomeni 109:c274780ff609 485 oldChunkNum = chunkNum;
ocomeni 109:c274780ff609 486 msecCount = 0;
ocomeni 109:c274780ff609 487 }
ocomeni 107:f1a83fd41b17 488 }
ocomeni 107:f1a83fd41b17 489 if(backgroundTaskCompleted)
ocomeni 107:f1a83fd41b17 490 {
ocomeni 109:c274780ff609 491 //queueATresponse(AT_INTERNET_CONFIG_RESP);
ocomeni 107:f1a83fd41b17 492 result = true;
ocomeni 107:f1a83fd41b17 493 }
ocomeni 107:f1a83fd41b17 494 else
ocomeni 107:f1a83fd41b17 495 {
ocomeni 109:c274780ff609 496 //_event_queue.cancel(event_id);
ocomeni 107:f1a83fd41b17 497 result = false;
ocomeni 107:f1a83fd41b17 498 }
ocomeni 107:f1a83fd41b17 499 backgroundTaskCompleted = false;
ocomeni 107:f1a83fd41b17 500 #else
ocomeni 99:05398b3184f8 501 result = createHttpsRequest();
ocomeni 107:f1a83fd41b17 502 #endif
ocomeni 118:8df0e9c2ee3f 503 if(result == false && outputBuffersAvailable())
ocomeni 98:65c2333a38b6 504 {
ocomeni 104:11e9605093c9 505 responseString = (char *) malloc(100);
ocomeni 104:11e9605093c9 506 if(http_result==TLS_CONNECTION_FAILED)
ocomeni 104:11e9605093c9 507 {
ocomeni 104:11e9605093c9 508 sprintf(responseString, "\r\nTLS CONNECTION FAILURE\r\n");
ocomeni 104:11e9605093c9 509 }
ocomeni 104:11e9605093c9 510 else
ocomeni 104:11e9605093c9 511 {
ocomeni 104:11e9605093c9 512 sprintf(responseString, "\r\nHTTP REQUEST FAILED\r\n");
ocomeni 104:11e9605093c9 513 }
ocomeni 98:65c2333a38b6 514 sendATresponseString(AT_COMMAND_FAILED);
ocomeni 98:65c2333a38b6 515 }
ocomeni 127:a21788227ca6 516 if(https_request_succeeded == false)
ocomeni 127:a21788227ca6 517 {
ocomeni 127:a21788227ca6 518 https_request_succeeded = result;
ocomeni 127:a21788227ca6 519 }
ocomeni 127:a21788227ca6 520
ocomeni 103:7b566b522427 521 dbg_printf(LOG, "after call to send http request \n");
ocomeni 88:7ffa053be662 522 print_memory_info();
ocomeni 84:7c7add00f4bf 523 wifiCmd = WIFI_CMD_NONE;
ocomeni 104:11e9605093c9 524 wifiBusy = 0;
ocomeni 119:8d939a902333 525 // enable keep alive after https request completes
ocomeni 119:8d939a902333 526 keep_alive_id = _event_queue.call_every(CLOUD_KEEP_ALIVE_INTERVAL, this, &WiFiManager::callInternetKeepAlive);
ocomeni 105:e5ce023eee93 527 //network->attach(callback(this, &WiFiManager::status_callback));
ocomeni 79:a2187bbfa407 528 break;
ocomeni 122:62166886db5f 529 }
ocomeni 122:62166886db5f 530 case WIFI_CMD_TLS_CONNECT:
ocomeni 122:62166886db5f 531 {
ocomeni 122:62166886db5f 532 char* hostName = strstr(internet_config->url,"//");
ocomeni 122:62166886db5f 533 wifiBusy = 1;
ocomeni 122:62166886db5f 534 if(hostName != NULL)
ocomeni 122:62166886db5f 535 {
ocomeni 122:62166886db5f 536 hostName += 2;
ocomeni 122:62166886db5f 537 https_connection_active = createTLSconnection(hostName);
ocomeni 122:62166886db5f 538 if(https_connection_active == false)
ocomeni 122:62166886db5f 539 {
ocomeni 122:62166886db5f 540 queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
ocomeni 123:a49e9ffbaca6 541 delete socket;
ocomeni 123:a49e9ffbaca6 542 socket = NULL;
ocomeni 122:62166886db5f 543 }
ocomeni 122:62166886db5f 544 }
ocomeni 122:62166886db5f 545 wifiCmd = WIFI_CMD_NONE;
ocomeni 122:62166886db5f 546 wifiBusy = 0;
ocomeni 122:62166886db5f 547 break;
ocomeni 107:f1a83fd41b17 548 }
ocomeni 119:8d939a902333 549 case WIFI_CMD_INTERNET_KEEP_ALIVE:
ocomeni 119:8d939a902333 550 wifiBusy = 1;
ocomeni 119:8d939a902333 551 keepSocketAlive();
ocomeni 119:8d939a902333 552 wifiCmd = WIFI_CMD_NONE;
ocomeni 119:8d939a902333 553 wifiBusy = 0;
ocomeni 119:8d939a902333 554 break;
ocomeni 123:a49e9ffbaca6 555 case WIFI_CMD_WIFI_MAC_ADDR:
ocomeni 123:a49e9ffbaca6 556 wifiBusy = 1;
ocomeni 123:a49e9ffbaca6 557 if(outputBuffersAvailable())
ocomeni 123:a49e9ffbaca6 558 {
ocomeni 123:a49e9ffbaca6 559 getWiFiMACaddress();
ocomeni 123:a49e9ffbaca6 560 sendATresponseString(AT_WIFI_MAC_RESP);
ocomeni 123:a49e9ffbaca6 561 }
ocomeni 123:a49e9ffbaca6 562 wifiCmd = WIFI_CMD_NONE;
ocomeni 123:a49e9ffbaca6 563 wifiBusy = 0;
ocomeni 123:a49e9ffbaca6 564 break;
ocomeni 79:a2187bbfa407 565 case WIFI_CMD_SEND_HTTP_REQ:
ocomeni 79:a2187bbfa407 566 break;
ocomeni 79:a2187bbfa407 567 default:
ocomeni 79:a2187bbfa407 568 break;
ocomeni 79:a2187bbfa407 569 }
ocomeni 105:e5ce023eee93 570 wait_ms(WIFI_MAIN_LOOP_WAIT_TIME_MS); //
ocomeni 79:a2187bbfa407 571 }
ocomeni 79:a2187bbfa407 572
ocomeni 78:07bb86e3ce14 573 }
ocomeni 79:a2187bbfa407 574
ocomeni 107:f1a83fd41b17 575 void WiFiManager::createSendHttpsRequest()
ocomeni 107:f1a83fd41b17 576 {
ocomeni 107:f1a83fd41b17 577 backgroundTaskCompleted = createHttpsRequest();
ocomeni 107:f1a83fd41b17 578 }
ocomeni 91:d6b6319ad681 579
ocomeni 95:290859010c8c 580 void WiFiManager::sendATresponseString(at_cmd_resp_t at_cmd)
ocomeni 91:d6b6319ad681 581 {
ocomeni 95:290859010c8c 582 int strLen = strlen(responseString) + 1;
ocomeni 91:d6b6319ad681 583 at_data_resp = new at_data_msg_t;
ocomeni 91:d6b6319ad681 584 // set string length
ocomeni 91:d6b6319ad681 585 at_data_resp->dataLen = strLen;
ocomeni 91:d6b6319ad681 586 memcpy(at_data_resp->buffer, responseString, strLen);
ocomeni 95:290859010c8c 587 free(responseString);
ocomeni 99:05398b3184f8 588 responseString = NULL;
ocomeni 91:d6b6319ad681 589 // package and send on wifi data queue
ocomeni 91:d6b6319ad681 590 at_data_resp->at_resp = at_cmd;
ocomeni 91:d6b6319ad681 591 bool queueResult = true;
ocomeni 91:d6b6319ad681 592 int wait_count = 0;
ocomeni 91:d6b6319ad681 593 do
ocomeni 91:d6b6319ad681 594 {
ocomeni 91:d6b6319ad681 595 if(!queueResult){
ocomeni 110:c722dda4f2ff 596 wait_count+=10;
ocomeni 119:8d939a902333 597 dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count);
ocomeni 91:d6b6319ad681 598 wait_ms(10);
ocomeni 91:d6b6319ad681 599 }
ocomeni 91:d6b6319ad681 600 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 110:c722dda4f2ff 601 }while(queueResult == false && wait_count<QUEUE_WAIT_TIMEOUT_MS);
ocomeni 91:d6b6319ad681 602 delete at_data_resp;
ocomeni 99:05398b3184f8 603 at_data_resp = NULL;
ocomeni 91:d6b6319ad681 604 }
ocomeni 91:d6b6319ad681 605
ocomeni 99:05398b3184f8 606
ocomeni 99:05398b3184f8 607 void WiFiManager::sendATresponseBytes(at_cmd_resp_t at_cmd, int len)
ocomeni 99:05398b3184f8 608 {
ocomeni 99:05398b3184f8 609 at_data_resp = new at_data_msg_t;
ocomeni 99:05398b3184f8 610 // set string length
ocomeni 99:05398b3184f8 611 at_data_resp->dataLen = len;
ocomeni 99:05398b3184f8 612 memcpy(at_data_resp->buffer, responseBytes, len);
ocomeni 107:f1a83fd41b17 613 delete responseBytes;
ocomeni 99:05398b3184f8 614 responseBytes = NULL;
ocomeni 99:05398b3184f8 615 // package and send on wifi data queue
ocomeni 99:05398b3184f8 616 at_data_resp->at_resp = at_cmd;
ocomeni 99:05398b3184f8 617 bool queueResult = true;
ocomeni 99:05398b3184f8 618 int wait_count = 0;
ocomeni 99:05398b3184f8 619 do
ocomeni 99:05398b3184f8 620 {
ocomeni 99:05398b3184f8 621 if(!queueResult){
ocomeni 110:c722dda4f2ff 622 wait_count+=10;
ocomeni 99:05398b3184f8 623 wait_ms(10);
ocomeni 119:8d939a902333 624 dbg_printf(LOG, "ATCMD Queue full waited %d ms so far...\n", wait_count);
ocomeni 99:05398b3184f8 625 }
ocomeni 99:05398b3184f8 626 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 110:c722dda4f2ff 627 }while(queueResult == false && wait_count<QUEUE_WAIT_TIMEOUT_MS);
ocomeni 99:05398b3184f8 628 delete at_data_resp;
ocomeni 99:05398b3184f8 629 at_data_resp = NULL;
ocomeni 103:7b566b522427 630 dbg_printf(LOG, "[WIFI-MAN] sendATresponseBytes completed successfully\r\n");
ocomeni 99:05398b3184f8 631 }
ocomeni 99:05398b3184f8 632
ocomeni 99:05398b3184f8 633
ocomeni 99:05398b3184f8 634
ocomeni 79:a2187bbfa407 635 bool WiFiManager::dequeueWiFiCommands(){
ocomeni 104:11e9605093c9 636 if(wifiCmd != WIFI_CMD_NONE || wifiBusy!=0) return false; // busy
ocomeni 81:637a87eb8170 637 osEvent evt = _aT2WiFiCmdQueue->get(0);
ocomeni 79:a2187bbfa407 638 if(evt.status == osEventMessage){
ocomeni 79:a2187bbfa407 639 wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
ocomeni 79:a2187bbfa407 640 setNextCommand(cmd->wifi_cmd);
ocomeni 92:ec9550034276 641 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 79:a2187bbfa407 642 _aT2WiFimPool->free(cmd);
ocomeni 96:f5ed273881af 643 cmd = NULL;
ocomeni 92:ec9550034276 644 #else
ocomeni 92:ec9550034276 645 free(cmd);
ocomeni 96:f5ed273881af 646 cmd = NULL;
ocomeni 92:ec9550034276 647 #endif
ocomeni 79:a2187bbfa407 648 }
ocomeni 79:a2187bbfa407 649 return true;
ocomeni 79:a2187bbfa407 650 }
ocomeni 79:a2187bbfa407 651
ocomeni 79:a2187bbfa407 652
ocomeni 81:637a87eb8170 653 bool WiFiManager::dequeueATdataResponse(){
ocomeni 104:11e9605093c9 654 if(wifiCmd != WIFI_CMD_NONE || wifiBusy!=0) return false; // busy
ocomeni 81:637a87eb8170 655 osEvent evt = _aT2WiFiDataQueue->get(0);
ocomeni 81:637a87eb8170 656 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 657 data_msg = (wifi_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 658 setNextCommand(data_msg->wifi_cmd);
ocomeni 81:637a87eb8170 659 //_wiFi2ATDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 660 }
ocomeni 81:637a87eb8170 661 return true;
ocomeni 81:637a87eb8170 662 }
ocomeni 81:637a87eb8170 663
ocomeni 81:637a87eb8170 664
ocomeni 79:a2187bbfa407 665 bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
ocomeni 78:07bb86e3ce14 666 {
ocomeni 103:7b566b522427 667 dbg_printf(LOG, "\n [WIFI-MAN] About to set next WiFi manager command to %d\n", cmd);
ocomeni 79:a2187bbfa407 668 if(wifiCmd == WIFI_CMD_NONE){
ocomeni 79:a2187bbfa407 669 wifiCmd = cmd;
ocomeni 79:a2187bbfa407 670 return true; // success
ocomeni 79:a2187bbfa407 671 }
ocomeni 103:7b566b522427 672 dbg_printf(LOG, "\n [WIFI-MAN] Busy : current state = %d \n", wifiCmd);
ocomeni 79:a2187bbfa407 673 return false; // wiFiManager busy
ocomeni 78:07bb86e3ce14 674 }
ocomeni 79:a2187bbfa407 675
ocomeni 81:637a87eb8170 676 const char * WiFiManager::sec2str(nsapi_security_t sec)
ocomeni 81:637a87eb8170 677 {
ocomeni 81:637a87eb8170 678 switch (sec) {
ocomeni 81:637a87eb8170 679 case NSAPI_SECURITY_NONE:
ocomeni 81:637a87eb8170 680 return "None";
ocomeni 81:637a87eb8170 681 case NSAPI_SECURITY_WEP:
ocomeni 81:637a87eb8170 682 return "WEP";
ocomeni 81:637a87eb8170 683 case NSAPI_SECURITY_WPA:
ocomeni 81:637a87eb8170 684 return "WPA";
ocomeni 81:637a87eb8170 685 case NSAPI_SECURITY_WPA2:
ocomeni 81:637a87eb8170 686 return "WPA2";
ocomeni 81:637a87eb8170 687 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 81:637a87eb8170 688 return "WPA/WPA2";
ocomeni 81:637a87eb8170 689 case NSAPI_SECURITY_UNKNOWN:
ocomeni 81:637a87eb8170 690 default:
ocomeni 81:637a87eb8170 691 return "Unknown";
ocomeni 81:637a87eb8170 692 }
ocomeni 81:637a87eb8170 693 }
ocomeni 81:637a87eb8170 694
ocomeni 79:a2187bbfa407 695
ocomeni 79:a2187bbfa407 696 nsapi_size_or_error_t WiFiManager::scanNetworks()
ocomeni 79:a2187bbfa407 697 {
ocomeni 114:b11bb96c09f3 698 getWiFiInstance();
ocomeni 114:b11bb96c09f3 699 if(network == NULL)
ocomeni 114:b11bb96c09f3 700 {
ocomeni 114:b11bb96c09f3 701 dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
ocomeni 114:b11bb96c09f3 702 return 0;
ocomeni 114:b11bb96c09f3 703 }
ocomeni 79:a2187bbfa407 704 nsapi_error_t error;
ocomeni 103:7b566b522427 705 dbg_printf(LOG, "\n [WIFI-MAN] About to start scan for WiFi networks\n");
ocomeni 79:a2187bbfa407 706 lastScanCount = network->scan(NULL, 0);
ocomeni 103:7b566b522427 707 dbg_printf(LOG, "\n [WIFI-MAN] Scan for WiFi networks completed - \n");
ocomeni 79:a2187bbfa407 708 return lastScanCount;
ocomeni 79:a2187bbfa407 709 }
ocomeni 79:a2187bbfa407 710
ocomeni 79:a2187bbfa407 711
ocomeni 81:637a87eb8170 712 //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res,
ocomeni 81:637a87eb8170 713 // nsapi_size_t ncount)
ocomeni 81:637a87eb8170 714 nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
ocomeni 79:a2187bbfa407 715 {
ocomeni 114:b11bb96c09f3 716 getWiFiInstance();
ocomeni 114:b11bb96c09f3 717 if(network == NULL)
ocomeni 114:b11bb96c09f3 718 {
ocomeni 114:b11bb96c09f3 719 dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
ocomeni 114:b11bb96c09f3 720 return 0;
ocomeni 114:b11bb96c09f3 721 }
ocomeni 81:637a87eb8170 722 WiFiAccessPoint *ap;
ocomeni 81:637a87eb8170 723 nsapi_size_or_error_t count;
ocomeni 81:637a87eb8170 724 count = ncount;
ocomeni 81:637a87eb8170 725 if (count <= 0) {
ocomeni 103:7b566b522427 726 dbg_printf(LOG, "[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 98:65c2333a38b6 727 return 0;
ocomeni 81:637a87eb8170 728 }
ocomeni 81:637a87eb8170 729 /* Limit number of network arbitrary to 15 */
ocomeni 81:637a87eb8170 730 count = count < 15 ? count : 15;
ocomeni 81:637a87eb8170 731 ap = new WiFiAccessPoint[count];
ocomeni 81:637a87eb8170 732 count = network->scan(ap, count);
ocomeni 81:637a87eb8170 733 if (count <= 0) {
ocomeni 103:7b566b522427 734 dbg_printf(LOG, "[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 98:65c2333a38b6 735 return 0;
ocomeni 81:637a87eb8170 736 }
ocomeni 81:637a87eb8170 737
ocomeni 81:637a87eb8170 738 for (int i = 0; i < count; i++) {
ocomeni 103:7b566b522427 739 dbg_printf(LOG, "[WIFI-MAN]: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
ocomeni 81:637a87eb8170 740 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ocomeni 81:637a87eb8170 741 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
ocomeni 81:637a87eb8170 742 }
ocomeni 103:7b566b522427 743 dbg_printf(LOG, "[WIFI-MAN] %d networks available.\n", count);
ocomeni 81:637a87eb8170 744
ocomeni 81:637a87eb8170 745 delete[] ap;
ocomeni 79:a2187bbfa407 746 return count;
ocomeni 79:a2187bbfa407 747 }
ocomeni 79:a2187bbfa407 748
ocomeni 79:a2187bbfa407 749
ocomeni 81:637a87eb8170 750 void WiFiManager::set_WIFI_CONFIG()
ocomeni 81:637a87eb8170 751 {
ocomeni 81:637a87eb8170 752 wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer;
ocomeni 82:10072c1794d3 753 if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid);
ocomeni 82:10072c1794d3 754 if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass);
ocomeni 82:10072c1794d3 755 if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security);
ocomeni 81:637a87eb8170 756 free_DataMsg();
ocomeni 81:637a87eb8170 757 }
ocomeni 81:637a87eb8170 758
ocomeni 78:07bb86e3ce14 759 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
ocomeni 78:07bb86e3ce14 760 {
ocomeni 116:2296cf274661 761 strcpy(wifi_config->ssid, wifi_ssid);
ocomeni 116:2296cf274661 762 dbg_printf(LOG, "[WIFI-MAN] wifi_ssid set to %s\n", wifi_config->ssid);
ocomeni 88:7ffa053be662 763 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 764 delete socket;
ocomeni 78:07bb86e3ce14 765 }
ocomeni 79:a2187bbfa407 766
ocomeni 79:a2187bbfa407 767
ocomeni 78:07bb86e3ce14 768 void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
ocomeni 78:07bb86e3ce14 769 {
ocomeni 116:2296cf274661 770 strcpy(wifi_config->pass, wifi_pass);
ocomeni 103:7b566b522427 771 dbg_printf(LOG, "[WIFI-MAN] wifi_pass set to %s\n", "****************");
ocomeni 88:7ffa053be662 772 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 773 delete socket;
ocomeni 78:07bb86e3ce14 774 }
ocomeni 79:a2187bbfa407 775
ocomeni 79:a2187bbfa407 776
ocomeni 78:07bb86e3ce14 777 void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
ocomeni 78:07bb86e3ce14 778 {
ocomeni 116:2296cf274661 779 wifi_config->security = wifi_security;
ocomeni 116:2296cf274661 780 dbg_printf(LOG, "[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config->security));
ocomeni 88:7ffa053be662 781 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 782 delete socket;
ocomeni 78:07bb86e3ce14 783 }
ocomeni 79:a2187bbfa407 784
ocomeni 100:80ef4bc31b7a 785 void WiFiManager::gethostbyname()
ocomeni 100:80ef4bc31b7a 786 {
ocomeni 100:80ef4bc31b7a 787 nsapi_value_or_error_t value_or_error;
ocomeni 111:3ab1d9644835 788 #ifdef DNANUDGE_DEBUG
ocomeni 111:3ab1d9644835 789 SocketAddress * addr = new SocketAddress;
ocomeni 111:3ab1d9644835 790 bool res;
ocomeni 111:3ab1d9644835 791 res = addr->set_ip_address("8.8.8.8");
ocomeni 111:3ab1d9644835 792 if(res)
ocomeni 111:3ab1d9644835 793 {
ocomeni 111:3ab1d9644835 794 dbg_printf(LOG, "[WIFI-MAN] added ip address %s\r\n", addr->get_ip_address());
ocomeni 111:3ab1d9644835 795 }
ocomeni 111:3ab1d9644835 796 else
ocomeni 111:3ab1d9644835 797 {
ocomeni 111:3ab1d9644835 798 dbg_printf(LOG, "[WIFI-MAN] Error adding ip address \r\n");
ocomeni 111:3ab1d9644835 799 }
ocomeni 111:3ab1d9644835 800 network->add_dns_server(*addr);
ocomeni 111:3ab1d9644835 801 #endif
ocomeni 111:3ab1d9644835 802 char * serverAddress = new char[100];
ocomeni 116:2296cf274661 803 char *p = strstr(internet_config->url, "//");
ocomeni 113:888e262ff0a9 804 if(p != NULL && use_full_hostname == false)
ocomeni 111:3ab1d9644835 805 {
ocomeni 111:3ab1d9644835 806 strcpy(serverAddress,p+2);
ocomeni 111:3ab1d9644835 807 }
ocomeni 111:3ab1d9644835 808 else
ocomeni 111:3ab1d9644835 809 {
ocomeni 116:2296cf274661 810 strcpy(serverAddress,internet_config->url);
ocomeni 111:3ab1d9644835 811 }
ocomeni 111:3ab1d9644835 812 value_or_error = network->gethostbyname_async(serverAddress,
ocomeni 100:80ef4bc31b7a 813 callback(this, &WiFiManager::gethostbyname_callback),
ocomeni 100:80ef4bc31b7a 814 NSAPI_UNSPEC);
ocomeni 111:3ab1d9644835 815
ocomeni 100:80ef4bc31b7a 816 if(value_or_error >= NSAPI_ERROR_OK) // success
ocomeni 100:80ef4bc31b7a 817 {
ocomeni 103:7b566b522427 818 dbg_printf(LOG, "[WIFI-MAN] hostname translation successful value_or_error = %d\r\n", value_or_error);
ocomeni 100:80ef4bc31b7a 819 //strcpy(responseString, UDDRP_WRITE_OK);
ocomeni 100:80ef4bc31b7a 820 //printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
ocomeni 100:80ef4bc31b7a 821 //sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
ocomeni 100:80ef4bc31b7a 822 }
ocomeni 118:8df0e9c2ee3f 823 else if(outputBuffersAvailable()) // -ve number means error
ocomeni 100:80ef4bc31b7a 824 {
ocomeni 100:80ef4bc31b7a 825 responseString = (char *) malloc(20);
ocomeni 103:7b566b522427 826 dbg_printf(LOG, "[WIFI-MAN] hostname translation failed\r\n");
ocomeni 100:80ef4bc31b7a 827 strcpy(responseString, UDDRP_ERROR);
ocomeni 100:80ef4bc31b7a 828 sendATresponseString(AT_COMMAND_FAILED);
ocomeni 100:80ef4bc31b7a 829 }
ocomeni 81:637a87eb8170 830
ocomeni 100:80ef4bc31b7a 831 }
ocomeni 100:80ef4bc31b7a 832
ocomeni 100:80ef4bc31b7a 833 void WiFiManager::set_internet_config()
ocomeni 81:637a87eb8170 834 {
ocomeni 81:637a87eb8170 835 internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer;
ocomeni 116:2296cf274661 836 internet_config->peer_id = internet_cfg->peer_id;
ocomeni 116:2296cf274661 837 strncpy(internet_config->url,internet_cfg->url, strlen(internet_cfg->url)+1);
ocomeni 116:2296cf274661 838 internet_config->connectionScheme = internet_cfg->connectionScheme;
ocomeni 81:637a87eb8170 839 free_DataMsg();
ocomeni 103:7b566b522427 840 dbg_printf(LOG, "[WIFI MAN] Internet configuration setup completed\n");
ocomeni 116:2296cf274661 841 dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config->peer_id,
ocomeni 116:2296cf274661 842 internet_config->url,
ocomeni 116:2296cf274661 843 internet_config->connectionScheme);
ocomeni 99:05398b3184f8 844 if(https_connection_active)
ocomeni 99:05398b3184f8 845 {
ocomeni 99:05398b3184f8 846 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 99:05398b3184f8 847 socket->close(); // close socket before deleting memory
ocomeni 99:05398b3184f8 848 delete socket;
ocomeni 99:05398b3184f8 849 socket = NULL;
ocomeni 99:05398b3184f8 850 }
ocomeni 100:80ef4bc31b7a 851 _event_queue.call_in(10, this, &WiFiManager::gethostbyname);
ocomeni 81:637a87eb8170 852 }
ocomeni 81:637a87eb8170 853
ocomeni 95:290859010c8c 854
ocomeni 95:290859010c8c 855
ocomeni 95:290859010c8c 856 void WiFiManager::getNetworkStatus(){
ocomeni 95:290859010c8c 857
ocomeni 95:290859010c8c 858 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 95:290859010c8c 859 net_stat_id_t status_id;
ocomeni 95:290859010c8c 860 char * nextStrPtr = responseString;
ocomeni 95:290859010c8c 861 for(int i=0; i< NumNetworkStatus;i++){
ocomeni 95:290859010c8c 862 status_id = netStatusIds[i]; // get current status id
ocomeni 95:290859010c8c 863 switch(status_id){
ocomeni 95:290859010c8c 864 case IF_HW_ADDRESS:
ocomeni 95:290859010c8c 865 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 866 WIFI_CHANNEL,
ocomeni 95:290859010c8c 867 status_id,
ocomeni 95:290859010c8c 868 network->get_mac_address());
ocomeni 95:290859010c8c 869 break;
ocomeni 95:290859010c8c 870 case NETWORK_IF_STATUS:
ocomeni 95:290859010c8c 871 sprintf(nextStrPtr, "\r\n%s%d,%d, %d\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 872 WIFI_CHANNEL,
ocomeni 95:290859010c8c 873 status_id,
ocomeni 95:290859010c8c 874 (uint8_t)is_connected);
ocomeni 95:290859010c8c 875 break;
ocomeni 95:290859010c8c 876 case INTERFACE_TYPE:
ocomeni 95:290859010c8c 877 sprintf(nextStrPtr, "\r\n%s%d,%d,%d\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 878 WIFI_CHANNEL,
ocomeni 95:290859010c8c 879 status_id,
ocomeni 95:290859010c8c 880 WIFI_STATION);
ocomeni 95:290859010c8c 881 break;
ocomeni 95:290859010c8c 882 case IPv4_ADDRESS:
ocomeni 95:290859010c8c 883 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 884 WIFI_CHANNEL,
ocomeni 95:290859010c8c 885 status_id,
ocomeni 95:290859010c8c 886 network->get_ip_address());
ocomeni 95:290859010c8c 887 break;
ocomeni 95:290859010c8c 888 case SUBNET_MASK:
ocomeni 95:290859010c8c 889 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 890 WIFI_CHANNEL,
ocomeni 95:290859010c8c 891 status_id,
ocomeni 95:290859010c8c 892 network->get_netmask());
ocomeni 95:290859010c8c 893 break;
ocomeni 95:290859010c8c 894 case GATEWAY_ADDRESS:
ocomeni 95:290859010c8c 895 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 896 WIFI_CHANNEL,
ocomeni 95:290859010c8c 897 status_id,
ocomeni 95:290859010c8c 898 network->get_gateway());
ocomeni 95:290859010c8c 899 break;
ocomeni 95:290859010c8c 900 case PRIMARY_DNS_SERVER:
ocomeni 95:290859010c8c 901 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 902 WIFI_CHANNEL,
ocomeni 95:290859010c8c 903 status_id,
ocomeni 95:290859010c8c 904 DEFAULT_DNS_ADDRESS);
ocomeni 95:290859010c8c 905 break;
ocomeni 95:290859010c8c 906 case SECONDARY_DNS_SERVER:
ocomeni 95:290859010c8c 907 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 908 WIFI_CHANNEL,
ocomeni 95:290859010c8c 909 status_id,
ocomeni 95:290859010c8c 910 DEFAULT_DNS_ADDRESS);
ocomeni 95:290859010c8c 911 break;
ocomeni 95:290859010c8c 912 case IPv6_ADDRESS:
ocomeni 95:290859010c8c 913 sprintf(nextStrPtr, "\r\n%s%d,%d,::\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 914 WIFI_CHANNEL,
ocomeni 95:290859010c8c 915 status_id);
ocomeni 95:290859010c8c 916 break;
ocomeni 95:290859010c8c 917 default:
ocomeni 95:290859010c8c 918 sprintf(nextStrPtr, "\r\n%s,::\r\n", NETWORK_STATUS);
ocomeni 95:290859010c8c 919 break;
ocomeni 95:290859010c8c 920 }
ocomeni 103:7b566b522427 921 nextStrPtr += strlen(nextStrPtr) ; // progress to end of current string
ocomeni 95:290859010c8c 922 }
ocomeni 103:7b566b522427 923 sprintf(nextStrPtr, "%s", UDDRP_WRITE_OK);
ocomeni 95:290859010c8c 924 }
ocomeni 95:290859010c8c 925
ocomeni 123:a49e9ffbaca6 926 void WiFiManager::getWiFiMACaddress()
ocomeni 123:a49e9ffbaca6 927 {
ocomeni 124:eae4512b131b 928 char * mp = new char[20];
ocomeni 124:eae4512b131b 929 sscanf(network->get_mac_address(),"%02s:%2s:%2s:%2s:%2s:%2s",&mp[0],&mp[2],&mp[4],&mp[6],&mp[8],&mp[10]);
ocomeni 123:a49e9ffbaca6 930 responseString = (char *) malloc(100);
ocomeni 124:eae4512b131b 931
ocomeni 124:eae4512b131b 932 sprintf(responseString, "\r\n%s%d,%sOK\r\n", LOCAL_ADDRESS_RESP,
ocomeni 124:eae4512b131b 933 WIFI_IF_ID,
ocomeni 124:eae4512b131b 934 mp);
ocomeni 124:eae4512b131b 935 delete mp;
ocomeni 123:a49e9ffbaca6 936 }
ocomeni 95:290859010c8c 937
ocomeni 95:290859010c8c 938
ocomeni 95:290859010c8c 939 void WiFiManager::getWiFiStatus(){
ocomeni 95:290859010c8c 940
ocomeni 95:290859010c8c 941 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 95:290859010c8c 942 wifi_stat_id_t status_id;
ocomeni 95:290859010c8c 943 char * nextStrPtr = responseString;
ocomeni 95:290859010c8c 944 for(int i=0; i< NumWiFiStatus;i++){
ocomeni 95:290859010c8c 945 status_id = wifiStatusIds[i]; // get current status id
ocomeni 95:290859010c8c 946 switch(status_id){
ocomeni 95:290859010c8c 947 case WIFI_SSID:
ocomeni 95:290859010c8c 948 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 949 status_id,
ocomeni 116:2296cf274661 950 wifi_config->ssid);
ocomeni 95:290859010c8c 951 break;
ocomeni 95:290859010c8c 952 case WIFI_BSSID:
ocomeni 95:290859010c8c 953 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 954 status_id,
ocomeni 95:290859010c8c 955 network->get_mac_address());
ocomeni 95:290859010c8c 956 break;
ocomeni 95:290859010c8c 957 case WIFI__CURRENT_CHANNEL:
ocomeni 95:290859010c8c 958 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 959 status_id,
ocomeni 95:290859010c8c 960 DEFAULT_WIFI_CHANNEL);
ocomeni 95:290859010c8c 961 break;
ocomeni 95:290859010c8c 962 case WIFI_STA_STATUS:
ocomeni 95:290859010c8c 963 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 964 status_id,
ocomeni 95:290859010c8c 965 (uint8_t)is_connected);
ocomeni 95:290859010c8c 966 break;
ocomeni 95:290859010c8c 967 case WIFI_RSSI:
ocomeni 95:290859010c8c 968 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 969 status_id,
ocomeni 95:290859010c8c 970 network->get_rssi());
ocomeni 95:290859010c8c 971 break;
ocomeni 95:290859010c8c 972 default:
ocomeni 95:290859010c8c 973 sprintf(nextStrPtr, "\r\n%s,::\r\n", WIFI_NETWORK_STATUS);
ocomeni 95:290859010c8c 974 break;
ocomeni 95:290859010c8c 975 }
ocomeni 103:7b566b522427 976 nextStrPtr += strlen(nextStrPtr) ; // progress to end of current string
ocomeni 95:290859010c8c 977 }
ocomeni 103:7b566b522427 978 sprintf(nextStrPtr, "%s", UDDRP_WRITE_OK);
ocomeni 95:290859010c8c 979 }
ocomeni 95:290859010c8c 980
ocomeni 95:290859010c8c 981
ocomeni 81:637a87eb8170 982 void WiFiManager::free_DataMsg()
ocomeni 81:637a87eb8170 983 {
ocomeni 81:637a87eb8170 984 // free memory after processing
ocomeni 81:637a87eb8170 985 _aT2WiFiDatamPool->free(data_msg);
ocomeni 96:f5ed273881af 986 data_msg = NULL;
ocomeni 81:637a87eb8170 987 }
ocomeni 81:637a87eb8170 988
ocomeni 81:637a87eb8170 989
ocomeni 88:7ffa053be662 990
ocomeni 81:637a87eb8170 991 void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
ocomeni 81:637a87eb8170 992 {
ocomeni 105:e5ce023eee93 993 dbg_printf(LOG, "[WIFI-MAN] about to call status_callback_event... \r\n");
ocomeni 98:65c2333a38b6 994 _event_queue.call_in(50, this, &WiFiManager::status_callback_event, status, param);
ocomeni 98:65c2333a38b6 995 }
ocomeni 98:65c2333a38b6 996 void WiFiManager::status_callback_event(nsapi_event_t status, intptr_t param)
ocomeni 98:65c2333a38b6 997 {
ocomeni 81:637a87eb8170 998 switch(param) {
ocomeni 81:637a87eb8170 999 case NSAPI_STATUS_LOCAL_UP:
ocomeni 103:7b566b522427 1000 dbg_printf(LOG, "[WIFI-MAN] Local IP address set!\r\n");
ocomeni 103:7b566b522427 1001 dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 1002 break;
ocomeni 81:637a87eb8170 1003 case NSAPI_STATUS_GLOBAL_UP:
ocomeni 103:7b566b522427 1004 dbg_printf(LOG, "Global IP address set!\r\n");
ocomeni 103:7b566b522427 1005 dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 116:2296cf274661 1006 dbg_printf(LOG, "[WIFI-MAN] Connected to the network %s\n", wifi_config->ssid);
ocomeni 118:8df0e9c2ee3f 1007 if(outputBuffersAvailable())
ocomeni 118:8df0e9c2ee3f 1008 {
ocomeni 118:8df0e9c2ee3f 1009 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 118:8df0e9c2ee3f 1010 sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED,
ocomeni 118:8df0e9c2ee3f 1011 WIFI_CHANNEL,
ocomeni 118:8df0e9c2ee3f 1012 network->get_mac_address(),
ocomeni 118:8df0e9c2ee3f 1013 DEFAULT_WIFI_CHANNEL);
ocomeni 118:8df0e9c2ee3f 1014
ocomeni 124:eae4512b131b 1015 wifiBusy = 0;
ocomeni 118:8df0e9c2ee3f 1016 is_connected = true;
ocomeni 118:8df0e9c2ee3f 1017 }
ocomeni 81:637a87eb8170 1018 break;
ocomeni 81:637a87eb8170 1019 case NSAPI_STATUS_DISCONNECTED:
ocomeni 103:7b566b522427 1020 dbg_printf(LOG, "No connection to network!\r\n");
ocomeni 103:7b566b522427 1021 dbg_printf(LOG, "\n [WIFI-MAN] No connection to network!\n");
ocomeni 124:eae4512b131b 1022 if(outputBuffersAvailable())
ocomeni 81:637a87eb8170 1023 {
ocomeni 124:eae4512b131b 1024 responseString = (char *) malloc(100);
ocomeni 124:eae4512b131b 1025 sprintf(responseString, "\r\n%s%d,5\r\n", WIFI_LINK_DISABLED, WIFI_CHANNEL);
ocomeni 124:eae4512b131b 1026 //sendATresponseString(AT_EVENT);
ocomeni 81:637a87eb8170 1027 }
ocomeni 124:eae4512b131b 1028 wifiBusy = 0;
ocomeni 124:eae4512b131b 1029 is_connected = false;
ocomeni 81:637a87eb8170 1030 break;
ocomeni 81:637a87eb8170 1031 case NSAPI_STATUS_CONNECTING:
ocomeni 103:7b566b522427 1032 dbg_printf(LOG, "Connecting to network!\r\n");
ocomeni 81:637a87eb8170 1033 break;
ocomeni 81:637a87eb8170 1034 default:
ocomeni 103:7b566b522427 1035 dbg_printf(LOG, "Not supported");
ocomeni 81:637a87eb8170 1036 break;
ocomeni 81:637a87eb8170 1037 }
ocomeni 81:637a87eb8170 1038 }
ocomeni 81:637a87eb8170 1039
ocomeni 81:637a87eb8170 1040
ocomeni 81:637a87eb8170 1041
ocomeni 79:a2187bbfa407 1042
ocomeni 79:a2187bbfa407 1043 nsapi_error_t WiFiManager::connect()
ocomeni 79:a2187bbfa407 1044 {
ocomeni 114:b11bb96c09f3 1045 getWiFiInstance();
ocomeni 114:b11bb96c09f3 1046 if(network == NULL)
ocomeni 114:b11bb96c09f3 1047 {
ocomeni 114:b11bb96c09f3 1048 dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
ocomeni 114:b11bb96c09f3 1049 return 0;
ocomeni 114:b11bb96c09f3 1050 }
ocomeni 79:a2187bbfa407 1051 nsapi_error_t error;
ocomeni 103:7b566b522427 1052 dbg_printf(LOG, "\n [WIFI-MAN] About to connect to WiFi network\n");
ocomeni 81:637a87eb8170 1053 network->attach(callback(this, &WiFiManager::status_callback));
ocomeni 79:a2187bbfa407 1054 error = network->set_blocking(false);
ocomeni 79:a2187bbfa407 1055 if(error)
ocomeni 79:a2187bbfa407 1056 {
ocomeni 103:7b566b522427 1057 dbg_printf(LOG, "\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
ocomeni 79:a2187bbfa407 1058 return error;
ocomeni 79:a2187bbfa407 1059 }
ocomeni 103:7b566b522427 1060 dbg_printf(LOG, "[WIFI-MAN] Connecting to network ssid = %s passwd = %s security = %s \r\n",
ocomeni 116:2296cf274661 1061 wifi_config->ssid,
ocomeni 96:f5ed273881af 1062 "****************",
ocomeni 116:2296cf274661 1063 sec2str(wifi_config->security));
ocomeni 116:2296cf274661 1064 error = network->connect(wifi_config->ssid,
ocomeni 116:2296cf274661 1065 wifi_config->pass,
ocomeni 116:2296cf274661 1066 wifi_config->security);
ocomeni 103:7b566b522427 1067 dbg_printf(LOG, "[WIFI-MAN] network->connect called. error = %d\r\n", error);
ocomeni 81:637a87eb8170 1068 return error;
ocomeni 79:a2187bbfa407 1069 }
ocomeni 79:a2187bbfa407 1070
ocomeni 113:888e262ff0a9 1071 void WiFiManager::processGetHostByNameResult(nsapi_error_t result, SocketAddress *address)
ocomeni 99:05398b3184f8 1072 {
ocomeni 118:8df0e9c2ee3f 1073
ocomeni 103:7b566b522427 1074 dbg_printf(LOG, "gethostbyname_callback called... result = %d \r\n", result);
ocomeni 107:f1a83fd41b17 1075 print_memory_info();
ocomeni 118:8df0e9c2ee3f 1076 if(outputBuffersAvailable())
ocomeni 99:05398b3184f8 1077 {
ocomeni 118:8df0e9c2ee3f 1078 responseBytes = new uint8_t[HOSTNAME_RESPONSE_LEN]; //malloc(HOSTNAME_RESPONSE_LEN);
ocomeni 118:8df0e9c2ee3f 1079 int i = 0;
ocomeni 118:8df0e9c2ee3f 1080 responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
ocomeni 118:8df0e9c2ee3f 1081 responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
ocomeni 118:8df0e9c2ee3f 1082 if(is_connected && result>=0)
ocomeni 107:f1a83fd41b17 1083 {
ocomeni 118:8df0e9c2ee3f 1084 memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
ocomeni 118:8df0e9c2ee3f 1085 strcpy(internet_config->remote_IPv4Address, address->get_ip_address());
ocomeni 118:8df0e9c2ee3f 1086 i +=4;
ocomeni 118:8df0e9c2ee3f 1087 uint16_t port = address->get_port();
ocomeni 118:8df0e9c2ee3f 1088 internet_config->remote_port = port;
ocomeni 118:8df0e9c2ee3f 1089 memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
ocomeni 118:8df0e9c2ee3f 1090 i +=2;
ocomeni 118:8df0e9c2ee3f 1091 // local IPv4 address
ocomeni 118:8df0e9c2ee3f 1092 int ipAddr[4];
ocomeni 118:8df0e9c2ee3f 1093 strcpy(internet_config->local_IPv4Address, network->get_ip_address());
ocomeni 118:8df0e9c2ee3f 1094 sscanf(internet_config->local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1],
ocomeni 118:8df0e9c2ee3f 1095 &ipAddr[2], &ipAddr[3]);
ocomeni 118:8df0e9c2ee3f 1096 responseBytes[i++] = (uint8_t) ipAddr[0];
ocomeni 118:8df0e9c2ee3f 1097 responseBytes[i++] = (uint8_t) ipAddr[1];
ocomeni 118:8df0e9c2ee3f 1098 responseBytes[i++] = (uint8_t) ipAddr[2];
ocomeni 118:8df0e9c2ee3f 1099 responseBytes[i++] = (uint8_t) ipAddr[3];
ocomeni 118:8df0e9c2ee3f 1100 // local port number
ocomeni 118:8df0e9c2ee3f 1101 responseBytes[i++] = 0;
ocomeni 118:8df0e9c2ee3f 1102 responseBytes[i] = 0;
ocomeni 118:8df0e9c2ee3f 1103 printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
ocomeni 118:8df0e9c2ee3f 1104 sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
ocomeni 107:f1a83fd41b17 1105 }
ocomeni 118:8df0e9c2ee3f 1106 else
ocomeni 118:8df0e9c2ee3f 1107 {
ocomeni 118:8df0e9c2ee3f 1108 // if unconnected set ip and port to zeroes
ocomeni 118:8df0e9c2ee3f 1109 memset(&responseBytes[i], 0x00, 6);
ocomeni 118:8df0e9c2ee3f 1110 delete responseBytes;
ocomeni 118:8df0e9c2ee3f 1111 dbg_printf(LOG, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
ocomeni 118:8df0e9c2ee3f 1112 if(responseString == NULL)
ocomeni 118:8df0e9c2ee3f 1113 {
ocomeni 118:8df0e9c2ee3f 1114 responseString = (char *) malloc(100);
ocomeni 118:8df0e9c2ee3f 1115 sprintf(responseString, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
ocomeni 118:8df0e9c2ee3f 1116 sendATresponseString(AT_EVENT);
ocomeni 118:8df0e9c2ee3f 1117 }
ocomeni 118:8df0e9c2ee3f 1118 use_full_hostname = not use_full_hostname;
ocomeni 118:8df0e9c2ee3f 1119 }
ocomeni 118:8df0e9c2ee3f 1120 wifiBusy = 0;
ocomeni 118:8df0e9c2ee3f 1121 backgroundTaskCompleted = true;
ocomeni 107:f1a83fd41b17 1122 }
ocomeni 113:888e262ff0a9 1123
ocomeni 113:888e262ff0a9 1124 }
ocomeni 104:11e9605093c9 1125
ocomeni 113:888e262ff0a9 1126 void WiFiManager::gethostbyname_callback(nsapi_error_t res, SocketAddress *addr)
ocomeni 113:888e262ff0a9 1127 {
ocomeni 113:888e262ff0a9 1128 nsapi_error_t result = res;
ocomeni 113:888e262ff0a9 1129 SocketAddress *address = new SocketAddress;
ocomeni 113:888e262ff0a9 1130 address = addr;
ocomeni 113:888e262ff0a9 1131 _event_queue.call(this, &WiFiManager::processGetHostByNameResult,
ocomeni 113:888e262ff0a9 1132 result, address);
ocomeni 113:888e262ff0a9 1133
ocomeni 113:888e262ff0a9 1134 #ifdef DNANUDGE_DEBUG
ocomeni 113:888e262ff0a9 1135 callback_semaphore.release();
ocomeni 113:888e262ff0a9 1136 #endif
ocomeni 99:05398b3184f8 1137 }
ocomeni 99:05398b3184f8 1138
ocomeni 100:80ef4bc31b7a 1139 void WiFiManager::sendSocketConnectionEvent()
ocomeni 100:80ef4bc31b7a 1140 {
ocomeni 100:80ef4bc31b7a 1141 //
ocomeni 100:80ef4bc31b7a 1142 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 100:80ef4bc31b7a 1143 sprintf(responseString, "\r\n%s%d,%d,%d,%s,%d,%s,%d\r\n", PEER_CONNECTED_URC,
ocomeni 100:80ef4bc31b7a 1144 IP_PEER_HANDLE,
ocomeni 100:80ef4bc31b7a 1145 IPv4_CONNECTION,
ocomeni 100:80ef4bc31b7a 1146 TCP_PROTOCOL,
ocomeni 116:2296cf274661 1147 internet_config->local_IPv4Address,
ocomeni 100:80ef4bc31b7a 1148 DEFAULT_LOCAL_PORT,
ocomeni 116:2296cf274661 1149 internet_config->remote_IPv4Address,
ocomeni 116:2296cf274661 1150 internet_config->remote_port);
ocomeni 100:80ef4bc31b7a 1151 sendATresponseString(AT_EVENT);
ocomeni 100:80ef4bc31b7a 1152 }
ocomeni 100:80ef4bc31b7a 1153
ocomeni 99:05398b3184f8 1154
ocomeni 79:a2187bbfa407 1155 nsapi_error_t WiFiManager::disconnect()
ocomeni 78:07bb86e3ce14 1156 {
ocomeni 79:a2187bbfa407 1157 nsapi_error_t error;
ocomeni 79:a2187bbfa407 1158 error = network->disconnect();
ocomeni 124:eae4512b131b 1159 if(error < 0)
ocomeni 124:eae4512b131b 1160 {
ocomeni 124:eae4512b131b 1161 responseString = (char *) malloc(100);
ocomeni 124:eae4512b131b 1162 sprintf(responseString, "%s", UDDRP_ERROR);
ocomeni 124:eae4512b131b 1163 sendATresponseString(AT_EVENT);
ocomeni 124:eae4512b131b 1164 }
ocomeni 79:a2187bbfa407 1165 return error;
ocomeni 78:07bb86e3ce14 1166 }
ocomeni 79:a2187bbfa407 1167
ocomeni 98:65c2333a38b6 1168 #define MIX_HDR_AND_BODY
ocomeni 88:7ffa053be662 1169 void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
ocomeni 90:ed0267eca7b5 1170 {
ocomeni 90:ed0267eca7b5 1171
ocomeni 88:7ffa053be662 1172 at_data_resp = new at_data_msg_t;
ocomeni 88:7ffa053be662 1173 at_data_resp->at_resp = at_cmd;
ocomeni 88:7ffa053be662 1174 size_t bufSize = sizeof(at_data_resp->buffer);
ocomeni 88:7ffa053be662 1175 int pos = 0;
ocomeni 88:7ffa053be662 1176 at_data_resp->dataLen = 0;
ocomeni 88:7ffa053be662 1177 bool queueResult = true;
ocomeni 90:ed0267eca7b5 1178 int hdrLen = 0;
ocomeni 94:fb4414aff957 1179 int wait_count = 0;
ocomeni 88:7ffa053be662 1180 do {
ocomeni 94:fb4414aff957 1181 if(!queueResult){
ocomeni 94:fb4414aff957 1182 wait_count++;
ocomeni 103:7b566b522427 1183 dbg_printf(LOG, "[WIFI-MAN] ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 94:fb4414aff957 1184 wait_ms(10);
ocomeni 90:ed0267eca7b5 1185 }
ocomeni 90:ed0267eca7b5 1186 else {
ocomeni 98:65c2333a38b6 1187 if(http_response_hdr_sent == false && chunkNum==1){ // only do this for first chunk
ocomeni 102:9748f290a1a5 1188 bool status = copyResponseHdr2Queue(buf);
ocomeni 98:65c2333a38b6 1189 if(status == true){
ocomeni 103:7b566b522427 1190 dbg_printf(LOG, "[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 98:65c2333a38b6 1191 hdrLen = at_data_resp->dataLen;
ocomeni 98:65c2333a38b6 1192 http_response_hdr_sent = true;
ocomeni 98:65c2333a38b6 1193 }
ocomeni 98:65c2333a38b6 1194 else {
ocomeni 103:7b566b522427 1195 dbg_printf(LOG, "[WIFI-MAN] Http Response header copy failed\r\n");
ocomeni 98:65c2333a38b6 1196 }
ocomeni 90:ed0267eca7b5 1197 }
ocomeni 94:fb4414aff957 1198 int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ;
ocomeni 103:7b566b522427 1199 dbg_printf(LOG, "[WIFI-MAN] Http Response body [bytes = %d] \r\n",cpyLen);
ocomeni 90:ed0267eca7b5 1200 at_data_resp->dataLen += cpyLen;
ocomeni 90:ed0267eca7b5 1201 memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen);
ocomeni 103:7b566b522427 1202 dbg_printf(LOG, "[WIFI-MAN] Http Response header and body copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 90:ed0267eca7b5 1203 }
ocomeni 88:7ffa053be662 1204 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 90:ed0267eca7b5 1205 if(queueResult){
ocomeni 90:ed0267eca7b5 1206 pos+= at_data_resp->dataLen;
ocomeni 90:ed0267eca7b5 1207 at_data_resp->dataLen = 0;
ocomeni 90:ed0267eca7b5 1208 hdrLen = 0;
ocomeni 90:ed0267eca7b5 1209 }
ocomeni 88:7ffa053be662 1210 }while(queueResult == false || pos < bufLen);
ocomeni 103:7b566b522427 1211 dbg_printf(LOG, "[WIFI-MAN] response data queued - deleting data memory\r\n");
ocomeni 88:7ffa053be662 1212 delete at_data_resp;
ocomeni 100:80ef4bc31b7a 1213 at_data_resp = NULL;
ocomeni 88:7ffa053be662 1214 }
ocomeni 79:a2187bbfa407 1215
ocomeni 102:9748f290a1a5 1216 bool WiFiManager::copyResponseHdr2Queue(const uint8_t * buf)
ocomeni 90:ed0267eca7b5 1217 {
ocomeni 102:9748f290a1a5 1218 const char *arbPtr = (const char *)buf - MAX_HTTP_HDR_LEN;
ocomeni 102:9748f290a1a5 1219 const char *hdrPtr;
ocomeni 102:9748f290a1a5 1220 int len;
ocomeni 102:9748f290a1a5 1221 bool hdrFound = false;
ocomeni 102:9748f290a1a5 1222 int i;
ocomeni 102:9748f290a1a5 1223 for(i=0;i<(MAX_HTTP_HDR_LEN-50);i++)
ocomeni 101:1cfd468e5009 1224 {
ocomeni 102:9748f290a1a5 1225 // get location of start of the http header string
ocomeni 102:9748f290a1a5 1226 hdrPtr = strstr(&arbPtr[i], HTTP_HEADER_START_LINE);
ocomeni 102:9748f290a1a5 1227 len = strlen(HTTP_HEADER_START_LINE);
ocomeni 102:9748f290a1a5 1228 // validate that header string
ocomeni 102:9748f290a1a5 1229 if((strstr(&arbPtr[i+len], HTTP_HEADER_START_LINE) == NULL ||
ocomeni 102:9748f290a1a5 1230 (strstr(&arbPtr[i+len], HTTP_HEADER_START_LINE) > (const char*)buf)) && //
ocomeni 102:9748f290a1a5 1231 strstr(&arbPtr[i+len], HTTP_HEADER_CONTENT_TYPE) != NULL &&
ocomeni 102:9748f290a1a5 1232 strstr(&arbPtr[i+len], HTTP_HEADER_CONTENT_LEN) != NULL &&
ocomeni 102:9748f290a1a5 1233 hdrPtr != NULL)
ocomeni 102:9748f290a1a5 1234 {
ocomeni 102:9748f290a1a5 1235 hdrFound = true;
ocomeni 102:9748f290a1a5 1236 break;
ocomeni 102:9748f290a1a5 1237 }
ocomeni 101:1cfd468e5009 1238 }
ocomeni 102:9748f290a1a5 1239 // calculate header length
ocomeni 102:9748f290a1a5 1240 int hdrLen = (const char*) buf -hdrPtr;
ocomeni 102:9748f290a1a5 1241 // copy header
ocomeni 102:9748f290a1a5 1242 memcpy(at_data_resp->buffer, (const uint8_t *) hdrPtr, hdrLen);
ocomeni 103:7b566b522427 1243 dbg_printf(LOG, "[i = %d] This is the header\r\n%s\r\n", i, hdrPtr);
ocomeni 102:9748f290a1a5 1244 if(hdrFound == false)
ocomeni 100:80ef4bc31b7a 1245 {
ocomeni 103:7b566b522427 1246 dbg_printf(LOG, "[WIFI-MAN] copy failed: HTTP header not found!!\r\n");
ocomeni 100:80ef4bc31b7a 1247 return false;
ocomeni 100:80ef4bc31b7a 1248 }
ocomeni 102:9748f290a1a5 1249 at_data_resp->dataLen = hdrLen;
ocomeni 98:65c2333a38b6 1250 return true;
ocomeni 90:ed0267eca7b5 1251 }
ocomeni 90:ed0267eca7b5 1252
ocomeni 84:7c7add00f4bf 1253 void WiFiManager::return_response(HttpResponse* res) {
ocomeni 84:7c7add00f4bf 1254
ocomeni 88:7ffa053be662 1255 at_data_resp = new at_data_msg_t;
ocomeni 87:99b37d26ff2a 1256 int numChars = 0;
ocomeni 87:99b37d26ff2a 1257 // create message pointer for response header generation
ocomeni 88:7ffa053be662 1258 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 87:99b37d26ff2a 1259 // do status line
ocomeni 87:99b37d26ff2a 1260 numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 87:99b37d26ff2a 1261 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 1262 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 87:99b37d26ff2a 1263 numChars = sprintf(msgPtr, "%s: %s\r\n",
ocomeni 87:99b37d26ff2a 1264 res->get_headers_fields()[ix]->c_str(),
ocomeni 87:99b37d26ff2a 1265 res->get_headers_values()[ix]->c_str());
ocomeni 87:99b37d26ff2a 1266 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 1267 }
ocomeni 87:99b37d26ff2a 1268 numChars = sprintf(msgPtr, "\r\n\r\n");
ocomeni 87:99b37d26ff2a 1269 msgPtr += numChars;
ocomeni 87:99b37d26ff2a 1270 // print out generated header
ocomeni 103:7b566b522427 1271 dbg_printf(LOG, "[WiFi MAN] generated response header:\n");
ocomeni 103:7b566b522427 1272 dbg_printf(LOG, "%s\r\n", (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 1273 // calculate header length
ocomeni 89:45f6db09a76d 1274 at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
ocomeni 84:7c7add00f4bf 1275
ocomeni 90:ed0267eca7b5 1276 // package and send on wifi data queue
ocomeni 88:7ffa053be662 1277 at_data_resp->at_resp = AT_HTTPS_RESP;
ocomeni 89:45f6db09a76d 1278 bool queueResult = true;
ocomeni 89:45f6db09a76d 1279 int wait_count = 0;
ocomeni 89:45f6db09a76d 1280 do
ocomeni 89:45f6db09a76d 1281 {
ocomeni 89:45f6db09a76d 1282 if(!queueResult){
ocomeni 89:45f6db09a76d 1283 wait_count++;
ocomeni 103:7b566b522427 1284 dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 89:45f6db09a76d 1285 wait_ms(10);
ocomeni 89:45f6db09a76d 1286 }
ocomeni 89:45f6db09a76d 1287 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 89:45f6db09a76d 1288 }while(queueResult == false);
ocomeni 88:7ffa053be662 1289 delete at_data_resp;
ocomeni 100:80ef4bc31b7a 1290 at_data_resp = NULL;
ocomeni 84:7c7add00f4bf 1291 }
ocomeni 90:ed0267eca7b5 1292
ocomeni 90:ed0267eca7b5 1293
ocomeni 104:11e9605093c9 1294 void WiFiManager::printBufferInHex(const uint8_t *buf, int pLen)
ocomeni 90:ed0267eca7b5 1295 {
ocomeni 104:11e9605093c9 1296 print_debug_hex(buf, pLen);
ocomeni 90:ed0267eca7b5 1297 }
ocomeni 90:ed0267eca7b5 1298
ocomeni 88:7ffa053be662 1299 //#define TRY_PRINTF
ocomeni 84:7c7add00f4bf 1300
ocomeni 90:ed0267eca7b5 1301 void WiFiManager::body_callback(const char *at, uint32_t length) {
ocomeni 103:7b566b522427 1302 dbg_printf(LOG, "\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
ocomeni 84:7c7add00f4bf 1303 chunkNum++;
ocomeni 105:e5ce023eee93 1304 dbg_printf(LOG, "This is the start when response is excluded\r\n%s\r\nend of packet \r\n", at);
ocomeni 102:9748f290a1a5 1305 if(http_response == NULL)
ocomeni 102:9748f290a1a5 1306 {
ocomeni 103:7b566b522427 1307 dbg_printf(LOG, "[WIFI-MAN] response pointer NULL!!\r\n");
ocomeni 102:9748f290a1a5 1308 }
ocomeni 102:9748f290a1a5 1309 else
ocomeni 102:9748f290a1a5 1310 {
ocomeni 103:7b566b522427 1311 dbg_printf(LOG, "[WIFI-MAN] response pointer NULL not!!\r\n");
ocomeni 102:9748f290a1a5 1312 }
ocomeni 122:62166886db5f 1313 #ifdef SEND_DEBUG_MESSAGES
ocomeni 104:11e9605093c9 1314 if(responseString == NULL && chunkNum==1)
ocomeni 104:11e9605093c9 1315 {
ocomeni 104:11e9605093c9 1316 responseString = (char *) malloc(100);
ocomeni 104:11e9605093c9 1317 sprintf(responseString, "\r\nHTTPS BODY CALLBACK RECEIVED\r\n");
ocomeni 104:11e9605093c9 1318 sendATresponseString(AT_EVENT);
ocomeni 104:11e9605093c9 1319 }
ocomeni 122:62166886db5f 1320 #endif
ocomeni 89:45f6db09a76d 1321 sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
ocomeni 84:7c7add00f4bf 1322 }
ocomeni 84:7c7add00f4bf 1323
ocomeni 105:e5ce023eee93 1324 //#define ENABLE_MBED_TRACE
ocomeni 88:7ffa053be662 1325 bool WiFiManager::createTLSconnection(const char * hostName)
ocomeni 88:7ffa053be662 1326 {
ocomeni 93:06e755a80187 1327 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 1328 mbed_trace_init();
ocomeni 93:06e755a80187 1329 #endif
ocomeni 88:7ffa053be662 1330 socket = new TLSSocket();
ocomeni 88:7ffa053be662 1331 nsapi_error_t r;
ocomeni 88:7ffa053be662 1332 // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 1333 r = socket->open(network);
ocomeni 88:7ffa053be662 1334 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 1335 {
ocomeni 103:7b566b522427 1336 dbg_printf(LOG, "TLS open failed!!\n");
ocomeni 88:7ffa053be662 1337 return false;
ocomeni 88:7ffa053be662 1338 }
ocomeni 103:7b566b522427 1339 dbg_printf(LOG, "TLS open passed!!\n");
ocomeni 88:7ffa053be662 1340 r = socket->set_root_ca_cert(SSL_CA_PEM);
ocomeni 88:7ffa053be662 1341 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 1342 {
ocomeni 103:7b566b522427 1343 dbg_printf(LOG, "TLS set_root_ca_cert failed!!\n");
ocomeni 98:65c2333a38b6 1344 socket->close();
ocomeni 103:7b566b522427 1345 dbg_printf(LOG, "closing TLS socket!!\n");
ocomeni 88:7ffa053be662 1346 return false;
ocomeni 88:7ffa053be662 1347 }
ocomeni 103:7b566b522427 1348 dbg_printf(LOG, "TLS set_root_ca_cert passed!!\n");
ocomeni 88:7ffa053be662 1349 r = socket->connect(hostName, 443);
ocomeni 88:7ffa053be662 1350 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 1351 {
ocomeni 93:06e755a80187 1352 char errstr[100];
ocomeni 93:06e755a80187 1353 mbedtls_strerror(r, errstr, 100);
ocomeni 105:e5ce023eee93 1354 dbg_printf(LOG, "TLS connect failed (err = %d) for hostname '%s' -- ERROR = %s !!\n", r, hostName, errstr);
ocomeni 93:06e755a80187 1355 socket->close();
ocomeni 88:7ffa053be662 1356 return false;
ocomeni 88:7ffa053be662 1357 }
ocomeni 103:7b566b522427 1358 dbg_printf(LOG, "TLS connection successful for https site : %s\n", hostName);
ocomeni 88:7ffa053be662 1359 return true;
ocomeni 88:7ffa053be662 1360 }
ocomeni 100:80ef4bc31b7a 1361
ocomeni 100:80ef4bc31b7a 1362
ocomeni 100:80ef4bc31b7a 1363 void WiFiManager::updateRemotePeerDetails()
ocomeni 100:80ef4bc31b7a 1364 {
ocomeni 103:7b566b522427 1365 dbg_printf(LOG, "Updating internet_config... \r\n");
ocomeni 100:80ef4bc31b7a 1366 nsapi_error_t error;
ocomeni 100:80ef4bc31b7a 1367 SocketAddress * address = new SocketAddress;
ocomeni 104:11e9605093c9 1368 error = socket->getpeername(address);
ocomeni 104:11e9605093c9 1369 if(error>=0)
ocomeni 104:11e9605093c9 1370 {
ocomeni 116:2296cf274661 1371 strcpy(internet_config->remote_IPv4Address, address->get_ip_address());
ocomeni 104:11e9605093c9 1372 uint16_t port = address->get_port();
ocomeni 116:2296cf274661 1373 internet_config->remote_port = port;
ocomeni 104:11e9605093c9 1374 }
ocomeni 104:11e9605093c9 1375 else
ocomeni 104:11e9605093c9 1376 {
ocomeni 116:2296cf274661 1377 strcpy(internet_config->remote_IPv4Address, "");
ocomeni 116:2296cf274661 1378 internet_config->remote_port = 0;
ocomeni 104:11e9605093c9 1379 }
ocomeni 100:80ef4bc31b7a 1380 delete address;
ocomeni 100:80ef4bc31b7a 1381 }
ocomeni 100:80ef4bc31b7a 1382
ocomeni 105:e5ce023eee93 1383
ocomeni 105:e5ce023eee93 1384 void WiFiManager::sendDebugMessage()
ocomeni 105:e5ce023eee93 1385 {
ocomeni 107:f1a83fd41b17 1386 #ifdef SEND_HTTPS_DEBUG_MESSAGES
ocomeni 105:e5ce023eee93 1387 sendATresponseString(AT_EVENT);
ocomeni 105:e5ce023eee93 1388 wait_ms(100);
ocomeni 107:f1a83fd41b17 1389 #else
ocomeni 107:f1a83fd41b17 1390 free(responseString);
ocomeni 107:f1a83fd41b17 1391 #endif
ocomeni 105:e5ce023eee93 1392 }
ocomeni 88:7ffa053be662 1393 #define TESTING_HTTPS
ocomeni 98:65c2333a38b6 1394
ocomeni 96:f5ed273881af 1395 //#define DONT_USE_TLS_SOCKET
ocomeni 98:65c2333a38b6 1396 bool WiFiManager::createHttpsRequest()
ocomeni 79:a2187bbfa407 1397 {
ocomeni 106:e1f04c3d0647 1398 int starttime;
ocomeni 106:e1f04c3d0647 1399 int stoptime;
ocomeni 87:99b37d26ff2a 1400 // reset chunk #;
ocomeni 87:99b37d26ff2a 1401 chunkNum = 0;
ocomeni 90:ed0267eca7b5 1402 http_response_hdr_sent = false;
ocomeni 103:7b566b522427 1403 dbg_printf(LOG, "\n[WIFI MAN] Http Request received:\n");
ocomeni 84:7c7add00f4bf 1404 http_req_cfg = (http_request_t *) data_msg->buffer;
ocomeni 105:e5ce023eee93 1405 #ifdef FULL_DEBUG_ENABLED
ocomeni 103:7b566b522427 1406 dbg_printf(LOG, "\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
ocomeni 116:2296cf274661 1407 dbg_printf(LOG, "\n[WIFI MAN] internet cfg url = %s\n", internet_config->url);
ocomeni 105:e5ce023eee93 1408 #endif
ocomeni 87:99b37d26ff2a 1409 char full_url[100];
ocomeni 88:7ffa053be662 1410 char host[60] ;
ocomeni 116:2296cf274661 1411 strncpy(full_url,internet_config->url, strlen(internet_config->url)+1);
ocomeni 90:ed0267eca7b5 1412 strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
ocomeni 90:ed0267eca7b5 1413 strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
ocomeni 105:e5ce023eee93 1414 #ifdef FULL_DEBUG_ENABLED
ocomeni 103:7b566b522427 1415 dbg_printf(LOG, "\n[WIFI MAN] server url+uri = %s\n", full_url);
ocomeni 103:7b566b522427 1416 dbg_printf(LOG, "\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName);
ocomeni 103:7b566b522427 1417 dbg_printf(LOG, "\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal);
ocomeni 103:7b566b522427 1418 dbg_printf(LOG, "\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType);
ocomeni 103:7b566b522427 1419 dbg_printf(LOG, "\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen);
ocomeni 105:e5ce023eee93 1420 #endif
ocomeni 84:7c7add00f4bf 1421
ocomeni 84:7c7add00f4bf 1422 int bodyLen;
ocomeni 90:ed0267eca7b5 1423 sscanf(http_req_cfg->contentLen, "%d", &bodyLen);
ocomeni 105:e5ce023eee93 1424 #ifdef FULL_DEBUG_ENABLED
ocomeni 103:7b566b522427 1425 dbg_printf(LOG, "contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
ocomeni 90:ed0267eca7b5 1426 if(bodyLen > 10){
ocomeni 103:7b566b522427 1427 dbg_printf(LOG, "\n [WIFI MAN] Message Body:\n");
ocomeni 90:ed0267eca7b5 1428 printBufferInHex(http_req_cfg->body, bodyLen);
ocomeni 90:ed0267eca7b5 1429 }
ocomeni 105:e5ce023eee93 1430 #endif
ocomeni 116:2296cf274661 1431 if(strstr(internet_config->url, "http:")!=NULL) // http request
ocomeni 87:99b37d26ff2a 1432 {
ocomeni 87:99b37d26ff2a 1433 http_request = new HttpRequest(network,
ocomeni 90:ed0267eca7b5 1434 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 1435 full_url,
ocomeni 90:ed0267eca7b5 1436 callback(this, &WiFiManager::body_callback));
ocomeni 87:99b37d26ff2a 1437 setHttpHeader("Host", http_req_cfg->hostName);
ocomeni 87:99b37d26ff2a 1438 setHttpHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 88:7ffa053be662 1439 if(http_req_cfg->method == HTTP_GET){
ocomeni 103:7b566b522427 1440 dbg_printf(LOG, "HTTP_GET -- ignoring body\n");
ocomeni 88:7ffa053be662 1441 http_response = http_request->send(NULL, 0);
ocomeni 88:7ffa053be662 1442 }
ocomeni 88:7ffa053be662 1443 else{
ocomeni 88:7ffa053be662 1444 setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 1445 setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 1446 http_response = http_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 1447 }
ocomeni 87:99b37d26ff2a 1448 free_DataMsg();
ocomeni 87:99b37d26ff2a 1449 if (!http_response) {
ocomeni 87:99b37d26ff2a 1450 char buf[100];
ocomeni 87:99b37d26ff2a 1451 mbedtls_strerror(http_request->get_error(), buf, 100);
ocomeni 103:7b566b522427 1452 dbg_printf(LOG, "HttpRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 1453 delete http_request; // free the memory
ocomeni 98:65c2333a38b6 1454 return false;
ocomeni 87:99b37d26ff2a 1455 }
ocomeni 88:7ffa053be662 1456 delete http_request; // free the memory
ocomeni 103:7b566b522427 1457 dbg_printf(LOG, "\n----- HTTP POST response -----\n");
ocomeni 84:7c7add00f4bf 1458 }
ocomeni 87:99b37d26ff2a 1459 else
ocomeni 87:99b37d26ff2a 1460 {
ocomeni 105:e5ce023eee93 1461 mbed_stats_heap_t heap_stats;
ocomeni 105:e5ce023eee93 1462 mbed_stats_heap_get(&heap_stats);
ocomeni 105:e5ce023eee93 1463 dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
ocomeni 106:e1f04c3d0647 1464 starttime = Kernel::get_ms_count();
ocomeni 90:ed0267eca7b5 1465 #ifndef DONT_USE_TLS_SOCKET
ocomeni 88:7ffa053be662 1466 if(https_connection_active == false){
ocomeni 88:7ffa053be662 1467 bool tlsResult;
ocomeni 88:7ffa053be662 1468 tlsResult = createTLSconnection(host);
ocomeni 93:06e755a80187 1469 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 1470 mbed_trace_free(); // free trace memory
ocomeni 93:06e755a80187 1471 #endif
ocomeni 106:e1f04c3d0647 1472 stoptime = Kernel::get_ms_count();
ocomeni 106:e1f04c3d0647 1473 dbg_printf(LOG, "\r\nTLS connection time : %d ms\r\n", (stoptime - starttime));
ocomeni 88:7ffa053be662 1474 if(tlsResult == false){
ocomeni 88:7ffa053be662 1475 delete socket;
ocomeni 103:7b566b522427 1476 dbg_printf(LOG, "TLS Socket connection failed - deleting data msg\r\n");
ocomeni 88:7ffa053be662 1477 free_DataMsg();
ocomeni 103:7b566b522427 1478 dbg_printf(LOG, "data msg deleted \r\n");
ocomeni 104:11e9605093c9 1479 http_result = TLS_CONNECTION_FAILED;
ocomeni 98:65c2333a38b6 1480 return false;
ocomeni 88:7ffa053be662 1481 }
ocomeni 100:80ef4bc31b7a 1482 // update remote peer details after socket connection
ocomeni 100:80ef4bc31b7a 1483 updateRemotePeerDetails();
ocomeni 100:80ef4bc31b7a 1484 // send socket connection event before proceeding to send https request
ocomeni 100:80ef4bc31b7a 1485 // give about 2 ms
ocomeni 104:11e9605093c9 1486 sendSocketConnectionEvent();
ocomeni 88:7ffa053be662 1487 }
ocomeni 88:7ffa053be662 1488 // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
ocomeni 106:e1f04c3d0647 1489 stoptime = Kernel::get_ms_count();
ocomeni 106:e1f04c3d0647 1490 dbg_printf(LOG, "\r\nTLS connection time : %d ms\r\n", (stoptime - starttime));
ocomeni 106:e1f04c3d0647 1491 starttime = Kernel::get_ms_count();
ocomeni 88:7ffa053be662 1492 https_request = new HttpsRequest(socket,
ocomeni 87:99b37d26ff2a 1493 http_req_cfg->method,
ocomeni 88:7ffa053be662 1494 full_url,
ocomeni 87:99b37d26ff2a 1495 callback(this, &WiFiManager::body_callback));
ocomeni 109:c274780ff609 1496 #ifdef SEND_DEBUG_MESSAGES
ocomeni 105:e5ce023eee93 1497 responseString = (char *) malloc(100);
ocomeni 105:e5ce023eee93 1498 sprintf(responseString, "\r\nHTTP REQUEST OBJECT CREATED\r\n");
ocomeni 105:e5ce023eee93 1499 sendDebugMessage();
ocomeni 109:c274780ff609 1500 #endif
ocomeni 90:ed0267eca7b5 1501 #else
ocomeni 90:ed0267eca7b5 1502 https_request = new HttpsRequest(network,
ocomeni 90:ed0267eca7b5 1503 SSL_CA_PEM,
ocomeni 90:ed0267eca7b5 1504 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 1505 full_url,
ocomeni 90:ed0267eca7b5 1506 callback(this, &WiFiManager::body_callback));
ocomeni 90:ed0267eca7b5 1507 #endif
ocomeni 88:7ffa053be662 1508 #ifdef TESTING_HTTPS
ocomeni 103:7b566b522427 1509 dbg_printf(LOG, "http_req_cfg->method = %d\n", http_req_cfg->method);
ocomeni 88:7ffa053be662 1510 if(http_req_cfg->method == HTTP_GET){
ocomeni 103:7b566b522427 1511 dbg_printf(LOG, "HTTP_GET -- ignoring body\n");
ocomeni 89:45f6db09a76d 1512 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 1513 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 88:7ffa053be662 1514 http_response = https_request->send(NULL, 0);
ocomeni 88:7ffa053be662 1515 }
ocomeni 88:7ffa053be662 1516 else{
ocomeni 89:45f6db09a76d 1517 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 1518 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 90:ed0267eca7b5 1519 setHttpsHeader("Content-Type", http_req_cfg->contentType);
ocomeni 90:ed0267eca7b5 1520 setHttpsHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 103:7b566b522427 1521 dbg_printf(LOG, "https headers setup - about to send request\r\n");
ocomeni 105:e5ce023eee93 1522 // Grab the heap statistics
ocomeni 105:e5ce023eee93 1523 dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
ocomeni 110:c722dda4f2ff 1524 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 1525 }
ocomeni 88:7ffa053be662 1526 #else
ocomeni 89:45f6db09a76d 1527 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 1528 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 96:f5ed273881af 1529 setHttpsHeader("Content-Type", http_req_cfg->contentType);
ocomeni 96:f5ed273881af 1530 setHttpsHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 87:99b37d26ff2a 1531 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 1532 #endif
ocomeni 105:e5ce023eee93 1533 nsapi_error_t error = https_request->get_error();
ocomeni 105:e5ce023eee93 1534 if(error < 0)
ocomeni 105:e5ce023eee93 1535 {
ocomeni 87:99b37d26ff2a 1536 char buf[100];
ocomeni 105:e5ce023eee93 1537 mbedtls_strerror(error, buf, 100);
ocomeni 105:e5ce023eee93 1538 printf("HttpsRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 1539 delete https_request; // free the memory
ocomeni 96:f5ed273881af 1540 https_request = NULL;
ocomeni 88:7ffa053be662 1541 https_connection_active = false; // reset true whenever connection fails
ocomeni 98:65c2333a38b6 1542 socket->close();
ocomeni 88:7ffa053be662 1543 delete socket;
ocomeni 96:f5ed273881af 1544 socket = NULL;
ocomeni 90:ed0267eca7b5 1545 free_DataMsg();
ocomeni 105:e5ce023eee93 1546 http_result = HTTP_REQUEST_FAILED;
ocomeni 98:65c2333a38b6 1547 return false;
ocomeni 87:99b37d26ff2a 1548 }
ocomeni 88:7ffa053be662 1549 https_connection_active = true; // set true whenever connection succeeds
ocomeni 103:7b566b522427 1550 dbg_printf(LOG, "\n----- HTTPS POST response -----\r\n");
ocomeni 110:c722dda4f2ff 1551 }
ocomeni 110:c722dda4f2ff 1552 if(http_response_hdr_sent == false)
ocomeni 110:c722dda4f2ff 1553 {
ocomeni 110:c722dda4f2ff 1554 socket->close();
ocomeni 110:c722dda4f2ff 1555 delete socket;
ocomeni 110:c722dda4f2ff 1556 https_connection_active = false;
ocomeni 110:c722dda4f2ff 1557 #ifdef SEND_DEBUG_MESSAGES
ocomeni 110:c722dda4f2ff 1558 responseString = (char *) malloc(100);
ocomeni 110:c722dda4f2ff 1559 sprintf(responseString, "\r\n[WIFI-MAN] NO RESPONSE RECEIVED:: CLOSING CURRENT TLS CONNECTION.\r\n");
ocomeni 110:c722dda4f2ff 1560 sendDebugMessage();
ocomeni 110:c722dda4f2ff 1561 #endif
ocomeni 110:c722dda4f2ff 1562 //return_response(http_response);
ocomeni 90:ed0267eca7b5 1563 }
ocomeni 96:f5ed273881af 1564 free_DataMsg();
ocomeni 96:f5ed273881af 1565 delete https_request; // free the request & response memory
ocomeni 103:7b566b522427 1566 dbg_printf(LOG, "deleted https_request\r\n");
ocomeni 96:f5ed273881af 1567 https_request = NULL;
ocomeni 104:11e9605093c9 1568 http_result = RESPONSE_OK;
ocomeni 106:e1f04c3d0647 1569 stoptime = Kernel::get_ms_count();
ocomeni 106:e1f04c3d0647 1570 dbg_printf(LOG, "\r\nhttp request to response time : %d ms\r\n", (stoptime - starttime));
ocomeni 98:65c2333a38b6 1571 return true;
ocomeni 79:a2187bbfa407 1572 }
ocomeni 79:a2187bbfa407 1573
ocomeni 79:a2187bbfa407 1574 void WiFiManager::createHttpRequest(http_method method,
ocomeni 79:a2187bbfa407 1575 const char* url,
ocomeni 79:a2187bbfa407 1576 Callback<void(const char *at, uint32_t length)> body_callback)
ocomeni 79:a2187bbfa407 1577 {
ocomeni 84:7c7add00f4bf 1578 http_request = new HttpRequest(network,
ocomeni 84:7c7add00f4bf 1579 method, url, body_callback);;
ocomeni 79:a2187bbfa407 1580 }
ocomeni 79:a2187bbfa407 1581
ocomeni 79:a2187bbfa407 1582 void WiFiManager::setHttpHeader(string key, string value)
ocomeni 79:a2187bbfa407 1583 {
ocomeni 79:a2187bbfa407 1584 http_request->set_header(key, value);
ocomeni 79:a2187bbfa407 1585 }
ocomeni 79:a2187bbfa407 1586
ocomeni 79:a2187bbfa407 1587 void WiFiManager::setHttpsHeader(string key, string value)
ocomeni 79:a2187bbfa407 1588 {
ocomeni 79:a2187bbfa407 1589 https_request->set_header(key, value);
ocomeni 79:a2187bbfa407 1590 }
ocomeni 79:a2187bbfa407 1591
ocomeni 79:a2187bbfa407 1592 void WiFiManager::sendHttpsRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 1593 {
ocomeni 78:07bb86e3ce14 1594 }
ocomeni 79:a2187bbfa407 1595
ocomeni 79:a2187bbfa407 1596 void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 1597 {
ocomeni 78:07bb86e3ce14 1598 }
ocomeni 79:a2187bbfa407 1599