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