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