nrf51822/source/nRF5xn.cpp

213 lines
5.8 KiB
C++
Raw Normal View History

2014-07-30 10:36:32 +00:00
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef YOTTA_CFG_MBED_OS
#include "mbed-drivers/mbed.h"
#else
#include "mbed.h"
#endif
#include "nRF5xn.h"
#include "ble/blecommon.h"
2014-07-30 10:36:32 +00:00
#include "nrf_soc.h"
#include "btle/btle.h"
2014-08-12 10:34:57 +00:00
#include "nrf_delay.h"
2014-07-30 10:36:32 +00:00
extern "C" {
#include "softdevice_handler.h"
}
2014-07-30 10:36:32 +00:00
/**
2015-06-17 08:25:18 +00:00
* The singleton which represents the nRF51822 transport for the BLE.
2014-07-30 10:36:32 +00:00
*/
static nRF5xn *deviceInstance = NULL;
2014-07-30 10:36:32 +00:00
/**
* BLE-API requires an implementation of the following function in order to
* obtain its transport handle.
*/
2015-06-17 08:25:18 +00:00
BLEInstanceBase *
createBLEInstance(void)
2014-07-30 10:36:32 +00:00
{
return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
}
nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId)
{
if (deviceInstance == NULL)
deviceInstance = new nRF5xn();
return *deviceInstance;
2014-07-30 10:36:32 +00:00
}
nRF5xn::nRF5xn(void) :
initialized(false),
instanceID(BLE::DEFAULT_INSTANCE),
gapInstance(),
gattServerInstance(NULL),
gattClientInstance(NULL),
securityManagerInstance(NULL)
2014-07-30 10:36:32 +00:00
{
}
nRF5xn::~nRF5xn(void)
2014-07-30 10:36:32 +00:00
{
}
const char *nRF5xn::getVersion(void)
2014-07-30 10:36:32 +00:00
{
if (!initialized) {
return "INITIALIZATION_INCOMPLETE";
}
static char versionString[32];
2014-07-30 10:36:32 +00:00
static bool versionFetched = false;
if (!versionFetched) {
ble_version_t version;
if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) {
switch (version.version_number) {
case 0x07:
case 0x08:
snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number);
break;
default:
snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number);
break;
}
2014-07-30 10:36:32 +00:00
versionFetched = true;
} else {
strncpy(versionString, "unknown", sizeof(versionString));
}
}
return versionString;
}
2015-12-21 09:30:01 +00:00
/**************************************************************************/
/*!
@brief Initialize the BLE stack.
@returns ble_error_t
@retval BLE_ERROR_NONE if everything executed properly and
BLE_ERROR_ALREADY_INITIALIZED if the stack has already
2015-12-21 09:35:49 +00:00
been initialized (possibly through a call to nRF5xn::init()).
2015-12-21 09:30:01 +00:00
BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization
of the internal stack (SoftDevice) failed.
*/
/**************************************************************************/
ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback)
2014-07-30 10:36:32 +00:00
{
if (initialized) {
BLE::InitializationCompleteCallbackContext context = {
BLE::Instance(instanceID),
BLE_ERROR_ALREADY_INITIALIZED
};
callback.call(&context);
return BLE_ERROR_ALREADY_INITIALIZED;
}
instanceID = instanceID;
2014-07-30 10:36:32 +00:00
/* ToDo: Clear memory contents, reset the SD, etc. */
2015-12-21 09:30:01 +00:00
if (btle_init() != ERROR_NONE) {
return BLE_ERROR_INTERNAL_STACK_FAILURE;
}
2014-07-30 10:36:32 +00:00
initialized = true;
BLE::InitializationCompleteCallbackContext context = {
BLE::Instance(instanceID),
BLE_ERROR_NONE
};
callback.call(&context);
2014-07-30 10:36:32 +00:00
return BLE_ERROR_NONE;
}
Improve shutdown to clear BLE API and not just SD Improve the shutdown functionality, such that a call to ble.shutdown() from the user application clears the API and nRF5x state and NOT only the SoftDevice. To achieve this the following changes are introduced: * Add a protected member cleanup() to nRF5xGap, nRF5xGattClient, nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery. * Modify the shutdown() implementation in nRF5xn such that it also calls the static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h, GattClient.h and GattServer.h. * Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager classes so that they dynamically create their respective objects only if needed. Previously the GattClient, GattServer and SecurityManager objects were declared as static, which means that they were always present even though they were not always needed. This increases memory consumption unnecessarily. Furthermore, pointers to the object instances are stored in static members of the classes as specified by the BLE API base classes. This ensures that calls to shutdown do not require calls to getInstance() functions that would otherwise result in undesired memory allocations. * nRF5xGap object is always needed, so this remains allocated statically. But the reference in Gap is pointed to this object. The shutdown procedure is as follows: 1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown() 1. The SoftDevice is shutdown 1. The static members of Gap.h, SecurityManager.h, GattClient.h and GattServer.h are called to clean up their own state. If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is returned.
2015-12-11 17:37:58 +00:00
/**************************************************************************/
/*!
@brief Purge the BLE stack of GATT and GAP state.
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@note When using S110, GattClient::shutdown() will not be called
since Gatt client features are not supported.
*/
/**************************************************************************/
ble_error_t nRF5xn::shutdown(void)
{
if (!initialized) {
return BLE_ERROR_INITIALIZATION_INCOMPLETE;
}
/*
* Shutdown the SoftDevice first. This is because we need to disable all
* interrupts. Otherwise if we clear the BLE API and glue code first there
* will be many NULL references and no config information which could lead
* to errors if the shutdown process is interrupted.
*/
2015-12-16 10:19:22 +00:00
if (softdevice_handler_sd_disable() != NRF_SUCCESS) {
return BLE_STACK_BUSY;
}
Improve shutdown to clear BLE API and not just SD Improve the shutdown functionality, such that a call to ble.shutdown() from the user application clears the API and nRF5x state and NOT only the SoftDevice. To achieve this the following changes are introduced: * Add a protected member cleanup() to nRF5xGap, nRF5xGattClient, nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery. * Modify the shutdown() implementation in nRF5xn such that it also calls the static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h, GattClient.h and GattServer.h. * Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager classes so that they dynamically create their respective objects only if needed. Previously the GattClient, GattServer and SecurityManager objects were declared as static, which means that they were always present even though they were not always needed. This increases memory consumption unnecessarily. Furthermore, pointers to the object instances are stored in static members of the classes as specified by the BLE API base classes. This ensures that calls to shutdown do not require calls to getInstance() functions that would otherwise result in undesired memory allocations. * nRF5xGap object is always needed, so this remains allocated statically. But the reference in Gap is pointed to this object. The shutdown procedure is as follows: 1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown() 1. The SoftDevice is shutdown 1. The static members of Gap.h, SecurityManager.h, GattClient.h and GattServer.h are called to clean up their own state. If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is returned.
2015-12-11 17:37:58 +00:00
/* Shutdown the BLE API and nRF51 glue code */
ble_error_t error;
if (gattServerInstance != NULL) {
error = gattServerInstance->reset();
if (error != BLE_ERROR_NONE) {
return error;
}
}
if (securityManagerInstance != NULL) {
error = securityManagerInstance->reset();
if (error != BLE_ERROR_NONE) {
return error;
}
}
/* S110 does not support BLE client features, nothing to reset. */
Improve shutdown to clear BLE API and not just SD Improve the shutdown functionality, such that a call to ble.shutdown() from the user application clears the API and nRF5x state and NOT only the SoftDevice. To achieve this the following changes are introduced: * Add a protected member cleanup() to nRF5xGap, nRF5xGattClient, nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery. * Modify the shutdown() implementation in nRF5xn such that it also calls the static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h, GattClient.h and GattServer.h. * Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager classes so that they dynamically create their respective objects only if needed. Previously the GattClient, GattServer and SecurityManager objects were declared as static, which means that they were always present even though they were not always needed. This increases memory consumption unnecessarily. Furthermore, pointers to the object instances are stored in static members of the classes as specified by the BLE API base classes. This ensures that calls to shutdown do not require calls to getInstance() functions that would otherwise result in undesired memory allocations. * nRF5xGap object is always needed, so this remains allocated statically. But the reference in Gap is pointed to this object. The shutdown procedure is as follows: 1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown() 1. The SoftDevice is shutdown 1. The static members of Gap.h, SecurityManager.h, GattClient.h and GattServer.h are called to clean up their own state. If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is returned.
2015-12-11 17:37:58 +00:00
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
if (gattClientInstance != NULL) {
error = gattClientInstance->reset();
if (error != BLE_ERROR_NONE) {
return error;
}
}
Improve shutdown to clear BLE API and not just SD Improve the shutdown functionality, such that a call to ble.shutdown() from the user application clears the API and nRF5x state and NOT only the SoftDevice. To achieve this the following changes are introduced: * Add a protected member cleanup() to nRF5xGap, nRF5xGattClient, nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery. * Modify the shutdown() implementation in nRF5xn such that it also calls the static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h, GattClient.h and GattServer.h. * Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager classes so that they dynamically create their respective objects only if needed. Previously the GattClient, GattServer and SecurityManager objects were declared as static, which means that they were always present even though they were not always needed. This increases memory consumption unnecessarily. Furthermore, pointers to the object instances are stored in static members of the classes as specified by the BLE API base classes. This ensures that calls to shutdown do not require calls to getInstance() functions that would otherwise result in undesired memory allocations. * nRF5xGap object is always needed, so this remains allocated statically. But the reference in Gap is pointed to this object. The shutdown procedure is as follows: 1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown() 1. The SoftDevice is shutdown 1. The static members of Gap.h, SecurityManager.h, GattClient.h and GattServer.h are called to clean up their own state. If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is returned.
2015-12-11 17:37:58 +00:00
#endif
2015-12-22 15:51:34 +00:00
/* Gap instance is always present */
error = gapInstance.reset();
if (error != BLE_ERROR_NONE) {
return error;
Improve shutdown to clear BLE API and not just SD Improve the shutdown functionality, such that a call to ble.shutdown() from the user application clears the API and nRF5x state and NOT only the SoftDevice. To achieve this the following changes are introduced: * Add a protected member cleanup() to nRF5xGap, nRF5xGattClient, nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery. * Modify the shutdown() implementation in nRF5xn such that it also calls the static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h, GattClient.h and GattServer.h. * Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager classes so that they dynamically create their respective objects only if needed. Previously the GattClient, GattServer and SecurityManager objects were declared as static, which means that they were always present even though they were not always needed. This increases memory consumption unnecessarily. Furthermore, pointers to the object instances are stored in static members of the classes as specified by the BLE API base classes. This ensures that calls to shutdown do not require calls to getInstance() functions that would otherwise result in undesired memory allocations. * nRF5xGap object is always needed, so this remains allocated statically. But the reference in Gap is pointed to this object. The shutdown procedure is as follows: 1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown() 1. The SoftDevice is shutdown 1. The static members of Gap.h, SecurityManager.h, GattClient.h and GattServer.h are called to clean up their own state. If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is returned.
2015-12-11 17:37:58 +00:00
}
initialized = false;
return BLE_ERROR_NONE;
}
2014-07-30 10:36:32 +00:00
void
nRF5xn::waitForEvent(void)
2014-07-30 10:36:32 +00:00
{
sd_app_evt_wait();
}