test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Apr 06 22:38:43 2016 +0100
Revision:
615:65ea2acfc6a2
Parent:
592:f9574772b816
Child:
616:a8f9b022d8fd
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.

Who changed what in which revision?

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