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

Dependencies:   mbed-http

Revision:
103:7b566b522427
Parent:
90:ed0267eca7b5
Child:
108:3c8fb2c6e7bf
diff -r 9748f290a1a5 -r 7b566b522427 source/BleManager.cpp
--- a/source/BleManager.cpp	Wed Apr 24 17:34:17 2019 +0000
+++ b/source/BleManager.cpp	Thu May 02 21:50:17 2019 +0000
@@ -16,6 +16,7 @@
 
 #include <events/mbed_events.h>
 #include <mbed.h>
+#include "debug.h"
 #include "common_config.h"
 #include "ble/BLE.h"
 #include "ble/services/UARTService.h"
@@ -25,6 +26,7 @@
 #include "LittleFileSystem.h"
 #include "HeapBlockDevice.h"
 #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT
+#define FILE_CODE       "btle"
 
 
 static const uint8_t DEVICE_NAME[] = "SM_device";
@@ -78,7 +80,7 @@
 /** Start BLE interface initialisation */
 void SMDevice::run()
 {
-    printf("\r\n [BTLE MAN]  Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
+    dbg_printf(LOG, "\r\n [BTLE MAN]  Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
 
     ble_error_t error;
 
@@ -91,7 +93,7 @@
 
 
     if (_ble.hasInitialized()) {
-        printf("Ble instance already initialised.\r\n");
+        dbg_printf(LOG, "Ble instance already initialised.\r\n");
         return;
     }
 
@@ -109,7 +111,7 @@
     error = _ble.init(this, &SMDevice::on_init_complete);
 
     if (error) {
-        printf("Error returned by BLE::init.\r\n");
+        dbg_printf(LOG, "Error returned by BLE::init.\r\n");
         return;
     }
 
@@ -122,7 +124,7 @@
 {
     if (_ble.hasInitialized()) {
         _ble.shutdown();
-        printf("Shutting down BLE Instance...\r\n");
+        dbg_printf(LOG, "Shutting down BLE Instance...\r\n");
         _event_queue.break_dispatch();
     }
 }
@@ -136,7 +138,7 @@
 void SMDevice::pairingRequest(
     ble::connection_handle_t connectionHandle
 ) {
-    printf("Pairing requested - authorising\r\n");
+    dbg_printf(LOG, "Pairing requested - authorising\r\n");
     _ble.securityManager().acceptPairingRequest(connectionHandle);
 }
 
@@ -146,9 +148,9 @@
     SecurityManager::SecurityCompletionStatus_t result
 ) {
     if (result == SecurityManager::SEC_STATUS_SUCCESS) {
-        printf("Pairing successful\r\n");
+        dbg_printf(LOG, "Pairing successful\r\n");
     } else {
-        printf("Pairing failed\r\n");
+        dbg_printf(LOG, "Pairing failed\r\n");
     }
 }
 
@@ -159,11 +161,11 @@
     ble::link_encryption_t result
 ) {
     if (result == ble::link_encryption_t::ENCRYPTED) {
-        printf("Link ENCRYPTED\r\n");
+        dbg_printf(LOG, "Link ENCRYPTED\r\n");
     } else if (result == ble::link_encryption_t::ENCRYPTED_WITH_MITM) {
-        printf("Link ENCRYPTED_WITH_MITM\r\n");
+        dbg_printf(LOG, "Link ENCRYPTED_WITH_MITM\r\n");
     } else if (result == ble::link_encryption_t::NOT_ENCRYPTED) {
-        printf("Link NOT_ENCRYPTED\r\n");
+        dbg_printf(LOG, "Link NOT_ENCRYPTED\r\n");
     }
 
 #ifdef DEMO_BLE_SECURITY
@@ -184,7 +186,7 @@
     ble_error_t error;
 
     if (event->error) {
-        printf("Error during the initialisation\r\n");
+        dbg_printf(LOG, "Error during the initialisation\r\n");
         return;
     }
 
@@ -203,14 +205,14 @@
     );
 
     if (error) {
-        printf("Error during init %d\r\n", error);
+        dbg_printf(LOG, "Error during init %d\r\n", error);
         return;
     }
 
     error = _ble.securityManager().preserveBondingStateOnReset(true);
 
     if (error) {
-        printf("Error during preserveBondingStateOnReset %d\r\n", error);
+        dbg_printf(LOG, "Error during preserveBondingStateOnReset %d\r\n", error);
     }
 
 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
@@ -218,7 +220,7 @@
     error = _ble.gap().enablePrivacy(true);
 
     if (error) {
-        printf("Error enabling privacy\r\n");
+        dbg_printf(LOG, "Error enabling privacy\r\n");
     }
 
     Gap::PeripheralPrivacyConfiguration_t configuration_p = {
@@ -245,7 +247,7 @@
     Gap::AddressType_t addr_type;
     Gap::Address_t addr;
     _ble.gap().getAddress(&addr_type, addr);
-    printf("Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+    dbg_printf(LOG, "Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
            addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
 
     /* when scanning we want to connect to a peer device so we need to
@@ -267,9 +269,9 @@
  *  in our case it ends the demonstration. */
 void SMDevice::on_disconnect(const Gap::DisconnectionCallbackParams_t *event)
 {
-    printf("Disconnected\r\n");
+    dbg_printf(LOG, "Disconnected\r\n");
 #ifndef DEMO_BLE_SECURITY
-    printf("Restarting advertising...\r\n");
+    dbg_printf(LOG, "Restarting advertising...\r\n");
     _ble.gap().startAdvertising();
 #else
     _event_queue.break_dispatch();
@@ -280,7 +282,7 @@
  * scanning or connection initiation */
 void SMDevice::on_timeout(const Gap::TimeoutSource_t source)
 {
-    printf("Unexpected timeout - aborting\r\n");
+    dbg_printf(LOG, "Unexpected timeout - aborting\r\n");
     _event_queue.break_dispatch();
 }
 
@@ -319,10 +321,10 @@
     if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
 
-        printf("received %u bytes\n\r ", bytesRead);
+        dbg_printf(LOG, "received %u bytes\n\r ", bytesRead);
 
         if(bytesRead >= 255){
-            printf("Overflow command %u n\r ", bytesRead);
+            dbg_printf(LOG, "Overflow command %u n\r ", bytesRead);
             bytesRead = 255;
         }
 
@@ -333,8 +335,8 @@
 
         buffer[index++] = 0;
 
-        printf("Data : %s ",buffer);
-        printf("\r\n");
+        dbg_printf(LOG, "Data : %s ",buffer);
+        dbg_printf(LOG, "\r\n");
         /* start echo in 50 ms */
         _event_queue.call_in(50, this, &SMDevice::EchoBleUartReceived);
         //_event_queue.call(EchoBleUartReceived);
@@ -364,7 +366,7 @@
      if(gapState.connected){
          strncpy(connStr, " Connected ", 20);
      }
-     printf("\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr);
+     dbg_printf(LOG, "\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr);
      
 }
 
@@ -412,7 +414,7 @@
     //error = _ble.gap().setAdvertisingPayload(advertising_data);
 
     if (error) {
-        printf("Error during Gap::setAdvertisingPayload\r\n");
+        dbg_printf(LOG, "Error during Gap::setAdvertisingPayload\r\n");
         return;
     }
 
@@ -428,11 +430,11 @@
     error = _ble.gap().startAdvertising();
 
     if (error) {
-        printf("Error during Gap::startAdvertising.\r\n");
+        dbg_printf(LOG, "Error during Gap::startAdvertising.\r\n");
         return;
     }
 
-    printf("Please connect to device\r\n");
+    dbg_printf(LOG, "Please connect to device\r\n");
 
     /** This tells the stack to generate a pairingRequest event
      * which will require this application to respond before pairing
@@ -451,7 +453,7 @@
      * during the next demonstration */
     memcpy(_peer_address, connection_event->peerAddr, sizeof(_peer_address));
 
-    printf("Connected to: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+    dbg_printf(LOG, "Connected to: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
             _peer_address[5], _peer_address[4], _peer_address[3],
             _peer_address[2], _peer_address[1], _peer_address[0]);
 
@@ -469,10 +471,10 @@
     );
 
     if (error) {
-        printf("Error during SM::setLinkSecurity %d\r\n", error);
+        dbg_printf(LOG, "Error during SM::setLinkSecurity %d\r\n", error);
         return;
     }
-    printf("SM::setLinkSecurity setup\r\n");
+    dbg_printf(LOG, "SM::setLinkSecurity setup\r\n");
 }
 
 void SMDevicePeripheral::stopAdvertising()
@@ -481,10 +483,10 @@
         ble_error_t error;
         error = _ble.gap().stopAdvertising();;
         if(error){
-            printf(" Error stopping advertising...\r\n");
+            dbg_printf(LOG, " Error stopping advertising...\r\n");
             return;
         }
-        printf("Stopping advertising...\r\n");
+        dbg_printf(LOG, "Stopping advertising...\r\n");
         //_event_queue.break_dispatch();
     }
 }
@@ -494,10 +496,10 @@
         ble_error_t error;
         error = _ble.gap().startAdvertising();
         if(error){
-            printf(" Error Restarting advertising...\r\n");
+            dbg_printf(LOG, " Error Restarting advertising...\r\n");
             return;
         }
-        printf("Restarting advertising...\r\n");
+        dbg_printf(LOG, "Restarting advertising...\r\n");
         //_event_queue.break_dispatch();
     }
 }
@@ -515,14 +517,14 @@
      * and scan requests responses */
     ble_error_t error = _ble.gap().startScan(this, &SMDeviceCentral::on_scan);
 
-    printf("Please advertise\r\n");
+    dbg_printf(LOG, "Please advertise\r\n");
 
-    printf("Scanning for: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+    dbg_printf(LOG, "Scanning for: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
            _peer_address[5], _peer_address[4], _peer_address[3],
            _peer_address[2], _peer_address[1], _peer_address[0]);
 
     if (error) {
-        printf("Error during Gap::startScan %d\r\n", error);
+        dbg_printf(LOG, "Error during Gap::startScan %d\r\n", error);
         return;
     }
 }
@@ -544,11 +546,11 @@
         );
 
         if (error) {
-            printf("Error during Gap::connect %d\r\n", error);
+            dbg_printf(LOG, "Error during Gap::connect %d\r\n", error);
             return;
         }
 
-        printf("Connecting... ");
+        dbg_printf(LOG, "Connecting... ");
 
         /* we may have already scan events waiting
          * to be processed so we need to remember
@@ -571,10 +573,10 @@
     /* in this example the local device is the master so we request pairing */
     error = _ble.securityManager().requestPairing(_handle);
 
-    printf("Connected\r\n");
+    dbg_printf(LOG, "Connected\r\n");
 
     if (error) {
-        printf("Error during SM::requestPairing %d\r\n", error);
+        dbg_printf(LOG, "Error during SM::requestPairing %d\r\n", error);
         return;
     }
 
@@ -607,7 +609,7 @@
 
     if (err) {
         /* Reformat if we can't mount the filesystem */
-        printf("No filesystem found, formatting...\r\n");
+        dbg_printf(LOG, "No filesystem found, formatting...\r\n");
 
         err = fs.reformat(&bd);
 
@@ -629,19 +631,19 @@
     /* 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");
+        dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n");
     }
 #endif
 
     while(1) {
         {
-            printf("\r\n PERIPHERAL \r\n\r\n");
+            dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
             SMDevicePeripheral peripheral(ble, queue, peer_address);
             peripheral.run();
         }
 
         {
-            printf("\r\n CENTRAL \r\n\r\n");
+            dbg_printf(LOG, "\r\n CENTRAL \r\n\r\n");
             SMDeviceCentral central(ble, queue, peer_address);
             central.run();
         }