sa

Fork of nRF51822 by Nordic Semiconductor

Committer:
nakamae
Date:
Thu Dec 29 07:05:48 2016 +0000
Revision:
639:fdeb2820ef26
Parent:
638:c90ae1400bf2
new;

Who changed what in which revision?

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