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

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
ocomeni
Date:
Wed Mar 20 21:02:47 2019 +0000
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9
Commit message:
now have working mechanism for comms between threads using memory pool and queue.; Next stage expand this for data.

Changed in this revision

source/ATCmdManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/ATCmdManager.h Show annotated file Show diff for this revision Revisions of this file
source/BleManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/BleManager.h Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.h Show annotated file Show diff for this revision Revisions of this file
source/common_types.h Show annotated file Show diff for this revision Revisions of this file
source/main-https.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/ATCmdManager.cpp	Wed Mar 20 21:02:47 2019 +0000
@@ -1,13 +1,21 @@
 #include "ATCmdManager.h"
 #include "common_config.h"
+#include "common_types.h"
+
 
 ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, 
-                           WiFiManager *wifi, bool debug)
+                           events::EventQueue &event_queue, WiFiManager *wifi, 
+                           MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
+                           Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
+                           bool debug)
     :
     _serial(tx, rx, DEFAULT_BAUD_RATE),
     _parser(&_serial),
     blePeripheral(blePeripheral),
-    wifi(wifi)
+    _event_queue(event_queue),
+    wiFiManager(wiFiManager),
+    _aT2WiFimPool(aT2WiFimPool),
+    _aT2WiFiCmdQueue(aT2WiFiCmdQueue)
 
 { 
 // constructor
@@ -25,6 +33,8 @@
     _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
     _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
     _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
+    _parser.oob("AT+UWSC=", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
+    _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
     
 
     //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
@@ -74,6 +84,11 @@
 void ATCmdManager::_oob_err(){
 }
 
+void ATCmdManager::_oob_get_fw_ver()
+{
+}
+
+
 void ATCmdManager::_oob_uart_setup(){
     int uOpts[NUM_UART_OPTIONS];
     //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
@@ -186,10 +201,89 @@
     _smutex.unlock();
 }
 
-void ATCmdManager::_oob_get_fw_ver(){
+const char * ATCmdManager::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";
+    }
+}
+bool  ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
+    wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
+    wifiCmd->wifi_cmd            = cmd;
+    _aT2WiFiCmdQueue->put(wifiCmd);
+    return true;
+}
+
+void ATCmdManager::_oob_scanWiFiNetworks(){
     _smutex.lock();
-    printf("\n Received Firmware Version command!!\n");
+    printf("\n Received scanWiFiNetworks command!!\n");
     _parser.send("OK\n");
     _smutex.unlock();
+    wifi_cmd_t cmd = WIFI_CMD_SCAN;
+    // queue next command
+    queueWiFiCommand(cmd);
+    bool success;
+    success = wiFiManager->setNextCommand(cmd);
+    if(success){
+        printf("\n scan command successfully sent to wiFiManager!!\n");
+    }
+    else {
+        printf("\n ERROR: Failed to send scan command wiFiManager!!\n");
+    }
+    return;
+    /* call WiFi Scan in 20 ms */
+    //_event_queue.call_in(
+    //    20, wiFiManager,
+    //    &WiFiManager::scanNetworks);
+    //return;
+    WiFiAccessPoint       *ap;
+    nsapi_size_or_error_t count;
+    //count = wiFiManager->scanNetworks();
+    if (count <= 0) {
+        _smutex.lock();
+        printf("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 = wiFiManager->getAvailableAPs(ap, count);
+    if (count <= 0) {
+        printf("scan() failed with return value: %d\n", count);
+        return;
+    }
+
+    for (int i = 0; i < count; i++) {
+        printf("Network: %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("%d networks available.\n", count);
+
+    delete[] ap;
+    
 }
 
+void ATCmdManager::_oob_connect2WiFiNetwork()
+{
+    wiFiManager->connect();
+}
+
+
+void ATCmdManager::_oob_disconnectWiFiNetwork()
+{
+    wiFiManager->disconnect();
+}
--- a/source/ATCmdManager.h	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/ATCmdManager.h	Wed Mar 20 21:02:47 2019 +0000
@@ -1,5 +1,6 @@
 #ifndef __ATCMD_MANAGER_H__
 #define __ATCMD_MANAGER_H__
+#include <events/mbed_events.h>
 #include <mbed.h>
 #include "ATCmdParser.h"
 #include "BleManager.h"
@@ -15,7 +16,10 @@
 class ATCmdManager {
 public:
     ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, 
-                 WiFiManager *wifi, bool debug = false);
+                 events::EventQueue &event_queue, WiFiManager *wifi, 
+                 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
+                 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
+                 bool debug = false);
 public:
     void runMain();
 
@@ -26,11 +30,14 @@
     UARTSerial _serial;
     Mutex _smutex; // Protect serial port access
     Mutex _rmutex; // Reset protection
-
+    // define event queue
+    events::EventQueue &_event_queue;
     // AT Command Parser
     ATCmdParser _parser;
     SMDevicePeripheral *blePeripheral;
-    WiFiManager *wifi;
+    WiFiManager *wiFiManager;
+    MemoryPool<wifi_cmd_message_t, 16> *_aT2WiFimPool;
+    Queue<wifi_cmd_message_t, 16> *_aT2WiFiCmdQueue;
     // OOB processing
     void _process_oob(uint32_t timeout, bool all);
     // OOB message handlers
@@ -48,6 +55,11 @@
     void _oob_ena_ble_peri();
     void _oob_reboot();
     void _oob_get_fw_ver();
+    void _oob_scanWiFiNetworks();
+    void _oob_connect2WiFiNetwork();
+    void _oob_disconnectWiFiNetwork();
+    const char * sec2str(nsapi_security_t sec);
+    bool  queueWiFiCommand(wifi_cmd_t cmd);
     
     /**
     * Allows timeout to be changed between commands
--- a/source/BleManager.cpp	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/BleManager.cpp	Wed Mar 20 21:02:47 2019 +0000
@@ -194,8 +194,8 @@
     error = _ble.securityManager().init(
         true,
         false,
-        SecurityManager::IO_CAPS_NONE,
-        NULL,
+        SecurityManager::IO_CAPS_DISPLAY_ONLY, // SecurityManager::IO_CAPS_NONE
+        ble_config.pairingKey,
         false,
         db_path
     );
@@ -251,6 +251,8 @@
     _ble.gap().onConnection(this, &SMDevice::on_connect);
     _ble.gap().onDisconnection(this, &SMDevice::on_disconnect);
     _ble.gattServer().onDataWritten(this, &SMDevice::onDataWrittenCallback);
+    //_ble.securityManager().onPasskeyDisplay(this, &SMDevice::passkeyDisplayCallback);
+    //_ble.securityManager().onSecuritySetupCompleted(this, &SMDevice::securitySetupCompletedCallback);
 
     /* start test in 500 ms */
     _event_queue.call_in(500, this, &SMDevice::start);
@@ -294,8 +296,19 @@
 }
 
 
+/** Send data aynchronously using BLE                                       */
+void SMDevice::sendBLEUartData(char * str)
+{
+    Gap::GapState_t gapState = _ble.gap().getState();
+    if(gapState.connected){    
+        uart->writeString(str);
+        uart->writeString("\n");    //flushes uart output buffer and sends data
+    }
+}
+
+
 /**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
+ * This callback allows the UARTService to receive updates.
  *
  * @param[in] params
  *     Information about the characterisitc being updated.
@@ -326,6 +339,10 @@
 
     }
 }
+
+
+
+ 
 /** Blink LED to show we're running */
 void SMDevice::blink(void)
 {
@@ -346,16 +363,6 @@
          strncpy(connStr, " Connected ", 20);
      }
      printf("\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr);
-     unsigned nLen;
-     ble_error_t error;
-     error =  _ble.gap().getDeviceName((uint8_t *) devName, &nLen);
-     if(error != BLE_ERROR_NONE)
-     {
-        printf("\n Error Reading BLE device Name \n");
-        return;
-     }
-     devName[nLen] = NULL;
-     printf("\n BLE Device name = %s : Name Len %d\n", devName, nLen);
      
 }
 
@@ -379,8 +386,8 @@
     /* add device name */
     advertising_data.addData(
         GapAdvertisingData::COMPLETE_LOCAL_NAME,
-        DEVICE_NAME,
-        sizeof(DEVICE_NAME)
+        (const uint8_t *)ble_config.deviceName,
+        sizeof(ble_config.deviceName)
         );
     /* Setup primary service */
     uart = new UARTService(_ble);
--- a/source/BleManager.h	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/BleManager.h	Wed Mar 20 21:02:47 2019 +0000
@@ -85,6 +85,7 @@
     );
     
     void shutDown();
+    void sendBLEUartData(char * str);
 
 private:
     /** Override to start chosen activity when initialisation completes */
@@ -111,6 +112,7 @@
     void blink(void);
     /** Echo received data back                                       */
     void EchoBleUartReceived();
+    
 
     void reportGapState();
     
@@ -121,6 +123,8 @@
      *     Information about the characterisitc being updated.
      */
     void onDataWrittenCallback(const GattWriteCallbackParams *params);
+    //void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey);
+    //void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status);
 
 private:
     DigitalOut _led1;
--- a/source/WiFiManager.cpp	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/WiFiManager.cpp	Wed Mar 20 21:02:47 2019 +0000
@@ -2,44 +2,210 @@
 #include "common_config.h"
 
 
-WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi)
+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)
+ network(wifi),
+ _aT2WiFimPool(aT2WiFimPool),
+ _aT2WiFiCmdQueue(aT2WiFiCmdQueue)
 
 {
+ lastScanCount = 0;
+ wifiCmd = WIFI_CMD_NONE;
 }
 
 WiFiManager::~WiFiManager()
 {
 }
-void WiFiManager::scanChannels()
-{
+  
+
+
+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); // 
+    }
+    
 }
-void WiFiManager::getAvailableAPs()
+
+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;
 }
-void WiFiManager::connect()
+
+
+   //     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;
 }
-void WiFiManager::disconnect()
+
+
+/*
+
+        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::sendHttpsRequest()
+
+void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
 {
 }
-void WiFiManager::sendHttpRequest()
-{
-}
+
--- a/source/WiFiManager.h	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/WiFiManager.h	Wed Mar 20 21:02:47 2019 +0000
@@ -1,26 +1,65 @@
 #ifndef __WIFI_MANAGER_H__
 #define __WIFI_MANAGER_H__
+#ifndef __MBED_H__
+#define __MBED_H__
+#include "mbed.h"
+#endif
+
+#include "mbed_trace.h"
+#include "https_request.h"
+#include "http_request.h"
+/* List of trusted root CA certificates
+ * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
+ *
+ * To add more root certificates, just concatenate them.
+ */
+#include "https_certificates.h"
 #include "common_types.h"
 
+
 class WiFiManager {
 public:
-    WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi);
+    WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, 
+                MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
+                Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue);
     ~WiFiManager();
-    void scanChannels();
-    void getAvailableAPs();
-    void set_WIFI_SSID(char * wifi_ssid);
-    void set_WIFI_PASSWORD(char * wifi_pass);
-    void set_WIFI_SECURITY(nsapi_security_t wifi_security);
-    void connect();
-    void disconnect();
-    void sendHttpsRequest();
-    void sendHttpRequest();
-
+    nsapi_size_or_error_t scanNetworks();
+    nsapi_size_or_error_t getAvailableAPs(WiFiAccessPoint * res, 
+                                          nsapi_size_t count);
+    void                  set_WIFI_SSID(char * wifi_ssid);
+    void                  set_WIFI_PASSWORD(char * wifi_pass);
+    void                  set_WIFI_SECURITY(nsapi_security_t wifi_security);
+    nsapi_error_t         connect();
+    nsapi_error_t         disconnect();
+    void                  createHttpsRequest(http_method method,
+                                           const char* url,
+                                           Callback<void(const char *at, uint32_t length)> body_callback = 0
+                                           );
+    void                  createHttpRequest(http_method method,
+                                          const char* url,
+                                          Callback<void(const char *at, uint32_t length)> body_callback = 0
+                                          );
+    void                  setHttpHeader(string key, string value);
+    void                  setHttpsHeader(string key, string value);
+    void                  sendHttpsRequest(const char * body, int bodyLen);
+    void                  sendHttpRequest(const char * body, int bodyLen);
+    bool                  setNextCommand(wifi_cmd_t cmd);
+    bool                  dequeueWiFiCommands();
+    void                  runMain();
+    
 
 
 private:
     wifi_config_t wifi_config;
     WiFiInterface *network;
+    HttpsRequest* https_request;
+    HttpRequest* http_request;
+    HttpResponse* http_response;
+    nsapi_size_or_error_t lastScanCount;
+    wifi_cmd_t  wifiCmd;
+    MemoryPool<wifi_cmd_message_t, 16> *_aT2WiFimPool;
+    Queue<wifi_cmd_message_t, 16> *_aT2WiFiCmdQueue;
+    
     /**
     * Allows timeout to be changed between commands
     *
--- a/source/common_types.h	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/common_types.h	Wed Mar 20 21:02:47 2019 +0000
@@ -1,6 +1,8 @@
 #ifndef __COMMON_TYPES_H__
 #define __COMMON_TYPES_H__
 #include <mbed.h>
+#include "ble/BLE.h"
+#include "SecurityManager.h"
 #define BLE_MAX_DEVICE_NAME_LEN 10
 #define MAX_SSID_LEN 32
 #define MAX_PASSKEY_LEN 32
@@ -8,9 +10,11 @@
 /** ble configuration structure
 */
 typedef struct ble_config {
-    char     deviceName[BLE_MAX_DEVICE_NAME_LEN]; /* BLE Device Name */
-    uint16_t advInterval; /* advertising interval in msecs */
-    uint16_t advTimeout;  /* advertising timeout in secs */
+    char      deviceName[BLE_MAX_DEVICE_NAME_LEN]; /* BLE Device Name */
+    uint16_t  advInterval; /* advertising interval in msecs */
+    uint16_t  advTimeout;  /* advertising timeout in secs */
+    //Passkey_t pairingKey;  /* pairing Key */
+    uint8_t   pairingKey[6];  /* pairing Key */
 } ble_config_t;
 
 /** ble configuration structure
@@ -29,4 +33,20 @@
 } app_config_t;
 
 
+typedef enum wifi_cmd
+{
+  WIFI_CMD_NONE,
+  WIFI_CMD_SCAN,
+  WIFI_CMD_CONNECT,
+  WIFI_CMD_DISCONNECT,
+  WIFI_CMD_SEND_HTTPS_REQ,
+  WIFI_CMD_SEND_HTTP_REQ
+}wifi_cmd_t;
+
+
+typedef struct {
+    wifi_cmd_t    wifi_cmd;   /* wifi command */
+} wifi_cmd_message_t;
+
+
 #endif  // __COMMON_TYPES_H__
\ No newline at end of file
--- a/source/main-https.cpp	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/main-https.cpp	Wed Mar 20 21:02:47 2019 +0000
@@ -15,6 +15,7 @@
 #include "LEDService.h"
 #include "ble/services/UARTService.h"
 #include "common_config.h"
+#include "common_types.h"
 #include "ATCmdManager.h"
 #include "BleManager.h"
 #include "WiFiManager.h"
@@ -35,6 +36,7 @@
 
 // BLE configuration
 static ble_config_t ble_config;
+const uint8_t pairingPassword[6] = "1101";
 // BLE interface pointer 
 //BLE &_ble;
 // BLE peripheral pointer
@@ -50,6 +52,9 @@
 
 LEDService *ledServicePtr;
 
+MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool;
+Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue;
+
 
 /* allocate statically stacks for the three threads */
 //unsigned char rt_stk[1024];
@@ -266,12 +271,36 @@
     }
 }
 
+void printWaitAbortKeyPress(int numSecs)
+{
+    printf("Waiting for %d seconds... [press key to abort]\n", numSecs);
+    char fmtstr[20];
+    for(int i=0;i<numSecs;i++){
+        printf("%d", i);
+        printf("\n");
+        sprintf(fmtstr, "BLE: loop # %d\n", i);
+        peripheral->sendBLEUartData(fmtstr);
+        wait(0.5);
+        eventQueue.dispatch(500);        // Dispatch time - 500msec
+        if(device->readable()){
+            printf("keypress detected aborting....\n");
+            device->getc();
+            break;
+        }
+    }
+}
+
+
 
 void setupDefaultBleConfig()
 {
     strcpy(ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
     ble_config.advInterval = 1000;             // set advertising interval to 1 second default
     ble_config.advTimeout = 0;                 // set advertising timeout to disabled by default
+    // This works in C and C++
+    memcpy(ble_config.pairingKey, pairingPassword, 6); // 
+
+    //ble_config.pairingKey = pairingPassword;
 }
 
 void setupDefaultWiFiConfig()
@@ -342,6 +371,7 @@
 }
 
 //#define DISABLE_WIFI
+#define DISABLE_WIFI_DEMO
 int main() {
     reset_counter++;
     //performFreeMemoryCheck();
@@ -349,8 +379,8 @@
     printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
     device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
     //ble_security_main();
+    setupDefaultWiFiConfig();
     setupDefaultBleConfig();
-    setupDefaultWiFiConfig();
     BLE& _ble = BLE::Instance();
     events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
@@ -372,7 +402,7 @@
     //performFreeMemoryCheck();
     //reportGapState();
     print_memory_info();
-    printWait(60); // lets give time to see BLE advertising... 
+    printWaitAbortKeyPress(120);
     //reportGapState();
     peripheral->stopAdvertising();
     //queue.break_dispatch();
@@ -384,7 +414,11 @@
 #ifndef DISABLE_WIFI // comment out wifi part
     int start = Kernel::get_ms_count();
 #ifdef DISABLE_WIFI_DEMO
-    wiFiManager = new WiFiManager(wifi_config, wifi);
+    network = WiFiInterface::get_default_instance();
+    if (!network) {
+        printf("ERROR: No WiFiInterface found.\n");
+    }
+    wiFiManager = new WiFiManager(wifi_config, network, &aT2WiFimPool, &aT2WiFiCmdQueue);
 #else
     NetworkInterface* network = connect_to_default_network_interface();
     int stop = Kernel::get_ms_count();
@@ -398,11 +432,11 @@
 #endif
 #else
     device->printf("\n Wifi Demo disabled so just waiting it out...  \n\n");
-    printWait(60); // lets wait for a minute before turning BLE back on
+    printWait(2); // lets wait for a minute before turning BLE back on
     device->printf("\n ++++++ restarting BLE ++++++ \n\n");
 #endif
     peripheral->startAdvertising();
-    printWait(60);
+    printWait(2);
 #ifdef ENABLE_UART_BACKGRND_DEMO
     for(int i=0;i<255;i++)
     {
@@ -424,10 +458,64 @@
         wait(0.1);
     }
 #endif 
+
+    device->printf("\r\n++++++ Press key for Wifi demo test ++++++ \r\n");
+    printWaitAbortKeyPress(120);
+#ifdef SKIP_WIFI_SCAN_DEMO
+    device->printf("\r\n++++++ Test WiFi Manager Network Scan ++++++ \r\n");
+    int count;
+    count = wiFiManager->scanNetworks();
+    if (count <= 0) {
+        device->printf("scan() failed with return value: %d\n", count);
+    }
+    else {
+        device->printf("\r\n++++++ Test WiFi Scan found %d networks ++++++ \r\n ++++ SUCCESS ++++\r\n", count);
+    }
+#endif    
+    device->printf("\r\n++++++ Test WiFi Manager Network connect ++++++ \r\n");
+    nsapi_error_t werror;
+    werror = wiFiManager->connect();
+    if (werror < 0) {
+        device->printf("connect() failed with return value: %d\n", werror);
+    }
+    else {
+        device->printf("\r\n++++++ Test WiFi connect SUCCESSFUL ++++++ \r\n");
+    }
+    if(!werror) // connect successful - test dicsonnection
+    {
+        device->printf("\r\n++++++ Test WiFi Manager Network disconnect ++++++ \r\n");
+        werror = wiFiManager->disconnect();
+        if (werror) {
+            device->printf("disconnect() failed with return value: %d\n", werror);
+        }
+        else {
+            device->printf("\r\n++++++ Test WiFi disconnect SUCCESSFUL ++++++ \r\n");
+        }
+    }
+    
+    //wiFiManager->runMain();
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    device->printf("\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n");
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
+    t.start(callback(wiFiManager, &WiFiManager::runMain));
+    /* disconnect in 2 s */
+    //_event_queue.call_in(
+    //    2000, &_ble.gap(),
+    //    &Gap::disconnect, _handle, Gap::REMOTE_USER_TERMINATED_CONNECTION
+    //eventQueue.dispatch_forever();
+    wifi_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
     device->printf("\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
-    ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral, wiFiManager, true);
+    ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral, 
+                                                eventQueue, wiFiManager, 
+                                                &aT2WiFimPool, &aT2WiFiCmdQueue, 
+                                                true);
     aTCmdManager->runMain();
-    atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
+    //atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
+    while(1) wait(0.1);
     //performFreeMemoryCheck();
 
     //eventQueue.dispatch_forever();
--- a/source/main.cpp	Sat Mar 16 13:05:52 2019 +0000
+++ b/source/main.cpp	Wed Mar 20 21:02:47 2019 +0000
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifdef false
+#ifdef OBSOLETE_MAIN
 #include <events/mbed_events.h>
 #include <mbed.h>
 #include "ble/BLE.h"