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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun May 05 08:24:46 2019 +0000
Revision:
104:11e9605093c9
Parent:
103:7b566b522427
Child:
105:e5ce023eee93
still having issues with http request not returning

Who changed what in which revision?

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