No changes
Fork of nRF51822 by
source/nRF5xn.cpp@565:cf03471a4ec4, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:18 2016 +0000
- Revision:
- 565:cf03471a4ec4
- Parent:
- 563:9c4b96f7be8d
- Child:
- 566:e425ad9e5d6e
Synchronized with git rev 0bcc2e96
Author: Andres Amaya Garcia
Modify shutdown due to BLE API change
The module is updated to comply with the changes to BLE API regarding correct
shutdown functionality. The following changes are introduced to ble-nrf51822:
* Calls to the old static function shutdown in Gap, GattClient, GattServer and
SecurityManager are removed.
* The cleanup function in Gap, GattClient, GattServer and SecurityManager is
renamed to `reset()` and made public.
* The static references inside nRF5xGap, nRF5xGattClient, nRF5xGattServer and
nRF5xSecurityManager to objects of their own class are moved to nRF5xn.
* The static getInstance accessors in nRF5xGap, nRF5xGattClient,
nRF5xGattServer and nRF5xSecurityManager are removed and their functionality is
moved to the implemented virtual accessors in nRF5xn i.e. getGap(),
getGattClient, etc.
* A static function Instance is added to nRF5xn class to make the transport
object accessible across the module.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 541:884f95bf5351 | 1 | /* mbed Microcontroller Library |
vcoubard | 541:884f95bf5351 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 541:884f95bf5351 | 3 | * |
vcoubard | 541:884f95bf5351 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 541:884f95bf5351 | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 541:884f95bf5351 | 6 | * You may obtain a copy of the License at |
vcoubard | 541:884f95bf5351 | 7 | * |
vcoubard | 541:884f95bf5351 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 541:884f95bf5351 | 9 | * |
vcoubard | 541:884f95bf5351 | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 541:884f95bf5351 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 541:884f95bf5351 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 541:884f95bf5351 | 13 | * See the License for the specific language governing permissions and |
vcoubard | 541:884f95bf5351 | 14 | * limitations under the License. |
vcoubard | 541:884f95bf5351 | 15 | */ |
vcoubard | 541:884f95bf5351 | 16 | |
vcoubard | 541:884f95bf5351 | 17 | #include "mbed.h" |
vcoubard | 541:884f95bf5351 | 18 | #include "nRF5xn.h" |
vcoubard | 541:884f95bf5351 | 19 | #include "ble/blecommon.h" |
vcoubard | 541:884f95bf5351 | 20 | #include "nrf_soc.h" |
vcoubard | 541:884f95bf5351 | 21 | |
vcoubard | 541:884f95bf5351 | 22 | #include "btle/btle.h" |
vcoubard | 541:884f95bf5351 | 23 | #include "nrf_delay.h" |
vcoubard | 541:884f95bf5351 | 24 | |
vcoubard | 549:3f782c64d014 | 25 | extern "C" { |
vcoubard | 541:884f95bf5351 | 26 | #include "softdevice_handler.h" |
vcoubard | 549:3f782c64d014 | 27 | } |
vcoubard | 541:884f95bf5351 | 28 | |
vcoubard | 541:884f95bf5351 | 29 | /** |
vcoubard | 541:884f95bf5351 | 30 | * The singleton which represents the nRF51822 transport for the BLE. |
vcoubard | 541:884f95bf5351 | 31 | */ |
vcoubard | 541:884f95bf5351 | 32 | static nRF5xn deviceInstance; |
vcoubard | 541:884f95bf5351 | 33 | |
vcoubard | 541:884f95bf5351 | 34 | /** |
vcoubard | 565:cf03471a4ec4 | 35 | * The singleton for nRF5xGap. This has been kept static because it is |
vcoubard | 565:cf03471a4ec4 | 36 | * always needed for any application that uses BLE. |
vcoubard | 565:cf03471a4ec4 | 37 | */ |
vcoubard | 565:cf03471a4ec4 | 38 | nRF5xGap nRF5xn::_gapInstance; |
vcoubard | 565:cf03471a4ec4 | 39 | |
vcoubard | 565:cf03471a4ec4 | 40 | /** |
vcoubard | 541:884f95bf5351 | 41 | * BLE-API requires an implementation of the following function in order to |
vcoubard | 541:884f95bf5351 | 42 | * obtain its transport handle. |
vcoubard | 541:884f95bf5351 | 43 | */ |
vcoubard | 541:884f95bf5351 | 44 | BLEInstanceBase * |
vcoubard | 541:884f95bf5351 | 45 | createBLEInstance(void) |
vcoubard | 541:884f95bf5351 | 46 | { |
vcoubard | 565:cf03471a4ec4 | 47 | return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE); |
vcoubard | 565:cf03471a4ec4 | 48 | } |
vcoubard | 565:cf03471a4ec4 | 49 | |
vcoubard | 565:cf03471a4ec4 | 50 | nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId) |
vcoubard | 565:cf03471a4ec4 | 51 | { |
vcoubard | 565:cf03471a4ec4 | 52 | return deviceInstance; |
vcoubard | 541:884f95bf5351 | 53 | } |
vcoubard | 541:884f95bf5351 | 54 | |
vcoubard | 565:cf03471a4ec4 | 55 | nRF5xn::nRF5xn(void) : |
vcoubard | 565:cf03471a4ec4 | 56 | initialized(false), |
vcoubard | 565:cf03471a4ec4 | 57 | instanceID(BLE::DEFAULT_INSTANCE), |
vcoubard | 565:cf03471a4ec4 | 58 | gapInstance(NULL), |
vcoubard | 565:cf03471a4ec4 | 59 | gattServerInstance(NULL), |
vcoubard | 565:cf03471a4ec4 | 60 | gattClientInstance(NULL), |
vcoubard | 565:cf03471a4ec4 | 61 | securityManagerInstance(NULL) |
vcoubard | 541:884f95bf5351 | 62 | { |
vcoubard | 541:884f95bf5351 | 63 | } |
vcoubard | 541:884f95bf5351 | 64 | |
vcoubard | 541:884f95bf5351 | 65 | nRF5xn::~nRF5xn(void) |
vcoubard | 541:884f95bf5351 | 66 | { |
vcoubard | 541:884f95bf5351 | 67 | } |
vcoubard | 541:884f95bf5351 | 68 | |
vcoubard | 541:884f95bf5351 | 69 | const char *nRF5xn::getVersion(void) |
vcoubard | 541:884f95bf5351 | 70 | { |
vcoubard | 541:884f95bf5351 | 71 | if (!initialized) { |
vcoubard | 541:884f95bf5351 | 72 | return "INITIALIZATION_INCOMPLETE"; |
vcoubard | 541:884f95bf5351 | 73 | } |
vcoubard | 541:884f95bf5351 | 74 | |
vcoubard | 541:884f95bf5351 | 75 | static char versionString[32]; |
vcoubard | 541:884f95bf5351 | 76 | static bool versionFetched = false; |
vcoubard | 541:884f95bf5351 | 77 | |
vcoubard | 541:884f95bf5351 | 78 | if (!versionFetched) { |
vcoubard | 541:884f95bf5351 | 79 | ble_version_t version; |
vcoubard | 541:884f95bf5351 | 80 | if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) { |
vcoubard | 541:884f95bf5351 | 81 | switch (version.version_number) { |
vcoubard | 541:884f95bf5351 | 82 | case 0x07: |
vcoubard | 541:884f95bf5351 | 83 | case 0x08: |
vcoubard | 541:884f95bf5351 | 84 | snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number); |
vcoubard | 541:884f95bf5351 | 85 | break; |
vcoubard | 541:884f95bf5351 | 86 | default: |
vcoubard | 541:884f95bf5351 | 87 | snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number); |
vcoubard | 541:884f95bf5351 | 88 | break; |
vcoubard | 541:884f95bf5351 | 89 | } |
vcoubard | 541:884f95bf5351 | 90 | versionFetched = true; |
vcoubard | 541:884f95bf5351 | 91 | } else { |
vcoubard | 541:884f95bf5351 | 92 | strncpy(versionString, "unknown", sizeof(versionString)); |
vcoubard | 541:884f95bf5351 | 93 | } |
vcoubard | 541:884f95bf5351 | 94 | } |
vcoubard | 541:884f95bf5351 | 95 | |
vcoubard | 541:884f95bf5351 | 96 | return versionString; |
vcoubard | 541:884f95bf5351 | 97 | } |
vcoubard | 541:884f95bf5351 | 98 | |
vcoubard | 541:884f95bf5351 | 99 | ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) |
vcoubard | 541:884f95bf5351 | 100 | { |
vcoubard | 541:884f95bf5351 | 101 | if (initialized) { |
vcoubard | 541:884f95bf5351 | 102 | BLE::InitializationCompleteCallbackContext context = { |
vcoubard | 541:884f95bf5351 | 103 | BLE::Instance(instanceID), |
vcoubard | 541:884f95bf5351 | 104 | BLE_ERROR_ALREADY_INITIALIZED |
vcoubard | 541:884f95bf5351 | 105 | }; |
vcoubard | 541:884f95bf5351 | 106 | callback.call(&context); |
vcoubard | 541:884f95bf5351 | 107 | return BLE_ERROR_ALREADY_INITIALIZED; |
vcoubard | 541:884f95bf5351 | 108 | } |
vcoubard | 541:884f95bf5351 | 109 | |
vcoubard | 541:884f95bf5351 | 110 | instanceID = instanceID; |
vcoubard | 541:884f95bf5351 | 111 | |
vcoubard | 541:884f95bf5351 | 112 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
vcoubard | 541:884f95bf5351 | 113 | btle_init(); |
vcoubard | 541:884f95bf5351 | 114 | |
vcoubard | 541:884f95bf5351 | 115 | initialized = true; |
vcoubard | 541:884f95bf5351 | 116 | BLE::InitializationCompleteCallbackContext context = { |
vcoubard | 541:884f95bf5351 | 117 | BLE::Instance(instanceID), |
vcoubard | 541:884f95bf5351 | 118 | BLE_ERROR_NONE |
vcoubard | 541:884f95bf5351 | 119 | }; |
vcoubard | 541:884f95bf5351 | 120 | callback.call(&context); |
vcoubard | 541:884f95bf5351 | 121 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 122 | } |
vcoubard | 541:884f95bf5351 | 123 | |
vcoubard | 565:cf03471a4ec4 | 124 | /**************************************************************************/ |
vcoubard | 565:cf03471a4ec4 | 125 | /*! |
vcoubard | 565:cf03471a4ec4 | 126 | @brief Purge the BLE stack of GATT and GAP state. |
vcoubard | 565:cf03471a4ec4 | 127 | |
vcoubard | 565:cf03471a4ec4 | 128 | @returns ble_error_t |
vcoubard | 565:cf03471a4ec4 | 129 | |
vcoubard | 565:cf03471a4ec4 | 130 | @retval BLE_ERROR_NONE |
vcoubard | 565:cf03471a4ec4 | 131 | Everything executed properly |
vcoubard | 565:cf03471a4ec4 | 132 | |
vcoubard | 565:cf03471a4ec4 | 133 | @note When using S110, GattClient::shutdown() will not be called |
vcoubard | 565:cf03471a4ec4 | 134 | since Gatt client features are not supported. |
vcoubard | 565:cf03471a4ec4 | 135 | */ |
vcoubard | 565:cf03471a4ec4 | 136 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 137 | ble_error_t nRF5xn::shutdown(void) |
vcoubard | 541:884f95bf5351 | 138 | { |
vcoubard | 541:884f95bf5351 | 139 | if (!initialized) { |
vcoubard | 541:884f95bf5351 | 140 | return BLE_ERROR_INITIALIZATION_INCOMPLETE; |
vcoubard | 541:884f95bf5351 | 141 | } |
vcoubard | 541:884f95bf5351 | 142 | |
vcoubard | 565:cf03471a4ec4 | 143 | /* |
vcoubard | 565:cf03471a4ec4 | 144 | * Shutdown the SoftDevice first. This is because we need to disable all |
vcoubard | 565:cf03471a4ec4 | 145 | * interrupts. Otherwise if we clear the BLE API and glue code first there |
vcoubard | 565:cf03471a4ec4 | 146 | * will be many NULL references and no config information which could lead |
vcoubard | 565:cf03471a4ec4 | 147 | * to errors if the shutdown process is interrupted. |
vcoubard | 565:cf03471a4ec4 | 148 | */ |
vcoubard | 541:884f95bf5351 | 149 | if(softdevice_handler_sd_disable() != NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 150 | return BLE_STACK_BUSY; |
vcoubard | 541:884f95bf5351 | 151 | } |
vcoubard | 541:884f95bf5351 | 152 | |
vcoubard | 565:cf03471a4ec4 | 153 | |
vcoubard | 565:cf03471a4ec4 | 154 | /* Shutdown the BLE API and nRF51 glue code */ |
vcoubard | 565:cf03471a4ec4 | 155 | if (gattServerInstance != NULL && |
vcoubard | 565:cf03471a4ec4 | 156 | gattServerInstance->reset() != BLE_ERROR_NONE) { |
vcoubard | 565:cf03471a4ec4 | 157 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 565:cf03471a4ec4 | 158 | } |
vcoubard | 565:cf03471a4ec4 | 159 | |
vcoubard | 565:cf03471a4ec4 | 160 | if (securityManagerInstance != NULL && |
vcoubard | 565:cf03471a4ec4 | 161 | securityManagerInstance->reset() != BLE_ERROR_NONE) { |
vcoubard | 565:cf03471a4ec4 | 162 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 565:cf03471a4ec4 | 163 | } |
vcoubard | 565:cf03471a4ec4 | 164 | |
vcoubard | 565:cf03471a4ec4 | 165 | /* S110 does not support BLE client features, nothing to reset. */ |
vcoubard | 565:cf03471a4ec4 | 166 | #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
vcoubard | 565:cf03471a4ec4 | 167 | if (gattClientInstance != NULL && |
vcoubard | 565:cf03471a4ec4 | 168 | gattClientInstance->reset() != BLE_ERROR_NONE) { |
vcoubard | 565:cf03471a4ec4 | 169 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 565:cf03471a4ec4 | 170 | } |
vcoubard | 565:cf03471a4ec4 | 171 | #endif |
vcoubard | 565:cf03471a4ec4 | 172 | |
vcoubard | 565:cf03471a4ec4 | 173 | if (gapInstance != NULL && |
vcoubard | 565:cf03471a4ec4 | 174 | gapInstance->reset() != BLE_ERROR_NONE) { |
vcoubard | 565:cf03471a4ec4 | 175 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 565:cf03471a4ec4 | 176 | } |
vcoubard | 565:cf03471a4ec4 | 177 | |
vcoubard | 541:884f95bf5351 | 178 | initialized = false; |
vcoubard | 541:884f95bf5351 | 179 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 180 | } |
vcoubard | 541:884f95bf5351 | 181 | |
vcoubard | 541:884f95bf5351 | 182 | void |
vcoubard | 541:884f95bf5351 | 183 | nRF5xn::waitForEvent(void) |
vcoubard | 541:884f95bf5351 | 184 | { |
vcoubard | 541:884f95bf5351 | 185 | sd_app_evt_wait(); |
rgrover1 | 388:db85a09c27ef | 186 | } |