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