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