For with fix for disconnection notifications

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Wed Jun 04 09:19:57 2014 +0100
Revision:
17:c3e11bb6cb72
Parent:
1:f84abedbf4fb
Child:
22:c6ee8136847e
white space diffs; uncrustify

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:eff01767de02 1 /* mbed Microcontroller Library
bogdanm 0:eff01767de02 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 0:eff01767de02 3 *
bogdanm 0:eff01767de02 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 0:eff01767de02 5 * you may not use this file except in compliance with the License.
bogdanm 0:eff01767de02 6 * You may obtain a copy of the License at
bogdanm 0:eff01767de02 7 *
bogdanm 0:eff01767de02 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 0:eff01767de02 9 *
bogdanm 0:eff01767de02 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 0:eff01767de02 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 0:eff01767de02 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 0:eff01767de02 13 * See the License for the specific language governing permissions and
bogdanm 0:eff01767de02 14 * limitations under the License.
bogdanm 0:eff01767de02 15 */
bogdanm 0:eff01767de02 16 #include "common/common.h"
bogdanm 0:eff01767de02 17
bogdanm 0:eff01767de02 18 #include "app_timer.h"
bogdanm 0:eff01767de02 19 #include "btle.h"
bogdanm 0:eff01767de02 20
bogdanm 0:eff01767de02 21 #include "ble_stack_handler_types.h"
bogdanm 0:eff01767de02 22 #include "ble_radio_notification.h"
bogdanm 0:eff01767de02 23 #include "ble_flash.h"
bogdanm 0:eff01767de02 24 #include "ble_bondmngr.h"
bogdanm 0:eff01767de02 25 #include "ble_conn_params.h"
bogdanm 0:eff01767de02 26
bogdanm 0:eff01767de02 27 #include "btle_gap.h"
bogdanm 0:eff01767de02 28 #include "btle_advertising.h"
bogdanm 0:eff01767de02 29 #include "custom/custom_helper.h"
bogdanm 0:eff01767de02 30
bogdanm 0:eff01767de02 31 #include "nordic_common.h"
bogdanm 0:eff01767de02 32 #include "softdevice_handler.h"
bogdanm 0:eff01767de02 33 #include "pstorage.h"
bogdanm 0:eff01767de02 34
bogdanm 0:eff01767de02 35 #include "hw/GapEvents.h"
bogdanm 0:eff01767de02 36 #include "nRF51Gap.h"
bogdanm 0:eff01767de02 37 #include "nRF51GattServer.h"
bogdanm 0:eff01767de02 38
bogdanm 0:eff01767de02 39 static void service_error_callback(uint32_t nrf_error);
Rohit Grover 17:c3e11bb6cb72 40 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
Rohit Grover 17:c3e11bb6cb72 41 void app_error_handler(uint32_t error_code,
Rohit Grover 17:c3e11bb6cb72 42 uint32_t line_num,
Rohit Grover 17:c3e11bb6cb72 43 const uint8_t *p_file_name);
bogdanm 0:eff01767de02 44
bogdanm 0:eff01767de02 45 static error_t bond_manager_init(void);
bogdanm 0:eff01767de02 46
Rohit Grover 17:c3e11bb6cb72 47 static void btle_handler(ble_evt_t *p_ble_evt);
bogdanm 0:eff01767de02 48
bogdanm 0:eff01767de02 49 /**************************************************************************/
bogdanm 0:eff01767de02 50 /*!
bogdanm 0:eff01767de02 51
bogdanm 0:eff01767de02 52 */
bogdanm 0:eff01767de02 53 /**************************************************************************/
bogdanm 0:eff01767de02 54 static void sys_evt_dispatch(uint32_t sys_evt)
bogdanm 0:eff01767de02 55 {
Rohit Grover 17:c3e11bb6cb72 56 pstorage_sys_event_handler(sys_evt);
bogdanm 0:eff01767de02 57 }
bogdanm 0:eff01767de02 58
bogdanm 0:eff01767de02 59 /**************************************************************************/
bogdanm 0:eff01767de02 60 /*!
bogdanm 0:eff01767de02 61 @brief Initialises BTLE and the underlying HW/SoftDevice
bogdanm 0:eff01767de02 62
bogdanm 0:eff01767de02 63 @returns
bogdanm 0:eff01767de02 64 */
bogdanm 0:eff01767de02 65 /**************************************************************************/
bogdanm 0:eff01767de02 66 error_t btle_init(void)
bogdanm 0:eff01767de02 67 {
Rohit Grover 17:c3e11bb6cb72 68 APP_TIMER_INIT(0, 8, 5, false);
Rohit Grover 17:c3e11bb6cb72 69 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
bogdanm 0:eff01767de02 70
Rohit Grover 17:c3e11bb6cb72 71 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
Rohit Grover 17:c3e11bb6cb72 72 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
bogdanm 0:eff01767de02 73
Rohit Grover 17:c3e11bb6cb72 74 bond_manager_init();
Rohit Grover 17:c3e11bb6cb72 75 btle_gap_init();
bogdanm 0:eff01767de02 76
Rohit Grover 17:c3e11bb6cb72 77 return ERROR_NONE;
bogdanm 0:eff01767de02 78 }
bogdanm 0:eff01767de02 79
bogdanm 0:eff01767de02 80 /**************************************************************************/
bogdanm 0:eff01767de02 81 /*!
bogdanm 0:eff01767de02 82 @brief
bogdanm 0:eff01767de02 83
bogdanm 0:eff01767de02 84 @param[in] p_ble_evt
Rohit Grover 17:c3e11bb6cb72 85
bogdanm 0:eff01767de02 86 @returns
bogdanm 0:eff01767de02 87 */
bogdanm 0:eff01767de02 88 /**************************************************************************/
Rohit Grover 17:c3e11bb6cb72 89 static void btle_handler(ble_evt_t *p_ble_evt)
bogdanm 0:eff01767de02 90 {
Rohit Grover 17:c3e11bb6cb72 91 /* Library service handlers */
Rohit Grover 17:c3e11bb6cb72 92 ble_bondmngr_on_ble_evt(p_ble_evt);
Rohit Grover 17:c3e11bb6cb72 93 ble_conn_params_on_ble_evt(p_ble_evt);
bogdanm 0:eff01767de02 94
Rohit Grover 17:c3e11bb6cb72 95 /* Custom event handler */
Rohit Grover 17:c3e11bb6cb72 96 switch (p_ble_evt->header.evt_id) {
bogdanm 0:eff01767de02 97 case BLE_GAP_EVT_CONNECTED:
Rohit Grover 17:c3e11bb6cb72 98 nRF51Gap::getInstance().setConnectionHandle(
Rohit Grover 17:c3e11bb6cb72 99 p_ble_evt->evt.gap_evt.conn_handle );
Rohit Grover 17:c3e11bb6cb72 100 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED);
Rohit Grover 17:c3e11bb6cb72 101 break;
bogdanm 0:eff01767de02 102
bogdanm 0:eff01767de02 103 case BLE_GAP_EVT_DISCONNECTED:
Rohit Grover 17:c3e11bb6cb72 104 // Since we are not in a connection and have not started advertising,
Rohit Grover 17:c3e11bb6cb72 105 // store bonds
Rohit Grover 17:c3e11bb6cb72 106 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
Rohit Grover 17:c3e11bb6cb72 107 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
Rohit Grover 17:c3e11bb6cb72 108 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED);
Rohit Grover 17:c3e11bb6cb72 109 break;
bogdanm 0:eff01767de02 110
bogdanm 0:eff01767de02 111 case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
Rohit Grover 17:c3e11bb6cb72 112 {
Rohit Grover 17:c3e11bb6cb72 113 ble_gap_sec_params_t sec_params = {0};
bogdanm 0:eff01767de02 114
Rohit Grover 17:c3e11bb6cb72 115 sec_params.timeout = 30; /*< Timeout for Pairing Request or
Rohit Grover 17:c3e11bb6cb72 116 * Security Request (in seconds). */
Rohit Grover 17:c3e11bb6cb72 117 sec_params.bond = 1; /**< Perform bonding. */
Rohit Grover 17:c3e11bb6cb72 118 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
Rohit Grover 17:c3e11bb6cb72 119 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
Rohit Grover 17:c3e11bb6cb72 120 sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
Rohit Grover 17:c3e11bb6cb72 121 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
Rohit Grover 17:c3e11bb6cb72 122 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
bogdanm 0:eff01767de02 123
Rohit Grover 17:c3e11bb6cb72 124 ASSERT_STATUS_RET_VOID(
Rohit Grover 17:c3e11bb6cb72 125 sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
Rohit Grover 17:c3e11bb6cb72 126 getConnectionHandle(),
Rohit Grover 17:c3e11bb6cb72 127 BLE_GAP_SEC_STATUS_SUCCESS,
Rohit Grover 17:c3e11bb6cb72 128 &sec_params));
Rohit Grover 17:c3e11bb6cb72 129 }
Rohit Grover 17:c3e11bb6cb72 130 break;
bogdanm 0:eff01767de02 131
bogdanm 0:eff01767de02 132 case BLE_GAP_EVT_TIMEOUT:
Rohit Grover 17:c3e11bb6cb72 133 if (p_ble_evt->evt.gap_evt.params.timeout.src ==
Rohit Grover 17:c3e11bb6cb72 134 BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
Rohit Grover 17:c3e11bb6cb72 135 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT);
Rohit Grover 17:c3e11bb6cb72 136 }
Rohit Grover 17:c3e11bb6cb72 137 break;
bogdanm 0:eff01767de02 138
bogdanm 0:eff01767de02 139 case BLE_GATTC_EVT_TIMEOUT:
bogdanm 0:eff01767de02 140 case BLE_GATTS_EVT_TIMEOUT:
Rohit Grover 17:c3e11bb6cb72 141 // Disconnect on GATT Server and Client timeout events.
Rohit Grover 17:c3e11bb6cb72 142 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
Rohit Grover 17:c3e11bb6cb72 143 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
Rohit Grover 17:c3e11bb6cb72 144 break;
bogdanm 0:eff01767de02 145
Rohit Grover 17:c3e11bb6cb72 146 default:
Rohit Grover 17:c3e11bb6cb72 147 break;
Rohit Grover 17:c3e11bb6cb72 148 }
bogdanm 0:eff01767de02 149
Rohit Grover 17:c3e11bb6cb72 150 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
bogdanm 0:eff01767de02 151 }
bogdanm 0:eff01767de02 152
bogdanm 0:eff01767de02 153 /**************************************************************************/
bogdanm 0:eff01767de02 154 /*!
bogdanm 0:eff01767de02 155 @brief Initialises the bond manager
bogdanm 0:eff01767de02 156
bogdanm 0:eff01767de02 157 @note Bond data will be cleared on reset if the bond delete
bogdanm 0:eff01767de02 158 button is pressed during initialisation (the button is
bogdanm 0:eff01767de02 159 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
bogdanm 0:eff01767de02 160
bogdanm 0:eff01767de02 161 @returns
bogdanm 0:eff01767de02 162 */
bogdanm 0:eff01767de02 163 /**************************************************************************/
bogdanm 0:eff01767de02 164 static error_t bond_manager_init(void)
bogdanm 0:eff01767de02 165 {
Rohit Grover 17:c3e11bb6cb72 166 ble_bondmngr_init_t bond_para = {0};
bogdanm 0:eff01767de02 167
Rohit Grover 17:c3e11bb6cb72 168 ASSERT_STATUS ( pstorage_init());
bogdanm 0:eff01767de02 169
Rohit Grover 17:c3e11bb6cb72 170 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND;
Rohit Grover 17:c3e11bb6cb72 171 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
bogdanm 0:eff01767de02 172 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
Rohit Grover 17:c3e11bb6cb72 173 bond_para.evt_handler = NULL;
Rohit Grover 17:c3e11bb6cb72 174 bond_para.error_handler = service_error_callback;
bogdanm 0:eff01767de02 175
Rohit Grover 17:c3e11bb6cb72 176 ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
bogdanm 0:eff01767de02 177
bogdanm 0:eff01767de02 178 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
bogdanm 0:eff01767de02 179 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
bogdanm 0:eff01767de02 180 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
bogdanm 0:eff01767de02 181 // ble_flash_on_radio_active_evt) );
bogdanm 0:eff01767de02 182
Rohit Grover 17:c3e11bb6cb72 183 return ERROR_NONE;
bogdanm 0:eff01767de02 184 }
bogdanm 0:eff01767de02 185
bogdanm 0:eff01767de02 186 /**************************************************************************/
bogdanm 0:eff01767de02 187 /*!
bogdanm 0:eff01767de02 188 @brief
bogdanm 0:eff01767de02 189 @param[in] nrf_error
bogdanm 0:eff01767de02 190 @returns
bogdanm 0:eff01767de02 191 */
bogdanm 0:eff01767de02 192 /**************************************************************************/
bogdanm 0:eff01767de02 193 static void service_error_callback(uint32_t nrf_error)
bogdanm 0:eff01767de02 194 {
Rohit Grover 17:c3e11bb6cb72 195 ASSERT_STATUS_RET_VOID( nrf_error );
bogdanm 0:eff01767de02 196 }
bogdanm 0:eff01767de02 197
bogdanm 0:eff01767de02 198 /**************************************************************************/
bogdanm 0:eff01767de02 199 /*!
bogdanm 0:eff01767de02 200 @brief Callback when an error occurs inside the SoftDevice
bogdanm 0:eff01767de02 201
bogdanm 0:eff01767de02 202 @param[in] line_num
bogdanm 0:eff01767de02 203 @param[in] p-file_name
bogdanm 0:eff01767de02 204
bogdanm 0:eff01767de02 205 @returns
bogdanm 0:eff01767de02 206 */
bogdanm 0:eff01767de02 207 /**************************************************************************/
Rohit Grover 17:c3e11bb6cb72 208 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
bogdanm 0:eff01767de02 209 {
Rohit Grover 17:c3e11bb6cb72 210 ASSERT(false, (void) 0);
bogdanm 0:eff01767de02 211 }
bogdanm 0:eff01767de02 212
bogdanm 0:eff01767de02 213 /**************************************************************************/
bogdanm 0:eff01767de02 214 /*!
bogdanm 0:eff01767de02 215 @brief Handler for general errors above the SoftDevice layer.
bogdanm 0:eff01767de02 216 Typically we can' recover from this so we do a reset.
bogdanm 0:eff01767de02 217
bogdanm 0:eff01767de02 218 @param[in] error_code
bogdanm 0:eff01767de02 219 @param[in] line_num
bogdanm 0:eff01767de02 220 @param[in] p-file_name
bogdanm 0:eff01767de02 221
bogdanm 0:eff01767de02 222 @returns
bogdanm 0:eff01767de02 223 */
bogdanm 0:eff01767de02 224 /**************************************************************************/
Rohit Grover 17:c3e11bb6cb72 225 void app_error_handler(uint32_t error_code,
Rohit Grover 17:c3e11bb6cb72 226 uint32_t line_num,
Rohit Grover 17:c3e11bb6cb72 227 const uint8_t *p_file_name)
bogdanm 0:eff01767de02 228 {
Rohit Grover 17:c3e11bb6cb72 229 ASSERT_STATUS_RET_VOID( error_code );
Rohit Grover 17:c3e11bb6cb72 230 NVIC_SystemReset();
bogdanm 0:eff01767de02 231 }