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