nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Aug 07 15:57:07 2015 +0100
Revision:
400:a471c3da4806
Parent:
394:0f7c5048efb3
Child:
403:448a564f5c9e
Synchronized with git rev 20f44177
Author: Jean-Philippe Brucker
Add SoftDevice s110 compatibility

This patch adds the ability to use the API with the s110 SoftDevice.
Only a minor change is needed so far, to support the absence of roles in
s110.

Who changed what in which revision?

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