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

Dependencies:   mbed-http

Revision:
77:0b505d1e15f4
Parent:
76:6afda865fbf8
Child:
78:07bb86e3ce14
--- a/source/main-https.cpp	Fri Mar 15 14:26:44 2019 +0000
+++ b/source/main-https.cpp	Fri Mar 15 23:25:30 2019 +0000
@@ -26,6 +26,7 @@
 static RawSerial *device; // tx, rx
 static UARTSerial *_serial; // tx, rx
 static ATCmdParser *_parser;
+static SMDevicePeripheral *peripheral;
 const static char     DEVICE_NAME_MAIN[] = "BLE-UART";
 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
 char buffer[BUFFER_LEN];
@@ -41,9 +42,9 @@
 //unsigned char rt_stk[1024];
 //unsigned char hp_stk[1024];
 //unsigned char lp_stk[1024];
-unsigned char btle_stk[1024];
+unsigned char btle_stk[8*1024];
 unsigned char wifi_stk[1024];
-unsigned char atcmd_stk[1024];
+unsigned char atcmd_stk[8*1024];
 static bool bleInitializationCompleted = false;
 
 /* creates three tread objects with different priorities */
@@ -71,11 +72,12 @@
 // wifi demo
 #include "wifi_demo.h"
 
-
+Mutex _smutex; // Protect memory access
 // check free memory
 
 void performFreeMemoryCheck()
 {
+    _smutex.lock();
        // perform free memory check
     int blockSize = 16;
     int i = 1;
@@ -88,6 +90,7 @@
         ++i;
     }
     printf("Ok for %d char\n", (i - 1) * blockSize);
+    _smutex.unlock();
 
 }
 
@@ -475,6 +478,7 @@
             printf("\r\n PERIPHERAL \r\n\r\n");
             SMDevicePeripheral peripheral(ble, queue, peer_address);
             peripheral.run();
+            return 0;
         }
         if(loopCount >= MAX_LOOP_COUNT)
         {
@@ -493,136 +497,115 @@
     return 0;
 }
 
+void print_memory_info() {
+    // allocate enough room for every thread's stack statistics
+    int cnt = osThreadGetCount();
+    mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
+ 
+    cnt = mbed_stats_stack_get_each(stats, cnt);
+    for (int i = 0; i < cnt; i++) {
+        printf("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
+    }
+    free(stats);
+ 
+    // Grab the heap statistics
+    mbed_stats_heap_t heap_stats;
+    mbed_stats_heap_get(&heap_stats);
+    printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
+}
+
+#define DISABLE_WIFI_DEMO
 int main() {
     reset_counter++;
-    //RawSerial *device(USBTX, USBRX); // tx, rx
+    //performFreeMemoryCheck();
+    print_memory_info();
+    printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
     device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
-#ifndef USE_MAIN_BLE
-    ble_security_main();
-    wait(1); // wait for advertising interval so advertising has started 
-    reportGapState();
-#else
-    device->printf("\n --- Running UART-BLE-UartService --- (rst_cnt = %d)\n", reset_counter);
-
-    eventQueue.call_every(500, blinkCallback);
-    eventQueue.call_every(60000, HelloUart);
-    //eventQueue.call_every(1000, checkUartReceive);
+    //ble_security_main();
     
-    device->printf("\n --- EventQueues setup --- \n");
-    ////////////////////////////////////////////////////////////////////////////////
-    // BLE Initialization /////////////////////////////////////////////////////////
-    device->printf("\n --- about to instantiate BLE instance --- \n");
-    BLE &ble = BLE::Instance();
-    device->printf("\n --- BLE Instance Instantiated --- \n");
-    btle_thread.start(callback(bleInitialization));
-    
-    device->printf("\n --- Waiting for BLE Initialization to be completed --- \n");
-    int 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));
-    }
-    btle_thread.join();
-    if(i < 20){
-        device->printf("\n --- BLE Initialization completed --- \n");
-    }
-    else {
-        device->printf("\n --- BLE Initialization failed --- \n");
+    BLE& ble = BLE::Instance();
+    events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
+#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");
     }
-    wait(1); // wait for advertising interval so advertising has started 
-    reportGapState();
-    ////////////////////////////////////////////////////////////////////////////////
-    // BLE Initialization /////////////////////////////////////////////////////////
-#ifdef false
-    device->printf("\n --- about to shutdown BLE instance --- \n");
-    device->printf("\n Press any key: ");
-    device->getc();
-    //ble.gap().clearAdvertisingPayload();
-    ble.gap().stopAdvertising();
-    ble.gap().reset();
-    //ble.shutdown();
-    //delete ble;
-    ////////////////////////////////////////////////////////////////////////////////////
-#endif
-     btle_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
-     printWait(5);
 #endif
-     //device->printf("\n Press any key to start Wifi demo: ");
-     //device->getc();
+    print_memory_info();
+    printf("\r\n PERIPHERAL \r\n\r\n");
+    //SMDevicePeripheral peripheral(ble, queue, peer_address);
+    peripheral = new SMDevicePeripheral(ble, queue, peer_address);
+    print_memory_info();
+
+    peripheral->run();
+    btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
+    //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
+    //performFreeMemoryCheck();
+    //reportGapState();
+    print_memory_info();
+    printWait(60); // lets give time to see BLE advertising... 
+    //reportGapState();
+    peripheral->stopAdvertising();
+    //queue.break_dispatch();
+    //reportGapState();
+    //btle_thread.join();
+    //performFreeMemoryCheck();
+    printf("\r\n BTLE THREAD HAS RETURNED \r\n\r\n");
+
 #ifndef DISABLE_WIFI_DEMO // 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.join(); 
+    printMacAddress();  
+    //reportGapState();
+    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.join(); 
+    network->disconnect(); 
+    delete network;
+    printMacAddress();  
+    //reportGapState();
+    device->printf("\n Wifi-Demo completed - restarting BLE  \n\n");
+#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
+    device->printf("\n ++++++ restarting BLE ++++++ \n\n");
 #endif
-     network->disconnect(); 
-     //delete network;
-     printMacAddress();  
-     reportGapState();
-     //device->printf("\n Press any key to restart BLE : ");
-     //device->getc();
-     
-    wait(1); // wait for advertising interval so advertising has started 
-    reportGapState();
-     
+    peripheral->startAdvertising();
+    //peripheral->run();
+    //btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
+    //peripheral.run();
+    //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
+    //t.start(callback(&queue, &EventQueue::dispatch_forever));
+    printWait(60);
+    //reportGapState();
+#ifdef ENABLE_UART_BACKGRND_DEMO
     for(int i=0;i<255;i++)
     {
         device->putc(i);
     }
-    int n;
+    //int n;
     //ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4);
     reportGapState();
     device->printf("\n\n\nEnter # of expected bytes: ");
-    n = ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true);
+    ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true);
     uint8_t rxLen = (uint8_t) (100*(RxBuffer[0]-'0') + 10*(RxBuffer[1]-'0') + (RxBuffer[2]-'0')) %256;
     device->printf("\n\nExpected # of Received Bytes = %d\n", rxLen);
     BackGndUartRead(RxBuffer, RX_BUFFER_LEN, (int) rxLen);
-    //device->printf("--- Writing back received data --- \n\n");
-    //n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, rxLen);
-    //write("\n\ntesting Serial Write\n", 40); //, checkUartReceive, SERIAL_EVENT_TX_COMPLETE);
- 
 
-    device->printf("\nATCmdParser with ESP8266 example");
     device->printf("\n Waiting for 5 seconds ");
     printWait(5);
-    device->printf("\n Waiting finished!!!\n\n ");
-    reportGapState();
+    device->printf("\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n");
     while(UartBusy){
         wait(0.1);
     }
+#endif 
+    device->printf("\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
     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);
-    _parser->printf("\n _serial printf being used for this \n\n");
-    device->printf("\n device printf being used now \n\n");
-    printf("\n ATCmdParser printf being used now \n\n");
-    _parser->debug_on( 1 );
-    _parser->set_delimiter( "\r\n" );
-
-    //Now get the FW version number of ESP8266 by sending an AT command
-    printf("\nATCmdParser: Retrieving FW version");
-    _parser->send("AT+GMR");
-    int version;
-    if(_parser->recv("SDK version:%d", &version) && _parser->recv("OK")) {
-        printf("\nATCmdParser: FW version: %d", version);
-        printf("\nATCmdParser: Retrieving FW version success");
-    } else {
-        printf("\nATCmdParser: Retrieving FW version failed");
-        return -1;
-    }
-    //parser.recv("+CIPDATA:%d,", &len);
-    //parser.read(buffer, len);
-    printf("\nDone\n");
-#endif
+    atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
     //performFreeMemoryCheck();
 
     //eventQueue.dispatch_forever();
@@ -630,11 +613,6 @@
     //eventQueue2.dispatch_forever();
 
     return 0;
-
-
-
-
-    //wait(osWaitForever);
 }
 
 #endif