Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Committer:
JonnyA
Date:
Wed Aug 31 18:59:36 2016 +0000
Revision:
616:b52326e38ebd
Parent:
592:f9574772b816
Workaround for build system bug

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 "common/common.h"
vcoubard 541:884f95bf5351 18 #include "nordic_common.h"
vcoubard 541:884f95bf5351 19
vcoubard 541:884f95bf5351 20 #include "btle.h"
vcoubard 541:884f95bf5351 21
vcoubard 541:884f95bf5351 22 #include "ble_flash.h"
vcoubard 541:884f95bf5351 23 #include "ble_conn_params.h"
vcoubard 541:884f95bf5351 24
vcoubard 541:884f95bf5351 25 #include "btle_gap.h"
vcoubard 541:884f95bf5351 26 #include "btle_advertising.h"
vcoubard 541:884f95bf5351 27 #include "custom/custom_helper.h"
vcoubard 541:884f95bf5351 28
vcoubard 541:884f95bf5351 29 #include "ble/GapEvents.h"
vcoubard 575:7023a8204a1b 30 #include "nRF5xn.h"
vcoubard 541:884f95bf5351 31
vcoubard 549:3f782c64d014 32 extern "C" {
vcoubard 549:3f782c64d014 33 #include "pstorage.h"
vcoubard 541:884f95bf5351 34 #include "device_manager.h"
vcoubard 549:3f782c64d014 35 #include "softdevice_handler.h"
vcoubard 549:3f782c64d014 36 #include "ble_stack_handler_types.h"
vcoubard 549:3f782c64d014 37 }
vcoubard 541:884f95bf5351 38
vcoubard 541:884f95bf5351 39 #include "ble_hci.h"
vcoubard 541:884f95bf5351 40 #include "btle_discovery.h"
vcoubard 541:884f95bf5351 41
vcoubard 592:f9574772b816 42 #include "nRF5xGattClient.h"
vcoubard 592:f9574772b816 43 #include "nRF5xServiceDiscovery.h"
vcoubard 592:f9574772b816 44 #include "nRF5xCharacteristicDescriptorDiscoverer.h"
vcoubard 592:f9574772b816 45
vcoubard 541:884f95bf5351 46 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
vcoubard 541:884f95bf5351 47 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
vcoubard 541:884f95bf5351 48
vcoubard 541:884f95bf5351 49 static void btle_handler(ble_evt_t *p_ble_evt);
vcoubard 541:884f95bf5351 50
vcoubard 541:884f95bf5351 51 static void sys_evt_dispatch(uint32_t sys_evt)
vcoubard 541:884f95bf5351 52 {
vcoubard 541:884f95bf5351 53 pstorage_sys_event_handler(sys_evt);
vcoubard 541:884f95bf5351 54 }
vcoubard 541:884f95bf5351 55
vcoubard 541:884f95bf5351 56 /**
vcoubard 541:884f95bf5351 57 * This function is called in interrupt context to handle BLE events; i.e. pull
vcoubard 541:884f95bf5351 58 * system and user events out of the pending events-queue of the BLE stack. The
vcoubard 541:884f95bf5351 59 * BLE stack signals the availability of events by the triggering the SWI2
vcoubard 541:884f95bf5351 60 * interrupt, which forwards the handling to this function.
vcoubard 541:884f95bf5351 61 *
vcoubard 541:884f95bf5351 62 * The event processing loop is implemented in intern_softdevice_events_execute().
vcoubard 541:884f95bf5351 63 *
vcoubard 541:884f95bf5351 64 * In mbed OS, a callback for intern_softdevice_events_execute() is posted
vcoubard 541:884f95bf5351 65 * to the scheduler, which then executes in thread mode. In mbed-classic,
vcoubard 541:884f95bf5351 66 * event processing happens right-away in interrupt context (which is more
vcoubard 541:884f95bf5351 67 * risk-prone). In either case, the logic of event processing is identical.
vcoubard 541:884f95bf5351 68 */
vcoubard 541:884f95bf5351 69 static uint32_t eventHandler()
vcoubard 541:884f95bf5351 70 {
vcoubard 541:884f95bf5351 71 #ifdef YOTTA_CFG_MBED_OS
vcoubard 541:884f95bf5351 72 minar::Scheduler::postCallback(intern_softdevice_events_execute);
vcoubard 541:884f95bf5351 73 #else
vcoubard 541:884f95bf5351 74 intern_softdevice_events_execute();
vcoubard 541:884f95bf5351 75 #endif
vcoubard 541:884f95bf5351 76
vcoubard 541:884f95bf5351 77 return NRF_SUCCESS;
vcoubard 541:884f95bf5351 78 }
vcoubard 541:884f95bf5351 79
vcoubard 541:884f95bf5351 80 error_t btle_init(void)
vcoubard 541:884f95bf5351 81 {
vcoubard 541:884f95bf5351 82 nrf_clock_lfclksrc_t clockSource;
vcoubard 541:884f95bf5351 83 if (NRF_CLOCK->LFCLKSRC & (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) {
vcoubard 541:884f95bf5351 84 clockSource = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM;
vcoubard 541:884f95bf5351 85 } else {
vcoubard 541:884f95bf5351 86 clockSource = NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION;
vcoubard 541:884f95bf5351 87 }
vcoubard 541:884f95bf5351 88 SOFTDEVICE_HANDLER_INIT(clockSource, eventHandler);
vcoubard 541:884f95bf5351 89
vcoubard 541:884f95bf5351 90 // Enable BLE stack
vcoubard 541:884f95bf5351 91 /**
vcoubard 541:884f95bf5351 92 * Using this call, the application can select whether to include the
vcoubard 541:884f95bf5351 93 * Service Changed characteristic in the GATT Server. The default in all
vcoubard 541:884f95bf5351 94 * previous releases has been to include the Service Changed characteristic,
vcoubard 541:884f95bf5351 95 * but this affects how GATT clients behave. Specifically, it requires
vcoubard 541:884f95bf5351 96 * clients to subscribe to this attribute and not to cache attribute handles
vcoubard 541:884f95bf5351 97 * between connections unless the devices are bonded. If the application
vcoubard 541:884f95bf5351 98 * does not need to change the structure of the GATT server attributes at
vcoubard 541:884f95bf5351 99 * runtime this adds unnecessary complexity to the interaction with peer
vcoubard 541:884f95bf5351 100 * clients. If the SoftDevice is enabled with the Service Changed
vcoubard 541:884f95bf5351 101 * Characteristics turned off, then clients are allowed to cache attribute
vcoubard 541:884f95bf5351 102 * handles making applications simpler on both sides.
vcoubard 541:884f95bf5351 103 */
vcoubard 541:884f95bf5351 104 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
vcoubard 541:884f95bf5351 105 ble_enable_params_t enableParams = {
vcoubard 541:884f95bf5351 106 .gatts_enable_params = {
vcoubard 541:884f95bf5351 107 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
vcoubard 541:884f95bf5351 108 }
vcoubard 541:884f95bf5351 109 };
vcoubard 541:884f95bf5351 110 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 111 return ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 112 }
vcoubard 541:884f95bf5351 113
vcoubard 541:884f95bf5351 114 ble_gap_addr_t addr;
vcoubard 541:884f95bf5351 115 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 116 return ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 117 }
vcoubard 541:884f95bf5351 118 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 119 return ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 120 }
vcoubard 541:884f95bf5351 121
vcoubard 541:884f95bf5351 122 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
vcoubard 541:884f95bf5351 123 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
vcoubard 541:884f95bf5351 124
vcoubard 586:533fd7fdb0fe 125 return btle_gap_init();
vcoubard 541:884f95bf5351 126 }
vcoubard 541:884f95bf5351 127
vcoubard 541:884f95bf5351 128 static void btle_handler(ble_evt_t *p_ble_evt)
vcoubard 541:884f95bf5351 129 {
vcoubard 541:884f95bf5351 130 /* Library service handlers */
vcoubard 541:884f95bf5351 131 #if SDK_CONN_PARAMS_MODULE_ENABLE
vcoubard 541:884f95bf5351 132 ble_conn_params_on_ble_evt(p_ble_evt);
vcoubard 541:884f95bf5351 133 #endif
vcoubard 541:884f95bf5351 134
vcoubard 541:884f95bf5351 135 dm_ble_evt_handler(p_ble_evt);
vcoubard 541:884f95bf5351 136
vcoubard 541:884f95bf5351 137 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 541:884f95bf5351 138 bleGattcEventHandler(p_ble_evt);
vcoubard 541:884f95bf5351 139 #endif
vcoubard 541:884f95bf5351 140
vcoubard 575:7023a8204a1b 141 nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
vcoubard 575:7023a8204a1b 142 nRF5xGap &gap = (nRF5xGap &) ble.getGap();
vcoubard 575:7023a8204a1b 143 nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
vcoubard 575:7023a8204a1b 144 nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();
vcoubard 575:7023a8204a1b 145
vcoubard 541:884f95bf5351 146 /* Custom event handler */
vcoubard 541:884f95bf5351 147 switch (p_ble_evt->header.evt_id) {
vcoubard 541:884f95bf5351 148 case BLE_GAP_EVT_CONNECTED: {
vcoubard 541:884f95bf5351 149 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
vcoubard 541:884f95bf5351 150 #if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 541:884f95bf5351 151 /* Only peripheral role is supported by S110 */
vcoubard 541:884f95bf5351 152 Gap::Role_t role = Gap::PERIPHERAL;
vcoubard 541:884f95bf5351 153 #else
vcoubard 541:884f95bf5351 154 Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
vcoubard 541:884f95bf5351 155 #endif
vcoubard 575:7023a8204a1b 156 gap.setConnectionHandle(handle);
vcoubard 541:884f95bf5351 157 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
vcoubard 541:884f95bf5351 158 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
vcoubard 541:884f95bf5351 159 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
vcoubard 575:7023a8204a1b 160 gap.processConnectionEvent(handle,
vcoubard 541:884f95bf5351 161 role,
vcoubard 577:d38f01a3e701 162 static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
vcoubard 577:d38f01a3e701 163 static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr,
vcoubard 541:884f95bf5351 164 params);
vcoubard 541:884f95bf5351 165 break;
vcoubard 541:884f95bf5351 166 }
vcoubard 541:884f95bf5351 167
vcoubard 541:884f95bf5351 168 case BLE_GAP_EVT_DISCONNECTED: {
vcoubard 541:884f95bf5351 169 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
vcoubard 541:884f95bf5351 170 // Since we are not in a connection and have not started advertising,
vcoubard 541:884f95bf5351 171 // store bonds
vcoubard 575:7023a8204a1b 172 gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);
vcoubard 541:884f95bf5351 173
vcoubard 541:884f95bf5351 174 Gap::DisconnectionReason_t reason;
vcoubard 541:884f95bf5351 175 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
vcoubard 541:884f95bf5351 176 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
vcoubard 541:884f95bf5351 177 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 178 break;
vcoubard 541:884f95bf5351 179 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
vcoubard 541:884f95bf5351 180 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 181 break;
vcoubard 541:884f95bf5351 182 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
vcoubard 541:884f95bf5351 183 reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
vcoubard 541:884f95bf5351 184 break;
vcoubard 541:884f95bf5351 185 default:
vcoubard 541:884f95bf5351 186 /* Please refer to the underlying transport library for an
vcoubard 541:884f95bf5351 187 * interpretion of this reason's value. */
vcoubard 541:884f95bf5351 188 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
vcoubard 541:884f95bf5351 189 break;
vcoubard 541:884f95bf5351 190 }
vcoubard 592:f9574772b816 191
vcoubard 592:f9574772b816 192 // Close all pending discoveries for this connection
JonnyA 616:b52326e38ebd 193 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 592:f9574772b816 194 nRF5xGattClient& gattClient = ble.getGattClient();
vcoubard 592:f9574772b816 195 gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE);
vcoubard 592:f9574772b816 196 gattClient.discovery().terminate(handle);
JonnyA 616:b52326e38ebd 197 #endif
vcoubard 592:f9574772b816 198
vcoubard 575:7023a8204a1b 199 gap.processDisconnectionEvent(handle, reason);
vcoubard 541:884f95bf5351 200 break;
vcoubard 541:884f95bf5351 201 }
vcoubard 541:884f95bf5351 202
vcoubard 541:884f95bf5351 203 case BLE_GAP_EVT_PASSKEY_DISPLAY:
vcoubard 575:7023a8204a1b 204 securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
vcoubard 541:884f95bf5351 205 break;
vcoubard 541:884f95bf5351 206
vcoubard 541:884f95bf5351 207 case BLE_GAP_EVT_TIMEOUT:
vcoubard 575:7023a8204a1b 208 gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
vcoubard 541:884f95bf5351 209 break;
vcoubard 541:884f95bf5351 210
vcoubard 541:884f95bf5351 211 case BLE_GATTC_EVT_TIMEOUT:
vcoubard 541:884f95bf5351 212 case BLE_GATTS_EVT_TIMEOUT:
vcoubard 541:884f95bf5351 213 // Disconnect on GATT Server and Client timeout events.
vcoubard 541:884f95bf5351 214 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
vcoubard 541:884f95bf5351 215 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
vcoubard 541:884f95bf5351 216 break;
vcoubard 541:884f95bf5351 217
vcoubard 541:884f95bf5351 218 case BLE_GAP_EVT_ADV_REPORT: {
vcoubard 541:884f95bf5351 219 const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
vcoubard 575:7023a8204a1b 220 gap.processAdvertisementReport(advReport->peer_addr.addr,
vcoubard 575:7023a8204a1b 221 advReport->rssi,
vcoubard 575:7023a8204a1b 222 advReport->scan_rsp,
vcoubard 575:7023a8204a1b 223 static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
vcoubard 575:7023a8204a1b 224 advReport->dlen,
vcoubard 575:7023a8204a1b 225 advReport->data);
vcoubard 541:884f95bf5351 226 break;
vcoubard 541:884f95bf5351 227 }
vcoubard 541:884f95bf5351 228
vcoubard 541:884f95bf5351 229 default:
vcoubard 541:884f95bf5351 230 break;
vcoubard 541:884f95bf5351 231 }
vcoubard 541:884f95bf5351 232
vcoubard 575:7023a8204a1b 233 gattServer.hwCallback(p_ble_evt);
vcoubard 541:884f95bf5351 234 }
vcoubard 541:884f95bf5351 235
vcoubard 541:884f95bf5351 236 /*! @brief Callback when an error occurs inside the SoftDevice */
vcoubard 541:884f95bf5351 237 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
vcoubard 541:884f95bf5351 238 {
vcoubard 541:884f95bf5351 239 ASSERT(false, (void) 0);
vcoubard 541:884f95bf5351 240 }
vcoubard 541:884f95bf5351 241
vcoubard 541:884f95bf5351 242 /*!
vcoubard 541:884f95bf5351 243 @brief Handler for general errors above the SoftDevice layer.
vcoubard 541:884f95bf5351 244 Typically we can' recover from this so we do a reset.
vcoubard 541:884f95bf5351 245 */
vcoubard 541:884f95bf5351 246 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
vcoubard 541:884f95bf5351 247 {
vcoubard 541:884f95bf5351 248 ASSERT_STATUS_RET_VOID( error_code );
vcoubard 541:884f95bf5351 249 NVIC_SystemReset();
rgrover1 77:9886b2865631 250 }