this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed Apr 24 17:34:17 2019 +0000
Revision:
102:9748f290a1a5
Parent:
101:1cfd468e5009
Child:
103:7b566b522427
added fix for response header issue.; passing regression tests.; known issue:; - UDDRP response incomplete

Who changed what in which revision?

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