aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Mon Sep 08 07:13:10 2014 +0000
Revision:
62:a022be4c545d
Parent:
61:d0158c65d0d7
Child:
65:98215c4f3a25
Minor improvement to Tsuboi San's pull request.

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 "app_timer.h"
Rohit Grover 22:c6ee8136847e 21 #include "btle.h"
Rohit Grover 22:c6ee8136847e 22
Rohit Grover 22:c6ee8136847e 23 #include "ble_stack_handler_types.h"
Rohit Grover 22:c6ee8136847e 24 #include "ble_radio_notification.h"
Rohit Grover 22:c6ee8136847e 25 #include "ble_flash.h"
Rohit Grover 37:c29c330d942c 26 #if NEED_BOND_MANAGER
Rohit Grover 22:c6ee8136847e 27 #include "ble_bondmngr.h"
Rohit Grover 37:c29c330d942c 28 #endif
Rohit Grover 22:c6ee8136847e 29 #include "ble_conn_params.h"
Rohit Grover 22:c6ee8136847e 30
Rohit Grover 22:c6ee8136847e 31 #include "btle_gap.h"
Rohit Grover 22:c6ee8136847e 32 #include "btle_advertising.h"
Rohit Grover 22:c6ee8136847e 33 #include "custom/custom_helper.h"
Rohit Grover 22:c6ee8136847e 34
Rohit Grover 22:c6ee8136847e 35 #include "softdevice_handler.h"
Rohit Grover 22:c6ee8136847e 36 #include "pstorage.h"
Rohit Grover 22:c6ee8136847e 37
Rohit Grover 52:120bd37b9d0d 38 #include "GapEvents.h"
Rohit Grover 22:c6ee8136847e 39 #include "nRF51Gap.h"
Rohit Grover 22:c6ee8136847e 40 #include "nRF51GattServer.h"
Rohit Grover 22:c6ee8136847e 41
Rohit Grover 56:a1071b629aa3 42 #include "ble_hci.h"
Rohit Grover 56:a1071b629aa3 43
Rohit Grover 32:84dea0924a63 44 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 45 static void service_error_callback(uint32_t nrf_error);
Rohit Grover 32:84dea0924a63 46 #endif
Rohit Grover 56:a1071b629aa3 47 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
Rohit Grover 56:a1071b629aa3 48 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
Rohit Grover 22:c6ee8136847e 49
Rohit Grover 32:84dea0924a63 50 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 51 static error_t bond_manager_init(void);
Rohit Grover 32:84dea0924a63 52 #endif
Rohit Grover 22:c6ee8136847e 53
Rohit Grover 22:c6ee8136847e 54 static void btle_handler(ble_evt_t *p_ble_evt);
Rohit Grover 22:c6ee8136847e 55
Rohit Grover 22:c6ee8136847e 56 static void sys_evt_dispatch(uint32_t sys_evt)
Rohit Grover 22:c6ee8136847e 57 {
Rohit Grover 33:8efbbf54b66f 58 #if NEED_PSTORAGE /* disabled by default */
Rohit Grover 22:c6ee8136847e 59 pstorage_sys_event_handler(sys_evt);
Rohit Grover 33:8efbbf54b66f 60 #endif
Rohit Grover 22:c6ee8136847e 61 }
Rohit Grover 22:c6ee8136847e 62
Rohit Grover 22:c6ee8136847e 63 error_t btle_init(void)
Rohit Grover 22:c6ee8136847e 64 {
Rohit Grover 56:a1071b629aa3 65 const bool useScheduler = false;
ytsuboi 61:d0158c65d0d7 66 #ifdef TARGET_HRM1017
rgrover1 62:a022be4c545d 67 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, useScheduler);
ytsuboi 61:d0158c65d0d7 68 #else
Rohit Grover 56:a1071b629aa3 69 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, useScheduler);
ytsuboi 61:d0158c65d0d7 70 #endif
Rohit Grover 56:a1071b629aa3 71
Rohit Grover 56:a1071b629aa3 72 // Enable BLE stack
Rohit Grover 56:a1071b629aa3 73 /**
Rohit Grover 56:a1071b629aa3 74 * Using this call, the application can select whether to include the
Rohit Grover 56:a1071b629aa3 75 * Service Changed characteristic in the GATT Server. The default in all
Rohit Grover 56:a1071b629aa3 76 * previous releases has been to include the Service Changed characteristic,
Rohit Grover 56:a1071b629aa3 77 * but this affects how GATT clients behave. Specifically, it requires
Rohit Grover 56:a1071b629aa3 78 * clients to subscribe to this attribute and not to cache attribute handles
Rohit Grover 56:a1071b629aa3 79 * between connections unless the devices are bonded. If the application
Rohit Grover 56:a1071b629aa3 80 * does not need to change the structure of the GATT server attributes at
Rohit Grover 56:a1071b629aa3 81 * runtime this adds unnecessary complexity to the interaction with peer
Rohit Grover 56:a1071b629aa3 82 * clients. If the SoftDevice is enabled with the Service Changed
Rohit Grover 56:a1071b629aa3 83 * Characteristics turned off, then clients are allowed to cache attribute
Rohit Grover 56:a1071b629aa3 84 * handles making applications simpler on both sides.
Rohit Grover 56:a1071b629aa3 85 */
Rohit Grover 56:a1071b629aa3 86 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = false;
Rohit Grover 56:a1071b629aa3 87 ble_enable_params_t enableParams = {
Rohit Grover 56:a1071b629aa3 88 .gatts_enable_params = {
Rohit Grover 56:a1071b629aa3 89 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
Rohit Grover 56:a1071b629aa3 90 }
Rohit Grover 56:a1071b629aa3 91 };
Rohit Grover 56:a1071b629aa3 92 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 93 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 94 }
Rohit Grover 56:a1071b629aa3 95
Rohit Grover 56:a1071b629aa3 96 ble_gap_addr_t addr;
Rohit Grover 56:a1071b629aa3 97 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 98 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 99 }
Rohit Grover 56:a1071b629aa3 100 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 101 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 102 }
Rohit Grover 22:c6ee8136847e 103
Rohit Grover 22:c6ee8136847e 104 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
Rohit Grover 22:c6ee8136847e 105 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
Rohit Grover 22:c6ee8136847e 106
Rohit Grover 32:84dea0924a63 107 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 108 bond_manager_init();
Rohit Grover 32:84dea0924a63 109 #endif
Rohit Grover 22:c6ee8136847e 110 btle_gap_init();
Rohit Grover 22:c6ee8136847e 111
Rohit Grover 22:c6ee8136847e 112 return ERROR_NONE;
Rohit Grover 22:c6ee8136847e 113 }
Rohit Grover 22:c6ee8136847e 114
Rohit Grover 22:c6ee8136847e 115 static void btle_handler(ble_evt_t *p_ble_evt)
Rohit Grover 22:c6ee8136847e 116 {
Rohit Grover 22:c6ee8136847e 117 /* Library service handlers */
Rohit Grover 32:84dea0924a63 118 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 119 ble_bondmngr_on_ble_evt(p_ble_evt);
Rohit Grover 32:84dea0924a63 120 #endif
Rohit Grover 56:a1071b629aa3 121 #if SDK_CONN_PARAMS_MODULE_ENABLE
Rohit Grover 22:c6ee8136847e 122 ble_conn_params_on_ble_evt(p_ble_evt);
Rohit Grover 56:a1071b629aa3 123 #endif
Rohit Grover 22:c6ee8136847e 124
Rohit Grover 22:c6ee8136847e 125 /* Custom event handler */
Rohit Grover 22:c6ee8136847e 126 switch (p_ble_evt->header.evt_id) {
Rohit Grover 41:6e66cd970659 127 case BLE_GAP_EVT_CONNECTED: {
Rohit Grover 41:6e66cd970659 128 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 41:6e66cd970659 129 nRF51Gap::getInstance().setConnectionHandle(handle);
Rohit Grover 56:a1071b629aa3 130 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
Rohit Grover 56:a1071b629aa3 131 nRF51Gap::getInstance().processConnectionEvent(handle, params);
Rohit Grover 22:c6ee8136847e 132 break;
Rohit Grover 41:6e66cd970659 133 }
Rohit Grover 22:c6ee8136847e 134
Rohit Grover 41:6e66cd970659 135 case BLE_GAP_EVT_DISCONNECTED: {
Rohit Grover 41:6e66cd970659 136 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 22:c6ee8136847e 137 // Since we are not in a connection and have not started advertising,
Rohit Grover 22:c6ee8136847e 138 // store bonds
Rohit Grover 22:c6ee8136847e 139 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
Rohit Grover 32:84dea0924a63 140 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 141 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
Rohit Grover 32:84dea0924a63 142 #endif
Rohit Grover 56:a1071b629aa3 143
Rohit Grover 56:a1071b629aa3 144 if (p_ble_evt->evt.gap_evt.params.disconnected.reason == BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION) {
Rohit Grover 56:a1071b629aa3 145 nRF51Gap::getInstance().processDisconnectionEvent(handle, Gap::LOCAL_HOST_TERMINATED_CONNECTION);
Rohit Grover 56:a1071b629aa3 146 }
Rohit Grover 22:c6ee8136847e 147 break;
Rohit Grover 41:6e66cd970659 148 }
Rohit Grover 22:c6ee8136847e 149
Rohit Grover 22:c6ee8136847e 150 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
Rohit Grover 22:c6ee8136847e 151 ble_gap_sec_params_t sec_params = {0};
Rohit Grover 22:c6ee8136847e 152
Rohit Grover 22:c6ee8136847e 153 sec_params.timeout = 30; /*< Timeout for Pairing Request or
Rohit Grover 22:c6ee8136847e 154 * Security Request (in seconds). */
Rohit Grover 22:c6ee8136847e 155 sec_params.bond = 1; /**< Perform bonding. */
Rohit Grover 22:c6ee8136847e 156 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
Rohit Grover 22:c6ee8136847e 157 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
Rohit Grover 22:c6ee8136847e 158 sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
Rohit Grover 22:c6ee8136847e 159 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
Rohit Grover 22:c6ee8136847e 160 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
Rohit Grover 22:c6ee8136847e 161
Rohit Grover 56:a1071b629aa3 162 ASSERT_STATUS_RET_VOID(sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(),
Rohit Grover 56:a1071b629aa3 163 BLE_GAP_SEC_STATUS_SUCCESS, &sec_params));
Rohit Grover 22:c6ee8136847e 164 }
Rohit Grover 22:c6ee8136847e 165 break;
Rohit Grover 22:c6ee8136847e 166
Rohit Grover 22:c6ee8136847e 167 case BLE_GAP_EVT_TIMEOUT:
Rohit Grover 40:1cb3707fa798 168 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
Rohit Grover 41:6e66cd970659 169 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
Rohit Grover 22:c6ee8136847e 170 }
Rohit Grover 22:c6ee8136847e 171 break;
Rohit Grover 22:c6ee8136847e 172
Rohit Grover 22:c6ee8136847e 173 case BLE_GATTC_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 174 case BLE_GATTS_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 175 // Disconnect on GATT Server and Client timeout events.
Rohit Grover 22:c6ee8136847e 176 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
Rohit Grover 22:c6ee8136847e 177 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
Rohit Grover 22:c6ee8136847e 178 break;
Rohit Grover 22:c6ee8136847e 179
Rohit Grover 22:c6ee8136847e 180 default:
Rohit Grover 22:c6ee8136847e 181 break;
Rohit Grover 22:c6ee8136847e 182 }
Rohit Grover 22:c6ee8136847e 183
Rohit Grover 22:c6ee8136847e 184 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
Rohit Grover 22:c6ee8136847e 185 }
Rohit Grover 22:c6ee8136847e 186
Rohit Grover 32:84dea0924a63 187 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 188 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 189 /*!
Rohit Grover 22:c6ee8136847e 190 @brief Initialises the bond manager
Rohit Grover 22:c6ee8136847e 191
Rohit Grover 22:c6ee8136847e 192 @note Bond data will be cleared on reset if the bond delete
Rohit Grover 22:c6ee8136847e 193 button is pressed during initialisation (the button is
Rohit Grover 22:c6ee8136847e 194 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
Rohit Grover 22:c6ee8136847e 195
Rohit Grover 22:c6ee8136847e 196 @returns
Rohit Grover 22:c6ee8136847e 197 */
Rohit Grover 22:c6ee8136847e 198 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 199 static error_t bond_manager_init(void)
Rohit Grover 22:c6ee8136847e 200 {
Rohit Grover 22:c6ee8136847e 201 ble_bondmngr_init_t bond_para = {0};
Rohit Grover 22:c6ee8136847e 202
Rohit Grover 22:c6ee8136847e 203 ASSERT_STATUS ( pstorage_init());
Rohit Grover 22:c6ee8136847e 204
Rohit Grover 22:c6ee8136847e 205 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND;
Rohit Grover 22:c6ee8136847e 206 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
Rohit Grover 22:c6ee8136847e 207 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
Rohit Grover 22:c6ee8136847e 208 bond_para.evt_handler = NULL;
Rohit Grover 22:c6ee8136847e 209 bond_para.error_handler = service_error_callback;
Rohit Grover 22:c6ee8136847e 210
Rohit Grover 22:c6ee8136847e 211 ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
Rohit Grover 22:c6ee8136847e 212
Rohit Grover 22:c6ee8136847e 213 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
Rohit Grover 22:c6ee8136847e 214 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
Rohit Grover 22:c6ee8136847e 215 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
Rohit Grover 22:c6ee8136847e 216 // ble_flash_on_radio_active_evt) );
Rohit Grover 22:c6ee8136847e 217
Rohit Grover 22:c6ee8136847e 218 return ERROR_NONE;
Rohit Grover 22:c6ee8136847e 219 }
Rohit Grover 32:84dea0924a63 220 #endif // #if NEED_BOND_MANAGER
Rohit Grover 22:c6ee8136847e 221
Rohit Grover 32:84dea0924a63 222 #if NEED_BOND_MANAGER /* disabled by default */
Rohit Grover 22:c6ee8136847e 223 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 224 /*!
Rohit Grover 22:c6ee8136847e 225 @brief
Rohit Grover 22:c6ee8136847e 226 @param[in] nrf_error
Rohit Grover 22:c6ee8136847e 227 @returns
Rohit Grover 22:c6ee8136847e 228 */
Rohit Grover 22:c6ee8136847e 229 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 230 static void service_error_callback(uint32_t nrf_error)
Rohit Grover 22:c6ee8136847e 231 {
Rohit Grover 22:c6ee8136847e 232 ASSERT_STATUS_RET_VOID( nrf_error );
Rohit Grover 22:c6ee8136847e 233 }
Rohit Grover 32:84dea0924a63 234 #endif // #if NEED_BOND_MANAGER
Rohit Grover 22:c6ee8136847e 235
Rohit Grover 22:c6ee8136847e 236 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 237 /*!
Rohit Grover 22:c6ee8136847e 238 @brief Callback when an error occurs inside the SoftDevice
Rohit Grover 22:c6ee8136847e 239
Rohit Grover 22:c6ee8136847e 240 @param[in] line_num
Rohit Grover 22:c6ee8136847e 241 @param[in] p-file_name
Rohit Grover 22:c6ee8136847e 242
Rohit Grover 22:c6ee8136847e 243 @returns
Rohit Grover 22:c6ee8136847e 244 */
Rohit Grover 22:c6ee8136847e 245 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 246 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 247 {
Rohit Grover 22:c6ee8136847e 248 ASSERT(false, (void) 0);
Rohit Grover 22:c6ee8136847e 249 }
Rohit Grover 22:c6ee8136847e 250
Rohit Grover 22:c6ee8136847e 251 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 252 /*!
Rohit Grover 22:c6ee8136847e 253 @brief Handler for general errors above the SoftDevice layer.
Rohit Grover 22:c6ee8136847e 254 Typically we can' recover from this so we do a reset.
Rohit Grover 22:c6ee8136847e 255
Rohit Grover 22:c6ee8136847e 256 @param[in] error_code
Rohit Grover 22:c6ee8136847e 257 @param[in] line_num
Rohit Grover 22:c6ee8136847e 258 @param[in] p-file_name
Rohit Grover 22:c6ee8136847e 259
Rohit Grover 22:c6ee8136847e 260 @returns
Rohit Grover 22:c6ee8136847e 261 */
Rohit Grover 22:c6ee8136847e 262 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 263 void app_error_handler(uint32_t error_code,
Rohit Grover 22:c6ee8136847e 264 uint32_t line_num,
Rohit Grover 22:c6ee8136847e 265 const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 266 {
Rohit Grover 22:c6ee8136847e 267 ASSERT_STATUS_RET_VOID( error_code );
Rohit Grover 22:c6ee8136847e 268 NVIC_SystemReset();
Rohit Grover 22:c6ee8136847e 269 }