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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sat May 18 15:44:48 2019 +0000
Branch:
PassingRegression
Revision:
114:b11bb96c09f3
Parent:
113:888e262ff0a9
Child:
116:2296cf274661
main program refactored:; 1. only atcmd manager started by default; 2. added state in main to allow ATCMD to trigger enabling BLE and WiFi; 3. added main.h to expose trigger functions to ATCMD; 4. added triggers to ATCMD; ; Testing not yet done.

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