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