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:
Fri Feb 21 09:33:31 2014 +0000
Revision:
17:d5600677d532
Child:
19:d84e7516652b
Added GATT callback support

Who changed what in which revision?

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