Skip to content

Commit

Permalink
Merge pull request adafruit#594 from ogatatsu/fix_connection
Browse files Browse the repository at this point in the history
Fix connection parameter
  • Loading branch information
hathach authored Oct 13, 2020
2 parents 405d48c + 2794dfe commit ee23edf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
18 changes: 16 additions & 2 deletions libraries/Bluefruit52Lib/src/BLEConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
_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 = evt_connected->conn_params.max_conn_interval;
_slave_latency = evt_connected->conn_params.slave_latency;
_sup_timeout = evt_connected->conn_params.conn_sup_timeout;
_peer_addr = evt_connected->peer_addr;
_role = evt_connected->role;

Expand Down Expand Up @@ -108,6 +110,16 @@ uint16_t BLEConnection::getConnectionInterval(void)
return _conn_interval;
}

uint16_t BLEConnection::getSlaveLatency(void)
{
return _slave_latency;
}

uint16_t BLEConnection::getSupervisionTimeout(void)
{
return _sup_timeout;
}

uint16_t BLEConnection::getDataLength(void)
{
return _data_length;
Expand Down Expand Up @@ -318,8 +330,10 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
{
ble_gap_conn_params_t* param = &evt->evt.gap_evt.params.conn_param_update.conn_params;
_conn_interval = param->max_conn_interval;
_slave_latency = param->slave_latency;
_sup_timeout = param->conn_sup_timeout;

LOG_LV1("GAP", "Conn Interval= %.2f ms, Latency = %d, Supervisor Timeout = %d ms", _conn_interval*1.25f, param->slave_latency, 10*param->conn_sup_timeout);
LOG_LV1("GAP", "Conn Interval= %.2f ms, Latency = %d, Supervisor Timeout = %d ms", _conn_interval*1.25f, _slave_latency, 10*_sup_timeout);
}
break;

Expand Down
4 changes: 4 additions & 0 deletions libraries/Bluefruit52Lib/src/BLEConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class BLEConnection
uint16_t _conn_hdl;
uint16_t _mtu;
uint16_t _conn_interval;
uint16_t _slave_latency;
uint16_t _sup_timeout;
uint16_t _data_length;
uint8_t _phy;

Expand Down Expand Up @@ -80,6 +82,8 @@ class BLEConnection
uint8_t getRole(void);
uint16_t getMtu (void);
uint16_t getConnectionInterval(void);
uint16_t getSlaveLatency(void);
uint16_t getSupervisionTimeout(void);
uint16_t getDataLength(void);
uint8_t getPHY(void);

Expand Down
23 changes: 11 additions & 12 deletions libraries/Bluefruit52Lib/src/BLEPeriph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ bool BLEPeriph::setConnIntervalMS (uint16_t min_ms, uint16_t max_ms)
return setConnInterval( MS100TO125(min_ms), MS100TO125(max_ms) );
}

bool BLEPeriph::setConnSlaveLatency(uint16_t latency)
{
_ppcp.slave_latency = latency;

VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false);

return true;
}

bool BLEPeriph::setConnSupervisionTimeout(uint16_t timeout)
{
_ppcp.conn_sup_timeout = timeout;
Expand Down Expand Up @@ -125,22 +134,13 @@ void BLEPeriph::setDisconnectCallback( ble_disconnect_callback_t fp )

void BLEPeriph::_eventHandler(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 = Bluefruit.Connection(conn_hdl);

switch ( evt->header.evt_id )
{
case BLE_GAP_EVT_CONNECTED:
{
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;

// Connection interval set by Central is out of preferred range
// Try to negotiate with Central using our preferred values
if ( !is_within(_ppcp.min_conn_interval, para->conn_params.min_conn_interval, _ppcp.max_conn_interval) )
{
VERIFY_STATUS( sd_ble_gap_conn_param_update(conn_hdl, &_ppcp), );
}
}

break;

case BLE_GAP_EVT_DISCONNECTED:
Expand All @@ -166,4 +166,3 @@ void BLEPeriph::printInfo(void)
logger.printf("%d ms", _ppcp.conn_sup_timeout*10);
logger.println();
}

1 change: 1 addition & 0 deletions libraries/Bluefruit52Lib/src/BLEPeriph.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BLEPeriph

bool setConnInterval (uint16_t min, uint16_t max);
bool setConnIntervalMS (uint16_t min_ms, uint16_t max_ms);
bool setConnSlaveLatency(uint16_t latency);
bool setConnSupervisionTimeout(uint16_t timeout);
bool setConnSupervisionTimeoutMS(uint16_t timeout_ms);

Expand Down

0 comments on commit ee23edf

Please sign in to comment.