WallbotBLE_Challenge ホワイトボード消し版

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge by JKSoft

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

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