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