Skip to content

Add ability to update connection params after connect #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BLEDeviceEventListener
virtual void BLEDeviceDisconnected(BLEDevice& /*device*/) { }
virtual void BLEDeviceBonded(BLEDevice& /*device*/) { }
virtual void BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) { }
virtual void BLEDeviceConnectionParamsUpdated(BLEDevice& /*device*/) { }

virtual void BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& /*characteristic*/, const unsigned char* /*value*/, unsigned char /*valueLength*/) { }
virtual void BLEDeviceCharacteristicSubscribedChanged(BLEDevice& /*device*/, BLECharacteristic& /*characteristic*/, bool /*subscribed*/) { }
Expand All @@ -53,6 +54,7 @@ class BLEDevice

void setAdvertisingInterval(unsigned short advertisingInterval);
void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) { }
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);

Expand Down
16 changes: 16 additions & 0 deletions src/BLEPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ void BLEPeripheral::setConnectionInterval(unsigned short minimumConnectionInterv
this->_device->setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
}

void BLEPeripheral::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
this->_device->updateConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
}

void BLEPeripheral::disconnect() {
this->_device->disconnect();
}
Expand Down Expand Up @@ -363,6 +367,18 @@ void BLEPeripheral::BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) {
}
}

void BLEPeripheral::BLEDeviceConnectionParamsUpdated(BLEDevice& /*device*/) {
#ifdef BLE_PERIPHERAL_DEBUG
Serial.print(F("Updated connection parameters from central: "));
Serial.println(this->_central.address());
#endif

BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnectionParamsUpdated];
if (eventHandler) {
eventHandler(this->_central);
}
}

void BLEPeripheral::BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) {
characteristic.setValue(this->_central, value, valueLength);
}
Expand Down
7 changes: 5 additions & 2 deletions src/BLEPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ enum BLEPeripheralEvent {
BLEConnected = 0,
BLEDisconnected = 1,
BLEBonded = 2,
BLERemoteServicesDiscovered = 3
BLERemoteServicesDiscovered = 3,
BLEConnectionParamsUpdated = 4
};

typedef void (*BLEPeripheralEventHandler)(BLECentral& central);
Expand All @@ -71,6 +72,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
// connection intervals in 1.25 ms increments,
// must be between 0x0006 (7.5 ms) and 0x0c80 (4 s), values outside of this range will be ignored
void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
bool setTxPower(int txPower);
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);
Expand Down Expand Up @@ -109,6 +111,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
virtual void BLEDeviceDisconnected(BLEDevice& device);
virtual void BLEDeviceBonded(BLEDevice& device);
virtual void BLEDeviceRemoteServicesDiscovered(BLEDevice& device);
virtual void BLEDeviceConnectionParamsUpdated(BLEDevice& device);

virtual void BLEDeviceCharacteristicValueChanged(BLEDevice& device, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength);
virtual void BLEDeviceCharacteristicSubscribedChanged(BLEDevice& device, BLECharacteristic& characteristic, bool subscribed);
Expand Down Expand Up @@ -152,7 +155,7 @@ class BLEPeripheral : public BLEDeviceEventListener,
BLERemoteCharacteristic _remoteServicesChangedCharacteristic;

BLECentral _central;
BLEPeripheralEventHandler _eventHandlers[4];
BLEPeripheralEventHandler _eventHandlers[5];
};

#endif
37 changes: 20 additions & 17 deletions src/nRF51822.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,7 @@ void nRF51822::poll() {

if (this->_minimumConnectionInterval >= BLE_GAP_CP_MIN_CONN_INTVL_MIN &&
this->_maximumConnectionInterval <= BLE_GAP_CP_MAX_CONN_INTVL_MAX) {
ble_gap_conn_params_t gap_conn_params;

gap_conn_params.min_conn_interval = this->_minimumConnectionInterval; // in 1.25ms units
gap_conn_params.max_conn_interval = this->_maximumConnectionInterval; // in 1.25ms unit
gap_conn_params.slave_latency = 0;
gap_conn_params.conn_sup_timeout = 4000 / 10; // in 10ms unit

sd_ble_gap_conn_param_update(this->_connectionHandle, &gap_conn_params);
updateConnectionInterval(this->_minimumConnectionInterval, this->_maximumConnectionInterval);
}

if (this->_numRemoteServices > 0) {
Expand Down Expand Up @@ -624,6 +617,9 @@ void nRF51822::poll() {
Serial.print(bleEvt->evt.gap_evt.params.conn_param_update.conn_params.conn_sup_timeout, HEX);
Serial.println();
#endif
if (this->_eventListener) {
this->_eventListener->BLEDeviceConnectionParamsUpdated(*this);
}
break;

case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
Expand Down Expand Up @@ -1035,6 +1031,19 @@ void nRF51822::end() {
this->_numRemoteCharacteristics = 0;
}

void nRF51822::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);

ble_gap_conn_params_t gap_conn_params;

gap_conn_params.min_conn_interval = this->_minimumConnectionInterval; // in 1.25ms units
gap_conn_params.max_conn_interval = this->_maximumConnectionInterval; // in 1.25ms unit
gap_conn_params.slave_latency = 0;
gap_conn_params.conn_sup_timeout = 4000 / 10; // in 10ms unit

sd_ble_gap_conn_param_update(this->_connectionHandle, &gap_conn_params);
}

bool nRF51822::updateCharacteristicValue(BLECharacteristic& characteristic) {
bool success = true;

Expand Down Expand Up @@ -1072,15 +1081,9 @@ bool nRF51822::updateCharacteristicValue(BLECharacteristic& characteristic) {
}

if (localCharacteristicInfo->indicateSubscribed) {
if (this->_txBufferCount > 0) {
this->_txBufferCount--;

hvxParams.type = BLE_GATT_HVX_INDICATION;
hvxParams.type = BLE_GATT_HVX_INDICATION;

sd_ble_gatts_hvx(this->_connectionHandle, &hvxParams);
} else {
success = false;
}
success = sd_ble_gatts_hvx(this->_connectionHandle, &hvxParams) == NRF_SUCCESS;
}
}
}
Expand Down Expand Up @@ -1131,7 +1134,7 @@ bool nRF51822::canNotifyCharacteristic(BLECharacteristic& /*characteristic*/) {
}

bool nRF51822::canIndicateCharacteristic(BLECharacteristic& /*characteristic*/) {
return (this->_txBufferCount > 0);
return true;
}

bool nRF51822::canReadRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
Expand Down
2 changes: 2 additions & 0 deletions src/nRF51822.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class nRF51822 : public BLEDevice

virtual ~nRF51822();

virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);

virtual void begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
Expand Down
9 changes: 9 additions & 0 deletions src/nRF8001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,9 @@ void nRF8001::poll() {
Serial.print(F("Timing change received conn Interval: 0x"));
Serial.println(aciEvt->params.timing.conn_rf_interval, HEX);
#endif
if (this->_eventListener) {
this->_eventListener->BLEDeviceConnectionParamsUpdated(*this);
}
break;

case ACI_EVT_DISCONNECTED:
Expand Down Expand Up @@ -1208,6 +1211,12 @@ void nRF8001::end() {
this->_numRemotePipeInfo = 0;
}

void nRF8001::updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) {
setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);

lib_aci_change_timing(this->_minimumConnectionInterval, this->_maximumConnectionInterval, 0, 4000 / 10);
}

bool nRF8001::updateCharacteristicValue(BLECharacteristic& characteristic) {
bool success = true;

Expand Down
2 changes: 2 additions & 0 deletions src/nRF8001.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class nRF8001 : protected BLEDevice

virtual ~nRF8001();

virtual void updateConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);

virtual void begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
Expand Down