library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

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_flash.h"
00023 #include "ble_conn_params.h"
00024 
00025 #include "btle_gap.h"
00026 #include "btle_advertising.h"
00027 #include "custom/custom_helper.h"
00028 
00029 #include "ble/GapEvents.h"
00030 #include "nRF5xn.h"
00031 
00032 extern "C" {
00033 #include "pstorage.h"
00034 #include "device_manager.h"
00035 #include "softdevice_handler.h"
00036 #include "ble_stack_handler_types.h"
00037 }
00038 
00039 #include "ble_hci.h"
00040 #include "btle_discovery.h"
00041 
00042 #include "nRF5xGattClient.h"
00043 #include "nRF5xServiceDiscovery.h"
00044 #include "nRF5xCharacteristicDescriptorDiscoverer.h"
00045 
00046 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
00047 void            app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
00048 
00049 static void btle_handler(ble_evt_t *p_ble_evt);
00050 
00051 static void sys_evt_dispatch(uint32_t sys_evt)
00052 {
00053     pstorage_sys_event_handler(sys_evt);
00054 }
00055 
00056 /**
00057  * This function is called in interrupt context to handle BLE events; i.e. pull
00058  * system and user events out of the pending events-queue of the BLE stack. The
00059  * BLE stack signals the availability of events by the triggering the SWI2
00060  * interrupt, which forwards the handling to this function.
00061  *
00062  * The event processing loop is implemented in intern_softdevice_events_execute().
00063  *
00064  * In mbed OS, a callback for intern_softdevice_events_execute() is posted
00065  * to the scheduler, which then executes in thread mode. In mbed-classic,
00066  * event processing happens right-away in interrupt context (which is more
00067  * risk-prone). In either case, the logic of event processing is identical.
00068  */
00069 static uint32_t eventHandler()
00070 {
00071 #ifdef YOTTA_CFG_MBED_OS
00072     minar::Scheduler::postCallback(intern_softdevice_events_execute);
00073 #else
00074     intern_softdevice_events_execute();
00075 #endif
00076 
00077     return NRF_SUCCESS;
00078 }
00079 
00080 error_t btle_init(void)
00081 {
00082     nrf_clock_lfclksrc_t clockSource;
00083     if (NRF_CLOCK->LFCLKSRC & (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) {
00084         clockSource = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM;
00085     } else {
00086         clockSource = NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION;
00087     }
00088     SOFTDEVICE_HANDLER_INIT(clockSource, eventHandler);
00089 
00090     // Enable BLE stack
00091     /**
00092      * Using this call, the application can select whether to include the
00093      * Service Changed characteristic in the GATT Server. The default in all
00094      * previous releases has been to include the Service Changed characteristic,
00095      * but this affects how GATT clients behave. Specifically, it requires
00096      * clients to subscribe to this attribute and not to cache attribute handles
00097      * between connections unless the devices are bonded. If the application
00098      * does not need to change the structure of the GATT server attributes at
00099      * runtime this adds unnecessary complexity to the interaction with peer
00100      * clients. If the SoftDevice is enabled with the Service Changed
00101      * Characteristics turned off, then clients are allowed to cache attribute
00102      * handles making applications simpler on both sides.
00103      */
00104     static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
00105     ble_enable_params_t enableParams = {
00106         .gatts_enable_params = {
00107             .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
00108         }
00109     };
00110     if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
00111         return ERROR_INVALID_PARAM;
00112     }
00113 
00114     ble_gap_addr_t addr;
00115     if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
00116         return ERROR_INVALID_PARAM;
00117     }
00118     if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
00119         return ERROR_INVALID_PARAM;
00120     }
00121 
00122     ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
00123     ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
00124 
00125     return btle_gap_init();
00126 }
00127 
00128 static void btle_handler(ble_evt_t *p_ble_evt)
00129 {
00130     /* Library service handlers */
00131 #if SDK_CONN_PARAMS_MODULE_ENABLE
00132     ble_conn_params_on_ble_evt(p_ble_evt);
00133 #endif
00134 
00135     dm_ble_evt_handler(p_ble_evt);
00136 
00137 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
00138     bleGattcEventHandler(p_ble_evt);
00139 #endif
00140 
00141     nRF5xn               &ble             = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
00142     nRF5xGap             &gap             = (nRF5xGap &) ble.getGap();
00143     nRF5xGattServer      &gattServer      = (nRF5xGattServer &) ble.getGattServer();
00144     nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();
00145 
00146     /* Custom event handler */
00147     switch (p_ble_evt->header.evt_id) {
00148         case BLE_GAP_EVT_CONNECTED: {
00149             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00150 #if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
00151             /* Only peripheral role is supported by S110 */
00152             Gap::Role_t role = Gap::PERIPHERAL;
00153 #else
00154             Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
00155 #endif
00156             gap.setConnectionHandle(handle);
00157             const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
00158             const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
00159             const ble_gap_addr_t *own  = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
00160             gap.processConnectionEvent(handle,
00161                                                            role,
00162                                                            static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
00163                                                            static_cast<BLEProtocol::AddressType_t>(own->addr_type),  own->addr,
00164                                                            params);
00165             break;
00166         }
00167 
00168         case BLE_GAP_EVT_DISCONNECTED: {
00169             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00170             // Since we are not in a connection and have not started advertising,
00171             // store bonds
00172             gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);
00173 
00174             Gap::DisconnectionReason_t reason;
00175             switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
00176                 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
00177                     reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
00178                     break;
00179                 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
00180                     reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
00181                     break;
00182                 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
00183                     reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
00184                     break;
00185                 default:
00186                     /* Please refer to the underlying transport library for an
00187                      * interpretion of this reason's value. */
00188                     reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
00189                     break;
00190             }
00191 
00192 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
00193             // Close all pending discoveries for this connection
00194             nRF5xGattClient& gattClient = ble.getGattClient();
00195             gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE);
00196             gattClient.discovery().terminate(handle);
00197 #endif
00198 
00199             gap.processDisconnectionEvent(handle, reason);
00200             break;
00201         }
00202 
00203         case BLE_GAP_EVT_PASSKEY_DISPLAY:
00204             securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
00205             break;
00206 
00207         case BLE_GAP_EVT_TIMEOUT:
00208             gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
00209             break;
00210 
00211         case BLE_GATTC_EVT_TIMEOUT:
00212         case BLE_GATTS_EVT_TIMEOUT:
00213             // Disconnect on GATT Server and Client timeout events.
00214             // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
00215             // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
00216             break;
00217 
00218         case BLE_GAP_EVT_ADV_REPORT: {
00219             const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
00220             gap.processAdvertisementReport(advReport->peer_addr.addr,
00221                                            advReport->rssi,
00222                                            advReport->scan_rsp,
00223                                            static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
00224                                            advReport->dlen,
00225                                            advReport->data);
00226             break;
00227         }
00228 
00229         default:
00230             break;
00231     }
00232 
00233     gattServer.hwCallback(p_ble_evt);
00234 }
00235 
00236 /*! @brief      Callback when an error occurs inside the SoftDevice */
00237 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
00238 {
00239     ASSERT(false, (void) 0);
00240 }
00241 
00242 /*!
00243     @brief      Handler for general errors above the SoftDevice layer.
00244                 Typically we can' recover from this so we do a reset.
00245 */
00246 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
00247 {
00248     ASSERT_STATUS_RET_VOID( error_code );
00249     NVIC_SystemReset();
00250 }