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