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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed Apr 17 18:11:45 2019 +0000
Revision:
95:290859010c8c
Parent:
94:fb4414aff957
Child:
96:f5ed273881af
response format for the following command implemented:; - AT+UWSCA; - AT+UNSTAT=0; - AT+UWSSTAT

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