library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Committer:
Siemen
Date:
Wed Nov 02 14:11:43 2016 +0000
Revision:
638:6307ad2949a7
Parent:
589:f27ac458e152
bug fixed advertisement interval indication

Who changed what in which revision?

UserRevisionLine numberNew 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 578:78f69f1be114 17 #ifdef YOTTA_CFG_MBED_OS
vcoubard 578:78f69f1be114 18 #include "mbed-drivers/mbed.h"
vcoubard 578:78f69f1be114 19 #else
vcoubard 578:78f69f1be114 20 #include "mbed.h"
vcoubard 578:78f69f1be114 21 #endif
vcoubard 541:884f95bf5351 22 #include "nRF5xn.h"
vcoubard 541:884f95bf5351 23 #include "ble/blecommon.h"
vcoubard 541:884f95bf5351 24 #include "nrf_soc.h"
vcoubard 541:884f95bf5351 25
vcoubard 541:884f95bf5351 26 #include "btle/btle.h"
vcoubard 541:884f95bf5351 27 #include "nrf_delay.h"
vcoubard 541:884f95bf5351 28
vcoubard 549:3f782c64d014 29 extern "C" {
vcoubard 541:884f95bf5351 30 #include "softdevice_handler.h"
vcoubard 549:3f782c64d014 31 }
vcoubard 541:884f95bf5351 32
vcoubard 541:884f95bf5351 33 /**
vcoubard 541:884f95bf5351 34 * The singleton which represents the nRF51822 transport for the BLE.
vcoubard 541:884f95bf5351 35 */
vcoubard 541:884f95bf5351 36 static nRF5xn deviceInstance;
vcoubard 541:884f95bf5351 37
vcoubard 541:884f95bf5351 38 /**
vcoubard 541:884f95bf5351 39 * BLE-API requires an implementation of the following function in order to
vcoubard 541:884f95bf5351 40 * obtain its transport handle.
vcoubard 541:884f95bf5351 41 */
vcoubard 541:884f95bf5351 42 BLEInstanceBase *
vcoubard 541:884f95bf5351 43 createBLEInstance(void)
vcoubard 541:884f95bf5351 44 {
vcoubard 575:7023a8204a1b 45 return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
vcoubard 575:7023a8204a1b 46 }
vcoubard 575:7023a8204a1b 47
vcoubard 575:7023a8204a1b 48 nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId)
vcoubard 575:7023a8204a1b 49 {
vcoubard 575:7023a8204a1b 50 return deviceInstance;
vcoubard 541:884f95bf5351 51 }
vcoubard 541:884f95bf5351 52
vcoubard 575:7023a8204a1b 53 nRF5xn::nRF5xn(void) :
vcoubard 575:7023a8204a1b 54 initialized(false),
vcoubard 575:7023a8204a1b 55 instanceID(BLE::DEFAULT_INSTANCE),
vcoubard 588:65d256258533 56 gapInstance(),
vcoubard 575:7023a8204a1b 57 gattServerInstance(NULL),
vcoubard 575:7023a8204a1b 58 gattClientInstance(NULL),
vcoubard 575:7023a8204a1b 59 securityManagerInstance(NULL)
vcoubard 541:884f95bf5351 60 {
vcoubard 541:884f95bf5351 61 }
vcoubard 541:884f95bf5351 62
vcoubard 541:884f95bf5351 63 nRF5xn::~nRF5xn(void)
vcoubard 541:884f95bf5351 64 {
vcoubard 541:884f95bf5351 65 }
vcoubard 541:884f95bf5351 66
vcoubard 541:884f95bf5351 67 const char *nRF5xn::getVersion(void)
vcoubard 541:884f95bf5351 68 {
vcoubard 541:884f95bf5351 69 if (!initialized) {
vcoubard 541:884f95bf5351 70 return "INITIALIZATION_INCOMPLETE";
vcoubard 541:884f95bf5351 71 }
vcoubard 541:884f95bf5351 72
vcoubard 541:884f95bf5351 73 static char versionString[32];
vcoubard 541:884f95bf5351 74 static bool versionFetched = false;
vcoubard 541:884f95bf5351 75
vcoubard 541:884f95bf5351 76 if (!versionFetched) {
vcoubard 541:884f95bf5351 77 ble_version_t version;
vcoubard 541:884f95bf5351 78 if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) {
vcoubard 541:884f95bf5351 79 switch (version.version_number) {
vcoubard 541:884f95bf5351 80 case 0x07:
vcoubard 541:884f95bf5351 81 case 0x08:
vcoubard 541:884f95bf5351 82 snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number);
vcoubard 541:884f95bf5351 83 break;
vcoubard 541:884f95bf5351 84 default:
vcoubard 541:884f95bf5351 85 snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number);
vcoubard 541:884f95bf5351 86 break;
vcoubard 541:884f95bf5351 87 }
vcoubard 541:884f95bf5351 88 versionFetched = true;
vcoubard 541:884f95bf5351 89 } else {
vcoubard 541:884f95bf5351 90 strncpy(versionString, "unknown", sizeof(versionString));
vcoubard 541:884f95bf5351 91 }
vcoubard 541:884f95bf5351 92 }
vcoubard 541:884f95bf5351 93
vcoubard 541:884f95bf5351 94 return versionString;
vcoubard 541:884f95bf5351 95 }
vcoubard 541:884f95bf5351 96
vcoubard 586:533fd7fdb0fe 97 /**************************************************************************/
vcoubard 586:533fd7fdb0fe 98 /*!
vcoubard 586:533fd7fdb0fe 99 @brief Initialize the BLE stack.
vcoubard 586:533fd7fdb0fe 100
vcoubard 586:533fd7fdb0fe 101 @returns ble_error_t
vcoubard 586:533fd7fdb0fe 102
vcoubard 586:533fd7fdb0fe 103 @retval BLE_ERROR_NONE if everything executed properly and
vcoubard 586:533fd7fdb0fe 104 BLE_ERROR_ALREADY_INITIALIZED if the stack has already
vcoubard 587:596071444447 105 been initialized (possibly through a call to nRF5xn::init()).
vcoubard 586:533fd7fdb0fe 106 BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization
vcoubard 586:533fd7fdb0fe 107 of the internal stack (SoftDevice) failed.
vcoubard 586:533fd7fdb0fe 108
vcoubard 586:533fd7fdb0fe 109 */
vcoubard 586:533fd7fdb0fe 110 /**************************************************************************/
vcoubard 541:884f95bf5351 111 ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback)
vcoubard 541:884f95bf5351 112 {
vcoubard 541:884f95bf5351 113 if (initialized) {
vcoubard 541:884f95bf5351 114 BLE::InitializationCompleteCallbackContext context = {
vcoubard 541:884f95bf5351 115 BLE::Instance(instanceID),
vcoubard 541:884f95bf5351 116 BLE_ERROR_ALREADY_INITIALIZED
vcoubard 541:884f95bf5351 117 };
vcoubard 541:884f95bf5351 118 callback.call(&context);
vcoubard 541:884f95bf5351 119 return BLE_ERROR_ALREADY_INITIALIZED;
vcoubard 541:884f95bf5351 120 }
vcoubard 541:884f95bf5351 121
vcoubard 541:884f95bf5351 122 instanceID = instanceID;
vcoubard 541:884f95bf5351 123
vcoubard 541:884f95bf5351 124 /* ToDo: Clear memory contents, reset the SD, etc. */
vcoubard 586:533fd7fdb0fe 125 if (btle_init() != ERROR_NONE) {
vcoubard 586:533fd7fdb0fe 126 return BLE_ERROR_INTERNAL_STACK_FAILURE;
vcoubard 586:533fd7fdb0fe 127 }
vcoubard 541:884f95bf5351 128
vcoubard 541:884f95bf5351 129 initialized = true;
vcoubard 541:884f95bf5351 130 BLE::InitializationCompleteCallbackContext context = {
vcoubard 541:884f95bf5351 131 BLE::Instance(instanceID),
vcoubard 541:884f95bf5351 132 BLE_ERROR_NONE
vcoubard 541:884f95bf5351 133 };
vcoubard 541:884f95bf5351 134 callback.call(&context);
vcoubard 541:884f95bf5351 135 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 136 }
vcoubard 541:884f95bf5351 137
vcoubard 575:7023a8204a1b 138 /**************************************************************************/
vcoubard 575:7023a8204a1b 139 /*!
vcoubard 575:7023a8204a1b 140 @brief Purge the BLE stack of GATT and GAP state.
vcoubard 575:7023a8204a1b 141
vcoubard 575:7023a8204a1b 142 @returns ble_error_t
vcoubard 575:7023a8204a1b 143
vcoubard 575:7023a8204a1b 144 @retval BLE_ERROR_NONE
vcoubard 575:7023a8204a1b 145 Everything executed properly
vcoubard 575:7023a8204a1b 146
vcoubard 575:7023a8204a1b 147 @note When using S110, GattClient::shutdown() will not be called
vcoubard 575:7023a8204a1b 148 since Gatt client features are not supported.
vcoubard 575:7023a8204a1b 149 */
vcoubard 575:7023a8204a1b 150 /**************************************************************************/
vcoubard 541:884f95bf5351 151 ble_error_t nRF5xn::shutdown(void)
vcoubard 541:884f95bf5351 152 {
vcoubard 541:884f95bf5351 153 if (!initialized) {
vcoubard 541:884f95bf5351 154 return BLE_ERROR_INITIALIZATION_INCOMPLETE;
vcoubard 541:884f95bf5351 155 }
vcoubard 541:884f95bf5351 156
vcoubard 575:7023a8204a1b 157 /*
vcoubard 575:7023a8204a1b 158 * Shutdown the SoftDevice first. This is because we need to disable all
vcoubard 575:7023a8204a1b 159 * interrupts. Otherwise if we clear the BLE API and glue code first there
vcoubard 575:7023a8204a1b 160 * will be many NULL references and no config information which could lead
vcoubard 575:7023a8204a1b 161 * to errors if the shutdown process is interrupted.
vcoubard 575:7023a8204a1b 162 */
vcoubard 582:37fd3190598e 163 if (softdevice_handler_sd_disable() != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 164 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 165 }
vcoubard 541:884f95bf5351 166
vcoubard 575:7023a8204a1b 167
vcoubard 575:7023a8204a1b 168 /* Shutdown the BLE API and nRF51 glue code */
vcoubard 575:7023a8204a1b 169 ble_error_t error;
vcoubard 575:7023a8204a1b 170
vcoubard 575:7023a8204a1b 171 if (gattServerInstance != NULL) {
vcoubard 575:7023a8204a1b 172 error = gattServerInstance->reset();
vcoubard 575:7023a8204a1b 173 if (error != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 174 return error;
vcoubard 575:7023a8204a1b 175 }
vcoubard 575:7023a8204a1b 176 }
vcoubard 575:7023a8204a1b 177
vcoubard 575:7023a8204a1b 178 if (securityManagerInstance != NULL) {
vcoubard 575:7023a8204a1b 179 error = securityManagerInstance->reset();
vcoubard 575:7023a8204a1b 180 if (error != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 181 return error;
vcoubard 575:7023a8204a1b 182 }
vcoubard 575:7023a8204a1b 183 }
vcoubard 575:7023a8204a1b 184
vcoubard 575:7023a8204a1b 185 /* S110 does not support BLE client features, nothing to reset. */
vcoubard 575:7023a8204a1b 186 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 575:7023a8204a1b 187 if (gattClientInstance != NULL) {
vcoubard 575:7023a8204a1b 188 error = gattClientInstance->reset();
vcoubard 575:7023a8204a1b 189 if (error != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 190 return error;
vcoubard 575:7023a8204a1b 191 }
vcoubard 575:7023a8204a1b 192 }
vcoubard 575:7023a8204a1b 193 #endif
vcoubard 575:7023a8204a1b 194
vcoubard 589:f27ac458e152 195 /* Gap instance is always present */
vcoubard 588:65d256258533 196 error = gapInstance.reset();
vcoubard 575:7023a8204a1b 197 if (error != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 198 return error;
vcoubard 575:7023a8204a1b 199 }
vcoubard 575:7023a8204a1b 200
vcoubard 541:884f95bf5351 201 initialized = false;
vcoubard 541:884f95bf5351 202 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 203 }
vcoubard 541:884f95bf5351 204
vcoubard 541:884f95bf5351 205 void
vcoubard 541:884f95bf5351 206 nRF5xn::waitForEvent(void)
vcoubard 541:884f95bf5351 207 {
vcoubard 541:884f95bf5351 208 sd_app_evt_wait();
rgrover1 388:db85a09c27ef 209 }