hr with 30102

Dependencies:   BLE_API mbed X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
mssarwar
Date:
Sun Jul 30 05:52:57 2017 +0000
Revision:
80:808f6f367f45
with 30102;

Who changed what in which revision?

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