From cbcd9eb3243db1e35bedec505d68d3aa201cb7fb Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 15 Aug 2019 12:32:46 +0700 Subject: [PATCH] implement #80, add requestPHY/getPHY support update prph and central throuhgput example. --- .../central_throughput/central_throughput.ino | 21 ++-- .../Peripheral/throughput/throughput.ino | 111 +++++++++++------- .../Bluefruit52Lib/src/BLEAdvertising.cpp | 21 ++-- .../Bluefruit52Lib/src/BLEConnection.cpp | 82 ++++++++++++- libraries/Bluefruit52Lib/src/BLEConnection.h | 10 +- libraries/Bluefruit52Lib/src/bluefruit.cpp | 56 +-------- 6 files changed, 179 insertions(+), 122 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Central/central_throughput/central_throughput.ino b/libraries/Bluefruit52Lib/examples/Central/central_throughput/central_throughput.ino index de969ecf..97405df5 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_throughput/central_throughput.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_throughput/central_throughput.ino @@ -20,6 +20,8 @@ BLEClientUart clientUart; // bleuart client +uint32_t rx_count = 0; + void setup() { Serial.begin(115200); @@ -27,6 +29,11 @@ void setup() Serial.println("Bluefruit52 Central BLEUART Example"); Serial.println("-----------------------------------\n"); + + // Config the connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configCentralBandwidth(BANDWIDTH_MAX); // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 // SRAM usage required by SoftDevice will increase dramatically with number of connections @@ -36,7 +43,7 @@ void setup() // Init BLE Central Uart Serivce clientUart.begin(); - clientUart.setRxCallback(bleuart_rx_callback); + clientUart.setRxCallback(bleuart_rx_callback); // Increase Blink rate to different from PrPh advertising mode Bluefruit.setConnLedInterval(250); @@ -101,7 +108,7 @@ void connect_callback(uint16_t conn_handle) Serial.println("Found NONE"); // disconnect since we couldn't find bleuart service - Bluefruit.disconnect(conn_handle); + //Bluefruit.disconnect(conn_handle); } } @@ -125,14 +132,8 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) */ void bleuart_rx_callback(BLEClientUart& uart_svc) { - Serial.print("[RX]: "); - - while ( uart_svc.available() ) - { - Serial.print( (char) uart_svc.read() ); - } - - Serial.println(); + int count = uart_svc.available(); + uart_svc.flush(); } void loop() diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino b/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino index c93e964a..beefd190 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino @@ -14,12 +14,18 @@ #include -// String to send in the throughput test -#define TEST_STRING "01234567899876543210" -const int TEST_STRLEN = strlen(TEST_STRING); +/* Best result is + * - 8.74 KB/s with 20 ms, MTU = 23 + * - 23.62 KB/s with 7.5 ms, MTU = 23 + * - 47.85 KB/s with 15 ms, MTU = 247 + */ -// Number of total data sent ( 1024 times the test string) -#define TOTAL_BYTES (1024 * strlen(TEST_STRING)) +// data to send in the throughput test +char test_data[256] = { 0 }; + +// Number of packet to sent +// actualy number of bytes depends on the MTU of the connection +#define PACKET_NUM 1024 BLEDis bledis; BLEUart bleuart; @@ -31,7 +37,7 @@ BLEUart bleuart; */ /**************************************************************************/ void setup(void) -{ +{ Serial.begin(115200); while ( !Serial ) delay(10); // for nrf52840 with native usb @@ -43,11 +49,17 @@ void setup(void) // here in case you want to control this manually via PIN 19 Bluefruit.autoConnLed(true); + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + Bluefruit.begin(); Bluefruit.setTxPower(4); // Check bluefruit.h for supported values Bluefruit.setName("Bluefruit52"); Bluefruit.Periph.setConnectCallback(connect_callback); Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); @@ -90,8 +102,19 @@ void startAdv(void) void connect_callback(uint16_t conn_handle) { + BLEConnection* conn = Bluefruit.Connection(conn_handle); (void) conn_handle; Serial.println("Connected"); + + + // request PHY changed to 2MB + + + // request to update data length + conn->requestDataLengthUpdate(); + + // request mtu exchange + conn->requestMtuExchange(247); } /** @@ -108,53 +131,55 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) Serial.println("Disconnected"); } -/**************************************************************************/ -/*! - @brief Constantly poll for new command or response data -*/ -/**************************************************************************/ -void loop(void) + +void test_throughput(void) { uint32_t start, stop, sent; - uint32_t remaining = TOTAL_BYTES; start = stop = sent = 0; + const uint16_t data_mtu = Bluefruit.Connection(0)->getMtu() - 3; + memset(test_data, '1', data_mtu); + + uint32_t remaining = data_mtu*PACKET_NUM; + + Serial.print("Sending "); + Serial.print(remaining); + Serial.println(" bytes ..."); + Serial.flush(); + + start = millis(); + while ( (remaining > 0) && Bluefruit.connected() && bleuart.notifyEnabled() ) + { + if ( !bleuart.write(test_data, data_mtu) ) break; + + sent += data_mtu; + remaining -= data_mtu; + } + stop = millis() - start; + + Serial.print("Sent "); + Serial.print(sent); + Serial.print(" bytes in "); + Serial.print(stop / 1000.0F, 2); + Serial.println(" seconds."); + + Serial.println("Speed "); + Serial.print( (sent / 1000.0F) / (stop / 1000.0F), 2); + Serial.println(" KB/s.\r\n"); +} + +void loop(void) +{ if (Bluefruit.connected() && bleuart.notifyEnabled()) { // Wait for user input before trying again Serial.println("Connected. Send a key and press enter to start test"); - getUserInput(); - - Serial.print("Sending "); - Serial.print(remaining); - Serial.println(" bytes ..."); + //getUserInput(); - start = millis(); - while ( (remaining > 0) && Bluefruit.connected() && bleuart.notifyEnabled() ) - { - if ( !bleuart.print(TEST_STRING) ) break; + test_throughput(); - sent += TEST_STRLEN; - remaining -= TEST_STRLEN; - - // Only print every 100th time - // if ( (sent % (100*TEST_STRLEN) ) == 0 ) - // { - // Serial.print("Sent: "); Serial.print(sent); - // Serial.print(" Remaining: "); Serial.println(remaining); - // } - } - stop = millis() - start; - - Serial.print("Sent "); - Serial.print(sent); - Serial.print(" bytes in "); - Serial.print(stop / 1000.0F, 2); - Serial.println(" seconds."); - - Serial.println("Speed "); - Serial.print( (sent / 1000.0F) / (stop / 1000.0F), 2); - Serial.println(" KB/s.\r\n"); + Bluefruit.disconnect(0); + delay(2000); } } diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp index 35852fb5..8cd51915 100644 --- a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp @@ -340,19 +340,18 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout) // ADV Params ble_gap_adv_params_t adv_para = { - .properties = { - .type = _type, - .anonymous = 0 - }, - .p_peer_addr = NULL , // Undirected advertisement - .interval = interval , // advertising interval (in units of 0.625 ms) - .duration = (uint16_t) (timeout*100), // in 10-ms unit + .properties = { .type = _type, .anonymous = 0 }, + .p_peer_addr = NULL , // Undirected advertisement + .interval = interval , // advertising interval (in units of 0.625 ms) + .duration = (uint16_t) (timeout*100) , // in 10-ms unit - .max_adv_evts = 0, // TODO can be used for fast/slow mode - .channel_mask = { 0, 0, 0, 0, 0 } , // 40 channel, set 1 to disable - .filter_policy = BLE_GAP_ADV_FP_ANY , + .max_adv_evts = 0 , // TODO can be used for fast/slow mode + .channel_mask = { 0, 0, 0, 0, 0 } , // 40 channel, set 1 to disable + .filter_policy = BLE_GAP_ADV_FP_ANY , - //.primary_phy, .secondary_phy, .set_id, .scan_req_notification + .primary_phy = BLE_GAP_PHY_AUTO , // 1 Mbps will be used + .secondary_phy = BLE_GAP_PHY_AUTO , // 1 Mbps will be used + // , .set_id, .scan_req_notification }; // gap_adv long-live is required by SD v6 diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 5139a58b..35a6cab9 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -46,6 +46,8 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e _connected = true; _mtu = BLE_GATT_ATT_MTU_DEFAULT; + _data_length = BLE_GATT_ATT_MTU_DEFAULT + 4; // 27 + _phy = BLE_GAP_PHY_1MBPS; _conn_interval = 0; _peer_addr = evt_connected->peer_addr; _role = evt_connected->role; @@ -99,6 +101,16 @@ uint16_t BLEConnection::getConnInterval(void) return _conn_interval; } +uint16_t BLEConnection::getDataLength(void) +{ + return _data_length; +} + +uint8_t BLEConnection::getPHY(void) +{ + return _phy; +} + ble_gap_addr_t BLEConnection::getPeerAddr (void) { return _peer_addr; @@ -138,12 +150,19 @@ bool BLEConnection::requestMtuExchange(uint16_t mtu) return true; } -bool BLEConnection::updateDataLength(ble_gap_data_length_params_t const *p_dl_params, ble_gap_data_length_limitation_t *p_dl_limitation) +bool BLEConnection::requestDataLengthUpdate(ble_gap_data_length_params_t const *p_dl_params, ble_gap_data_length_limitation_t *p_dl_limitation) { VERIFY_STATUS(sd_ble_gap_data_length_update(_conn_hdl, p_dl_params, p_dl_limitation), false); return true; } +bool BLEConnection::requestPHY(uint8_t phy) +{ + ble_gap_phys_t gap_phy = { .tx_phys = phy, .rx_phys = phy }; + VERIFY_STATUS( sd_ble_gap_phy_update(_conn_hdl, &gap_phy), false); + return true; +} + bool BLEConnection::disconnect(void) { return ERROR_NONE == sd_ble_gap_disconnect(_conn_hdl, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); @@ -421,6 +440,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) } break; + //------------- MTU -------------// case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: { uint16_t const max_mtu = Bluefruit.getMaxMtu(_role); @@ -437,6 +457,66 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) LOG_LV1("GAP", "ATT MTU is changed to %d", _mtu); break; + //------------- Data Length -------------// + 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; + (void) param; + + LOG_LV1("GAP", "Data Length Request 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 + VERIFY_STATUS( sd_ble_gap_data_length_update(_conn_hdl, 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; + (void) datalen; + + LOG_LV1("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); + + _data_length = datalen->max_tx_octets; + } + break; + + //------------- PHY -------------// + case BLE_GAP_EVT_PHY_UPDATE_REQUEST: + { + ble_gap_phys_t* req_phy = &evt->evt.gap_evt.params.phy_update_request.peer_preferred_phys; + char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" }; + + (void) req_phy; + (void) phy_str; + LOG_LV1("GAP", "PHY request tx: %s, rx: %s", phy_str[req_phy->tx_phys], phy_str[req_phy->rx_phys]); + + // 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_hdl, &phy); + } + break; + + case BLE_GAP_EVT_PHY_UPDATE: + { + ble_gap_evt_phy_update_t* active_phy = &evt->evt.gap_evt.params.phy_update; + + 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" }; + (void) phy_str; + LOG_LV1("GAP", "PHY active tx: %s, rx: %s", phy_str[active_phy->tx_phy], phy_str[active_phy->rx_phy]); + + _phy = active_phy->tx_phy; + } + } + break; + //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.h b/libraries/Bluefruit52Lib/src/BLEConnection.h index 3a132dd7..af43570c 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.h +++ b/libraries/Bluefruit52Lib/src/BLEConnection.h @@ -47,8 +47,11 @@ class BLEConnection uint16_t _conn_hdl; uint16_t _mtu; uint16_t _conn_interval; - uint16_t _ediv; + uint16_t _data_length; + uint8_t _phy; + uint8_t _role; + uint16_t _ediv; bool _connected; bool _paired; @@ -76,6 +79,8 @@ class BLEConnection uint8_t getRole(void); uint16_t getMtu (void); uint16_t getConnInterval(void); + uint16_t getDataLength(void); + uint8_t getPHY(void); ble_gap_addr_t getPeerAddr(void); uint16_t getPeerName(char* buf, uint16_t bufsize); @@ -83,8 +88,9 @@ class BLEConnection bool disconnect(void); bool setTxPower(int8_t power); // set power for this connection - bool updateDataLength(ble_gap_data_length_params_t const *p_dl_params = NULL, ble_gap_data_length_limitation_t *p_dl_limitation = NULL); + bool requestDataLengthUpdate(ble_gap_data_length_params_t const *p_dl_params = NULL, ble_gap_data_length_limitation_t *p_dl_limitation = NULL); bool requestMtuExchange(uint16_t mtu); + bool requestPHY(uint8_t phy = BLE_GAP_PHY_AUTO); bool monitorRssi(uint8_t threshold = BLE_GAP_RSSI_THRESHOLD_INVALID); int8_t getRssi(void); diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index fae9a09b..2e08200e 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -779,7 +779,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) uint16_t const conn_hdl = evt->evt.common_evt.conn_handle; BLEConnection* conn = this->Connection(conn_hdl); - LOG_LV1("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), conn_hdl); + LOG_LV2("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), conn_hdl); // GAP handler if ( conn ) conn->_eventHandler(evt); @@ -852,60 +852,6 @@ void AdafruitBluefruit::_ble_handler(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; - (void) param; - - 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 - VERIFY_STATUS( sd_ble_gap_data_length_update(conn_hdl, 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; - (void) datalen; - - 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; - char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" }; - - (void) req_phy; - (void) phy_str; - LOG_LV1("GAP", "PHY request tx: %s, rx: %s", phy_str[req_phy->tx_phys], phy_str[req_phy->rx_phys]); - - // 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_hdl, &phy); - } - break; - - case BLE_GAP_EVT_PHY_UPDATE: - { - ble_gap_evt_phy_update_t* active_phy = &evt->evt.gap_evt.params.phy_update; - - 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" }; - (void) phy_str; - LOG_LV1("GAP", "PHY active tx: %s, rx: %s", phy_str[active_phy->tx_phy], phy_str[active_phy->rx_phy]); - } - } - break; - case BLE_EVT_USER_MEM_REQUEST: // We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ) sd_ble_user_mem_reply(conn_hdl, NULL);