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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Mon Apr 22 11:11:41 2019 +0000
Revision:
99:05398b3184f8
Parent:
98:65c2333a38b6
Child:
100:80ef4bc31b7a
failing at internet url configuration and verification.

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