test code 123
Fork of LinkNode-Test by
nRF51822_Science_Journal/nRF51822/source/nRF5xGap.h@1:b0d4fbbdb244, 2016-10-28 (annotated)
- Committer:
- youkee
- Date:
- Fri Oct 28 13:04:10 2016 +0000
- Revision:
- 1:b0d4fbbdb244
- Parent:
- 0:1ad0e04b1bc5
ghhbfdd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
youkee | 0:1ad0e04b1bc5 | 1 | /* mbed Microcontroller Library |
youkee | 0:1ad0e04b1bc5 | 2 | * Copyright (c) 2006-2013 ARM Limited |
youkee | 0:1ad0e04b1bc5 | 3 | * |
youkee | 0:1ad0e04b1bc5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
youkee | 0:1ad0e04b1bc5 | 5 | * you may not use this file except in compliance with the License. |
youkee | 0:1ad0e04b1bc5 | 6 | * You may obtain a copy of the License at |
youkee | 0:1ad0e04b1bc5 | 7 | * |
youkee | 0:1ad0e04b1bc5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
youkee | 0:1ad0e04b1bc5 | 9 | * |
youkee | 0:1ad0e04b1bc5 | 10 | * Unless required by applicable law or agreed to in writing, software |
youkee | 0:1ad0e04b1bc5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
youkee | 0:1ad0e04b1bc5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
youkee | 0:1ad0e04b1bc5 | 13 | * See the License for the specific language governing permissions and |
youkee | 0:1ad0e04b1bc5 | 14 | * limitations under the License. |
youkee | 0:1ad0e04b1bc5 | 15 | */ |
youkee | 0:1ad0e04b1bc5 | 16 | |
youkee | 0:1ad0e04b1bc5 | 17 | #ifndef __NRF5x_GAP_H__ |
youkee | 0:1ad0e04b1bc5 | 18 | #define __NRF5x_GAP_H__ |
youkee | 0:1ad0e04b1bc5 | 19 | |
youkee | 0:1ad0e04b1bc5 | 20 | #include "mbed.h" |
youkee | 0:1ad0e04b1bc5 | 21 | #include "ble/blecommon.h" |
youkee | 0:1ad0e04b1bc5 | 22 | #include "ble.h" |
youkee | 0:1ad0e04b1bc5 | 23 | #include "ble/GapAdvertisingParams.h" |
youkee | 0:1ad0e04b1bc5 | 24 | #include "ble/GapAdvertisingData.h" |
youkee | 0:1ad0e04b1bc5 | 25 | #include "ble/Gap.h" |
youkee | 0:1ad0e04b1bc5 | 26 | #include "ble/GapScanningParams.h" |
youkee | 0:1ad0e04b1bc5 | 27 | |
youkee | 0:1ad0e04b1bc5 | 28 | #include "nrf_soc.h" |
youkee | 0:1ad0e04b1bc5 | 29 | |
youkee | 0:1ad0e04b1bc5 | 30 | extern "C" { |
youkee | 0:1ad0e04b1bc5 | 31 | #include "ble_radio_notification.h" |
youkee | 0:1ad0e04b1bc5 | 32 | } |
youkee | 0:1ad0e04b1bc5 | 33 | |
youkee | 0:1ad0e04b1bc5 | 34 | #include "btle_security.h" |
youkee | 0:1ad0e04b1bc5 | 35 | |
youkee | 0:1ad0e04b1bc5 | 36 | void radioNotificationStaticCallback(bool param); |
youkee | 0:1ad0e04b1bc5 | 37 | |
youkee | 0:1ad0e04b1bc5 | 38 | /**************************************************************************/ |
youkee | 0:1ad0e04b1bc5 | 39 | /*! |
youkee | 0:1ad0e04b1bc5 | 40 | \brief |
youkee | 0:1ad0e04b1bc5 | 41 | |
youkee | 0:1ad0e04b1bc5 | 42 | */ |
youkee | 0:1ad0e04b1bc5 | 43 | /**************************************************************************/ |
youkee | 0:1ad0e04b1bc5 | 44 | class nRF5xGap : public Gap |
youkee | 0:1ad0e04b1bc5 | 45 | { |
youkee | 0:1ad0e04b1bc5 | 46 | public: |
youkee | 0:1ad0e04b1bc5 | 47 | static nRF5xGap &getInstance(); |
youkee | 0:1ad0e04b1bc5 | 48 | |
youkee | 0:1ad0e04b1bc5 | 49 | /* Functions that must be implemented from Gap */ |
youkee | 0:1ad0e04b1bc5 | 50 | virtual ble_error_t setAddress(AddressType_t type, const Address_t address); |
youkee | 0:1ad0e04b1bc5 | 51 | virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address); |
youkee | 0:1ad0e04b1bc5 | 52 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &); |
youkee | 0:1ad0e04b1bc5 | 53 | |
youkee | 0:1ad0e04b1bc5 | 54 | virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);} |
youkee | 0:1ad0e04b1bc5 | 55 | virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);} |
youkee | 0:1ad0e04b1bc5 | 56 | virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);} |
youkee | 0:1ad0e04b1bc5 | 57 | |
youkee | 0:1ad0e04b1bc5 | 58 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &); |
youkee | 0:1ad0e04b1bc5 | 59 | virtual ble_error_t stopAdvertising(void); |
youkee | 0:1ad0e04b1bc5 | 60 | virtual ble_error_t connect(const Address_t, Gap::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams); |
youkee | 0:1ad0e04b1bc5 | 61 | virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason); |
youkee | 0:1ad0e04b1bc5 | 62 | virtual ble_error_t disconnect(DisconnectionReason_t reason); |
youkee | 0:1ad0e04b1bc5 | 63 | |
youkee | 0:1ad0e04b1bc5 | 64 | virtual ble_error_t setDeviceName(const uint8_t *deviceName); |
youkee | 0:1ad0e04b1bc5 | 65 | virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP); |
youkee | 0:1ad0e04b1bc5 | 66 | virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance); |
youkee | 0:1ad0e04b1bc5 | 67 | virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP); |
youkee | 0:1ad0e04b1bc5 | 68 | |
youkee | 0:1ad0e04b1bc5 | 69 | virtual ble_error_t setTxPower(int8_t txPower); |
youkee | 0:1ad0e04b1bc5 | 70 | virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP); |
youkee | 0:1ad0e04b1bc5 | 71 | |
youkee | 0:1ad0e04b1bc5 | 72 | void setConnectionHandle(uint16_t con_handle); |
youkee | 0:1ad0e04b1bc5 | 73 | uint16_t getConnectionHandle(void); |
youkee | 0:1ad0e04b1bc5 | 74 | |
youkee | 0:1ad0e04b1bc5 | 75 | virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params); |
youkee | 0:1ad0e04b1bc5 | 76 | virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params); |
youkee | 0:1ad0e04b1bc5 | 77 | virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params); |
youkee | 0:1ad0e04b1bc5 | 78 | |
youkee | 0:1ad0e04b1bc5 | 79 | virtual ble_error_t initRadioNotification(void) { |
youkee | 0:1ad0e04b1bc5 | 80 | if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) { |
youkee | 0:1ad0e04b1bc5 | 81 | return BLE_ERROR_NONE; |
youkee | 0:1ad0e04b1bc5 | 82 | } |
youkee | 0:1ad0e04b1bc5 | 83 | |
youkee | 0:1ad0e04b1bc5 | 84 | return BLE_ERROR_UNSPECIFIED; |
youkee | 0:1ad0e04b1bc5 | 85 | } |
youkee | 0:1ad0e04b1bc5 | 86 | |
youkee | 0:1ad0e04b1bc5 | 87 | /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */ |
youkee | 0:1ad0e04b1bc5 | 88 | #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
youkee | 0:1ad0e04b1bc5 | 89 | virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) { |
youkee | 0:1ad0e04b1bc5 | 90 | ble_gap_scan_params_t scanParams = { |
youkee | 0:1ad0e04b1bc5 | 91 | .active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */ |
youkee | 0:1ad0e04b1bc5 | 92 | .selective = 0, /**< If 1, ignore unknown devices (non whitelisted). */ |
youkee | 0:1ad0e04b1bc5 | 93 | .p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */ |
youkee | 0:1ad0e04b1bc5 | 94 | .interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
youkee | 0:1ad0e04b1bc5 | 95 | .window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
youkee | 0:1ad0e04b1bc5 | 96 | .timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */ |
youkee | 0:1ad0e04b1bc5 | 97 | }; |
youkee | 0:1ad0e04b1bc5 | 98 | |
youkee | 0:1ad0e04b1bc5 | 99 | if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) { |
youkee | 0:1ad0e04b1bc5 | 100 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
youkee | 0:1ad0e04b1bc5 | 101 | } |
youkee | 0:1ad0e04b1bc5 | 102 | |
youkee | 0:1ad0e04b1bc5 | 103 | return BLE_ERROR_NONE; |
youkee | 0:1ad0e04b1bc5 | 104 | } |
youkee | 0:1ad0e04b1bc5 | 105 | |
youkee | 0:1ad0e04b1bc5 | 106 | virtual ble_error_t stopScan(void) { |
youkee | 0:1ad0e04b1bc5 | 107 | if (sd_ble_gap_scan_stop() == NRF_SUCCESS) { |
youkee | 0:1ad0e04b1bc5 | 108 | return BLE_ERROR_NONE; |
youkee | 0:1ad0e04b1bc5 | 109 | } |
youkee | 0:1ad0e04b1bc5 | 110 | |
youkee | 0:1ad0e04b1bc5 | 111 | return BLE_STACK_BUSY; |
youkee | 0:1ad0e04b1bc5 | 112 | } |
youkee | 0:1ad0e04b1bc5 | 113 | #endif |
youkee | 0:1ad0e04b1bc5 | 114 | |
youkee | 0:1ad0e04b1bc5 | 115 | private: |
youkee | 0:1ad0e04b1bc5 | 116 | bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */ |
youkee | 0:1ad0e04b1bc5 | 117 | Timeout radioNotificationTimeout; |
youkee | 0:1ad0e04b1bc5 | 118 | |
youkee | 0:1ad0e04b1bc5 | 119 | /* |
youkee | 0:1ad0e04b1bc5 | 120 | * A helper function to post radio notification callbacks with low interrupt priority. |
youkee | 0:1ad0e04b1bc5 | 121 | */ |
youkee | 0:1ad0e04b1bc5 | 122 | void postRadioNotificationCallback(void) { |
youkee | 0:1ad0e04b1bc5 | 123 | #ifdef YOTTA_CFG_MBED_OS |
youkee | 0:1ad0e04b1bc5 | 124 | /* |
youkee | 0:1ad0e04b1bc5 | 125 | * In mbed OS, all user-facing BLE events (interrupts) are posted to the |
youkee | 0:1ad0e04b1bc5 | 126 | * MINAR scheduler to be executed as callbacks in thread mode. MINAR guards |
youkee | 0:1ad0e04b1bc5 | 127 | * its critical sections from interrupts by acquiring CriticalSectionLock, |
youkee | 0:1ad0e04b1bc5 | 128 | * which results in a call to sd_nvic_critical_region_enter(). Thus, it is |
youkee | 0:1ad0e04b1bc5 | 129 | * safe to invoke MINAR APIs from interrupt context as long as those |
youkee | 0:1ad0e04b1bc5 | 130 | * interrupts are blocked by sd_nvic_critical_region_enter(). |
youkee | 0:1ad0e04b1bc5 | 131 | * |
youkee | 0:1ad0e04b1bc5 | 132 | * Radio notifications are a special case for the above. The Radio |
youkee | 0:1ad0e04b1bc5 | 133 | * Notification IRQ is handled at a very high priority--higher than the |
youkee | 0:1ad0e04b1bc5 | 134 | * level blocked by sd_nvic_critical_region_enter(). Thus Radio Notification |
youkee | 0:1ad0e04b1bc5 | 135 | * events can preempt MINAR's critical sections. Using MINAR APIs (such as |
youkee | 0:1ad0e04b1bc5 | 136 | * posting an event) directly in processRadioNotification() may result in a |
youkee | 0:1ad0e04b1bc5 | 137 | * race condition ending in a hard-fault. |
youkee | 0:1ad0e04b1bc5 | 138 | * |
youkee | 0:1ad0e04b1bc5 | 139 | * The solution is to *not* call MINAR APIs directly from the Radio |
youkee | 0:1ad0e04b1bc5 | 140 | * Notification handling; i.e. to do the bulk of RadioNotification |
youkee | 0:1ad0e04b1bc5 | 141 | * processing at a reduced priority which respects MINAR's critical |
youkee | 0:1ad0e04b1bc5 | 142 | * sections. Unfortunately, on a cortex-M0, there is no clean way to demote |
youkee | 0:1ad0e04b1bc5 | 143 | * priority for the currently executing interrupt--we wouldn't want to |
youkee | 0:1ad0e04b1bc5 | 144 | * demote the radio notification handling anyway because it is sensitive to |
youkee | 0:1ad0e04b1bc5 | 145 | * timing, and the system expects to finish this handling very quickly. The |
youkee | 0:1ad0e04b1bc5 | 146 | * workaround is to employ a Timeout to trigger |
youkee | 0:1ad0e04b1bc5 | 147 | * postRadioNotificationCallback() after a very short delay (~0 us) and post |
youkee | 0:1ad0e04b1bc5 | 148 | * the MINAR callback that context. |
youkee | 0:1ad0e04b1bc5 | 149 | * |
youkee | 0:1ad0e04b1bc5 | 150 | * !!!WARNING!!! Radio notifications are very time critical events. The |
youkee | 0:1ad0e04b1bc5 | 151 | * current solution is expected to work under the assumption that |
youkee | 0:1ad0e04b1bc5 | 152 | * postRadioNotificationCalback() will be executed BEFORE the next radio |
youkee | 0:1ad0e04b1bc5 | 153 | * notification event is generated. |
youkee | 0:1ad0e04b1bc5 | 154 | */ |
youkee | 0:1ad0e04b1bc5 | 155 | minar::Scheduler::postCallback( |
youkee | 0:1ad0e04b1bc5 | 156 | mbed::util::FunctionPointer1<void, bool>(&radioNotificationCallback, &FunctionPointerWithContext<bool>::call).bind(radioNotificationCallbackParam) |
youkee | 0:1ad0e04b1bc5 | 157 | ); |
youkee | 0:1ad0e04b1bc5 | 158 | #else |
youkee | 0:1ad0e04b1bc5 | 159 | /* |
youkee | 0:1ad0e04b1bc5 | 160 | * In mbed classic, all user-facing BLE events execute callbacks in interrupt |
youkee | 0:1ad0e04b1bc5 | 161 | * mode. Radio Notifications are a special case because its IRQ is handled at |
youkee | 0:1ad0e04b1bc5 | 162 | * a very high priority. Thus Radio Notification events can preempt other |
youkee | 0:1ad0e04b1bc5 | 163 | * operations that require interaction with the SoftDevice such as advertising |
youkee | 0:1ad0e04b1bc5 | 164 | * payload updates and changing the Gap state. Therefore, executing a Radio |
youkee | 0:1ad0e04b1bc5 | 165 | * Notification callback directly from processRadioNotification() may result |
youkee | 0:1ad0e04b1bc5 | 166 | * in a race condition ending in a hard-fault. |
youkee | 0:1ad0e04b1bc5 | 167 | * |
youkee | 0:1ad0e04b1bc5 | 168 | * The solution is to *not* execute the Radio Notification callback directly |
youkee | 0:1ad0e04b1bc5 | 169 | * from the Radio Notification handling; i.e. to do the bulk of the |
youkee | 0:1ad0e04b1bc5 | 170 | * Radio Notification processing at a reduced priority. Unfortunately, on a |
youkee | 0:1ad0e04b1bc5 | 171 | * cortex-M0, there is no clean way to demote priority for the currently |
youkee | 0:1ad0e04b1bc5 | 172 | * executing interrupt--we wouldn't want to demote the radio notification |
youkee | 0:1ad0e04b1bc5 | 173 | * handling anyway because it is sensitive to timing, and the system expects |
youkee | 0:1ad0e04b1bc5 | 174 | * to finish this handling very quickly. The workaround is to employ a Timeout |
youkee | 0:1ad0e04b1bc5 | 175 | * to trigger postRadioNotificationCallback() after a very short delay (~0 us) |
youkee | 0:1ad0e04b1bc5 | 176 | * and execute the callback in that context. |
youkee | 0:1ad0e04b1bc5 | 177 | * |
youkee | 0:1ad0e04b1bc5 | 178 | * !!!WARNING!!! Radio notifications are very time critical events. The |
youkee | 0:1ad0e04b1bc5 | 179 | * current solution is expected to work under the assumption that |
youkee | 0:1ad0e04b1bc5 | 180 | * postRadioNotificationCalback() will be executed BEFORE the next radio |
youkee | 0:1ad0e04b1bc5 | 181 | * notification event is generated. |
youkee | 0:1ad0e04b1bc5 | 182 | */ |
youkee | 0:1ad0e04b1bc5 | 183 | radioNotificationCallback.call(radioNotificationCallbackParam); |
youkee | 0:1ad0e04b1bc5 | 184 | #endif /* #ifdef YOTTA_CFG_MBED_OS */ |
youkee | 0:1ad0e04b1bc5 | 185 | } |
youkee | 0:1ad0e04b1bc5 | 186 | |
youkee | 0:1ad0e04b1bc5 | 187 | /** |
youkee | 0:1ad0e04b1bc5 | 188 | * A helper function to process radio-notification events; to be called internally. |
youkee | 0:1ad0e04b1bc5 | 189 | * @param param [description] |
youkee | 0:1ad0e04b1bc5 | 190 | */ |
youkee | 0:1ad0e04b1bc5 | 191 | void processRadioNotificationEvent(bool param) { |
youkee | 0:1ad0e04b1bc5 | 192 | radioNotificationCallbackParam = param; |
youkee | 0:1ad0e04b1bc5 | 193 | radioNotificationTimeout.attach_us(this, &nRF5xGap::postRadioNotificationCallback, 0); |
youkee | 0:1ad0e04b1bc5 | 194 | } |
youkee | 0:1ad0e04b1bc5 | 195 | friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */ |
youkee | 0:1ad0e04b1bc5 | 196 | |
youkee | 0:1ad0e04b1bc5 | 197 | private: |
youkee | 0:1ad0e04b1bc5 | 198 | uint16_t m_connectionHandle; |
youkee | 0:1ad0e04b1bc5 | 199 | nRF5xGap() { |
youkee | 0:1ad0e04b1bc5 | 200 | m_connectionHandle = BLE_CONN_HANDLE_INVALID; |
youkee | 0:1ad0e04b1bc5 | 201 | } |
youkee | 0:1ad0e04b1bc5 | 202 | |
youkee | 0:1ad0e04b1bc5 | 203 | nRF5xGap(nRF5xGap const &); |
youkee | 0:1ad0e04b1bc5 | 204 | void operator=(nRF5xGap const &); |
youkee | 0:1ad0e04b1bc5 | 205 | }; |
youkee | 0:1ad0e04b1bc5 | 206 | |
youkee | 0:1ad0e04b1bc5 | 207 | #endif // ifndef __NRF5x_GAP_H__ |