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

Dependencies:   mbed-http

Revision:
81:637a87eb8170
Parent:
80:e8f0e92e3ac9
Child:
82:10072c1794d3
--- a/source/WiFiManager.cpp	Thu Mar 21 22:17:28 2019 +0000
+++ b/source/WiFiManager.cpp	Sat Mar 23 16:28:34 2019 +0000
@@ -5,44 +5,95 @@
 WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, 
                          MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                          Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
-                         MemoryPool<wifi_cmd_message_t, 16> *wiFi2ATmPool, 
-                         Queue<wifi_cmd_message_t, 16> *wiFi2ATCmdQueue, 
+                         MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, 
+                         Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, 
                          MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool, 
                          Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue, 
-                         MemoryPool<wifi_data_msg_t, 4> *wiFi2ATDatamPool, 
-                         Queue<wifi_data_msg_t, 4> *wiFi2ATDataQueue) 
+                         MemoryPool<at_data_msg_t, 4> *wiFi2ATDatamPool, 
+                         Queue<at_data_msg_t, 4> *wiFi2ATDataQueue) 
 :
- wifi_config(wifi_config),
- network(wifi),
- _aT2WiFimPool(aT2WiFimPool),
- _aT2WiFiCmdQueue(aT2WiFiCmdQueue)
+     wifi_config(wifi_config),
+     network(wifi),
+    _aT2WiFimPool(aT2WiFimPool),
+    _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
+    
+    _wiFi2ATmPool(wiFi2ATmPool),
+    _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
+    
+    _aT2WiFiDatamPool(aT2WiFiDatamPool),
+    _aT2WiFiDataQueue(aT2WiFiDataQueue),
+    
+    _wiFi2ATDatamPool(wiFi2ATDatamPool),
+    _wiFi2ATDataQueue(wiFi2ATDataQueue)
 
 {
  lastScanCount = 0;
  wifiCmd = WIFI_CMD_NONE;
+ internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
+ is_connected = false;
 }
 
 WiFiManager::~WiFiManager()
 {
 }
   
+bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
+    at_resp_message_t *atResp  = _wiFi2ATmPool->alloc();
+    atResp->at_resp            = resp;
+    _wiFi2ATCmdQueue->put(atResp);
+    return true;
+}
+
+
+bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){
+    at_data_msg_t *atData = _wiFi2ATDatamPool->alloc();
+    atData->at_resp        = at_resp.at_resp;
+    atData->dataLen        = at_resp.dataLen;
+    memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen);
+    _wiFi2ATDataQueue->put(atData);
+    return true;
+}
+
 
 
 void WiFiManager::runMain(){
+    nsapi_error_t error;
     while(true){
         dequeueWiFiCommands();
+        dequeueATdataResponse();
         switch(wifiCmd){
             case WIFI_CMD_NONE:
                 // IDLE STATE
                 break;
             case WIFI_CMD_SCAN:
-                nsapi_error_t error;
                 error = scanNetworks();
                 wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_SCAN_RESP);
+                break;
+            case WIFI_CMD_DETAILED_SCAN:
+                nsapi_size_or_error_t cnt_err;
+                cnt_err = getAvailableAPs(lastScanCount);
+                wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_DETAILED_SCAN_RESP);
                 break;
             case WIFI_CMD_CONNECT:
+                error = connect();
+                wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_CONNECT_RESP);
                 break;
             case WIFI_CMD_DISCONNECT:
+                error = disconnect();
+                wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_DISCONNECT_RESP);
+                break;
+            case WIFI_CMD_CONFIG:
+                set_WIFI_CONFIG();
+                wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_CONFIG_RESP);
+            case WIFI_CMD_INTERNET_CONFIG:
+                set_internet_config();
+                wifiCmd = WIFI_CMD_NONE;
+                queueATresponse(AT_INTERNET_CONFIG_RESP);
                 break;
             case WIFI_CMD_SEND_HTTPS_REQ:
                 break;
@@ -57,7 +108,8 @@
 }
 
 bool  WiFiManager::dequeueWiFiCommands(){
-    osEvent evt = _aT2WiFiCmdQueue->get();
+    if(wifiCmd != WIFI_CMD_NONE) return false; // busy
+    osEvent evt = _aT2WiFiCmdQueue->get(0);
     if(evt.status == osEventMessage){
         wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
         setNextCommand(cmd->wifi_cmd);
@@ -67,6 +119,18 @@
 }
 
 
+bool WiFiManager::dequeueATdataResponse(){
+    if(wifiCmd != WIFI_CMD_NONE) return false; // busy
+    osEvent evt = _aT2WiFiDataQueue->get(0);
+    if(evt.status == osEventMessage){
+        data_msg = (wifi_data_msg_t*)evt.value.p;
+        setNextCommand(data_msg->wifi_cmd);
+        //_wiFi2ATDatamPool->free(data_msg);
+    }
+    return true;
+}
+
+
 bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
 {
     printf("\n [WIFI-MAN] About to set next WiFi manager command \n");
@@ -77,6 +141,25 @@
     return false; // wiFiManager busy
 }
 
+const char * WiFiManager::sec2str(nsapi_security_t sec)
+{
+    switch (sec) {
+        case NSAPI_SECURITY_NONE:
+            return "None";
+        case NSAPI_SECURITY_WEP:
+            return "WEP";
+        case NSAPI_SECURITY_WPA:
+            return "WPA";
+        case NSAPI_SECURITY_WPA2:
+            return "WPA2";
+        case NSAPI_SECURITY_WPA_WPA2:
+            return "WPA/WPA2";
+        case NSAPI_SECURITY_UNKNOWN:
+        default:
+            return "Unknown";
+    }
+}
+
 
 nsapi_size_or_error_t WiFiManager::scanNetworks()
 {
@@ -88,14 +171,50 @@
 }
 
 
-nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, 
-                                                   nsapi_size_t count)
+//nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, 
+//                                                   nsapi_size_t ncount)
+nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
 {
-    count = network->scan(res, count);
+    WiFiAccessPoint       *ap;
+    nsapi_size_or_error_t count;
+    count = ncount;
+    //count = wiFiManager->scanNetworks();
+    if (count <= 0) {
+        //_smutex.lock();
+        printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
+        //_smutex.unlock();
+        return;
+    }
+    /* Limit number of network arbitrary to 15 */
+    count = count < 15 ? count : 15;
+    ap = new WiFiAccessPoint[count];
+    count = network->scan(ap, count);
+    if (count <= 0) {
+        printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
+        return;
+    }
+
+    for (int i = 0; i < count; i++) {
+        printf("[WIFI-MAN]: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
+               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
+               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
+    }
+    printf("[WIFI-MAN]  %d networks available.\n", count);
+
+    delete[] ap;
     return count;
 }
 
 
+void WiFiManager::set_WIFI_CONFIG()
+{
+    wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer;
+    set_WIFI_SSID(wifi_cfg->ssid);
+    set_WIFI_PASSWORD(wifi_cfg->pass);
+    set_WIFI_SECURITY(wifi_cfg->security);
+    free_DataMsg();
+}
+
 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
 {
     strcpy(wifi_config.ssid, wifi_ssid);
@@ -114,6 +233,66 @@
 }
 
 
+
+void WiFiManager::set_internet_config()
+{
+    internet_config_t *internet_cfg  = (internet_config_t *) data_msg->buffer;
+    internet_config.peer_id          = internet_cfg->peer_id;
+    internet_config.url              = internet_cfg->url;
+    internet_config.connectionScheme = internet_cfg->connectionScheme;
+    free_DataMsg();
+    printf("[WIFI MAN] Internet configuration setup completed\n"); 
+    printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, 
+                                                      internet_config.url.c_str(), 
+                                                      internet_config.connectionScheme);
+}
+
+void WiFiManager::free_DataMsg()
+{
+    // free memory after processing
+    _aT2WiFiDatamPool->free(data_msg);
+}
+
+
+void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
+{
+    //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
+    //}
+    switch(param) {
+        case NSAPI_STATUS_LOCAL_UP:
+            printf("[WIFI-MAN] Local IP address set!\r\n");
+            printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
+            break;
+        case NSAPI_STATUS_GLOBAL_UP:
+            printf("Global IP address set!\r\n");
+            printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
+            printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
+            is_connected = true;
+            break;
+        case NSAPI_STATUS_DISCONNECTED:
+            printf("No connection to network!\r\n");
+            printf("\n [WIFI-MAN] No connection to network!\n");
+            is_connected = false;
+            //queueATresponse(AT_DISCONNECT_RESP);
+            // attempt reconnection if always connected scheme is set
+            if(internet_config.connectionScheme == ALWAYS_CONNECTED)
+            {
+                nsapi_error_t error;
+                error = scanNetworks();
+                queueATresponse(WIFI_RECONNECT_INFO);
+            }
+            break;
+        case NSAPI_STATUS_CONNECTING:
+            printf("Connecting to network!\r\n");
+            break;
+        default:
+            printf("Not supported");
+            break;
+    }
+}
+
+
+
    //     NSAPI_STATUS_LOCAL_UP           = 0,        /*!< local IP address set */
    //     NSAPI_STATUS_GLOBAL_UP          = 1,        /*!< global IP address set */
    //     NSAPI_STATUS_DISCONNECTED       = 2,        /*!< no connection to network */
@@ -123,16 +302,20 @@
 nsapi_error_t WiFiManager::connect()
 {
     nsapi_error_t error;
-    printf("\n [WIFI-MAN] About to connect to WiFi networks\n");
+    printf("\n [WIFI-MAN] About to connect to WiFi network\n");
+    network->attach(callback(this, &WiFiManager::status_callback));
     error = network->set_blocking(false);
     if(error)
     {
         printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
         return error;
     }
+    printf("[WIFI-MAN] Connecting to network %s\n", wifi_config.ssid);
     error = network->connect(wifi_config.ssid,
                      wifi_config.pass,
                      wifi_config.security);
+    return error;
+    /*
     if(error)
     {
         printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n");
@@ -151,8 +334,10 @@
         printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status);
         return conn_status;
     }
-    printf("\n [WIFI-MAN] Connect to WiFi network completed - \n");
+    printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
+    printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
     return conn_status;
+    */
 }