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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun May 19 13:09:27 2019 +0000
Branch:
PassingRegression
Revision:
116:2296cf274661
Parent:
114:b11bb96c09f3
Child:
118:8df0e9c2ee3f
refactoring main, atcmd, BLE and WiFi. added data structures into main and replaced values with references in main. tested and works.

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