High level Bluetooth Low Energy API and radio abstraction layer

Fork of BLE_API by Bluetooth Low Energy

Revision:
1090:148d8b9b56a5
Parent:
1089:8e810a8e083e
Child:
1091:58b2dac13261
--- a/ble/Gap.h	Mon Jan 11 08:51:49 2016 +0000
+++ b/ble/Gap.h	Mon Jan 11 08:51:50 2016 +0000
@@ -38,7 +38,6 @@
 public:
     /**
      * Address-type for BLEProtocol addresses.
-     *
      * @note: deprecated. Use BLEProtocol::AddressType_t instead.
      */
     typedef BLEProtocol::AddressType_t AddressType_t;
@@ -49,21 +48,6 @@
      */
     typedef BLEProtocol::AddressType_t addr_type_t;
 
-    /**
-     * Address-type for BLEProtocol addresses.
-     * @note: deprecated. Use BLEProtocol::AddressType_t instead.
-     *
-     * DEPRECATION ALERT: The following constants have been left in their
-     * deprecated state to transparenly support existing applications which may
-     * have used Gap::ADDR_TYPE_*.
-     */
-    enum {
-        ADDR_TYPE_PUBLIC                        = BLEProtocol::AddressType::PUBLIC,
-        ADDR_TYPE_RANDOM_STATIC                 = BLEProtocol::AddressType::RANDOM_STATIC,
-        ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE     = BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE,
-        ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
-    };
-
     static const unsigned ADDR_LEN = BLEProtocol::ADDR_LEN; /**< Length (in octets) of the BLE MAC address. */
     typedef BLEProtocol::Address_t Address_t; /**< 48-bit address, LSB format. @Note: Deprecated. Use BLEProtocol::Address_t instead. */
     typedef BLEProtocol::Address_t address_t; /**< 48-bit address, LSB format. @Note: Deprecated. Use BLEProtocol::Address_t instead. */
@@ -177,6 +161,9 @@
 
     typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t;
 
+    typedef FunctionPointerWithContext<const Gap *> GapShutdownCallback_t;
+    typedef CallChainOfFunctionPointersWithContext<const Gap *> GapShutdownCallbackChain_t;
+
     /*
      * The following functions are meant to be overridden in the platform-specific sub-class.
      */
@@ -1009,9 +996,43 @@
         radioNotificationCallback.attach(tptr, mptr);
     }
 
+    /**
+     * Setup a callback to be invoked to notify the user application that the
+     * Gap instance is about to shutdown (possibly as a result of a call
+     * to BLE::shutdown()).
+     *
+     * @Note: It is possible to chain together multiple onShutdown callbacks
+     * (potentially from different modules of an application) to be notified
+     * before the Gap instance is shutdown.
+     *
+     * @Note: It is also possible to set up a callback into a member function of
+     * some object.
+     *
+     * @Note It is possible to unregister a callback using onShutdown().detach(callback)
+     */
+    void onShutdown(const GapShutdownCallback_t& callback) {
+        shutdownCallChain.add(callback);
+    }
+    template <typename T>
+    void onShutdown(T *objPtr, void (T::*memberPtr)(void)) {
+        shutdownCallChain.add(objPtr, memberPtr);
+    }
+
+    /**
+     * @brief provide access to the callchain of shutdown event callbacks
+     * It is possible to register callbacks using onShutdown().add(callback);
+     * It is possible to unregister callbacks using onShutdown().detach(callback)
+     * @return The shutdown event callbacks chain
+     */
+    GapShutdownCallbackChain_t& onShutdown() {
+        return shutdownCallChain;
+    }
+
 public:
     /**
-     * Clear all Gap state of the associated object.
+     * Notify all registered onShutdown callbacks that the Gap instance is
+     * about to be shutdown and clear all Gap state of the
+     * associated object.
      *
      * This function is meant to be overridden in the platform-specific
      * sub-class. Nevertheless, the sub-class is only expected to reset its
@@ -1024,6 +1045,10 @@
      * scan parameters to default values.
      */
     virtual ble_error_t reset(void) {
+        /* Notify that the instance is about to shutdown */
+        shutdownCallChain.call(this);
+        shutdownCallChain.clear();
+
         /* Clear Gap state */
         state.advertising = 0;
         state.connected   = 0;
@@ -1121,6 +1146,9 @@
     DisconnectionEventCallbackChain_t disconnectionCallChain;
 
 private:
+    GapShutdownCallbackChain_t shutdownCallChain;
+
+private:
     /* Disallow copy and assignment. */
     Gap(const Gap &);
     Gap& operator=(const Gap &);