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