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