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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Mar 24 17:32:06 2019 +0000
Revision:
82:10072c1794d3
Parent:
81:637a87eb8170
Child:
84:7c7add00f4bf
wifi configuration setup implemented

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 80:e8f0e92e3ac9 10 MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool,
ocomeni 80:e8f0e92e3ac9 11 Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue,
ocomeni 81:637a87eb8170 12 MemoryPool<at_data_msg_t, 4> *wiFi2ATDatamPool,
ocomeni 81:637a87eb8170 13 Queue<at_data_msg_t, 4> *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 78:07bb86e3ce14 34 }
ocomeni 78:07bb86e3ce14 35
ocomeni 78:07bb86e3ce14 36 WiFiManager::~WiFiManager()
ocomeni 78:07bb86e3ce14 37 {
ocomeni 78:07bb86e3ce14 38 }
ocomeni 79:a2187bbfa407 39
ocomeni 81:637a87eb8170 40 bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
ocomeni 81:637a87eb8170 41 at_resp_message_t *atResp = _wiFi2ATmPool->alloc();
ocomeni 81:637a87eb8170 42 atResp->at_resp = resp;
ocomeni 81:637a87eb8170 43 _wiFi2ATCmdQueue->put(atResp);
ocomeni 81:637a87eb8170 44 return true;
ocomeni 81:637a87eb8170 45 }
ocomeni 81:637a87eb8170 46
ocomeni 81:637a87eb8170 47
ocomeni 81:637a87eb8170 48 bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){
ocomeni 81:637a87eb8170 49 at_data_msg_t *atData = _wiFi2ATDatamPool->alloc();
ocomeni 81:637a87eb8170 50 atData->at_resp = at_resp.at_resp;
ocomeni 81:637a87eb8170 51 atData->dataLen = at_resp.dataLen;
ocomeni 81:637a87eb8170 52 memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen);
ocomeni 81:637a87eb8170 53 _wiFi2ATDataQueue->put(atData);
ocomeni 81:637a87eb8170 54 return true;
ocomeni 81:637a87eb8170 55 }
ocomeni 81:637a87eb8170 56
ocomeni 79:a2187bbfa407 57
ocomeni 79:a2187bbfa407 58
ocomeni 79:a2187bbfa407 59 void WiFiManager::runMain(){
ocomeni 81:637a87eb8170 60 nsapi_error_t error;
ocomeni 79:a2187bbfa407 61 while(true){
ocomeni 79:a2187bbfa407 62 dequeueWiFiCommands();
ocomeni 81:637a87eb8170 63 dequeueATdataResponse();
ocomeni 79:a2187bbfa407 64 switch(wifiCmd){
ocomeni 79:a2187bbfa407 65 case WIFI_CMD_NONE:
ocomeni 79:a2187bbfa407 66 // IDLE STATE
ocomeni 79:a2187bbfa407 67 break;
ocomeni 79:a2187bbfa407 68 case WIFI_CMD_SCAN:
ocomeni 79:a2187bbfa407 69 error = scanNetworks();
ocomeni 79:a2187bbfa407 70 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 71 queueATresponse(AT_SCAN_RESP);
ocomeni 81:637a87eb8170 72 break;
ocomeni 81:637a87eb8170 73 case WIFI_CMD_DETAILED_SCAN:
ocomeni 81:637a87eb8170 74 nsapi_size_or_error_t cnt_err;
ocomeni 81:637a87eb8170 75 cnt_err = getAvailableAPs(lastScanCount);
ocomeni 81:637a87eb8170 76 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 77 queueATresponse(AT_DETAILED_SCAN_RESP);
ocomeni 79:a2187bbfa407 78 break;
ocomeni 79:a2187bbfa407 79 case WIFI_CMD_CONNECT:
ocomeni 81:637a87eb8170 80 error = connect();
ocomeni 81:637a87eb8170 81 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 82 queueATresponse(AT_CONNECT_RESP);
ocomeni 79:a2187bbfa407 83 break;
ocomeni 79:a2187bbfa407 84 case WIFI_CMD_DISCONNECT:
ocomeni 81:637a87eb8170 85 error = disconnect();
ocomeni 81:637a87eb8170 86 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 87 queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 88 break;
ocomeni 81:637a87eb8170 89 case WIFI_CMD_CONFIG:
ocomeni 81:637a87eb8170 90 set_WIFI_CONFIG();
ocomeni 81:637a87eb8170 91 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 92 queueATresponse(AT_CONFIG_RESP);
ocomeni 82:10072c1794d3 93 break;
ocomeni 81:637a87eb8170 94 case WIFI_CMD_INTERNET_CONFIG:
ocomeni 81:637a87eb8170 95 set_internet_config();
ocomeni 81:637a87eb8170 96 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 97 queueATresponse(AT_INTERNET_CONFIG_RESP);
ocomeni 79:a2187bbfa407 98 break;
ocomeni 79:a2187bbfa407 99 case WIFI_CMD_SEND_HTTPS_REQ:
ocomeni 79:a2187bbfa407 100 break;
ocomeni 79:a2187bbfa407 101 case WIFI_CMD_SEND_HTTP_REQ:
ocomeni 79:a2187bbfa407 102 break;
ocomeni 79:a2187bbfa407 103 default:
ocomeni 79:a2187bbfa407 104 break;
ocomeni 79:a2187bbfa407 105 }
ocomeni 79:a2187bbfa407 106 wait_ms(100); //
ocomeni 79:a2187bbfa407 107 }
ocomeni 79:a2187bbfa407 108
ocomeni 78:07bb86e3ce14 109 }
ocomeni 79:a2187bbfa407 110
ocomeni 79:a2187bbfa407 111 bool WiFiManager::dequeueWiFiCommands(){
ocomeni 81:637a87eb8170 112 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 113 osEvent evt = _aT2WiFiCmdQueue->get(0);
ocomeni 79:a2187bbfa407 114 if(evt.status == osEventMessage){
ocomeni 79:a2187bbfa407 115 wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
ocomeni 79:a2187bbfa407 116 setNextCommand(cmd->wifi_cmd);
ocomeni 79:a2187bbfa407 117 _aT2WiFimPool->free(cmd);
ocomeni 79:a2187bbfa407 118 }
ocomeni 79:a2187bbfa407 119 return true;
ocomeni 79:a2187bbfa407 120 }
ocomeni 79:a2187bbfa407 121
ocomeni 79:a2187bbfa407 122
ocomeni 81:637a87eb8170 123 bool WiFiManager::dequeueATdataResponse(){
ocomeni 81:637a87eb8170 124 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 125 osEvent evt = _aT2WiFiDataQueue->get(0);
ocomeni 81:637a87eb8170 126 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 127 data_msg = (wifi_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 128 setNextCommand(data_msg->wifi_cmd);
ocomeni 81:637a87eb8170 129 //_wiFi2ATDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 130 }
ocomeni 81:637a87eb8170 131 return true;
ocomeni 81:637a87eb8170 132 }
ocomeni 81:637a87eb8170 133
ocomeni 81:637a87eb8170 134
ocomeni 79:a2187bbfa407 135 bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
ocomeni 78:07bb86e3ce14 136 {
ocomeni 79:a2187bbfa407 137 printf("\n [WIFI-MAN] About to set next WiFi manager command \n");
ocomeni 79:a2187bbfa407 138 if(wifiCmd == WIFI_CMD_NONE){
ocomeni 79:a2187bbfa407 139 wifiCmd = cmd;
ocomeni 79:a2187bbfa407 140 return true; // success
ocomeni 79:a2187bbfa407 141 }
ocomeni 79:a2187bbfa407 142 return false; // wiFiManager busy
ocomeni 78:07bb86e3ce14 143 }
ocomeni 79:a2187bbfa407 144
ocomeni 81:637a87eb8170 145 const char * WiFiManager::sec2str(nsapi_security_t sec)
ocomeni 81:637a87eb8170 146 {
ocomeni 81:637a87eb8170 147 switch (sec) {
ocomeni 81:637a87eb8170 148 case NSAPI_SECURITY_NONE:
ocomeni 81:637a87eb8170 149 return "None";
ocomeni 81:637a87eb8170 150 case NSAPI_SECURITY_WEP:
ocomeni 81:637a87eb8170 151 return "WEP";
ocomeni 81:637a87eb8170 152 case NSAPI_SECURITY_WPA:
ocomeni 81:637a87eb8170 153 return "WPA";
ocomeni 81:637a87eb8170 154 case NSAPI_SECURITY_WPA2:
ocomeni 81:637a87eb8170 155 return "WPA2";
ocomeni 81:637a87eb8170 156 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 81:637a87eb8170 157 return "WPA/WPA2";
ocomeni 81:637a87eb8170 158 case NSAPI_SECURITY_UNKNOWN:
ocomeni 81:637a87eb8170 159 default:
ocomeni 81:637a87eb8170 160 return "Unknown";
ocomeni 81:637a87eb8170 161 }
ocomeni 81:637a87eb8170 162 }
ocomeni 81:637a87eb8170 163
ocomeni 79:a2187bbfa407 164
ocomeni 79:a2187bbfa407 165 nsapi_size_or_error_t WiFiManager::scanNetworks()
ocomeni 79:a2187bbfa407 166 {
ocomeni 79:a2187bbfa407 167 nsapi_error_t error;
ocomeni 79:a2187bbfa407 168 printf("\n [WIFI-MAN] About to start scan for WiFi networks\n");
ocomeni 79:a2187bbfa407 169 lastScanCount = network->scan(NULL, 0);
ocomeni 79:a2187bbfa407 170 printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n");
ocomeni 79:a2187bbfa407 171 return lastScanCount;
ocomeni 79:a2187bbfa407 172 }
ocomeni 79:a2187bbfa407 173
ocomeni 79:a2187bbfa407 174
ocomeni 81:637a87eb8170 175 //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res,
ocomeni 81:637a87eb8170 176 // nsapi_size_t ncount)
ocomeni 81:637a87eb8170 177 nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
ocomeni 79:a2187bbfa407 178 {
ocomeni 81:637a87eb8170 179 WiFiAccessPoint *ap;
ocomeni 81:637a87eb8170 180 nsapi_size_or_error_t count;
ocomeni 81:637a87eb8170 181 count = ncount;
ocomeni 81:637a87eb8170 182 //count = wiFiManager->scanNetworks();
ocomeni 81:637a87eb8170 183 if (count <= 0) {
ocomeni 81:637a87eb8170 184 //_smutex.lock();
ocomeni 81:637a87eb8170 185 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 186 //_smutex.unlock();
ocomeni 81:637a87eb8170 187 return;
ocomeni 81:637a87eb8170 188 }
ocomeni 81:637a87eb8170 189 /* Limit number of network arbitrary to 15 */
ocomeni 81:637a87eb8170 190 count = count < 15 ? count : 15;
ocomeni 81:637a87eb8170 191 ap = new WiFiAccessPoint[count];
ocomeni 81:637a87eb8170 192 count = network->scan(ap, count);
ocomeni 81:637a87eb8170 193 if (count <= 0) {
ocomeni 81:637a87eb8170 194 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 195 return;
ocomeni 81:637a87eb8170 196 }
ocomeni 81:637a87eb8170 197
ocomeni 81:637a87eb8170 198 for (int i = 0; i < count; i++) {
ocomeni 81:637a87eb8170 199 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 200 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ocomeni 81:637a87eb8170 201 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 202 }
ocomeni 81:637a87eb8170 203 printf("[WIFI-MAN] %d networks available.\n", count);
ocomeni 81:637a87eb8170 204
ocomeni 81:637a87eb8170 205 delete[] ap;
ocomeni 79:a2187bbfa407 206 return count;
ocomeni 79:a2187bbfa407 207 }
ocomeni 79:a2187bbfa407 208
ocomeni 79:a2187bbfa407 209
ocomeni 81:637a87eb8170 210 void WiFiManager::set_WIFI_CONFIG()
ocomeni 81:637a87eb8170 211 {
ocomeni 81:637a87eb8170 212 wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer;
ocomeni 82:10072c1794d3 213 if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid);
ocomeni 82:10072c1794d3 214 if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass);
ocomeni 82:10072c1794d3 215 if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security);
ocomeni 81:637a87eb8170 216 free_DataMsg();
ocomeni 81:637a87eb8170 217 }
ocomeni 81:637a87eb8170 218
ocomeni 78:07bb86e3ce14 219 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
ocomeni 78:07bb86e3ce14 220 {
ocomeni 78:07bb86e3ce14 221 strcpy(wifi_config.ssid, wifi_ssid);
ocomeni 82:10072c1794d3 222 printf("[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid);
ocomeni 82:10072c1794d3 223
ocomeni 78:07bb86e3ce14 224 }
ocomeni 79:a2187bbfa407 225
ocomeni 79:a2187bbfa407 226
ocomeni 78:07bb86e3ce14 227 void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
ocomeni 78:07bb86e3ce14 228 {
ocomeni 78:07bb86e3ce14 229 strcpy(wifi_config.pass, wifi_pass);
ocomeni 82:10072c1794d3 230 printf("[WIFI-MAN] wifi_pass set to %s\n", wifi_config.pass);
ocomeni 78:07bb86e3ce14 231 }
ocomeni 79:a2187bbfa407 232
ocomeni 79:a2187bbfa407 233
ocomeni 78:07bb86e3ce14 234 void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
ocomeni 78:07bb86e3ce14 235 {
ocomeni 78:07bb86e3ce14 236 wifi_config.security = wifi_security;
ocomeni 82:10072c1794d3 237 printf("[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security));
ocomeni 78:07bb86e3ce14 238 }
ocomeni 79:a2187bbfa407 239
ocomeni 79:a2187bbfa407 240
ocomeni 81:637a87eb8170 241
ocomeni 81:637a87eb8170 242 void WiFiManager::set_internet_config()
ocomeni 81:637a87eb8170 243 {
ocomeni 81:637a87eb8170 244 internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer;
ocomeni 81:637a87eb8170 245 internet_config.peer_id = internet_cfg->peer_id;
ocomeni 81:637a87eb8170 246 internet_config.url = internet_cfg->url;
ocomeni 81:637a87eb8170 247 internet_config.connectionScheme = internet_cfg->connectionScheme;
ocomeni 81:637a87eb8170 248 free_DataMsg();
ocomeni 81:637a87eb8170 249 printf("[WIFI MAN] Internet configuration setup completed\n");
ocomeni 81:637a87eb8170 250 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 81:637a87eb8170 251 internet_config.url.c_str(),
ocomeni 81:637a87eb8170 252 internet_config.connectionScheme);
ocomeni 81:637a87eb8170 253 }
ocomeni 81:637a87eb8170 254
ocomeni 81:637a87eb8170 255 void WiFiManager::free_DataMsg()
ocomeni 81:637a87eb8170 256 {
ocomeni 81:637a87eb8170 257 // free memory after processing
ocomeni 81:637a87eb8170 258 _aT2WiFiDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 259 }
ocomeni 81:637a87eb8170 260
ocomeni 81:637a87eb8170 261
ocomeni 81:637a87eb8170 262 void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
ocomeni 81:637a87eb8170 263 {
ocomeni 81:637a87eb8170 264 //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
ocomeni 81:637a87eb8170 265 //}
ocomeni 81:637a87eb8170 266 switch(param) {
ocomeni 81:637a87eb8170 267 case NSAPI_STATUS_LOCAL_UP:
ocomeni 81:637a87eb8170 268 printf("[WIFI-MAN] Local IP address set!\r\n");
ocomeni 81:637a87eb8170 269 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 270 break;
ocomeni 81:637a87eb8170 271 case NSAPI_STATUS_GLOBAL_UP:
ocomeni 81:637a87eb8170 272 printf("Global IP address set!\r\n");
ocomeni 81:637a87eb8170 273 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 274 printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
ocomeni 81:637a87eb8170 275 is_connected = true;
ocomeni 81:637a87eb8170 276 break;
ocomeni 81:637a87eb8170 277 case NSAPI_STATUS_DISCONNECTED:
ocomeni 81:637a87eb8170 278 printf("No connection to network!\r\n");
ocomeni 81:637a87eb8170 279 printf("\n [WIFI-MAN] No connection to network!\n");
ocomeni 81:637a87eb8170 280 is_connected = false;
ocomeni 81:637a87eb8170 281 //queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 282 // attempt reconnection if always connected scheme is set
ocomeni 81:637a87eb8170 283 if(internet_config.connectionScheme == ALWAYS_CONNECTED)
ocomeni 81:637a87eb8170 284 {
ocomeni 81:637a87eb8170 285 nsapi_error_t error;
ocomeni 81:637a87eb8170 286 error = scanNetworks();
ocomeni 81:637a87eb8170 287 queueATresponse(WIFI_RECONNECT_INFO);
ocomeni 81:637a87eb8170 288 }
ocomeni 81:637a87eb8170 289 break;
ocomeni 81:637a87eb8170 290 case NSAPI_STATUS_CONNECTING:
ocomeni 81:637a87eb8170 291 printf("Connecting to network!\r\n");
ocomeni 81:637a87eb8170 292 break;
ocomeni 81:637a87eb8170 293 default:
ocomeni 81:637a87eb8170 294 printf("Not supported");
ocomeni 81:637a87eb8170 295 break;
ocomeni 81:637a87eb8170 296 }
ocomeni 81:637a87eb8170 297 }
ocomeni 81:637a87eb8170 298
ocomeni 81:637a87eb8170 299
ocomeni 81:637a87eb8170 300
ocomeni 79:a2187bbfa407 301 // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */
ocomeni 79:a2187bbfa407 302 // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */
ocomeni 79:a2187bbfa407 303 // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */
ocomeni 79:a2187bbfa407 304 // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */
ocomeni 79:a2187bbfa407 305 // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED
ocomeni 79:a2187bbfa407 306
ocomeni 79:a2187bbfa407 307 nsapi_error_t WiFiManager::connect()
ocomeni 79:a2187bbfa407 308 {
ocomeni 79:a2187bbfa407 309 nsapi_error_t error;
ocomeni 81:637a87eb8170 310 printf("\n [WIFI-MAN] About to connect to WiFi network\n");
ocomeni 81:637a87eb8170 311 network->attach(callback(this, &WiFiManager::status_callback));
ocomeni 79:a2187bbfa407 312 error = network->set_blocking(false);
ocomeni 79:a2187bbfa407 313 if(error)
ocomeni 79:a2187bbfa407 314 {
ocomeni 79:a2187bbfa407 315 printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
ocomeni 79:a2187bbfa407 316 return error;
ocomeni 79:a2187bbfa407 317 }
ocomeni 81:637a87eb8170 318 printf("[WIFI-MAN] Connecting to network %s\n", wifi_config.ssid);
ocomeni 79:a2187bbfa407 319 error = network->connect(wifi_config.ssid,
ocomeni 79:a2187bbfa407 320 wifi_config.pass,
ocomeni 79:a2187bbfa407 321 wifi_config.security);
ocomeni 81:637a87eb8170 322 return error;
ocomeni 81:637a87eb8170 323 /*
ocomeni 79:a2187bbfa407 324 if(error)
ocomeni 79:a2187bbfa407 325 {
ocomeni 79:a2187bbfa407 326 printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n");
ocomeni 79:a2187bbfa407 327 return error;
ocomeni 79:a2187bbfa407 328 }
ocomeni 79:a2187bbfa407 329 nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING;
ocomeni 79:a2187bbfa407 330 int loopCount = 0;
ocomeni 79:a2187bbfa407 331 while(conn_status == NSAPI_STATUS_CONNECTING){
ocomeni 79:a2187bbfa407 332 loopCount++;
ocomeni 79:a2187bbfa407 333 conn_status = network->get_connection_status();
ocomeni 79:a2187bbfa407 334 printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount);
ocomeni 79:a2187bbfa407 335 wait(0.1);
ocomeni 79:a2187bbfa407 336 }
ocomeni 79:a2187bbfa407 337 if(conn_status < 0)
ocomeni 79:a2187bbfa407 338 {
ocomeni 79:a2187bbfa407 339 printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status);
ocomeni 79:a2187bbfa407 340 return conn_status;
ocomeni 79:a2187bbfa407 341 }
ocomeni 81:637a87eb8170 342 printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
ocomeni 81:637a87eb8170 343 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 79:a2187bbfa407 344 return conn_status;
ocomeni 81:637a87eb8170 345 */
ocomeni 79:a2187bbfa407 346 }
ocomeni 79:a2187bbfa407 347
ocomeni 79:a2187bbfa407 348
ocomeni 79:a2187bbfa407 349 nsapi_error_t WiFiManager::disconnect()
ocomeni 78:07bb86e3ce14 350 {
ocomeni 79:a2187bbfa407 351 nsapi_error_t error;
ocomeni 79:a2187bbfa407 352 error = network->disconnect();
ocomeni 79:a2187bbfa407 353 return error;
ocomeni 78:07bb86e3ce14 354 }
ocomeni 79:a2187bbfa407 355
ocomeni 79:a2187bbfa407 356
ocomeni 79:a2187bbfa407 357 /*
ocomeni 79:a2187bbfa407 358
ocomeni 79:a2187bbfa407 359 HttpsRequest(NetworkInterface* network,
ocomeni 79:a2187bbfa407 360 const char* ssl_ca_pem,
ocomeni 79:a2187bbfa407 361 http_method method,
ocomeni 79:a2187bbfa407 362 const char* url,
ocomeni 79:a2187bbfa407 363 Callback<void(const char *at, uint32_t length)> body_callback = 0)
ocomeni 79:a2187bbfa407 364 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response);
ocomeni 79:a2187bbfa407 365 HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0)
ocomeni 79:a2187bbfa407 366 post_req->set_header("Content-Type", "application/json");
ocomeni 79:a2187bbfa407 367 HttpResponse* get_res = get_req->send();
ocomeni 79:a2187bbfa407 368
ocomeni 79:a2187bbfa407 369 const char body[] = "{\"hello\":\"world\"}";
ocomeni 79:a2187bbfa407 370
ocomeni 79:a2187bbfa407 371 HttpResponse* post_res = post_req->send(body, strlen(body));
ocomeni 79:a2187bbfa407 372
ocomeni 79:a2187bbfa407 373
ocomeni 79:a2187bbfa407 374 */
ocomeni 79:a2187bbfa407 375
ocomeni 79:a2187bbfa407 376 void WiFiManager::createHttpsRequest(http_method method,
ocomeni 79:a2187bbfa407 377 const char* url,
ocomeni 79:a2187bbfa407 378 Callback<void(const char *at, uint32_t length)> body_callback)
ocomeni 79:a2187bbfa407 379 {
ocomeni 79:a2187bbfa407 380 https_request = new HttpsRequest(network, SSL_CA_PEM, method, url, body_callback);
ocomeni 79:a2187bbfa407 381 }
ocomeni 79:a2187bbfa407 382
ocomeni 79:a2187bbfa407 383 void WiFiManager::createHttpRequest(http_method method,
ocomeni 79:a2187bbfa407 384 const char* url,
ocomeni 79:a2187bbfa407 385 Callback<void(const char *at, uint32_t length)> body_callback)
ocomeni 79:a2187bbfa407 386 {
ocomeni 79:a2187bbfa407 387 http_request = new HttpRequest(network, method, url, body_callback);;
ocomeni 79:a2187bbfa407 388 }
ocomeni 79:a2187bbfa407 389
ocomeni 79:a2187bbfa407 390 void WiFiManager::setHttpHeader(string key, string value)
ocomeni 79:a2187bbfa407 391 {
ocomeni 79:a2187bbfa407 392 http_request->set_header(key, value);
ocomeni 79:a2187bbfa407 393 }
ocomeni 79:a2187bbfa407 394
ocomeni 79:a2187bbfa407 395 void WiFiManager::setHttpsHeader(string key, string value)
ocomeni 79:a2187bbfa407 396 {
ocomeni 79:a2187bbfa407 397 https_request->set_header(key, value);
ocomeni 79:a2187bbfa407 398 }
ocomeni 79:a2187bbfa407 399
ocomeni 79:a2187bbfa407 400 void WiFiManager::sendHttpsRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 401 {
ocomeni 78:07bb86e3ce14 402 }
ocomeni 79:a2187bbfa407 403
ocomeni 79:a2187bbfa407 404 void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 405 {
ocomeni 78:07bb86e3ce14 406 }
ocomeni 79:a2187bbfa407 407