From 1f15da7ae0c708812ff203588458f16a1d331f4f Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Dec 2017 16:49:12 +0700 Subject: [PATCH] Enable data length extension --- cores/nRF5/debug.cpp | 4 +- libraries/Bluefruit52Lib/src/BLEGap.cpp | 47 +++++++++++++++++++--- libraries/Bluefruit52Lib/src/bluefruit.cpp | 8 ++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/cores/nRF5/debug.cpp b/cores/nRF5/debug.cpp index bda1f30a..40a6345e 100644 --- a/cores/nRF5/debug.cpp +++ b/cores/nRF5/debug.cpp @@ -227,6 +227,8 @@ void dbgDumpMemory(void const *buf, uint8_t size, uint16_t count, bool printOffs #include "ble.h" #include "ble_hci.h" +// TODO require update when upgrading SoftDevice + // Common BLE Event base static const char* _base_evt_str[] = { @@ -256,8 +258,6 @@ static const char* _gap_evt_str[] = "BLE_GAP_EVT_SEC_REQUEST" , "BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST" , "BLE_GAP_EVT_SCAN_REQ_REPORT" , - "BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST" , - "BLE_GAP_EVT_SCAN_REQ_REPORT" , "BLE_GAP_EVT_PHY_UPDATE_REQUEST" , "BLE_GAP_EVT_PHY_UPDATE" , "BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST" , diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 2298dd2f..a67022b9 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -182,7 +182,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt) } break; - #if SD_VER < 500 +#if SD_VER < 500 case BLE_EVT_TX_COMPLETE: if ( peer->hvn_tx_sem ) { @@ -193,7 +193,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt) } break; - #else +#else case BLE_GATTS_EVT_HVN_TX_COMPLETE: if ( peer->hvn_tx_sem ) { @@ -215,19 +215,56 @@ void BLEGap::_eventHandler(ble_evt_t* evt) break; case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST: + { + ble_gap_data_length_params_t* param = &evt->evt.gap_evt.params.data_length_update_request.peer_params; + LOG_LV2("GAP", "Data Length Req is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us", + param->max_tx_octets, param->max_rx_octets, param->max_tx_time_us, param->max_rx_time_us); + // Let Softdevice decide the data length - // ble_gap_data_length_params_t* param = &evt->evt.gap_evt.params.data_length_update_request.peer_params VERIFY_STATUS( sd_ble_gap_data_length_update(conn_handle, NULL, NULL), ); + } break; case BLE_GAP_EVT_DATA_LENGTH_UPDATE: { ble_gap_data_length_params_t* datalen = &evt->evt.gap_evt.params.data_length_update.effective_params; - LOG_LV1("GAP", "Data Length is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us", + LOG_LV2("GAP", "Data Length is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us", datalen->max_tx_octets, datalen->max_rx_octets, datalen->max_tx_time_us, datalen->max_rx_time_us); } break; + case BLE_GAP_EVT_PHY_UPDATE_REQUEST: + { + ble_gap_phys_t* req_phy = &evt->evt.gap_evt.params.phy_update_request.peer_preferred_phys; + + #if CFG_DEBUG >= 1 + char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" }; + LOG_LV1("GAP", "PHY request tx: %s, rx: %s", phy_str[req_phy->tx_phys], phy_str[req_phy->rx_phys]); + #endif + + // Tell SoftDevice to choose PHY automatically + ble_gap_phys_t phy = { BLE_GAP_PHY_AUTO, BLE_GAP_PHY_AUTO }; + (void) sd_ble_gap_phy_update(conn_handle, &phy); + } + break; + + case BLE_GAP_EVT_PHY_UPDATE: + { + ble_gap_evt_phy_update_t* active_phy = &evt->evt.gap_evt.params.phy_update; + + #if CFG_DEBUG >= 1 + if ( active_phy->status != BLE_HCI_STATUS_CODE_SUCCESS ) + { + LOG_LV1("GAP", "Failed HCI status = 0x%02X", active_phy->status); + }else + { + char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" }; + LOG_LV1("GAP", "PHY active tx: %s, rx: %s", phy_str[active_phy->tx_phy], phy_str[active_phy->rx_phy]); + } + #endif + } + break; + case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: { peer->att_mtu = minof(evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, BLEGATT_ATT_MTU_MAX); @@ -237,7 +274,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt) } break; - #endif +#endif default: break; } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 231c0a35..83ce89a5 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -251,6 +251,14 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) LOG_LV1("CFG", "SoftDevice's RAM requires: 0x%08X", ram_start); VERIFY_STATUS(err); + + /*------------- Configure BLE Option -------------*/ + ble_opt_t opt; + + varclr(&opt); + opt.common_opt.conn_evt_ext.enable = 1; // enable Data Length Extension + + VERIFY_STATUS( sd_ble_opt_set(BLE_COMMON_OPT_CONN_EVT_EXT, &opt) ); #endif