this is using the mbed os version 5-13-1
Revision 77:0b505d1e15f4, committed 2019-03-15
- Comitter:
- ocomeni
- Date:
- Fri Mar 15 23:25:30 2019 +0000
- Parent:
- 76:6afda865fbf8
- Child:
- 78:07bb86e3ce14
- Commit message:
- BLE & ATCmdManager working concurrently
Changed in this revision
--- a/mbed_app.json Fri Mar 15 14:26:44 2019 +0000
+++ b/mbed_app.json Fri Mar 15 23:25:30 2019 +0000
@@ -25,7 +25,9 @@
"MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\"",
"MBEDTLS_TEST_NULL_ENTROPY",
"MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
- "MBED_HEAP_STATS_ENABLED=1"
+ "MBED_HEAP_STATS_ENABLED=1",
+ "MBED_STACK_STATS_ENABLED=1",
+ "MBED_MEM_TRACING_ENABLED=1"
],
"target_overrides": {
"*": {
--- a/source/BleManager.cpp Fri Mar 15 14:26:44 2019 +0000
+++ b/source/BleManager.cpp Fri Mar 15 23:25:30 2019 +0000
@@ -81,6 +81,11 @@
/* to show we're running we'll blink every 500ms */
_event_queue.call_every(500, this, &SMDevice::blink);
+ /* to show we're advertising we'll print status every minute */
+ _event_queue.call_every(60000, this, &SMDevice::reportGapState);
+
+
+
if (_ble.hasInitialized()) {
printf("Ble instance already initialised.\r\n");
return;
@@ -105,9 +110,20 @@
}
/* this will not return until shutdown */
- _event_queue.dispatch_forever();
+ //_event_queue.dispatch_forever();
}
+
+void SMDevice::shutDown()
+{
+ if (_ble.hasInitialized()) {
+ _ble.shutdown();
+ printf("Shutting down BLE Instance...\r\n");
+ _event_queue.break_dispatch();
+ }
+}
+
+
/* event handler functions */
/** Respond to a pairing request. This will be called by the stack
@@ -246,7 +262,12 @@
void SMDevice::on_disconnect(const Gap::DisconnectionCallbackParams_t *event)
{
printf("Disconnected\r\n");
+#ifndef DEMO_BLE_SECURITY
+ printf("Restarting advertising...\r\n");
+ _ble.gap().startAdvertising();
+#else
_event_queue.break_dispatch();
+#endif
}
/** End demonstration unexpectedly. Called if timeout is reached during advertising,
@@ -310,6 +331,33 @@
}
+void SMDevice::reportGapState()
+{
+ Gap::GapState_t gapState = _ble.gap().getState();
+ char connStr[20] = " Not Connected ";
+ char advStr[20] = " Not Advertising ";
+ char devName[20] = "";
+ if(gapState.advertising){
+ strncpy(advStr, " Advertising ", 20);
+ }
+ if(gapState.connected){
+ 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);
+
+}
+
+
/** A peripheral device will advertise, accept the connection and request
* a change in link security. */
SMDevicePeripheral::SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address)
@@ -413,8 +461,38 @@
printf("Error during SM::setLinkSecurity %d\r\n", error);
return;
}
+ printf("SM::setLinkSecurity setup\r\n");
}
+void SMDevicePeripheral::stopAdvertising()
+{
+ if (_ble.hasInitialized()) {
+ ble_error_t error;
+ error = _ble.gap().stopAdvertising();;
+ if(error){
+ printf(" Error stopping advertising...\r\n");
+ return;
+ }
+ printf("Stopping advertising...\r\n");
+ //_event_queue.break_dispatch();
+ }
+}
+void SMDevicePeripheral::startAdvertising()
+{
+ if (_ble.hasInitialized()) {
+ ble_error_t error;
+ error = _ble.gap().startAdvertising();
+ if(error){
+ printf(" Error Restarting advertising...\r\n");
+ return;
+ }
+ printf("Restarting advertising...\r\n");
+ //_event_queue.break_dispatch();
+ }
+}
+
+
+
/** A central device will scan, connect to a peer and request pairing. */
SMDeviceCentral::SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address)
--- a/source/BleManager.h Fri Mar 15 14:26:44 2019 +0000
+++ b/source/BleManager.h Fri Mar 15 23:25:30 2019 +0000
@@ -81,6 +81,8 @@
ble::connection_handle_t connectionHandle,
ble::link_encryption_t result
);
+
+ void shutDown();
private:
/** Override to start chosen activity when initialisation completes */
@@ -107,6 +109,8 @@
void blink(void);
/** Echo received data back */
void EchoBleUartReceived();
+
+ void reportGapState();
/**
* This callback allows the LEDService to receive updates to the ledState Characteristic.
@@ -138,6 +142,8 @@
/** This is called by Gap to notify the application we connected,
* in our case it immediately requests a change in link security */
virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event);
+ void stopAdvertising();
+ void startAdvertising();
};
--- 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