test
Fork of nRF51822 by
Diff: source/btle/btle.cpp
- Revision:
- 616:a8f9b022d8fd
- Parent:
- 615:65ea2acfc6a2
diff -r 65ea2acfc6a2 -r a8f9b022d8fd source/btle/btle.cpp --- a/source/btle/btle.cpp Wed Apr 06 22:38:43 2016 +0100 +++ b/source/btle/btle.cpp Wed Apr 06 22:39:17 2016 +0100 @@ -19,7 +19,6 @@ #include "btle.h" -#include "ble_stack_handler_types.h" #include "ble_flash.h" #include "ble_conn_params.h" @@ -27,23 +26,28 @@ #include "btle_advertising.h" #include "custom/custom_helper.h" -#include "softdevice_handler.h" -#include "pstorage.h" +#include "ble/GapEvents.h" +#include "nRF5xn.h" -#include "ble/GapEvents.h" -#include "nRF5xGap.h" -#include "nRF5xGattServer.h" -#include "nRF5xSecurityManager.h" - +extern "C" { +#include "pstorage.h" #include "device_manager.h" +#include "softdevice_handler.h" +#include "ble_stack_handler_types.h" +} #include "ble_hci.h" #include "btle_discovery.h" +#include "nRF5xGattClient.h" +#include "nRF5xServiceDiscovery.h" +#include "nRF5xCharacteristicDescriptorDiscoverer.h" + extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); static void btle_handler(ble_evt_t *p_ble_evt); +static uint32_t gatt_table_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT; static void sys_evt_dispatch(uint32_t sys_evt) { @@ -74,6 +78,18 @@ return NRF_SUCCESS; } +error_t +btle_set_gatt_table_size(uint32_t size) +{ + if (size >= BLE_GATTS_ATTR_TAB_SIZE_MIN) + { + gatt_table_size = size; + return ERROR_NONE; + } + + return ERROR_INVALID_PARAM; +} + error_t btle_init(void) { nrf_clock_lfclksrc_t clockSource; @@ -101,7 +117,8 @@ static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; ble_enable_params_t enableParams = { .gatts_enable_params = { - .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT + .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT, + .attr_tab_size = gatt_table_size } }; if (sd_ble_enable(&enableParams) != NRF_SUCCESS) { @@ -119,9 +136,7 @@ ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); - btle_gap_init(); - - return ERROR_NONE; + return btle_gap_init(); } static void btle_handler(ble_evt_t *p_ble_evt) @@ -137,6 +152,11 @@ bleGattcEventHandler(p_ble_evt); #endif + nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE); + nRF5xGap &gap = (nRF5xGap &) ble.getGap(); + nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer(); + nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager(); + /* Custom event handler */ switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: { @@ -147,14 +167,14 @@ #else Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role); #endif - nRF5xGap::getInstance().setConnectionHandle(handle); + gap.setConnectionHandle(handle); const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; - nRF5xGap::getInstance().processConnectionEvent(handle, + gap.processConnectionEvent(handle, role, - static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr, - static_cast<Gap::AddressType_t>(own->addr_type), own->addr, + static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr, + static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr, params); break; } @@ -163,7 +183,7 @@ Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; // Since we are not in a connection and have not started advertising, // store bonds - nRF5xGap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); + gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID); Gap::DisconnectionReason_t reason; switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { @@ -182,16 +202,24 @@ reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); break; } - nRF5xGap::getInstance().processDisconnectionEvent(handle, reason); + + // Close all pending discoveries for this connection +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) + nRF5xGattClient& gattClient = ble.getGattClient(); + gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE); + gattClient.discovery().terminate(handle); +#endif + + gap.processDisconnectionEvent(handle, reason); break; } case BLE_GAP_EVT_PASSKEY_DISPLAY: - nRF5xSecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); + securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); break; case BLE_GAP_EVT_TIMEOUT: - nRF5xGap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); + gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); break; case BLE_GATTC_EVT_TIMEOUT: @@ -203,12 +231,12 @@ case BLE_GAP_EVT_ADV_REPORT: { const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; - nRF5xGap::getInstance().processAdvertisementReport(advReport->peer_addr.addr, - advReport->rssi, - advReport->scan_rsp, - static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), - advReport->dlen, - advReport->data); + gap.processAdvertisementReport(advReport->peer_addr.addr, + advReport->rssi, + advReport->scan_rsp, + static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), + advReport->dlen, + advReport->data); break; } @@ -216,7 +244,7 @@ break; } - nRF5xGattServer::getInstance().hwCallback(p_ble_evt); + gattServer.hwCallback(p_ble_evt); } /*! @brief Callback when an error occurs inside the SoftDevice */