Originally from Donal's blog article. http://mbed.org/users/donalm/code/BLE_Health_Thermometer_Blog/ Changed low freq. clock source from XTAL to IRC.

Dependents:   BLE_Health_Thermometer_IRC BLE_RCBController_micono_test BLE_konashi_PIO_test BLE_ADT7410_TMP102_Sample ... more

Fork of BLE_API_Native_blog by Donal Morrissey

Committer:
ktownsend
Date:
Thu Feb 06 11:51:06 2014 +0000
Revision:
0:4c3097c65247
Native mode drivers

Who changed what in which revision?

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