test
Fork of nRF51822 by
btle/btle.cpp@37:c29c330d942c, 2014-07-07 (annotated)
- Committer:
- Rohit Grover
- Date:
- Mon Jul 07 13:43:31 2014 +0100
- Revision:
- 37:c29c330d942c
- Parent:
- 34:48d24b1d2fe6
- Child:
- 40:1cb3707fa798
changes required to upgrade to V7 of the soft-device
Who changed what in which revision?
User | Revision | Line number | New 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 |
22:c6ee8136847e | 16 | #include "common/common.h" |
Rohit Grover |
37:c29c330d942c | 17 | #include "nordic_common.h" |
Rohit Grover |
22:c6ee8136847e | 18 | |
Rohit Grover |
22:c6ee8136847e | 19 | #include "app_timer.h" |
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_radio_notification.h" |
Rohit Grover |
22:c6ee8136847e | 24 | #include "ble_flash.h" |
Rohit Grover |
37:c29c330d942c | 25 | #if NEED_BOND_MANAGER |
Rohit Grover |
22:c6ee8136847e | 26 | #include "ble_bondmngr.h" |
Rohit Grover |
37:c29c330d942c | 27 | #endif |
Rohit Grover |
22:c6ee8136847e | 28 | #include "ble_conn_params.h" |
Rohit Grover |
22:c6ee8136847e | 29 | |
Rohit Grover |
22:c6ee8136847e | 30 | #include "btle_gap.h" |
Rohit Grover |
22:c6ee8136847e | 31 | #include "btle_advertising.h" |
Rohit Grover |
22:c6ee8136847e | 32 | #include "custom/custom_helper.h" |
Rohit Grover |
22:c6ee8136847e | 33 | |
Rohit Grover |
22:c6ee8136847e | 34 | #include "softdevice_handler.h" |
Rohit Grover |
22:c6ee8136847e | 35 | #include "pstorage.h" |
Rohit Grover |
22:c6ee8136847e | 36 | |
Rohit Grover |
22:c6ee8136847e | 37 | #include "hw/GapEvents.h" |
Rohit Grover |
22:c6ee8136847e | 38 | #include "nRF51Gap.h" |
Rohit Grover |
22:c6ee8136847e | 39 | #include "nRF51GattServer.h" |
Rohit Grover |
22:c6ee8136847e | 40 | |
Rohit Grover |
32:84dea0924a63 | 41 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 42 | static void service_error_callback(uint32_t nrf_error); |
Rohit Grover |
32:84dea0924a63 | 43 | #endif |
Rohit Grover |
22:c6ee8136847e | 44 | void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); |
Rohit Grover |
22:c6ee8136847e | 45 | void app_error_handler(uint32_t error_code, |
Rohit Grover |
22:c6ee8136847e | 46 | uint32_t line_num, |
Rohit Grover |
22:c6ee8136847e | 47 | const uint8_t *p_file_name); |
Rohit Grover |
22:c6ee8136847e | 48 | |
Rohit Grover |
32:84dea0924a63 | 49 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 50 | static error_t bond_manager_init(void); |
Rohit Grover |
32:84dea0924a63 | 51 | #endif |
Rohit Grover |
22:c6ee8136847e | 52 | |
Rohit Grover |
22:c6ee8136847e | 53 | static void btle_handler(ble_evt_t *p_ble_evt); |
Rohit Grover |
22:c6ee8136847e | 54 | |
Rohit Grover |
22:c6ee8136847e | 55 | static void sys_evt_dispatch(uint32_t sys_evt) |
Rohit Grover |
22:c6ee8136847e | 56 | { |
Rohit Grover |
33:8efbbf54b66f | 57 | #if NEED_PSTORAGE /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 58 | pstorage_sys_event_handler(sys_evt); |
Rohit Grover |
33:8efbbf54b66f | 59 | #endif |
Rohit Grover |
22:c6ee8136847e | 60 | } |
Rohit Grover |
22:c6ee8136847e | 61 | |
Rohit Grover |
22:c6ee8136847e | 62 | error_t btle_init(void) |
Rohit Grover |
22:c6ee8136847e | 63 | { |
Rohit Grover |
22:c6ee8136847e | 64 | APP_TIMER_INIT(0, 8, 5, false); |
Rohit Grover |
22:c6ee8136847e | 65 | SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false); |
Rohit Grover |
22:c6ee8136847e | 66 | |
Rohit Grover |
22:c6ee8136847e | 67 | ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); |
Rohit Grover |
22:c6ee8136847e | 68 | ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); |
Rohit Grover |
22:c6ee8136847e | 69 | |
Rohit Grover |
32:84dea0924a63 | 70 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 71 | bond_manager_init(); |
Rohit Grover |
32:84dea0924a63 | 72 | #endif |
Rohit Grover |
22:c6ee8136847e | 73 | btle_gap_init(); |
Rohit Grover |
22:c6ee8136847e | 74 | |
Rohit Grover |
22:c6ee8136847e | 75 | return ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 76 | } |
Rohit Grover |
22:c6ee8136847e | 77 | |
Rohit Grover |
22:c6ee8136847e | 78 | static void btle_handler(ble_evt_t *p_ble_evt) |
Rohit Grover |
22:c6ee8136847e | 79 | { |
Rohit Grover |
22:c6ee8136847e | 80 | /* Library service handlers */ |
Rohit Grover |
32:84dea0924a63 | 81 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 82 | ble_bondmngr_on_ble_evt(p_ble_evt); |
Rohit Grover |
32:84dea0924a63 | 83 | #endif |
Rohit Grover |
22:c6ee8136847e | 84 | ble_conn_params_on_ble_evt(p_ble_evt); |
Rohit Grover |
22:c6ee8136847e | 85 | |
Rohit Grover |
22:c6ee8136847e | 86 | /* Custom event handler */ |
Rohit Grover |
22:c6ee8136847e | 87 | switch (p_ble_evt->header.evt_id) { |
Rohit Grover |
22:c6ee8136847e | 88 | case BLE_GAP_EVT_CONNECTED: |
Rohit Grover |
22:c6ee8136847e | 89 | nRF51Gap::getInstance().setConnectionHandle( |
Rohit Grover |
22:c6ee8136847e | 90 | p_ble_evt->evt.gap_evt.conn_handle ); |
Rohit Grover |
22:c6ee8136847e | 91 | nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); |
Rohit Grover |
22:c6ee8136847e | 92 | break; |
Rohit Grover |
22:c6ee8136847e | 93 | |
Rohit Grover |
22:c6ee8136847e | 94 | case BLE_GAP_EVT_DISCONNECTED: |
Rohit Grover |
22:c6ee8136847e | 95 | // Since we are not in a connection and have not started advertising, |
Rohit Grover |
22:c6ee8136847e | 96 | // store bonds |
Rohit Grover |
22:c6ee8136847e | 97 | nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); |
Rohit Grover |
32:84dea0924a63 | 98 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 99 | ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); |
Rohit Grover |
32:84dea0924a63 | 100 | #endif |
Rohit Grover |
22:c6ee8136847e | 101 | nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); |
Rohit Grover |
22:c6ee8136847e | 102 | break; |
Rohit Grover |
22:c6ee8136847e | 103 | |
Rohit Grover |
22:c6ee8136847e | 104 | case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { |
Rohit Grover |
22:c6ee8136847e | 105 | ble_gap_sec_params_t sec_params = {0}; |
Rohit Grover |
22:c6ee8136847e | 106 | |
Rohit Grover |
22:c6ee8136847e | 107 | sec_params.timeout = 30; /*< Timeout for Pairing Request or |
Rohit Grover |
22:c6ee8136847e | 108 | * Security Request (in seconds). */ |
Rohit Grover |
22:c6ee8136847e | 109 | sec_params.bond = 1; /**< Perform bonding. */ |
Rohit Grover |
22:c6ee8136847e | 110 | sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; |
Rohit Grover |
22:c6ee8136847e | 111 | sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; |
Rohit Grover |
22:c6ee8136847e | 112 | sec_params.oob = CFG_BLE_SEC_PARAM_OOB; |
Rohit Grover |
22:c6ee8136847e | 113 | sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE; |
Rohit Grover |
22:c6ee8136847e | 114 | sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE; |
Rohit Grover |
22:c6ee8136847e | 115 | |
Rohit Grover |
22:c6ee8136847e | 116 | ASSERT_STATUS_RET_VOID( |
Rohit Grover |
22:c6ee8136847e | 117 | sd_ble_gap_sec_params_reply(nRF51Gap::getInstance(). |
Rohit Grover |
22:c6ee8136847e | 118 | getConnectionHandle(), |
Rohit Grover |
22:c6ee8136847e | 119 | BLE_GAP_SEC_STATUS_SUCCESS, |
Rohit Grover |
22:c6ee8136847e | 120 | &sec_params)); |
Rohit Grover |
22:c6ee8136847e | 121 | } |
Rohit Grover |
22:c6ee8136847e | 122 | break; |
Rohit Grover |
22:c6ee8136847e | 123 | |
Rohit Grover |
22:c6ee8136847e | 124 | case BLE_GAP_EVT_TIMEOUT: |
Rohit Grover |
22:c6ee8136847e | 125 | if (p_ble_evt->evt.gap_evt.params.timeout.src == |
Rohit Grover |
22:c6ee8136847e | 126 | BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { |
Rohit Grover |
22:c6ee8136847e | 127 | nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT); |
Rohit Grover |
22:c6ee8136847e | 128 | } |
Rohit Grover |
22:c6ee8136847e | 129 | break; |
Rohit Grover |
22:c6ee8136847e | 130 | |
Rohit Grover |
22:c6ee8136847e | 131 | case BLE_GATTC_EVT_TIMEOUT: |
Rohit Grover |
22:c6ee8136847e | 132 | case BLE_GATTS_EVT_TIMEOUT: |
Rohit Grover |
22:c6ee8136847e | 133 | // Disconnect on GATT Server and Client timeout events. |
Rohit Grover |
22:c6ee8136847e | 134 | // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, |
Rohit Grover |
22:c6ee8136847e | 135 | // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); |
Rohit Grover |
22:c6ee8136847e | 136 | break; |
Rohit Grover |
22:c6ee8136847e | 137 | |
Rohit Grover |
22:c6ee8136847e | 138 | default: |
Rohit Grover |
22:c6ee8136847e | 139 | break; |
Rohit Grover |
22:c6ee8136847e | 140 | } |
Rohit Grover |
22:c6ee8136847e | 141 | |
Rohit Grover |
22:c6ee8136847e | 142 | nRF51GattServer::getInstance().hwCallback(p_ble_evt); |
Rohit Grover |
22:c6ee8136847e | 143 | } |
Rohit Grover |
22:c6ee8136847e | 144 | |
Rohit Grover |
32:84dea0924a63 | 145 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 146 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 147 | /*! |
Rohit Grover |
22:c6ee8136847e | 148 | @brief Initialises the bond manager |
Rohit Grover |
22:c6ee8136847e | 149 | |
Rohit Grover |
22:c6ee8136847e | 150 | @note Bond data will be cleared on reset if the bond delete |
Rohit Grover |
22:c6ee8136847e | 151 | button is pressed during initialisation (the button is |
Rohit Grover |
22:c6ee8136847e | 152 | defined as CFG_BLE_BOND_DELETE_BUTTON_NUM). |
Rohit Grover |
22:c6ee8136847e | 153 | |
Rohit Grover |
22:c6ee8136847e | 154 | @returns |
Rohit Grover |
22:c6ee8136847e | 155 | */ |
Rohit Grover |
22:c6ee8136847e | 156 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 157 | static error_t bond_manager_init(void) |
Rohit Grover |
22:c6ee8136847e | 158 | { |
Rohit Grover |
22:c6ee8136847e | 159 | ble_bondmngr_init_t bond_para = {0}; |
Rohit Grover |
22:c6ee8136847e | 160 | |
Rohit Grover |
22:c6ee8136847e | 161 | ASSERT_STATUS ( pstorage_init()); |
Rohit Grover |
22:c6ee8136847e | 162 | |
Rohit Grover |
22:c6ee8136847e | 163 | bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND; |
Rohit Grover |
22:c6ee8136847e | 164 | bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR; |
Rohit Grover |
22:c6ee8136847e | 165 | //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; |
Rohit Grover |
22:c6ee8136847e | 166 | bond_para.evt_handler = NULL; |
Rohit Grover |
22:c6ee8136847e | 167 | bond_para.error_handler = service_error_callback; |
Rohit Grover |
22:c6ee8136847e | 168 | |
Rohit Grover |
22:c6ee8136847e | 169 | ASSERT_STATUS( ble_bondmngr_init( &bond_para )); |
Rohit Grover |
22:c6ee8136847e | 170 | |
Rohit Grover |
22:c6ee8136847e | 171 | /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ |
Rohit Grover |
22:c6ee8136847e | 172 | // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, |
Rohit Grover |
22:c6ee8136847e | 173 | // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, |
Rohit Grover |
22:c6ee8136847e | 174 | // ble_flash_on_radio_active_evt) ); |
Rohit Grover |
22:c6ee8136847e | 175 | |
Rohit Grover |
22:c6ee8136847e | 176 | return ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 177 | } |
Rohit Grover |
32:84dea0924a63 | 178 | #endif // #if NEED_BOND_MANAGER |
Rohit Grover |
22:c6ee8136847e | 179 | |
Rohit Grover |
32:84dea0924a63 | 180 | #if NEED_BOND_MANAGER /* disabled by default */ |
Rohit Grover |
22:c6ee8136847e | 181 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 182 | /*! |
Rohit Grover |
22:c6ee8136847e | 183 | @brief |
Rohit Grover |
22:c6ee8136847e | 184 | @param[in] nrf_error |
Rohit Grover |
22:c6ee8136847e | 185 | @returns |
Rohit Grover |
22:c6ee8136847e | 186 | */ |
Rohit Grover |
22:c6ee8136847e | 187 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 188 | static void service_error_callback(uint32_t nrf_error) |
Rohit Grover |
22:c6ee8136847e | 189 | { |
Rohit Grover |
22:c6ee8136847e | 190 | ASSERT_STATUS_RET_VOID( nrf_error ); |
Rohit Grover |
22:c6ee8136847e | 191 | } |
Rohit Grover |
32:84dea0924a63 | 192 | #endif // #if NEED_BOND_MANAGER |
Rohit Grover |
22:c6ee8136847e | 193 | |
Rohit Grover |
22:c6ee8136847e | 194 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 195 | /*! |
Rohit Grover |
22:c6ee8136847e | 196 | @brief Callback when an error occurs inside the SoftDevice |
Rohit Grover |
22:c6ee8136847e | 197 | |
Rohit Grover |
22:c6ee8136847e | 198 | @param[in] line_num |
Rohit Grover |
22:c6ee8136847e | 199 | @param[in] p-file_name |
Rohit Grover |
22:c6ee8136847e | 200 | |
Rohit Grover |
22:c6ee8136847e | 201 | @returns |
Rohit Grover |
22:c6ee8136847e | 202 | */ |
Rohit Grover |
22:c6ee8136847e | 203 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 204 | void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) |
Rohit Grover |
22:c6ee8136847e | 205 | { |
Rohit Grover |
22:c6ee8136847e | 206 | ASSERT(false, (void) 0); |
Rohit Grover |
22:c6ee8136847e | 207 | } |
Rohit Grover |
22:c6ee8136847e | 208 | |
Rohit Grover |
22:c6ee8136847e | 209 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 210 | /*! |
Rohit Grover |
22:c6ee8136847e | 211 | @brief Handler for general errors above the SoftDevice layer. |
Rohit Grover |
22:c6ee8136847e | 212 | Typically we can' recover from this so we do a reset. |
Rohit Grover |
22:c6ee8136847e | 213 | |
Rohit Grover |
22:c6ee8136847e | 214 | @param[in] error_code |
Rohit Grover |
22:c6ee8136847e | 215 | @param[in] line_num |
Rohit Grover |
22:c6ee8136847e | 216 | @param[in] p-file_name |
Rohit Grover |
22:c6ee8136847e | 217 | |
Rohit Grover |
22:c6ee8136847e | 218 | @returns |
Rohit Grover |
22:c6ee8136847e | 219 | */ |
Rohit Grover |
22:c6ee8136847e | 220 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 221 | void app_error_handler(uint32_t error_code, |
Rohit Grover |
22:c6ee8136847e | 222 | uint32_t line_num, |
Rohit Grover |
22:c6ee8136847e | 223 | const uint8_t *p_file_name) |
Rohit Grover |
22:c6ee8136847e | 224 | { |
Rohit Grover |
22:c6ee8136847e | 225 | ASSERT_STATUS_RET_VOID( error_code ); |
Rohit Grover |
22:c6ee8136847e | 226 | NVIC_SystemReset(); |
Rohit Grover |
22:c6ee8136847e | 227 | } |