implement #80, add requestPHY/getPHY support

update prph and central throuhgput example.
This commit is contained in:
hathach committed 2019-08-15 12:32:46 +07:00
1 parent 4e95f95722
commit cbcd9eb324
6 files changed
+177 -120

No files matched your search

@@ -20,6 +20,8 @@
BLEClientUart clientUart; // bleuart client BLEClientUart clientUart; // bleuart client
uint32_t rx_count = 0;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -28,6 +30,11 @@ void setup()
Serial.println("Bluefruit52 Central BLEUART Example"); Serial.println("Bluefruit52 Central BLEUART Example");
Serial.println("-----------------------------------\n"); 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 // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
// SRAM usage required by SoftDevice will increase dramatically with number of connections // SRAM usage required by SoftDevice will increase dramatically with number of connections
Bluefruit.begin(0, 1); Bluefruit.begin(0, 1);
@@ -101,7 +108,7 @@ void connect_callback(uint16_t conn_handle)
Serial.println("Found NONE"); Serial.println("Found NONE");
// disconnect since we couldn't find bleuart service // 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) void bleuart_rx_callback(BLEClientUart& uart_svc)
{ {
Serial.print("[RX]: "); int count = uart_svc.available();
uart_svc.flush();
while ( uart_svc.available() )
{
Serial.print( (char) uart_svc.read() );
}
Serial.println();
} }
void loop() void loop()
@@ -14,12 +14,18 @@
#include <bluefruit.h> #include <bluefruit.h>
// String to send in the throughput test /* Best result is
#define TEST_STRING "01234567899876543210" * - 8.74 KB/s with 20 ms, MTU = 23
const int TEST_STRLEN = strlen(TEST_STRING); * - 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) // data to send in the throughput test
#define TOTAL_BYTES (1024 * strlen(TEST_STRING)) 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; BLEDis bledis;
BLEUart bleuart; BLEUart bleuart;
@@ -43,11 +49,17 @@ void setup(void)
// here in case you want to control this manually via PIN 19 // here in case you want to control this manually via PIN 19
Bluefruit.autoConnLed(true); 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.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("Bluefruit52"); Bluefruit.setName("Bluefruit52");
Bluefruit.Periph.setConnectCallback(connect_callback); Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback); Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
// Configure and Start Device Information Service // Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries"); bledis.setManufacturer("Adafruit Industries");
@@ -90,8 +102,19 @@ void startAdv(void)
void connect_callback(uint16_t conn_handle) void connect_callback(uint16_t conn_handle)
{ {
BLEConnection* conn = Bluefruit.Connection(conn_handle);
(void) conn_handle; (void) conn_handle;
Serial.println("Connected"); 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"); Serial.println("Disconnected");
} }
/**************************************************************************/
/*! void test_throughput(void)
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{ {
uint32_t start, stop, sent; uint32_t start, stop, sent;
uint32_t remaining = TOTAL_BYTES;
start = stop = sent = 0; 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()) if (Bluefruit.connected() && bleuart.notifyEnabled())
{ {
// Wait for user input before trying again // Wait for user input before trying again
Serial.println("Connected. Send a key and press enter to start test"); Serial.println("Connected. Send a key and press enter to start test");
getUserInput(); //getUserInput();
Serial.print("Sending "); test_throughput();
Serial.print(remaining);
Serial.println(" bytes ...");
start = millis(); Bluefruit.disconnect(0);
while ( (remaining > 0) && Bluefruit.connected() && bleuart.notifyEnabled() ) delay(2000);
{
if ( !bleuart.print(TEST_STRING) ) break;
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");
} }
} }
+10 -11
View File
@@ -340,19 +340,18 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
// ADV Params // ADV Params
ble_gap_adv_params_t adv_para = ble_gap_adv_params_t adv_para =
{ {
.properties = { .properties = { .type = _type, .anonymous = 0 },
.type = _type, .p_peer_addr = NULL , // Undirected advertisement
.anonymous = 0 .interval = interval , // advertising interval (in units of 0.625 ms)
}, .duration = (uint16_t) (timeout*100) , // in 10-ms unit
.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 .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 .channel_mask = { 0, 0, 0, 0, 0 } , // 40 channel, set 1 to disable
.filter_policy = BLE_GAP_ADV_FP_ANY , .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 // gap_adv long-live is required by SD v6
+81 -1
View File
@@ -46,6 +46,8 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
_connected = true; _connected = true;
_mtu = BLE_GATT_ATT_MTU_DEFAULT; _mtu = BLE_GATT_ATT_MTU_DEFAULT;
_data_length = BLE_GATT_ATT_MTU_DEFAULT + 4; // 27
_phy = BLE_GAP_PHY_1MBPS;
_conn_interval = 0; _conn_interval = 0;
_peer_addr = evt_connected->peer_addr; _peer_addr = evt_connected->peer_addr;
_role = evt_connected->role; _role = evt_connected->role;
@@ -99,6 +101,16 @@ uint16_t BLEConnection::getConnInterval(void)
return _conn_interval; 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) ble_gap_addr_t BLEConnection::getPeerAddr (void)
{ {
return _peer_addr; return _peer_addr;
@@ -138,12 +150,19 @@ bool BLEConnection::requestMtuExchange(uint16_t mtu)
return true; 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); VERIFY_STATUS(sd_ble_gap_data_length_update(_conn_hdl, p_dl_params, p_dl_limitation), false);
return true; 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) bool BLEConnection::disconnect(void)
{ {
return ERROR_NONE == sd_ble_gap_disconnect(_conn_hdl, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); 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; break;
//------------- MTU -------------//
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
{ {
uint16_t const max_mtu = Bluefruit.getMaxMtu(_role); 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); LOG_LV1("GAP", "ATT MTU is changed to %d", _mtu);
break; 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;
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// //
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
+8 -2
View File
@@ -47,8 +47,11 @@ class BLEConnection
uint16_t _conn_hdl; uint16_t _conn_hdl;
uint16_t _mtu; uint16_t _mtu;
uint16_t _conn_interval; uint16_t _conn_interval;
uint16_t _ediv; uint16_t _data_length;
uint8_t _phy;
uint8_t _role; uint8_t _role;
uint16_t _ediv;
bool _connected; bool _connected;
bool _paired; bool _paired;
@@ -76,6 +79,8 @@ class BLEConnection
uint8_t getRole(void); uint8_t getRole(void);
uint16_t getMtu (void); uint16_t getMtu (void);
uint16_t getConnInterval(void); uint16_t getConnInterval(void);
uint16_t getDataLength(void);
uint8_t getPHY(void);
ble_gap_addr_t getPeerAddr(void); ble_gap_addr_t getPeerAddr(void);
uint16_t getPeerName(char* buf, uint16_t bufsize); uint16_t getPeerName(char* buf, uint16_t bufsize);
@@ -83,8 +88,9 @@ class BLEConnection
bool disconnect(void); bool disconnect(void);
bool setTxPower(int8_t power); // set power for this connection 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 requestMtuExchange(uint16_t mtu);
bool requestPHY(uint8_t phy = BLE_GAP_PHY_AUTO);
bool monitorRssi(uint8_t threshold = BLE_GAP_RSSI_THRESHOLD_INVALID); bool monitorRssi(uint8_t threshold = BLE_GAP_RSSI_THRESHOLD_INVALID);
int8_t getRssi(void); int8_t getRssi(void);
+1 -55
View File
@@ -779,7 +779,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
uint16_t const conn_hdl = evt->evt.common_evt.conn_handle; uint16_t const conn_hdl = evt->evt.common_evt.conn_handle;
BLEConnection* conn = this->Connection(conn_hdl); 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 // GAP handler
if ( conn ) conn->_eventHandler(evt); if ( conn ) conn->_eventHandler(evt);
@@ -852,60 +852,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
} }
break; 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: case BLE_EVT_USER_MEM_REQUEST:
// We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ) // We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ)
sd_ble_user_mem_reply(conn_hdl, NULL); sd_ble_user_mem_reply(conn_hdl, NULL);