No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:24 2016 +0000
Revision:
575:7023a8204a1b
Parent:
571:bbf6410b6a89
Child:
578:78f69f1be114
Synchronized with git rev 252ef3cf
Author: Andres Amaya Garcia
Make _gapInstance non-static in nRF5xn

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