From 3cd2c31e1017a4c0f196482b72e0ff9e8885c5a8 Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 10:52:43 +0000 Subject: [PATCH 1/6] white space diffs. --- ble/FunctionPointerWithContext.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/ble/FunctionPointerWithContext.h b/ble/FunctionPointerWithContext.h index 41f8687..26447aa 100644 --- a/ble/FunctionPointerWithContext.h +++ b/ble/FunctionPointerWithContext.h @@ -19,8 +19,6 @@ #include - - /** A class for storing and calling a pointer to a static or member void function * which takes a context. */ @@ -107,10 +105,10 @@ private: template static void membercaller(pFunctionPointerWithContext_t self, ContextType context) { if (self->_memberFunctionAndPointer._object) { - T *o = static_cast(self->_memberFunctionAndPointer._object); + T *o = static_cast(self->_memberFunctionAndPointer._object); void (T::*m)(ContextType); memcpy((char*) &m, self->_memberFunctionAndPointer._memberFunction, sizeof(m)); - (o->*m)(context); + (o->*m)(context); } } @@ -122,9 +120,9 @@ private: struct MemberFunctionAndPtr { /* - * forward declaration of a class and a member function to this class. - * Because the compiler doesn't know anything about the forwarded member - * function, it will always use the biggest size and the biggest alignment + * forward declaration of a class and a member function to this class. + * Because the compiler doesn't know anything about the forwarded member + * function, it will always use the biggest size and the biggest alignment * that a member function can take for objects of type UndefinedMemberFunction. */ class UndefinedClass; @@ -133,17 +131,17 @@ private: void* _object; union { char _memberFunction[sizeof(UndefinedMemberFunction)]; - UndefinedMemberFunction _alignment; + UndefinedMemberFunction _alignment; }; }; union { pvoidfcontext_t _function; /**< static function pointer - NULL if none attached */ - /** - * object this pointer and pointer to member - - * _memberFunctionAndPointer._object will be NULL if none attached - */ - MemberFunctionAndPtr _memberFunctionAndPointer; + /** + * object this pointer and pointer to member - + * _memberFunctionAndPointer._object will be NULL if none attached + */ + MemberFunctionAndPtr _memberFunctionAndPointer; }; void (*_caller)(FunctionPointerWithContext*, ContextType); From aa7e4b2da7bb239a61d486466014dbaf4c97075c Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 10:55:00 +0000 Subject: [PATCH 2/6] BLE::init() should also be able to take a tuple. --- ble/BLE.h | 41 +++++++++++++++++++++++++++++++++-------- ble/BLEInstanceBase.h | 3 ++- source/BLE.cpp | 22 +++++++++++++++++++++- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/ble/BLE.h b/ble/BLE.h index 00bce18..18cbc16 100644 --- a/ble/BLE.h +++ b/ble/BLE.h @@ -41,16 +41,28 @@ public: typedef unsigned InstanceID_t; /** The type returned by BLE::getInstanceID(). */ /** - * The function signature for callbacks for initialization completion. + * The context provided to init-completion-callbacks (see init() below). + * * @param ble * A reference to the BLE instance being initialized. * @param error * This captures the result of initialization. It is set to * BLE_ERROR_NONE if initialization completed successfully. Else * the error value is implementation specific. - * */ - typedef void (*InitializationCompleteCallback_t)(BLE &ble, ble_error_t error); + struct InitializationCompleteCallbackContext { + BLE& ble; /* Reference to the BLE object which has been initialized */ + ble_error_t error; /* Error status of the initialization. It is set to BLE_ERROR_NONE initialization completed successfully. */ + }; + + /** + * The signature for function-pointer like callbacks for initialization-completion. + * + * @note There are two versions of init(). In addition to the simple + * function-pointer, init() can also take a tuple as its + * callback target. In case of the latter, the following declaration doesn't apply. + */ + typedef void (*InitializationCompleteCallback_t)(InitializationCompleteCallbackContext *context); /** * Initialize the BLE controller. This should be called before using @@ -72,18 +84,31 @@ public: * @return BLE_ERROR_NONE if the initialization procedure was started * successfully. * - * @note The underlying stack must invoke the initialization completion - * callback in response to init(). In some cases, initialization is - * instantaneous (or blocking); if so, it is acceptable for the stack- - * specific implementation of init() to invoke the completion callback - * directly--i.e. within its own context. + * @note If init() returns BLE_ERROR_NONE, the underlying stack must invoke + * the initialization completion callback at some point. + * + * @note In some cases, initialization is instantaneous (or blocking); if + * so, it is acceptable for the stack-specific implementation of init() + * to invoke the completion callback directly--i.e. within its own + * context. * * @note Nearly all BLE APIs would return * BLE_ERROR_INITIALIZATION_INCOMPLETE if used on an instance before the * corresponding transport is initialized. + * + * @note There are two versions of init(). In addition to the simple + * function-pointer, init() can also take an tuple as its + * callback target. */ ble_error_t init(InitializationCompleteCallback_t callback = NULL); + /** + * An alternate declaration for init(). This one takes an tuple as its + * callback target. + */ + template + ble_error_t init(T *object, void (T::*initCompleteCallback)(InitializationCompleteCallbackContext *context)); + /** * @return true if initialization has completed for the underlying BLE * transport. diff --git a/ble/BLEInstanceBase.h b/ble/BLEInstanceBase.h index e8cfc0d..7949ed4 100644 --- a/ble/BLEInstanceBase.h +++ b/ble/BLEInstanceBase.h @@ -32,7 +32,8 @@ class GattClient; class BLEInstanceBase { public: - virtual ble_error_t init(BLE::InstanceID_t instanceID, BLE::InitializationCompleteCallback_t) = 0; + virtual ble_error_t init(BLE::InstanceID_t instanceID, + FunctionPointerWithContext initCallback) = 0; virtual bool hasInitialized(void) const = 0; virtual ble_error_t shutdown(void) = 0; virtual const char * getVersion(void) = 0; diff --git a/source/BLE.cpp b/source/BLE.cpp index 0328ef9..eb673c1 100644 --- a/source/BLE.cpp +++ b/source/BLE.cpp @@ -22,8 +22,28 @@ #endif ble_error_t -BLE::init(BLE::InitializationCompleteCallback_t callback) +BLE::init(BLE::InitializationCompleteCallback_t initCompleteCallback) { + FunctionPointerWithContextcallback(initCompleteCallback); + ble_error_t err = transport->init(instanceID, callback); + if (err != BLE_ERROR_NONE) { + return err; + } + + /* Platforms enabled for DFU should introduce the DFU Service into + * applications automatically. */ +#if defined(TARGET_OTA_ENABLED) + static DFUService dfu(*this); // defined static so that the object remains alive +#endif // TARGET_OTA_ENABLED + + return BLE_ERROR_NONE; +} + +template +ble_error_t +BLE::init(T *object, void (T::*initCompleteCallback)(InitializationCompleteCallbackContext *)) +{ + FunctionPointerWithContextcallback(object, initCompleteCallback); ble_error_t err = transport->init(instanceID, callback); if (err != BLE_ERROR_NONE) { return err; From 1f30b48a1535afa0260ef3161291219b1e41d353 Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 11:56:46 +0000 Subject: [PATCH 3/6] fix the build error resulting from missing template instantiation in the case tuple was used for BLE::init() --- ble/BLE.h | 17 ++++++++++++++--- source/BLE.cpp | 22 +--------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/ble/BLE.h b/ble/BLE.h index 18cbc16..3c14409 100644 --- a/ble/BLE.h +++ b/ble/BLE.h @@ -22,6 +22,8 @@ #include "GattServer.h" #include "GattClient.h" +#include "ble/FunctionPointerWithContext.h" + #ifdef YOTTA_CFG_MBED_OS #include "mbed-drivers/mbed_error.h" #else @@ -75,7 +77,7 @@ public: * context where ordering is compiler specific and can't be guaranteed--it * is safe to call BLE::init() from within main(). * - * @param callback + * @param initCompleteCallback * A callback for when initialization completes for a BLE * instance. This is an optional parameter, if no callback is * setup the application can still determine the status of @@ -100,14 +102,20 @@ public: * function-pointer, init() can also take an tuple as its * callback target. */ - ble_error_t init(InitializationCompleteCallback_t callback = NULL); + ble_error_t init(InitializationCompleteCallback_t initCompleteCallback = NULL) { + FunctionPointerWithContext callback(initCompleteCallback); + initImplementation(callback); + } /** * An alternate declaration for init(). This one takes an tuple as its * callback target. */ template - ble_error_t init(T *object, void (T::*initCompleteCallback)(InitializationCompleteCallbackContext *context)); + ble_error_t init(T *object, void (T::*initCompleteCallback)(InitializationCompleteCallbackContext *context)) { + FunctionPointerWithContext callback(object, initCompleteCallback); + initImplementation(callback); + } /** * @return true if initialization has completed for the underlying BLE @@ -1412,6 +1420,9 @@ public: return securityManager().onPasskeyDisplay(callback); } +private: + ble_error_t BLE::initImplementation(FunctionPointerWithContext callback); + private: BLE(const BLE&); BLE &operator=(const BLE &); diff --git a/source/BLE.cpp b/source/BLE.cpp index eb673c1..f2e537a 100644 --- a/source/BLE.cpp +++ b/source/BLE.cpp @@ -22,28 +22,8 @@ #endif ble_error_t -BLE::init(BLE::InitializationCompleteCallback_t initCompleteCallback) +BLE::initImplementation(FunctionPointerWithContext callback) { - FunctionPointerWithContextcallback(initCompleteCallback); - ble_error_t err = transport->init(instanceID, callback); - if (err != BLE_ERROR_NONE) { - return err; - } - - /* Platforms enabled for DFU should introduce the DFU Service into - * applications automatically. */ -#if defined(TARGET_OTA_ENABLED) - static DFUService dfu(*this); // defined static so that the object remains alive -#endif // TARGET_OTA_ENABLED - - return BLE_ERROR_NONE; -} - -template -ble_error_t -BLE::init(T *object, void (T::*initCompleteCallback)(InitializationCompleteCallbackContext *)) -{ - FunctionPointerWithContextcallback(object, initCompleteCallback); ble_error_t err = transport->init(instanceID, callback); if (err != BLE_ERROR_NONE) { return err; From 8fab6bf8630e4f5b3407db71cbb4cda01ff27ffc Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 12:29:08 +0000 Subject: [PATCH 4/6] remove extra qualification 'BLE::' on member 'initImplementation' --- ble/BLE.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ble/BLE.h b/ble/BLE.h index 3c14409..5db926a 100644 --- a/ble/BLE.h +++ b/ble/BLE.h @@ -1421,7 +1421,7 @@ public: } private: - ble_error_t BLE::initImplementation(FunctionPointerWithContext callback); + ble_error_t initImplementation(FunctionPointerWithContext callback); private: BLE(const BLE&); From 43e7e93db90a412f761c3ec7bceaf7cf404505ae Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 12:31:49 +0000 Subject: [PATCH 5/6] add a comment header for initImplementation --- ble/BLE.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ble/BLE.h b/ble/BLE.h index 5db926a..6a92ca1 100644 --- a/ble/BLE.h +++ b/ble/BLE.h @@ -1421,6 +1421,12 @@ public: } private: + /** + * Implementation of init() [internal to BLE_API]. + * + * The implementation is separated into a private method because it isn't + * suitable to be included in the header. + */ ble_error_t initImplementation(FunctionPointerWithContext callback); private: From 2d6aade7138b7e9eb55b0c8e5a81baa4d76a15f8 Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 3 Nov 2015 12:33:35 +0000 Subject: [PATCH 6/6] version v2.0.2 --- module.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module.json b/module.json index a55af03..431bd76 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,6 @@ { "name": "ble", - "version": "2.0.1", + "version": "2.0.2", "description": "The BLE module offers a high level abstraction for using Bluetooth Low Energy on multiple platforms.", "keywords": [ "Bluetooth",