No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:34 2016 +0000
Revision:
596:b66851544182
Parent:
581:1dd90ec7527b
Child:
598:814c1ce92947
Synchronized with git rev fdead9ec
Author: Andres Amaya Garcia
Early whitelisting API prototype implementation

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 #ifndef __NRF5x_GAP_H__
vcoubard 541:884f95bf5351 18 #define __NRF5x_GAP_H__
vcoubard 541:884f95bf5351 19
vcoubard 578:78f69f1be114 20 #ifdef YOTTA_CFG_MBED_OS
vcoubard 578:78f69f1be114 21 #include "mbed-drivers/mbed.h"
vcoubard 578:78f69f1be114 22 #else
vcoubard 578:78f69f1be114 23 #include "mbed.h"
vcoubard 578:78f69f1be114 24 #endif
vcoubard 596:b66851544182 25 #ifndef YOTTA_CFG_WHITELIST_MAX_SIZE
vcoubard 596:b66851544182 26 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 596:b66851544182 27 #elif YOTTA_CFG_WHITELIST_MAX_SIZE > BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 596:b66851544182 28 #undef YOTTA_CFG_WHITELIST_MAX_SIZE
vcoubard 596:b66851544182 29 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
vcoubard 596:b66851544182 30 #endif
vcoubard 541:884f95bf5351 31 #include "ble/blecommon.h"
vcoubard 541:884f95bf5351 32 #include "ble.h"
vcoubard 541:884f95bf5351 33 #include "ble/GapAdvertisingParams.h"
vcoubard 541:884f95bf5351 34 #include "ble/GapAdvertisingData.h"
vcoubard 541:884f95bf5351 35 #include "ble/Gap.h"
vcoubard 541:884f95bf5351 36 #include "ble/GapScanningParams.h"
vcoubard 541:884f95bf5351 37
vcoubard 541:884f95bf5351 38 #include "nrf_soc.h"
vcoubard 549:3f782c64d014 39
vcoubard 549:3f782c64d014 40 extern "C" {
vcoubard 541:884f95bf5351 41 #include "ble_radio_notification.h"
vcoubard 549:3f782c64d014 42 }
vcoubard 549:3f782c64d014 43
vcoubard 541:884f95bf5351 44 #include "btle_security.h"
vcoubard 541:884f95bf5351 45
vcoubard 596:b66851544182 46 #include <set>
vcoubard 596:b66851544182 47
vcoubard 541:884f95bf5351 48 void radioNotificationStaticCallback(bool param);
vcoubard 541:884f95bf5351 49
vcoubard 541:884f95bf5351 50 /**************************************************************************/
vcoubard 541:884f95bf5351 51 /*!
vcoubard 541:884f95bf5351 52 \brief
vcoubard 541:884f95bf5351 53
vcoubard 541:884f95bf5351 54 */
vcoubard 541:884f95bf5351 55 /**************************************************************************/
vcoubard 541:884f95bf5351 56 class nRF5xGap : public Gap
vcoubard 541:884f95bf5351 57 {
vcoubard 541:884f95bf5351 58 public:
vcoubard 541:884f95bf5351 59 /* Functions that must be implemented from Gap */
vcoubard 541:884f95bf5351 60 virtual ble_error_t setAddress(AddressType_t type, const Address_t address);
vcoubard 541:884f95bf5351 61 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
vcoubard 541:884f95bf5351 62 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
vcoubard 541:884f95bf5351 63
vcoubard 541:884f95bf5351 64 virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
vcoubard 541:884f95bf5351 65 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
vcoubard 541:884f95bf5351 66 virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
vcoubard 541:884f95bf5351 67
vcoubard 541:884f95bf5351 68 virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
vcoubard 541:884f95bf5351 69 virtual ble_error_t stopAdvertising(void);
vcoubard 581:1dd90ec7527b 70 virtual ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
vcoubard 541:884f95bf5351 71 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
vcoubard 541:884f95bf5351 72 virtual ble_error_t disconnect(DisconnectionReason_t reason);
vcoubard 541:884f95bf5351 73
vcoubard 541:884f95bf5351 74 virtual ble_error_t setDeviceName(const uint8_t *deviceName);
vcoubard 541:884f95bf5351 75 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
vcoubard 541:884f95bf5351 76 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
vcoubard 541:884f95bf5351 77 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
vcoubard 541:884f95bf5351 78
vcoubard 541:884f95bf5351 79 virtual ble_error_t setTxPower(int8_t txPower);
vcoubard 541:884f95bf5351 80 virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
vcoubard 541:884f95bf5351 81
vcoubard 541:884f95bf5351 82 void setConnectionHandle(uint16_t con_handle);
vcoubard 541:884f95bf5351 83 uint16_t getConnectionHandle(void);
vcoubard 541:884f95bf5351 84
vcoubard 541:884f95bf5351 85 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
vcoubard 541:884f95bf5351 86 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
vcoubard 541:884f95bf5351 87 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
vcoubard 541:884f95bf5351 88
vcoubard 575:7023a8204a1b 89 virtual ble_error_t reset(void);
vcoubard 575:7023a8204a1b 90
vcoubard 596:b66851544182 91 /////////////////// WHITELISTING
vcoubard 596:b66851544182 92 virtual int8_t getMaxWhitelistSize(void) const;
vcoubard 596:b66851544182 93 virtual ble_error_t getWhitelist(std::set<BLEProtocol::Address_t> &whitelist) const;
vcoubard 596:b66851544182 94 virtual ble_error_t setWhitelist(std::set<BLEProtocol::Address_t> whitelist);
vcoubard 596:b66851544182 95
vcoubard 596:b66851544182 96 // Accessors
vcoubard 596:b66851544182 97 virtual void setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
vcoubard 596:b66851544182 98 virtual void setScanningPolicyMode(ScanningPolicyMode_t mode);
vcoubard 596:b66851544182 99 virtual void setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
vcoubard 596:b66851544182 100 virtual Gap::AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
vcoubard 596:b66851544182 101 virtual Gap::ScanningPolicyMode_t getScanningPolicyMode(void) const;
vcoubard 596:b66851544182 102 virtual Gap::InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
vcoubard 596:b66851544182 103 ///////////////////
vcoubard 596:b66851544182 104
vcoubard 541:884f95bf5351 105 virtual ble_error_t initRadioNotification(void) {
vcoubard 541:884f95bf5351 106 if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 107 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 108 }
vcoubard 541:884f95bf5351 109
vcoubard 541:884f95bf5351 110 return BLE_ERROR_UNSPECIFIED;
vcoubard 541:884f95bf5351 111 }
vcoubard 541:884f95bf5351 112
vcoubard 541:884f95bf5351 113 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
vcoubard 541:884f95bf5351 114 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 541:884f95bf5351 115 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
vcoubard 541:884f95bf5351 116 ble_gap_scan_params_t scanParams = {
vcoubard 541:884f95bf5351 117 .active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
vcoubard 596:b66851544182 118 .selective = scanningPolicyMode, /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 596:b66851544182 119 .p_whitelist = &whitelist, /**< Pointer to whitelist, NULL if none is given. */
vcoubard 541:884f95bf5351 120 .interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 121 .window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 122 .timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 123 };
vcoubard 541:884f95bf5351 124
vcoubard 541:884f95bf5351 125 if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 126 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 127 }
vcoubard 541:884f95bf5351 128
vcoubard 541:884f95bf5351 129 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 130 }
vcoubard 541:884f95bf5351 131
vcoubard 541:884f95bf5351 132 virtual ble_error_t stopScan(void) {
vcoubard 541:884f95bf5351 133 if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 134 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 135 }
vcoubard 541:884f95bf5351 136
vcoubard 541:884f95bf5351 137 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 138 }
vcoubard 541:884f95bf5351 139 #endif
vcoubard 541:884f95bf5351 140
vcoubard 541:884f95bf5351 141 private:
vcoubard 596:b66851544182 142 /////////////////WHITELISTING
vcoubard 596:b66851544182 143 Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
vcoubard 596:b66851544182 144 Gap::ScanningPolicyMode_t scanningPolicyMode;
vcoubard 596:b66851544182 145 Gap::InitiatorPolicyMode_t initiatorPolicyMode; /* Unused */
vcoubard 596:b66851544182 146
vcoubard 596:b66851544182 147 ble_gap_addr_t whitelistAddrs[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 596:b66851544182 148 ble_gap_addr_t *whitelistAddresses[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 596:b66851544182 149 uint8_t whitelistAddressesSize;
vcoubard 596:b66851544182 150 ble_gap_irk_t *whitelistIrks[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 596:b66851544182 151 uint8_t whitelistIrksSize;
vcoubard 596:b66851544182 152
vcoubard 596:b66851544182 153 ble_gap_whitelist_t whitelist;
vcoubard 596:b66851544182 154
vcoubard 596:b66851544182 155 /////////////////
vcoubard 596:b66851544182 156
vcoubard 546:1e147322b2b5 157 bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */
vcoubard 546:1e147322b2b5 158 Timeout radioNotificationTimeout;
vcoubard 546:1e147322b2b5 159
vcoubard 546:1e147322b2b5 160 /*
vcoubard 550:bcb632fc92df 161 * A helper function to post radio notification callbacks with low interrupt priority.
vcoubard 546:1e147322b2b5 162 */
vcoubard 546:1e147322b2b5 163 void postRadioNotificationCallback(void) {
vcoubard 550:bcb632fc92df 164 #ifdef YOTTA_CFG_MBED_OS
vcoubard 550:bcb632fc92df 165 /*
vcoubard 550:bcb632fc92df 166 * In mbed OS, all user-facing BLE events (interrupts) are posted to the
vcoubard 550:bcb632fc92df 167 * MINAR scheduler to be executed as callbacks in thread mode. MINAR guards
vcoubard 550:bcb632fc92df 168 * its critical sections from interrupts by acquiring CriticalSectionLock,
vcoubard 550:bcb632fc92df 169 * which results in a call to sd_nvic_critical_region_enter(). Thus, it is
vcoubard 550:bcb632fc92df 170 * safe to invoke MINAR APIs from interrupt context as long as those
vcoubard 550:bcb632fc92df 171 * interrupts are blocked by sd_nvic_critical_region_enter().
vcoubard 550:bcb632fc92df 172 *
vcoubard 550:bcb632fc92df 173 * Radio notifications are a special case for the above. The Radio
vcoubard 550:bcb632fc92df 174 * Notification IRQ is handled at a very high priority--higher than the
vcoubard 550:bcb632fc92df 175 * level blocked by sd_nvic_critical_region_enter(). Thus Radio Notification
vcoubard 550:bcb632fc92df 176 * events can preempt MINAR's critical sections. Using MINAR APIs (such as
vcoubard 550:bcb632fc92df 177 * posting an event) directly in processRadioNotification() may result in a
vcoubard 550:bcb632fc92df 178 * race condition ending in a hard-fault.
vcoubard 550:bcb632fc92df 179 *
vcoubard 550:bcb632fc92df 180 * The solution is to *not* call MINAR APIs directly from the Radio
vcoubard 550:bcb632fc92df 181 * Notification handling; i.e. to do the bulk of RadioNotification
vcoubard 550:bcb632fc92df 182 * processing at a reduced priority which respects MINAR's critical
vcoubard 550:bcb632fc92df 183 * sections. Unfortunately, on a cortex-M0, there is no clean way to demote
vcoubard 550:bcb632fc92df 184 * priority for the currently executing interrupt--we wouldn't want to
vcoubard 550:bcb632fc92df 185 * demote the radio notification handling anyway because it is sensitive to
vcoubard 550:bcb632fc92df 186 * timing, and the system expects to finish this handling very quickly. The
vcoubard 550:bcb632fc92df 187 * workaround is to employ a Timeout to trigger
vcoubard 550:bcb632fc92df 188 * postRadioNotificationCallback() after a very short delay (~0 us) and post
vcoubard 550:bcb632fc92df 189 * the MINAR callback that context.
vcoubard 550:bcb632fc92df 190 *
vcoubard 550:bcb632fc92df 191 * !!!WARNING!!! Radio notifications are very time critical events. The
vcoubard 550:bcb632fc92df 192 * current solution is expected to work under the assumption that
vcoubard 550:bcb632fc92df 193 * postRadioNotificationCalback() will be executed BEFORE the next radio
vcoubard 550:bcb632fc92df 194 * notification event is generated.
vcoubard 550:bcb632fc92df 195 */
vcoubard 546:1e147322b2b5 196 minar::Scheduler::postCallback(
vcoubard 546:1e147322b2b5 197 mbed::util::FunctionPointer1<void, bool>(&radioNotificationCallback, &FunctionPointerWithContext<bool>::call).bind(radioNotificationCallbackParam)
vcoubard 546:1e147322b2b5 198 );
vcoubard 550:bcb632fc92df 199 #else
vcoubard 550:bcb632fc92df 200 /*
vcoubard 550:bcb632fc92df 201 * In mbed classic, all user-facing BLE events execute callbacks in interrupt
vcoubard 550:bcb632fc92df 202 * mode. Radio Notifications are a special case because its IRQ is handled at
vcoubard 550:bcb632fc92df 203 * a very high priority. Thus Radio Notification events can preempt other
vcoubard 550:bcb632fc92df 204 * operations that require interaction with the SoftDevice such as advertising
vcoubard 550:bcb632fc92df 205 * payload updates and changing the Gap state. Therefore, executing a Radio
vcoubard 550:bcb632fc92df 206 * Notification callback directly from processRadioNotification() may result
vcoubard 550:bcb632fc92df 207 * in a race condition ending in a hard-fault.
vcoubard 550:bcb632fc92df 208 *
vcoubard 550:bcb632fc92df 209 * The solution is to *not* execute the Radio Notification callback directly
vcoubard 550:bcb632fc92df 210 * from the Radio Notification handling; i.e. to do the bulk of the
vcoubard 550:bcb632fc92df 211 * Radio Notification processing at a reduced priority. Unfortunately, on a
vcoubard 550:bcb632fc92df 212 * cortex-M0, there is no clean way to demote priority for the currently
vcoubard 550:bcb632fc92df 213 * executing interrupt--we wouldn't want to demote the radio notification
vcoubard 550:bcb632fc92df 214 * handling anyway because it is sensitive to timing, and the system expects
vcoubard 550:bcb632fc92df 215 * to finish this handling very quickly. The workaround is to employ a Timeout
vcoubard 550:bcb632fc92df 216 * to trigger postRadioNotificationCallback() after a very short delay (~0 us)
vcoubard 550:bcb632fc92df 217 * and execute the callback in that context.
vcoubard 550:bcb632fc92df 218 *
vcoubard 550:bcb632fc92df 219 * !!!WARNING!!! Radio notifications are very time critical events. The
vcoubard 550:bcb632fc92df 220 * current solution is expected to work under the assumption that
vcoubard 550:bcb632fc92df 221 * postRadioNotificationCalback() will be executed BEFORE the next radio
vcoubard 550:bcb632fc92df 222 * notification event is generated.
vcoubard 550:bcb632fc92df 223 */
vcoubard 550:bcb632fc92df 224 radioNotificationCallback.call(radioNotificationCallbackParam);
vcoubard 550:bcb632fc92df 225 #endif /* #ifdef YOTTA_CFG_MBED_OS */
vcoubard 546:1e147322b2b5 226 }
vcoubard 546:1e147322b2b5 227
vcoubard 541:884f95bf5351 228 /**
vcoubard 541:884f95bf5351 229 * A helper function to process radio-notification events; to be called internally.
vcoubard 541:884f95bf5351 230 * @param param [description]
vcoubard 541:884f95bf5351 231 */
vcoubard 541:884f95bf5351 232 void processRadioNotificationEvent(bool param) {
vcoubard 546:1e147322b2b5 233 radioNotificationCallbackParam = param;
vcoubard 546:1e147322b2b5 234 radioNotificationTimeout.attach_us(this, &nRF5xGap::postRadioNotificationCallback, 0);
vcoubard 541:884f95bf5351 235 }
vcoubard 541:884f95bf5351 236 friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */
vcoubard 541:884f95bf5351 237
vcoubard 541:884f95bf5351 238 private:
vcoubard 541:884f95bf5351 239 uint16_t m_connectionHandle;
vcoubard 575:7023a8204a1b 240
vcoubard 575:7023a8204a1b 241 /*
vcoubard 575:7023a8204a1b 242 * Allow instantiation from nRF5xn when required.
vcoubard 575:7023a8204a1b 243 */
vcoubard 575:7023a8204a1b 244 friend class nRF5xn;
vcoubard 575:7023a8204a1b 245
vcoubard 596:b66851544182 246 nRF5xGap() :
vcoubard 596:b66851544182 247 advertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST),
vcoubard 596:b66851544182 248 scanningPolicyMode(Gap::SCAN_POLICY_IGNORE_WHITELIST),
vcoubard 596:b66851544182 249 initiatorPolicyMode(Gap::INIT_POLICY_IGNORE_WHITELIST),
vcoubard 596:b66851544182 250 whitelistAddressesSize(0),
vcoubard 596:b66851544182 251 whitelistIrksSize(0) {
vcoubard 541:884f95bf5351 252 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
vcoubard 596:b66851544182 253
vcoubard 596:b66851544182 254 whitelist.pp_addrs = whitelistAddresses;
vcoubard 596:b66851544182 255 for (int i = 0; i < YOTTA_CFG_WHITELIST_MAX_SIZE; i++) {
vcoubard 596:b66851544182 256 whitelistAddresses[i] = &(whitelistAddrs[i]);
vcoubard 596:b66851544182 257 }
vcoubard 596:b66851544182 258 whitelist.pp_irks = whitelistIrks;
vcoubard 541:884f95bf5351 259 }
vcoubard 541:884f95bf5351 260
vcoubard 541:884f95bf5351 261 nRF5xGap(nRF5xGap const &);
vcoubard 541:884f95bf5351 262 void operator=(nRF5xGap const &);
vcoubard 541:884f95bf5351 263 };
vcoubard 541:884f95bf5351 264
rgrover1 388:db85a09c27ef 265 #endif // ifndef __NRF5x_GAP_H__