diff --git a/source/nRF5xn.cpp b/source/nRF5xn.cpp index 66499b9..ce8ed82 100644 --- a/source/nRF5xn.cpp +++ b/source/nRF5xn.cpp @@ -38,7 +38,7 @@ createBLEInstance(void) return (&deviceInstance); } -nRF5xn::nRF5xn(void) +nRF5xn::nRF5xn(void) : initialized(false), instanceID(BLE::DEFAULT_INSTANCE) { } @@ -72,11 +72,24 @@ const char *nRF5xn::getVersion(void) return versionString; } -ble_error_t nRF5xn::init(void) +ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, BLE::InitializationCompleteCallback_t callback) { + if (initialized) { + if (callback) { + callback(BLE::Instance(instanceID), BLE_ERROR_ALREADY_INITIALIZED); + } + return BLE_ERROR_ALREADY_INITIALIZED; + } + + instanceID = instanceID; + /* ToDo: Clear memory contents, reset the SD, etc. */ btle_init(); + initialized = true; + if (callback) { + callback(BLE::Instance(instanceID), BLE_ERROR_NONE); + } return BLE_ERROR_NONE; } diff --git a/source/nRF5xn.h b/source/nRF5xn.h index 387eb6e..105c0af 100644 --- a/source/nRF5xn.h +++ b/source/nRF5xn.h @@ -17,13 +17,15 @@ #ifndef __NRF51822_H__ #define __NRF51822_H__ -#include "mbed.h" -#include "ble/blecommon.h" #include "ble/BLE.h" +#include "ble/blecommon.h" +#include "ble/BLEInstanceBase.h" + #include "nRF5xGap.h" #include "nRF5xGattServer.h" #include "nRF5xGattClient.h" #include "nRF5xSecurityManager.h" + #include "btle.h" class nRF5xn : public BLEInstanceBase @@ -32,7 +34,10 @@ public: nRF5xn(void); virtual ~nRF5xn(void); - virtual ble_error_t init(void); + virtual ble_error_t init(BLE::InstanceID_t instanceID, BLE::InitializationCompleteCallback_t); + virtual bool hasInitialized(void) const { + return initialized; + } virtual ble_error_t shutdown(void); virtual const char *getVersion(void); @@ -58,6 +63,10 @@ public: return nRF5xSecurityManager::getInstance(); } virtual void waitForEvent(void); + +private: + bool initialized; + BLE::InstanceID_t instanceID; }; #endif