Demo for the LinkLoss Service.

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Revision:
5:04920b552c37
Parent:
4:88dca48b2397
--- a/main.cpp	Sat Jun 20 23:37:56 2015 +0000
+++ b/main.cpp	Tue Dec 29 09:29:26 2015 +0000
@@ -15,14 +15,14 @@
  */
 
 #include "mbed.h"
-#include "BLE.h"
-#include "LinkLossService.h"
+#include "ble/BLE.h"
+#include "ble/services/LinkLossService.h"
 
-BLE ble;
+static LinkLossService* linkLossPtr;
 
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    ble.gap().startAdvertising();
+    BLE::Instance().gap().startAdvertising();
 }
 
 void linkLossCallback(LinkLossService::AlertLevel_t level)
@@ -30,18 +30,53 @@
     printf("received link loss alert\r\n");
 }
 
-int main(void)
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
 {
-    ble.init();
+    /* Avoid compiler warnings */
+    (void) ble;
+    (void) error;
+    
+    /* Initialization error handling should go here */
+}    
+
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+
     ble.gap().onDisconnection(disconnectionCallback);
 
-    LinkLossService linkLoss(ble, linkLossCallback, LinkLossService::HIGH_ALERT);
+    /* Setup primary service */
+    linkLossPtr = new LinkLossService(ble, linkLossCallback, LinkLossService::HIGH_ALERT);
 
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(1000); /* 1second. */
     ble.gap().startAdvertising();
+}
 
+int main(void)
+{
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+    
     while (true) {
         ble.waitForEvent();
     }