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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Tue Jun 11 20:23:43 2019 +0000
Branch:
PassingRegression
Revision:
122:62166886db5f
Parent:
119:8d939a902333
Child:
123:a49e9ffbaca6
added error handling for Cloud connection keep alive

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