library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Apr 11 15:07:15 2016 +0100
Revision:
631:e613866d34ba
Parent:
604:0f5c8725146c
Synchronized with git rev 4e6cc020
Author: Vincent Coubard
Replace inclusion of nordic header ble.h into nrf_ble.h

This change has been introduced in the SDK to mitigate
compilation issues with mbed-classic.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 631:e613866d34ba 1 /* mbed Microcontroller Library
vcoubard 631:e613866d34ba 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 631:e613866d34ba 3 *
vcoubard 631:e613866d34ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 631:e613866d34ba 5 * you may not use this file except in compliance with the License.
vcoubard 631:e613866d34ba 6 * You may obtain a copy of the License at
vcoubard 631:e613866d34ba 7 *
vcoubard 631:e613866d34ba 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 631:e613866d34ba 9 *
vcoubard 631:e613866d34ba 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 631:e613866d34ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 631:e613866d34ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 631:e613866d34ba 13 * See the License for the specific language governing permissions and
vcoubard 631:e613866d34ba 14 * limitations under the License.
vcoubard 631:e613866d34ba 15 */
vcoubard 631:e613866d34ba 16
vcoubard 631:e613866d34ba 17 #ifndef __NRF5x_GAP_H__
vcoubard 631:e613866d34ba 18 #define __NRF5x_GAP_H__
vcoubard 631:e613866d34ba 19
vcoubard 631:e613866d34ba 20 #ifdef YOTTA_CFG_MBED_OS
vcoubard 631:e613866d34ba 21 #include "mbed-drivers/mbed.h"
vcoubard 631:e613866d34ba 22 #else
vcoubard 631:e613866d34ba 23 #include "mbed.h"
vcoubard 631:e613866d34ba 24 #endif
vcoubard 631:e613866d34ba 25 #ifndef YOTTA_CFG_WHITELIST_MAX_SIZE
vcoubard 631:e613866d34ba 26 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 631:e613866d34ba 27 #elif YOTTA_CFG_WHITELIST_MAX_SIZE > BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 631:e613866d34ba 28 #undef YOTTA_CFG_WHITELIST_MAX_SIZE
vcoubard 631:e613866d34ba 29 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 631:e613866d34ba 30 #endif
vcoubard 631:e613866d34ba 31 #ifndef YOTTA_CFG_IRK_TABLE_MAX_SIZE
vcoubard 631:e613866d34ba 32 #define YOTTA_CFG_IRK_TABLE_MAX_SIZE BLE_GAP_WHITELIST_IRK_MAX_COUNT
vcoubard 631:e613866d34ba 33 #elif YOTTA_CFG_IRK_TABLE_MAX_SIZE > BLE_GAP_WHITELIST_IRK_MAX_COUNT
vcoubard 631:e613866d34ba 34 #undef YOTTA_CFG_IRK_TABLE_MAX_SIZE
vcoubard 631:e613866d34ba 35 #define YOTTA_CFG_IRK_TABLE_MAX_SIZE BLE_GAP_WHITELIST_IRK_MAX_COUNT
vcoubard 631:e613866d34ba 36 #endif
vcoubard 631:e613866d34ba 37 #include "ble/blecommon.h"
vcoubard 631:e613866d34ba 38 #include "nrf_ble.h"
vcoubard 631:e613866d34ba 39 #include "ble/GapAdvertisingParams.h"
vcoubard 631:e613866d34ba 40 #include "ble/GapAdvertisingData.h"
vcoubard 631:e613866d34ba 41 #include "ble/Gap.h"
vcoubard 631:e613866d34ba 42 #include "ble/GapScanningParams.h"
vcoubard 631:e613866d34ba 43
vcoubard 631:e613866d34ba 44 #include "nrf_soc.h"
vcoubard 631:e613866d34ba 45
vcoubard 631:e613866d34ba 46 extern "C" {
vcoubard 631:e613866d34ba 47 #include "ble_radio_notification.h"
vcoubard 631:e613866d34ba 48 }
vcoubard 631:e613866d34ba 49
vcoubard 631:e613866d34ba 50 #include "btle_security.h"
vcoubard 631:e613866d34ba 51
vcoubard 631:e613866d34ba 52 void radioNotificationStaticCallback(bool param);
vcoubard 631:e613866d34ba 53
vcoubard 631:e613866d34ba 54 /**************************************************************************/
vcoubard 631:e613866d34ba 55 /*!
vcoubard 631:e613866d34ba 56 \brief
vcoubard 631:e613866d34ba 57
vcoubard 631:e613866d34ba 58 */
vcoubard 631:e613866d34ba 59 /**************************************************************************/
vcoubard 631:e613866d34ba 60 class nRF5xGap : public Gap
vcoubard 631:e613866d34ba 61 {
vcoubard 631:e613866d34ba 62 public:
vcoubard 631:e613866d34ba 63 /* Functions that must be implemented from Gap */
vcoubard 631:e613866d34ba 64 virtual ble_error_t setAddress(AddressType_t type, const Address_t address);
vcoubard 631:e613866d34ba 65 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
vcoubard 631:e613866d34ba 66 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
vcoubard 631:e613866d34ba 67
vcoubard 631:e613866d34ba 68 virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
vcoubard 631:e613866d34ba 69 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
vcoubard 631:e613866d34ba 70 virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
vcoubard 631:e613866d34ba 71
vcoubard 631:e613866d34ba 72 virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
vcoubard 631:e613866d34ba 73 virtual ble_error_t stopAdvertising(void);
vcoubard 631:e613866d34ba 74 virtual ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
vcoubard 631:e613866d34ba 75 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
vcoubard 631:e613866d34ba 76 virtual ble_error_t disconnect(DisconnectionReason_t reason);
vcoubard 631:e613866d34ba 77
vcoubard 631:e613866d34ba 78 virtual ble_error_t setDeviceName(const uint8_t *deviceName);
vcoubard 631:e613866d34ba 79 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
vcoubard 631:e613866d34ba 80 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
vcoubard 631:e613866d34ba 81 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
vcoubard 631:e613866d34ba 82
vcoubard 631:e613866d34ba 83 virtual ble_error_t setTxPower(int8_t txPower);
vcoubard 631:e613866d34ba 84 virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
vcoubard 631:e613866d34ba 85
vcoubard 631:e613866d34ba 86 void setConnectionHandle(uint16_t con_handle);
vcoubard 631:e613866d34ba 87 uint16_t getConnectionHandle(void);
vcoubard 631:e613866d34ba 88
vcoubard 631:e613866d34ba 89 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
vcoubard 631:e613866d34ba 90 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
vcoubard 631:e613866d34ba 91 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
vcoubard 631:e613866d34ba 92
vcoubard 631:e613866d34ba 93 virtual ble_error_t reset(void);
vcoubard 631:e613866d34ba 94
vcoubard 631:e613866d34ba 95 /*
vcoubard 631:e613866d34ba 96 * The following functions are part of the whitelisting experimental API.
vcoubard 631:e613866d34ba 97 * Therefore, this functionality can change in the near future.
vcoubard 631:e613866d34ba 98 */
vcoubard 631:e613866d34ba 99 virtual uint8_t getMaxWhitelistSize(void) const;
vcoubard 631:e613866d34ba 100 virtual ble_error_t getWhitelist(Gap::Whitelist_t &whitelistOut) const;
vcoubard 631:e613866d34ba 101 virtual ble_error_t setWhitelist(const Gap::Whitelist_t &whitelistIn);
vcoubard 631:e613866d34ba 102
vcoubard 631:e613866d34ba 103 virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
vcoubard 631:e613866d34ba 104 virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
vcoubard 631:e613866d34ba 105 virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
vcoubard 631:e613866d34ba 106 virtual Gap::AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
vcoubard 631:e613866d34ba 107 virtual Gap::ScanningPolicyMode_t getScanningPolicyMode(void) const;
vcoubard 631:e613866d34ba 108 virtual Gap::InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
vcoubard 631:e613866d34ba 109
vcoubard 631:e613866d34ba 110 virtual ble_error_t initRadioNotification(void) {
vcoubard 631:e613866d34ba 111 if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
vcoubard 631:e613866d34ba 112 return BLE_ERROR_NONE;
vcoubard 631:e613866d34ba 113 }
vcoubard 631:e613866d34ba 114
vcoubard 631:e613866d34ba 115 return BLE_ERROR_UNSPECIFIED;
vcoubard 631:e613866d34ba 116 }
vcoubard 631:e613866d34ba 117
vcoubard 631:e613866d34ba 118 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
vcoubard 631:e613866d34ba 119 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 631:e613866d34ba 120 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
vcoubard 631:e613866d34ba 121 virtual ble_error_t stopScan(void);
vcoubard 631:e613866d34ba 122 #endif
vcoubard 631:e613866d34ba 123
vcoubard 631:e613866d34ba 124 private:
vcoubard 631:e613866d34ba 125 /*
vcoubard 631:e613866d34ba 126 * Whitelisting API related structures and helper functions.
vcoubard 631:e613866d34ba 127 */
vcoubard 631:e613866d34ba 128
vcoubard 631:e613866d34ba 129 /* Policy modes set by the user. By default these are set to ignore the whitelist */
vcoubard 631:e613866d34ba 130 Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
vcoubard 631:e613866d34ba 131 Gap::ScanningPolicyMode_t scanningPolicyMode;
vcoubard 631:e613866d34ba 132
vcoubard 631:e613866d34ba 133 /* Internal representation of a whitelist */
vcoubard 631:e613866d34ba 134 uint8_t whitelistAddressesSize;
vcoubard 631:e613866d34ba 135 ble_gap_addr_t whitelistAddresses[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 631:e613866d34ba 136
vcoubard 631:e613866d34ba 137 /*
vcoubard 631:e613866d34ba 138 * An internal function used to populate the ble_gap_whitelist_t that will be used by
vcoubard 631:e613866d34ba 139 * the SoftDevice for filtering requests. This function is needed because for the BLE
vcoubard 631:e613866d34ba 140 * API the whitelist is just a collection of keys, but for the stack it also includes
vcoubard 631:e613866d34ba 141 * the IRK table.
vcoubard 631:e613866d34ba 142 */
vcoubard 631:e613866d34ba 143 ble_error_t generateStackWhitelist(ble_gap_whitelist_t &whitelist);
vcoubard 631:e613866d34ba 144
vcoubard 631:e613866d34ba 145 private:
vcoubard 631:e613866d34ba 146 bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */
vcoubard 631:e613866d34ba 147 Timeout radioNotificationTimeout;
vcoubard 631:e613866d34ba 148
vcoubard 631:e613866d34ba 149 /*
vcoubard 631:e613866d34ba 150 * A helper function to post radio notification callbacks with low interrupt priority.
vcoubard 631:e613866d34ba 151 */
vcoubard 631:e613866d34ba 152 void postRadioNotificationCallback(void) {
vcoubard 631:e613866d34ba 153 #ifdef YOTTA_CFG_MBED_OS
vcoubard 631:e613866d34ba 154 /*
vcoubard 631:e613866d34ba 155 * In mbed OS, all user-facing BLE events (interrupts) are posted to the
vcoubard 631:e613866d34ba 156 * MINAR scheduler to be executed as callbacks in thread mode. MINAR guards
vcoubard 631:e613866d34ba 157 * its critical sections from interrupts by acquiring CriticalSectionLock,
vcoubard 631:e613866d34ba 158 * which results in a call to sd_nvic_critical_region_enter(). Thus, it is
vcoubard 631:e613866d34ba 159 * safe to invoke MINAR APIs from interrupt context as long as those
vcoubard 631:e613866d34ba 160 * interrupts are blocked by sd_nvic_critical_region_enter().
vcoubard 631:e613866d34ba 161 *
vcoubard 631:e613866d34ba 162 * Radio notifications are a special case for the above. The Radio
vcoubard 631:e613866d34ba 163 * Notification IRQ is handled at a very high priority--higher than the
vcoubard 631:e613866d34ba 164 * level blocked by sd_nvic_critical_region_enter(). Thus Radio Notification
vcoubard 631:e613866d34ba 165 * events can preempt MINAR's critical sections. Using MINAR APIs (such as
vcoubard 631:e613866d34ba 166 * posting an event) directly in processRadioNotification() may result in a
vcoubard 631:e613866d34ba 167 * race condition ending in a hard-fault.
vcoubard 631:e613866d34ba 168 *
vcoubard 631:e613866d34ba 169 * The solution is to *not* call MINAR APIs directly from the Radio
vcoubard 631:e613866d34ba 170 * Notification handling; i.e. to do the bulk of RadioNotification
vcoubard 631:e613866d34ba 171 * processing at a reduced priority which respects MINAR's critical
vcoubard 631:e613866d34ba 172 * sections. Unfortunately, on a cortex-M0, there is no clean way to demote
vcoubard 631:e613866d34ba 173 * priority for the currently executing interrupt--we wouldn't want to
vcoubard 631:e613866d34ba 174 * demote the radio notification handling anyway because it is sensitive to
vcoubard 631:e613866d34ba 175 * timing, and the system expects to finish this handling very quickly. The
vcoubard 631:e613866d34ba 176 * workaround is to employ a Timeout to trigger
vcoubard 631:e613866d34ba 177 * postRadioNotificationCallback() after a very short delay (~0 us) and post
vcoubard 631:e613866d34ba 178 * the MINAR callback that context.
vcoubard 631:e613866d34ba 179 *
vcoubard 631:e613866d34ba 180 * !!!WARNING!!! Radio notifications are very time critical events. The
vcoubard 631:e613866d34ba 181 * current solution is expected to work under the assumption that
vcoubard 631:e613866d34ba 182 * postRadioNotificationCalback() will be executed BEFORE the next radio
vcoubard 631:e613866d34ba 183 * notification event is generated.
vcoubard 631:e613866d34ba 184 */
vcoubard 631:e613866d34ba 185 minar::Scheduler::postCallback(
vcoubard 631:e613866d34ba 186 mbed::util::FunctionPointer1<void, bool>(&radioNotificationCallback, &FunctionPointerWithContext<bool>::call).bind(radioNotificationCallbackParam)
vcoubard 631:e613866d34ba 187 );
vcoubard 631:e613866d34ba 188 #else
vcoubard 631:e613866d34ba 189 /*
vcoubard 631:e613866d34ba 190 * In mbed classic, all user-facing BLE events execute callbacks in interrupt
vcoubard 631:e613866d34ba 191 * mode. Radio Notifications are a special case because its IRQ is handled at
vcoubard 631:e613866d34ba 192 * a very high priority. Thus Radio Notification events can preempt other
vcoubard 631:e613866d34ba 193 * operations that require interaction with the SoftDevice such as advertising
vcoubard 631:e613866d34ba 194 * payload updates and changing the Gap state. Therefore, executing a Radio
vcoubard 631:e613866d34ba 195 * Notification callback directly from processRadioNotification() may result
vcoubard 631:e613866d34ba 196 * in a race condition ending in a hard-fault.
vcoubard 631:e613866d34ba 197 *
vcoubard 631:e613866d34ba 198 * The solution is to *not* execute the Radio Notification callback directly
vcoubard 631:e613866d34ba 199 * from the Radio Notification handling; i.e. to do the bulk of the
vcoubard 631:e613866d34ba 200 * Radio Notification processing at a reduced priority. Unfortunately, on a
vcoubard 631:e613866d34ba 201 * cortex-M0, there is no clean way to demote priority for the currently
vcoubard 631:e613866d34ba 202 * executing interrupt--we wouldn't want to demote the radio notification
vcoubard 631:e613866d34ba 203 * handling anyway because it is sensitive to timing, and the system expects
vcoubard 631:e613866d34ba 204 * to finish this handling very quickly. The workaround is to employ a Timeout
vcoubard 631:e613866d34ba 205 * to trigger postRadioNotificationCallback() after a very short delay (~0 us)
vcoubard 631:e613866d34ba 206 * and execute the callback in that context.
vcoubard 631:e613866d34ba 207 *
vcoubard 631:e613866d34ba 208 * !!!WARNING!!! Radio notifications are very time critical events. The
vcoubard 631:e613866d34ba 209 * current solution is expected to work under the assumption that
vcoubard 631:e613866d34ba 210 * postRadioNotificationCalback() will be executed BEFORE the next radio
vcoubard 631:e613866d34ba 211 * notification event is generated.
vcoubard 631:e613866d34ba 212 */
vcoubard 631:e613866d34ba 213 radioNotificationCallback.call(radioNotificationCallbackParam);
vcoubard 631:e613866d34ba 214 #endif /* #ifdef YOTTA_CFG_MBED_OS */
vcoubard 631:e613866d34ba 215 }
vcoubard 631:e613866d34ba 216
vcoubard 631:e613866d34ba 217 /**
vcoubard 631:e613866d34ba 218 * A helper function to process radio-notification events; to be called internally.
vcoubard 631:e613866d34ba 219 * @param param [description]
vcoubard 631:e613866d34ba 220 */
vcoubard 631:e613866d34ba 221 void processRadioNotificationEvent(bool param) {
vcoubard 631:e613866d34ba 222 radioNotificationCallbackParam = param;
vcoubard 631:e613866d34ba 223 radioNotificationTimeout.attach_us(this, &nRF5xGap::postRadioNotificationCallback, 0);
vcoubard 631:e613866d34ba 224 }
vcoubard 631:e613866d34ba 225 friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */
vcoubard 631:e613866d34ba 226
vcoubard 631:e613866d34ba 227 private:
vcoubard 631:e613866d34ba 228 uint16_t m_connectionHandle;
vcoubard 631:e613866d34ba 229
vcoubard 631:e613866d34ba 230 /*
vcoubard 631:e613866d34ba 231 * Allow instantiation from nRF5xn when required.
vcoubard 631:e613866d34ba 232 */
vcoubard 631:e613866d34ba 233 friend class nRF5xn;
vcoubard 631:e613866d34ba 234
vcoubard 631:e613866d34ba 235 nRF5xGap() :
vcoubard 631:e613866d34ba 236 advertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST),
vcoubard 631:e613866d34ba 237 scanningPolicyMode(Gap::SCAN_POLICY_IGNORE_WHITELIST),
vcoubard 631:e613866d34ba 238 whitelistAddressesSize(0) {
vcoubard 631:e613866d34ba 239 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
vcoubard 631:e613866d34ba 240 }
vcoubard 631:e613866d34ba 241
vcoubard 631:e613866d34ba 242 nRF5xGap(nRF5xGap const &);
vcoubard 631:e613866d34ba 243 void operator=(nRF5xGap const &);
vcoubard 631:e613866d34ba 244 };
vcoubard 631:e613866d34ba 245
rgrover1 388:db85a09c27ef 246 #endif // ifndef __NRF5x_GAP_H__