/ 11111

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers btle.cpp Source File

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 "ble/GapEvents.h"
00034 #include "nRF51Gap.h"
00035 #include "nRF51GattServer.h"
00036 #include "nRF51SecurityManager.h"
00037 
00038 #include "device_manager.h "
00039 
00040 #include "ble_hci.h"
00041 #include "btle_discovery.h"
00042 
00043 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
00044 void            app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
00045 
00046 static void btle_handler(ble_evt_t *p_ble_evt);
00047 
00048 static void sys_evt_dispatch(uint32_t sys_evt)
00049 {
00050     pstorage_sys_event_handler(sys_evt);
00051 }
00052 
00053 error_t btle_init(void)
00054 {
00055 #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017)
00056     SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, NULL);
00057 #else
00058     SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
00059 #endif
00060 
00061     // Enable BLE stack
00062     /**
00063      * Using this call, the application can select whether to include the
00064      * Service Changed characteristic in the GATT Server. The default in all
00065      * previous releases has been to include the Service Changed characteristic,
00066      * but this affects how GATT clients behave. Specifically, it requires
00067      * clients to subscribe to this attribute and not to cache attribute handles
00068      * between connections unless the devices are bonded. If the application
00069      * does not need to change the structure of the GATT server attributes at
00070      * runtime this adds unnecessary complexity to the interaction with peer
00071      * clients. If the SoftDevice is enabled with the Service Changed
00072      * Characteristics turned off, then clients are allowed to cache attribute
00073      * handles making applications simpler on both sides.
00074      */
00075     static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
00076     ble_enable_params_t enableParams = {
00077         .gatts_enable_params = {
00078             .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
00079         }
00080     };
00081     if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
00082         return ERROR_INVALID_PARAM;
00083     }
00084 
00085     ble_gap_addr_t addr;
00086     if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
00087         return ERROR_INVALID_PARAM;
00088     }
00089     if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
00090         return ERROR_INVALID_PARAM;
00091     }
00092 
00093     ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
00094     ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
00095 
00096     btle_gap_init();
00097 
00098     return ERROR_NONE;
00099 }
00100 
00101 static void btle_handler(ble_evt_t *p_ble_evt)
00102 {
00103     /* Library service handlers */
00104 #if SDK_CONN_PARAMS_MODULE_ENABLE
00105     ble_conn_params_on_ble_evt(p_ble_evt);
00106 #endif
00107 
00108     dm_ble_evt_handler(p_ble_evt);
00109 
00110     bleGattcEventHandler(p_ble_evt);
00111 
00112     /* Custom event handler */
00113     switch (p_ble_evt->header.evt_id) {
00114         case BLE_GAP_EVT_CONNECTED: {
00115             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00116             nRF51Gap::getInstance().setConnectionHandle(handle);
00117             const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
00118             const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
00119             const ble_gap_addr_t *own  = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
00120             nRF51Gap::getInstance().processConnectionEvent(handle,
00121                                                            static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role),
00122                                                            static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr,
00123                                                            static_cast<Gap::AddressType_t>(own->addr_type),  own->addr,
00124                                                            params);
00125             break;
00126         }
00127 
00128         case BLE_GAP_EVT_DISCONNECTED: {
00129             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00130             // Since we are not in a connection and have not started advertising,
00131             // store bonds
00132             nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
00133 
00134             Gap::DisconnectionReason_t reason;
00135             switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
00136                 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
00137                     reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
00138                     break;
00139                 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
00140                     reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
00141                     break;
00142                 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
00143                     reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
00144                     break;
00145                 default:
00146                     /* Please refer to the underlying transport library for an
00147                      * interpretion of this reason's value. */
00148                     reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
00149                     break;
00150             }
00151             nRF51Gap::getInstance().processDisconnectionEvent(handle, reason);
00152             break;
00153         }
00154 
00155         case BLE_GAP_EVT_PASSKEY_DISPLAY:
00156             nRF51SecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
00157             break;
00158 
00159         case BLE_GAP_EVT_TIMEOUT:
00160             nRF51Gap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
00161             break;
00162 
00163         case BLE_GATTC_EVT_TIMEOUT:
00164         case BLE_GATTS_EVT_TIMEOUT:
00165             // Disconnect on GATT Server and Client timeout events.
00166             // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
00167             // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
00168             break;
00169 
00170         case BLE_GAP_EVT_ADV_REPORT: {
00171             const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
00172             nRF51Gap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
00173                                                                advReport->rssi,
00174                                                                advReport->scan_rsp,
00175                                                                static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
00176                                                                advReport->dlen,
00177                                                                advReport->data);
00178             break;
00179         }
00180 
00181         default:
00182             break;
00183     }
00184 
00185     nRF51GattServer::getInstance().hwCallback(p_ble_evt);
00186 }
00187 
00188 /*! @brief      Callback when an error occurs inside the SoftDevice */
00189 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
00190 {
00191     ASSERT(false, (void) 0);
00192 }
00193 
00194 /*!
00195     @brief      Handler for general errors above the SoftDevice layer.
00196                 Typically we can' recover from this so we do a reset.
00197 */
00198 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
00199 {
00200     ASSERT_STATUS_RET_VOID( error_code );
00201     NVIC_SystemReset();
00202 }