Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
btle.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "common/common.h" 00018 #include "nordic_common.h" 00019 00020 #include "btle.h" 00021 00022 #include "ble_stack_handler_types.h " 00023 #include "ble_flash.h " 00024 #include "ble_conn_params.h " 00025 00026 #include "btle_gap.h" 00027 #include "btle_advertising.h" 00028 #include "custom/custom_helper.h" 00029 00030 #include "softdevice_handler.h " 00031 #include "pstorage.h " 00032 00033 #include "GapEvents.h" 00034 #include "nRF51Gap.h" 00035 #include "nRF51GattServer.h" 00036 #include "device_manager.h " 00037 00038 #include "ble_hci.h" 00039 00040 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); 00041 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); 00042 00043 static void btle_handler(ble_evt_t *p_ble_evt); 00044 00045 static void sys_evt_dispatch(uint32_t sys_evt) 00046 { 00047 pstorage_sys_event_handler(sys_evt); 00048 } 00049 00050 error_t btle_init(void) 00051 { 00052 #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017) 00053 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, NULL); 00054 #else 00055 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL); 00056 #endif 00057 00058 // Enable BLE stack 00059 /** 00060 * Using this call, the application can select whether to include the 00061 * Service Changed characteristic in the GATT Server. The default in all 00062 * previous releases has been to include the Service Changed characteristic, 00063 * but this affects how GATT clients behave. Specifically, it requires 00064 * clients to subscribe to this attribute and not to cache attribute handles 00065 * between connections unless the devices are bonded. If the application 00066 * does not need to change the structure of the GATT server attributes at 00067 * runtime this adds unnecessary complexity to the interaction with peer 00068 * clients. If the SoftDevice is enabled with the Service Changed 00069 * Characteristics turned off, then clients are allowed to cache attribute 00070 * handles making applications simpler on both sides. 00071 */ 00072 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; 00073 ble_enable_params_t enableParams = { 00074 .gatts_enable_params = { 00075 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT 00076 } 00077 }; 00078 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) { 00079 return ERROR_INVALID_PARAM; 00080 } 00081 00082 ble_gap_addr_t addr; 00083 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { 00084 return ERROR_INVALID_PARAM; 00085 } 00086 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { 00087 return ERROR_INVALID_PARAM; 00088 } 00089 00090 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); 00091 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); 00092 00093 btle_gap_init(); 00094 00095 return ERROR_NONE; 00096 } 00097 00098 static void btle_handler(ble_evt_t *p_ble_evt) 00099 { 00100 /* Library service handlers */ 00101 #if SDK_CONN_PARAMS_MODULE_ENABLE 00102 ble_conn_params_on_ble_evt(p_ble_evt); 00103 #endif 00104 00105 dm_ble_evt_handler(p_ble_evt); 00106 00107 /* Custom event handler */ 00108 switch (p_ble_evt->header.evt_id) { 00109 case BLE_GAP_EVT_CONNECTED: { 00110 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00111 nRF51Gap::getInstance().setConnectionHandle(handle); 00112 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); 00113 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; 00114 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; 00115 nRF51Gap::getInstance().processConnectionEvent(handle, 00116 static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr, 00117 static_cast<Gap::AddressType_t>(own->addr_type), own->addr, 00118 params); 00119 break; 00120 } 00121 00122 case BLE_GAP_EVT_DISCONNECTED: { 00123 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00124 // Since we are not in a connection and have not started advertising, 00125 // store bonds 00126 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); 00127 00128 Gap::DisconnectionReason_t reason; 00129 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { 00130 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: 00131 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; 00132 break; 00133 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: 00134 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; 00135 break; 00136 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: 00137 reason = Gap::CONN_INTERVAL_UNACCEPTABLE; 00138 break; 00139 default: 00140 /* Please refer to the underlying transport library for an 00141 * interpretion of this reason's value. */ 00142 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); 00143 break; 00144 } 00145 nRF51Gap::getInstance().processDisconnectionEvent(handle, reason); 00146 break; 00147 } 00148 00149 case BLE_GAP_EVT_PASSKEY_DISPLAY: 00150 nRF51Gap::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); 00151 break; 00152 00153 case BLE_GAP_EVT_TIMEOUT: 00154 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) { 00155 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT); 00156 } 00157 break; 00158 00159 case BLE_GATTC_EVT_TIMEOUT: 00160 case BLE_GATTS_EVT_TIMEOUT: 00161 // Disconnect on GATT Server and Client timeout events. 00162 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, 00163 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); 00164 break; 00165 00166 case BLE_GAP_EVT_ADV_REPORT: { 00167 const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; 00168 nRF51Gap::getInstance().processAdvertisementReport(advReport->peer_addr.addr, 00169 advReport->rssi, 00170 advReport->scan_rsp, 00171 static_cast<Gap::AdvertisementType_t>(advReport->type), 00172 advReport->dlen, 00173 advReport->data); 00174 break; 00175 } 00176 00177 default: 00178 break; 00179 } 00180 00181 nRF51GattServer::getInstance().hwCallback(p_ble_evt); 00182 } 00183 00184 /*! @brief Callback when an error occurs inside the SoftDevice */ 00185 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) 00186 { 00187 ASSERT(false, (void) 0); 00188 } 00189 00190 /*! 00191 @brief Handler for general errors above the SoftDevice layer. 00192 Typically we can' recover from this so we do a reset. 00193 */ 00194 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) 00195 { 00196 ASSERT_STATUS_RET_VOID( error_code ); 00197 NVIC_SystemReset(); 00198 }
Generated on Tue Jul 12 2022 19:22:46 by
1.7.2
