テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8468a4403fea 1 /* mbed Microcontroller Library
jksoft 0:8468a4403fea 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 0:8468a4403fea 3 *
jksoft 0:8468a4403fea 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:8468a4403fea 5 * you may not use this file except in compliance with the License.
jksoft 0:8468a4403fea 6 * You may obtain a copy of the License at
jksoft 0:8468a4403fea 7 *
jksoft 0:8468a4403fea 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:8468a4403fea 9 *
jksoft 0:8468a4403fea 10 * Unless required by applicable law or agreed to in writing, software
jksoft 0:8468a4403fea 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:8468a4403fea 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:8468a4403fea 13 * See the License for the specific language governing permissions and
jksoft 0:8468a4403fea 14 * limitations under the License.
jksoft 0:8468a4403fea 15 */
jksoft 0:8468a4403fea 16
jksoft 0:8468a4403fea 17 #include "common/common.h"
jksoft 0:8468a4403fea 18 #include "nordic_common.h"
jksoft 0:8468a4403fea 19
jksoft 0:8468a4403fea 20 #include "app_timer.h"
jksoft 0:8468a4403fea 21 #include "btle.h"
jksoft 0:8468a4403fea 22
jksoft 0:8468a4403fea 23 #include "ble_stack_handler_types.h"
jksoft 0:8468a4403fea 24 #include "ble_radio_notification.h"
jksoft 0:8468a4403fea 25 #include "ble_flash.h"
jksoft 0:8468a4403fea 26 #if NEED_BOND_MANAGER
jksoft 0:8468a4403fea 27 #include "ble_bondmngr.h"
jksoft 0:8468a4403fea 28 #endif
jksoft 0:8468a4403fea 29 #include "ble_conn_params.h"
jksoft 0:8468a4403fea 30
jksoft 0:8468a4403fea 31 #include "btle_gap.h"
jksoft 0:8468a4403fea 32 #include "btle_advertising.h"
jksoft 0:8468a4403fea 33 #include "custom/custom_helper.h"
jksoft 0:8468a4403fea 34
jksoft 0:8468a4403fea 35 #include "softdevice_handler.h"
jksoft 0:8468a4403fea 36 #include "pstorage.h"
jksoft 0:8468a4403fea 37
jksoft 0:8468a4403fea 38 #include "GapEvents.h"
jksoft 0:8468a4403fea 39 #include "nRF51Gap.h"
jksoft 0:8468a4403fea 40 #include "nRF51GattServer.h"
jksoft 0:8468a4403fea 41
jksoft 0:8468a4403fea 42 #include "ble_hci.h"
jksoft 0:8468a4403fea 43
jksoft 0:8468a4403fea 44 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 45 static void service_error_callback(uint32_t nrf_error);
jksoft 0:8468a4403fea 46 #endif
jksoft 0:8468a4403fea 47 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
jksoft 0:8468a4403fea 48 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
jksoft 0:8468a4403fea 49
jksoft 0:8468a4403fea 50 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 51 static error_t bond_manager_init(void);
jksoft 0:8468a4403fea 52 #endif
jksoft 0:8468a4403fea 53
jksoft 0:8468a4403fea 54 static void btle_handler(ble_evt_t *p_ble_evt);
jksoft 0:8468a4403fea 55
jksoft 0:8468a4403fea 56 static void sys_evt_dispatch(uint32_t sys_evt)
jksoft 0:8468a4403fea 57 {
jksoft 0:8468a4403fea 58 #if NEED_PSTORAGE /* disabled by default */
jksoft 0:8468a4403fea 59 pstorage_sys_event_handler(sys_evt);
jksoft 0:8468a4403fea 60 #endif
jksoft 0:8468a4403fea 61 }
jksoft 0:8468a4403fea 62
jksoft 0:8468a4403fea 63 error_t btle_init(void)
jksoft 0:8468a4403fea 64 {
jksoft 0:8468a4403fea 65 const bool useScheduler = false;
jksoft 0:8468a4403fea 66 #ifdef TARGET_HRM1017
jksoft 0:8468a4403fea 67 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, useScheduler);
jksoft 0:8468a4403fea 68 #else
jksoft 0:8468a4403fea 69 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, useScheduler);
jksoft 0:8468a4403fea 70 #endif
jksoft 0:8468a4403fea 71
jksoft 0:8468a4403fea 72 // Enable BLE stack
jksoft 0:8468a4403fea 73 /**
jksoft 0:8468a4403fea 74 * Using this call, the application can select whether to include the
jksoft 0:8468a4403fea 75 * Service Changed characteristic in the GATT Server. The default in all
jksoft 0:8468a4403fea 76 * previous releases has been to include the Service Changed characteristic,
jksoft 0:8468a4403fea 77 * but this affects how GATT clients behave. Specifically, it requires
jksoft 0:8468a4403fea 78 * clients to subscribe to this attribute and not to cache attribute handles
jksoft 0:8468a4403fea 79 * between connections unless the devices are bonded. If the application
jksoft 0:8468a4403fea 80 * does not need to change the structure of the GATT server attributes at
jksoft 0:8468a4403fea 81 * runtime this adds unnecessary complexity to the interaction with peer
jksoft 0:8468a4403fea 82 * clients. If the SoftDevice is enabled with the Service Changed
jksoft 0:8468a4403fea 83 * Characteristics turned off, then clients are allowed to cache attribute
jksoft 0:8468a4403fea 84 * handles making applications simpler on both sides.
jksoft 0:8468a4403fea 85 */
jksoft 0:8468a4403fea 86 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
jksoft 0:8468a4403fea 87 ble_enable_params_t enableParams = {
jksoft 0:8468a4403fea 88 .gatts_enable_params = {
jksoft 0:8468a4403fea 89 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
jksoft 0:8468a4403fea 90 }
jksoft 0:8468a4403fea 91 };
jksoft 0:8468a4403fea 92 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
jksoft 0:8468a4403fea 93 return ERROR_INVALID_PARAM;
jksoft 0:8468a4403fea 94 }
jksoft 0:8468a4403fea 95
jksoft 0:8468a4403fea 96 ble_gap_addr_t addr;
jksoft 0:8468a4403fea 97 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
jksoft 0:8468a4403fea 98 return ERROR_INVALID_PARAM;
jksoft 0:8468a4403fea 99 }
jksoft 0:8468a4403fea 100 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
jksoft 0:8468a4403fea 101 return ERROR_INVALID_PARAM;
jksoft 0:8468a4403fea 102 }
jksoft 0:8468a4403fea 103
jksoft 0:8468a4403fea 104 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
jksoft 0:8468a4403fea 105 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
jksoft 0:8468a4403fea 106
jksoft 0:8468a4403fea 107 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 108 bond_manager_init();
jksoft 0:8468a4403fea 109 #endif
jksoft 0:8468a4403fea 110 btle_gap_init();
jksoft 0:8468a4403fea 111
jksoft 0:8468a4403fea 112 return ERROR_NONE;
jksoft 0:8468a4403fea 113 }
jksoft 0:8468a4403fea 114
jksoft 0:8468a4403fea 115 static void btle_handler(ble_evt_t *p_ble_evt)
jksoft 0:8468a4403fea 116 {
jksoft 0:8468a4403fea 117 /* Library service handlers */
jksoft 0:8468a4403fea 118 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 119 ble_bondmngr_on_ble_evt(p_ble_evt);
jksoft 0:8468a4403fea 120 #endif
jksoft 0:8468a4403fea 121 #if SDK_CONN_PARAMS_MODULE_ENABLE
jksoft 0:8468a4403fea 122 ble_conn_params_on_ble_evt(p_ble_evt);
jksoft 0:8468a4403fea 123 #endif
jksoft 0:8468a4403fea 124
jksoft 0:8468a4403fea 125 /* Custom event handler */
jksoft 0:8468a4403fea 126 switch (p_ble_evt->header.evt_id) {
jksoft 0:8468a4403fea 127 case BLE_GAP_EVT_CONNECTED: {
jksoft 0:8468a4403fea 128 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
jksoft 0:8468a4403fea 129 nRF51Gap::getInstance().setConnectionHandle(handle);
jksoft 0:8468a4403fea 130 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
jksoft 0:8468a4403fea 131 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
jksoft 0:8468a4403fea 132 nRF51Gap::getInstance().processConnectionEvent(handle, static_cast<Gap::addr_type_t>(peer->addr_type), peer->addr, params);
jksoft 0:8468a4403fea 133 break;
jksoft 0:8468a4403fea 134 }
jksoft 0:8468a4403fea 135
jksoft 0:8468a4403fea 136 case BLE_GAP_EVT_DISCONNECTED: {
jksoft 0:8468a4403fea 137 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
jksoft 0:8468a4403fea 138 // Since we are not in a connection and have not started advertising,
jksoft 0:8468a4403fea 139 // store bonds
jksoft 0:8468a4403fea 140 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
jksoft 0:8468a4403fea 141 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 142 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
jksoft 0:8468a4403fea 143 #endif
jksoft 0:8468a4403fea 144
jksoft 0:8468a4403fea 145 Gap::DisconnectionReason_t reason;
jksoft 0:8468a4403fea 146 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
jksoft 0:8468a4403fea 147 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
jksoft 0:8468a4403fea 148 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
jksoft 0:8468a4403fea 149 break;
jksoft 0:8468a4403fea 150 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
jksoft 0:8468a4403fea 151 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
jksoft 0:8468a4403fea 152 break;
jksoft 0:8468a4403fea 153 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
jksoft 0:8468a4403fea 154 reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
jksoft 0:8468a4403fea 155 break;
jksoft 0:8468a4403fea 156 default:
jksoft 0:8468a4403fea 157 /* Please refer to the underlying transport library for an
jksoft 0:8468a4403fea 158 * interpretion of this reason's value. */
jksoft 0:8468a4403fea 159 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
jksoft 0:8468a4403fea 160 break;
jksoft 0:8468a4403fea 161 }
jksoft 0:8468a4403fea 162 nRF51Gap::getInstance().processDisconnectionEvent(handle, reason);
jksoft 0:8468a4403fea 163 break;
jksoft 0:8468a4403fea 164 }
jksoft 0:8468a4403fea 165
jksoft 0:8468a4403fea 166 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
jksoft 0:8468a4403fea 167 ble_gap_sec_params_t sec_params = {0};
jksoft 0:8468a4403fea 168
jksoft 0:8468a4403fea 169 sec_params.timeout = 30; /*< Timeout for Pairing Request or
jksoft 0:8468a4403fea 170 * Security Request (in seconds). */
jksoft 0:8468a4403fea 171 sec_params.bond = 1; /**< Perform bonding. */
jksoft 0:8468a4403fea 172 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
jksoft 0:8468a4403fea 173 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
jksoft 0:8468a4403fea 174 sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
jksoft 0:8468a4403fea 175 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
jksoft 0:8468a4403fea 176 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
jksoft 0:8468a4403fea 177
jksoft 0:8468a4403fea 178 ASSERT_STATUS_RET_VOID(sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(),
jksoft 0:8468a4403fea 179 BLE_GAP_SEC_STATUS_SUCCESS, &sec_params));
jksoft 0:8468a4403fea 180 }
jksoft 0:8468a4403fea 181 break;
jksoft 0:8468a4403fea 182
jksoft 0:8468a4403fea 183 case BLE_GAP_EVT_TIMEOUT:
jksoft 0:8468a4403fea 184 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
jksoft 0:8468a4403fea 185 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
jksoft 0:8468a4403fea 186 }
jksoft 0:8468a4403fea 187 break;
jksoft 0:8468a4403fea 188
jksoft 0:8468a4403fea 189 case BLE_GATTC_EVT_TIMEOUT:
jksoft 0:8468a4403fea 190 case BLE_GATTS_EVT_TIMEOUT:
jksoft 0:8468a4403fea 191 // Disconnect on GATT Server and Client timeout events.
jksoft 0:8468a4403fea 192 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
jksoft 0:8468a4403fea 193 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
jksoft 0:8468a4403fea 194 break;
jksoft 0:8468a4403fea 195
jksoft 0:8468a4403fea 196 default:
jksoft 0:8468a4403fea 197 break;
jksoft 0:8468a4403fea 198 }
jksoft 0:8468a4403fea 199
jksoft 0:8468a4403fea 200 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
jksoft 0:8468a4403fea 201 }
jksoft 0:8468a4403fea 202
jksoft 0:8468a4403fea 203 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 204 /**************************************************************************/
jksoft 0:8468a4403fea 205 /*!
jksoft 0:8468a4403fea 206 @brief Initialises the bond manager
jksoft 0:8468a4403fea 207
jksoft 0:8468a4403fea 208 @note Bond data will be cleared on reset if the bond delete
jksoft 0:8468a4403fea 209 button is pressed during initialisation (the button is
jksoft 0:8468a4403fea 210 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
jksoft 0:8468a4403fea 211
jksoft 0:8468a4403fea 212 @returns
jksoft 0:8468a4403fea 213 */
jksoft 0:8468a4403fea 214 /**************************************************************************/
jksoft 0:8468a4403fea 215 static error_t bond_manager_init(void)
jksoft 0:8468a4403fea 216 {
jksoft 0:8468a4403fea 217 ble_bondmngr_init_t bond_para = {0};
jksoft 0:8468a4403fea 218
jksoft 0:8468a4403fea 219 ASSERT_STATUS ( pstorage_init());
jksoft 0:8468a4403fea 220
jksoft 0:8468a4403fea 221 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND;
jksoft 0:8468a4403fea 222 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
jksoft 0:8468a4403fea 223 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
jksoft 0:8468a4403fea 224 bond_para.evt_handler = NULL;
jksoft 0:8468a4403fea 225 bond_para.error_handler = service_error_callback;
jksoft 0:8468a4403fea 226
jksoft 0:8468a4403fea 227 ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
jksoft 0:8468a4403fea 228
jksoft 0:8468a4403fea 229 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
jksoft 0:8468a4403fea 230 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
jksoft 0:8468a4403fea 231 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
jksoft 0:8468a4403fea 232 // ble_flash_on_radio_active_evt) );
jksoft 0:8468a4403fea 233
jksoft 0:8468a4403fea 234 return ERROR_NONE;
jksoft 0:8468a4403fea 235 }
jksoft 0:8468a4403fea 236 #endif // #if NEED_BOND_MANAGER
jksoft 0:8468a4403fea 237
jksoft 0:8468a4403fea 238 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 0:8468a4403fea 239 /**************************************************************************/
jksoft 0:8468a4403fea 240 /*!
jksoft 0:8468a4403fea 241 @brief
jksoft 0:8468a4403fea 242 @param[in] nrf_error
jksoft 0:8468a4403fea 243 @returns
jksoft 0:8468a4403fea 244 */
jksoft 0:8468a4403fea 245 /**************************************************************************/
jksoft 0:8468a4403fea 246 static void service_error_callback(uint32_t nrf_error)
jksoft 0:8468a4403fea 247 {
jksoft 0:8468a4403fea 248 ASSERT_STATUS_RET_VOID( nrf_error );
jksoft 0:8468a4403fea 249 }
jksoft 0:8468a4403fea 250 #endif // #if NEED_BOND_MANAGER
jksoft 0:8468a4403fea 251
jksoft 0:8468a4403fea 252 /**************************************************************************/
jksoft 0:8468a4403fea 253 /*!
jksoft 0:8468a4403fea 254 @brief Callback when an error occurs inside the SoftDevice
jksoft 0:8468a4403fea 255
jksoft 0:8468a4403fea 256 @param[in] line_num
jksoft 0:8468a4403fea 257 @param[in] p-file_name
jksoft 0:8468a4403fea 258
jksoft 0:8468a4403fea 259 @returns
jksoft 0:8468a4403fea 260 */
jksoft 0:8468a4403fea 261 /**************************************************************************/
jksoft 0:8468a4403fea 262 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
jksoft 0:8468a4403fea 263 {
jksoft 0:8468a4403fea 264 ASSERT(false, (void) 0);
jksoft 0:8468a4403fea 265 }
jksoft 0:8468a4403fea 266
jksoft 0:8468a4403fea 267 /**************************************************************************/
jksoft 0:8468a4403fea 268 /*!
jksoft 0:8468a4403fea 269 @brief Handler for general errors above the SoftDevice layer.
jksoft 0:8468a4403fea 270 Typically we can' recover from this so we do a reset.
jksoft 0:8468a4403fea 271
jksoft 0:8468a4403fea 272 @param[in] error_code
jksoft 0:8468a4403fea 273 @param[in] line_num
jksoft 0:8468a4403fea 274 @param[in] p-file_name
jksoft 0:8468a4403fea 275
jksoft 0:8468a4403fea 276 @returns
jksoft 0:8468a4403fea 277 */
jksoft 0:8468a4403fea 278 /**************************************************************************/
jksoft 0:8468a4403fea 279 void app_error_handler(uint32_t error_code,
jksoft 0:8468a4403fea 280 uint32_t line_num,
jksoft 0:8468a4403fea 281 const uint8_t *p_file_name)
jksoft 0:8468a4403fea 282 {
jksoft 0:8468a4403fea 283 ASSERT_STATUS_RET_VOID( error_code );
jksoft 0:8468a4403fea 284 NVIC_SystemReset();
jksoft 0:8468a4403fea 285 }