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

Dependencies:   mbed-http

Revision:
75:08eff6258e1b
Parent:
74:f26e846adfe9
Child:
76:6afda865fbf8
--- a/source/main-https.cpp	Sun Mar 10 09:46:06 2019 +0000
+++ b/source/main-https.cpp	Thu Mar 14 21:34:06 2019 +0000
@@ -16,6 +16,7 @@
 #include "ble/services/UARTService.h"
 #include "common_config.h"
 #include "ATCmdManager.h"
+#include "BleManager.h"
 UARTService *uart;
 
 DigitalOut alivenessLED(LED1, 0);
@@ -25,7 +26,7 @@
 static RawSerial *device; // tx, rx
 static UARTSerial *_serial; // tx, rx
 static ATCmdParser *_parser;
-const static char     DEVICE_NAME[] = "BLE-UART";
+const static char     DEVICE_NAME_MAIN[] = "BLE-UART";
 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
 char buffer[BUFFER_LEN];
 uint8_t TxBuffer[TX_BUFFER_LEN];
@@ -214,7 +215,7 @@
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN));
     /* set up the services that can be discovered */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -252,7 +253,7 @@
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN));
     /* set up the services that can be discovered */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -280,6 +281,24 @@
     return (int) txLen;
 }
 
+void printUartRxResult()
+{
+    
+    if(uartCharRcvCount == 0)
+    {
+        device->printf("\nFirst Call to UART attach callback!!\n");
+    }
+    else if(uartCharRcvCount >= uartExpectedRcvCount)
+    {
+        device->printf("\nNumber of Received Bytes = %d\n\n", uartCharRcvCount);
+        device->printf("--- Writing back received bytes --- \n");
+        int n;
+        n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount);
+        UartBusy = false;
+    }
+}
+
+
 void UartRxcallback_ex() {
     if(uartCharRcvCount >= uartExpectedRcvCount)
     {
@@ -288,7 +307,7 @@
     }
     if(uartCharRcvCount == 0)
     {
-        device->printf("\nFirst Call to UART attach callback!!\n");
+        eventQueue.call(printUartRxResult);
     }
     // Note: you need to actually read from the serial to clear the RX interrupt
     RxBuffer[uartCharRcvCount] = (uint8_t) device->getc();
@@ -296,11 +315,7 @@
     if(uartCharRcvCount >= uartExpectedRcvCount)
     {
         alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
-        device->printf("\nNumber of Received Bytes = %d\n\n", uartCharRcvCount);
-        device->printf("--- Writing back received bytes --- \n");
-        int n;
-        n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount);
-        UartBusy = false;
+        eventQueue.call(printUartRxResult);
     }
 }
 
@@ -314,14 +329,9 @@
         rxLen = bufSize;
     }
     uartExpectedRcvCount = rxLen;
+    device->printf("\nattaching to device UART\n\n");
     device->attach(&UartRxcallback_ex);
     device->printf("\nBackground UART read setup completed\n\n");
-    //for(int i=0;i<rxLen;i++)
-    //{
-    //    rxBuffer[i] = (uint8_t) getc();
-    //}
-    // return number of bytes written to UART
-    //return rxLen;
 }
 
 int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo)
@@ -435,6 +445,7 @@
     printf("Waiting for %d seconds...\n", numSecs);
     for(int i=0;i<numSecs;i++){
         printf("%d", i);
+        printf("\n");
         wait(0.5);
         eventQueue.dispatch(500);        // Dispatch time - 500msec
     }
@@ -442,30 +453,58 @@
 
 static int reset_counter = 0;
 
+
+
+
+
+int ble_security_main()
+{
+    BLE& ble = BLE::Instance();
+    events::EventQueue queue;
+
+#if MBED_CONF_APP_FILESYSTEM_SUPPORT
+    /* if filesystem creation fails or there is no filesystem the security manager
+     * will fallback to storing the security database in memory */
+    if (!create_filesystem()) {
+        printf("Filesystem creation failed, will use memory storage\r\n");
+    }
+#endif
+
+    while(1) {
+        {
+            printf("\r\n PERIPHERAL \r\n\r\n");
+            SMDevicePeripheral peripheral(ble, queue, peer_address);
+            peripheral.run();
+        }
+
+        {
+            printf("\r\n CENTRAL \r\n\r\n");
+            SMDeviceCentral central(ble, queue, peer_address);
+            central.run();
+        }
+    }
+
+    return 0;
+}
+
 int main() {
     reset_counter++;
     //RawSerial *device(USBTX, USBRX); // tx, rx
     device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
-    
+    ble_security_main();
     device->printf("\n --- Running UART-BLE-UartService --- (rst_cnt = %d)\n", reset_counter);
-    // Start the event queue
-    //t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
 
     eventQueue.call_every(500, blinkCallback);
     eventQueue.call_every(60000, HelloUart);
     //eventQueue.call_every(1000, checkUartReceive);
+    
     device->printf("\n --- EventQueues setup --- \n");
     ////////////////////////////////////////////////////////////////////////////////
     // BLE Initialization /////////////////////////////////////////////////////////
     device->printf("\n --- about to instantiate BLE instance --- \n");
-    //cbMAIN_driverLock();
     BLE &ble = BLE::Instance();
     device->printf("\n --- BLE Instance Instantiated --- \n");
-    //ble.onEventsToProcess(scheduleBleEventsProcessing);
-    //device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n");
-    //ble.init(bleInitComplete);
     btle_thread.start(callback(bleInitialization));
-    //cbMAIN_driverUnlock();
     
     device->printf("\n --- Waiting for BLE Initialization to be completed --- \n");
     int i = 0;
@@ -477,26 +516,13 @@
         device->putc('0'+(i++ % 10));
     }
     btle_thread.join();
-    if(i < 20)
-    device->printf("\n --- BLE Initialization completed --- \n");
-    else
-    device->printf("\n --- BLE Initialization failed --- \n");
+    if(i < 20){
+        device->printf("\n --- BLE Initialization completed --- \n");
+    }
+    else {
+        device->printf("\n --- BLE Initialization failed --- \n");
+    }
     wait(1); // wait for advertising interval so advertising has started 
-    //reportGapState();
-    //device->printf("\n Press any key to stop BLE advertising: ");
-    //ble.gap().stopAdvertising();
-    //ble.shutdown();
-    //reportGapState();
-    //device->getc();
-    //device->printf("\n Press any key to restart BLE advertising: ");
-    //device->getc();
-    //restartBleAdvertising();
-    //reportGapState();
-    //device->printf("\n Press any key to stop BLE advertising: ");
-    //device->getc();
-    //device->baud(115200);
-    //ble.gap().stopAdvertising();
-    //ble.shutdown();
     reportGapState();
     ////////////////////////////////////////////////////////////////////////////////
     // BLE Initialization /////////////////////////////////////////////////////////
@@ -512,17 +538,18 @@
     ////////////////////////////////////////////////////////////////////////////////////
 #endif
      btle_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
-     printWait(30);
+     printWait(5);
      //device->printf("\n Press any key to start Wifi demo: ");
      //device->getc();
+#ifdef false // comment out wifi part
      int start = Kernel::get_ms_count();
      NetworkInterface* network = connect_to_default_network_interface();
      int stop = Kernel::get_ms_count();
      device->printf("\n The Wifi Network scan took %d ms or %4.1f seconds\n", (stop - start), (float)((stop - start)/1000.0));
      // run on separate thread;
      t.start(callback(wifi_demo, network));
-     //t.start(wifi_demo2());
      t.join(); 
+#endif
      //network->disconnect(); 
      //delete network;
      printMacAddress();  
@@ -530,54 +557,9 @@
      //device->printf("\n Press any key to restart BLE : ");
      //device->getc();
      
-     
-    
-    ////////////////////////////////////////////////////////////////////////////////
-    // BLE Initialization /////////////////////////////////////////////////////////
-    //device->printf("\n --- about to shutdown BLE instance --- \n");
-    //ble.shutdown();
-    //ble.gap().clearAdvertisingPayload();
-    //delete ble;
-    //device->printf("\n --- about to instantiate new BLE instance --- \n");
-    //cbMAIN_driverLock();
-    //BLE &ble = BLE::Instance();
-    //device->printf("\n --- BLE Instance Instantiated --- \n");
-    //ble.onEventsToProcess(scheduleBleEventsProcessing);
-    //device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n");
-    //ble.init(bleInitComplete);
-#ifdef false
-    device->printf("\n --- Restarting BLE Initialization --- \n");
-    ble.gap().reset();
-  
-    btle_thread.start(callback(bleInitialization));
-    btle_thread.join();
-    
-    //ble.init(bleInitComplete);
-    //cbMAIN_driverUnlock();
-    
-    device->printf("\n --- Waiting for BLE Initialization to be completed --- \n");
-    i = 0;
-    while(!ble.hasInitialized() && i < 20){
-        // dispatch function
-        eventQueue.dispatch(1000);        // Dispatch time - 1000msec
-        // 400 msec - Only 2 set of events will be dispatched as period is 200 msec
-        
-        device->putc('0'+(i++ % 10));
-    }
-    if(i < 20)
-    device->printf("\n --- BLE Initialization completed --- \n");
-    else
-    device->printf("\n --- BLE Initialization failed --- \n");
-#endif
     wait(1); // wait for advertising interval so advertising has started 
     reportGapState();
-    
      
-     //reportGapState();
-     //restartBleAdvertising();
-     //printMacAddress();
-     //reportGapState();
-    
     for(int i=0;i<255;i++)
     {
         device->putc(i);
@@ -596,18 +578,16 @@
  
 
     device->printf("\nATCmdParser with ESP8266 example");
-    device->printf("\n Waiting for 2 minutes ");
-    wait(2);
+    device->printf("\n Waiting for 5 seconds ");
+    printWait(5);
     device->printf("\n Waiting finished!!!\n\n ");
     reportGapState();
-    //eventQueue.dispatch_forever();
-    
-    
-    
-    //btle_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
-    device->printf("\n About to delete RawSerial device instance ");
-    delete device;
+    while(UartBusy){
+        wait(0.1);
+    }
     ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, true);
+    //ATCmdManager *aTCmdManager = new ATCmdManager(D1, D0, true);
+    aTCmdManager->runMain();
 #ifdef false
     _serial = new UARTSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
     _parser = new ATCmdParser(_serial);
@@ -632,7 +612,7 @@
     //parser.read(buffer, len);
     printf("\nDone\n");
 #endif
-    performFreeMemoryCheck();
+    //performFreeMemoryCheck();
 
     //eventQueue.dispatch_forever();
     //t.start(callback(&eventQueue, &EventQueue::dispatch_forever));