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

Dependencies:   mbed-http

source/WiFiManager.cpp

Committer:
ocomeni
Date:
2019-03-20
Revision:
79:a2187bbfa407
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9

File content as of revision 79:a2187bbfa407:

#include "WiFiManager.h"
#include "common_config.h"


WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, 
                MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue)
:
 wifi_config(wifi_config),
 network(wifi),
 _aT2WiFimPool(aT2WiFimPool),
 _aT2WiFiCmdQueue(aT2WiFiCmdQueue)

{
 lastScanCount = 0;
 wifiCmd = WIFI_CMD_NONE;
}

WiFiManager::~WiFiManager()
{
}
  


void WiFiManager::runMain(){
    while(true){
        dequeueWiFiCommands();
        switch(wifiCmd){
            case WIFI_CMD_NONE:
                // IDLE STATE
                break;
            case WIFI_CMD_SCAN:
                nsapi_error_t error;
                error = scanNetworks();
                wifiCmd = WIFI_CMD_NONE;
                break;
            case WIFI_CMD_CONNECT:
                break;
            case WIFI_CMD_DISCONNECT:
                break;
            case WIFI_CMD_SEND_HTTPS_REQ:
                break;
            case WIFI_CMD_SEND_HTTP_REQ:
                break;
            default:
                break;
        }
        wait_ms(100); // 
    }
    
}

bool  WiFiManager::dequeueWiFiCommands(){
    osEvent evt = _aT2WiFiCmdQueue->get();
    if(evt.status == osEventMessage){
        wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
        setNextCommand(cmd->wifi_cmd);
        _aT2WiFimPool->free(cmd);
    }
    return true;
}


bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
{
    printf("\n [WIFI-MAN] About to set next WiFi manager command \n");
    if(wifiCmd == WIFI_CMD_NONE){
        wifiCmd = cmd;
        return true; // success
    }
    return false; // wiFiManager busy
}


nsapi_size_or_error_t WiFiManager::scanNetworks()
{
    nsapi_error_t error;
    printf("\n [WIFI-MAN] About to start scan for WiFi networks\n");
    lastScanCount = network->scan(NULL, 0);
    printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n");
    return lastScanCount;
}


nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, 
                                                   nsapi_size_t count)
{
    count = network->scan(res, count);
    return count;
}


void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
{
    strcpy(wifi_config.ssid, wifi_ssid);
}


void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
{
    strcpy(wifi_config.pass, wifi_pass);
}


void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
{
    wifi_config.security = wifi_security;
}


   //     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 */
   //     NSAPI_STATUS_CONNECTING         = 3,        /*!< connecting to network */
   //     NSAPI_STATUS_ERROR_UNSUPPORTED  = NSAPI_ERROR_UNSUPPORTED   
   
nsapi_error_t WiFiManager::connect()
{
    nsapi_error_t error;
    printf("\n [WIFI-MAN] About to connect to WiFi networks\n");
    error = network->set_blocking(false);
    if(error)
    {
        printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
        return error;
    }
    error = network->connect(wifi_config.ssid,
                     wifi_config.pass,
                     wifi_config.security);
    if(error)
    {
        printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n");
        return error;
    }
    nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING;
    int loopCount = 0;
    while(conn_status == NSAPI_STATUS_CONNECTING){
        loopCount++;
        conn_status = network->get_connection_status();
        printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount);
        wait(0.1);
    }
    if(conn_status < 0)
    {
        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");
    return conn_status;
}


nsapi_error_t WiFiManager::disconnect()
{
    nsapi_error_t error;
    error = network->disconnect();
    return error;
}


/*

        HttpsRequest(NetworkInterface* network,
                 const char* ssl_ca_pem,
                 http_method method,
                 const char* url,
                 Callback<void(const char *at, uint32_t length)> body_callback = 0)
       HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response);
    HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0)
        post_req->set_header("Content-Type", "application/json");
        HttpResponse* get_res = get_req->send();

        const char body[] = "{\"hello\":\"world\"}";

        HttpResponse* post_res = post_req->send(body, strlen(body));


*/

void WiFiManager::createHttpsRequest(http_method method,
                                     const char* url,
                                     Callback<void(const char *at, uint32_t length)> body_callback)
{
    https_request = new HttpsRequest(network, SSL_CA_PEM, method, url, body_callback);
}

void WiFiManager::createHttpRequest(http_method method,
                                    const char* url,
                                    Callback<void(const char *at, uint32_t length)> body_callback)
{
    http_request = new HttpRequest(network, method, url, body_callback);;
}

void WiFiManager::setHttpHeader(string key, string value)
{
    http_request->set_header(key, value);
}

void WiFiManager::setHttpsHeader(string key, string value)
{
    https_request->set_header(key, value);
}

void WiFiManager::sendHttpsRequest(const char * body, int bodyLen)
{
}

void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
{
}