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

Dependencies:   mbed-http

Revision:
79:a2187bbfa407
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9
--- 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();