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