Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 9cfaadc

Browse files
[Fix] BLECentralRole library: write / read remote characteristic only if it belongs to the right peripheral (#54)
1 parent 8af8154 commit 9cfaadc

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

libraries/BLE/BLECentralRole.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ void BLECentralRole::begin(){
510510
this->_remoteServiceInfo = (struct remoteServiceInfo*)malloc(sizeof(struct remoteServiceInfo) * this->_numRemoteServices);
511511
this->_remoteCharacteristicInfo = (struct remoteCharacteristicInfo*)malloc(sizeof(struct remoteCharacteristicInfo) * this->_numRemoteCharacteristics);
512512

513+
for(int i = 0; i < this->_numRemoteCharacteristics; i++){
514+
for(int j = 0; j < 7; j++)
515+
this->_remoteCharacteristicInfo[i].connectionIndex[j] = 0;
516+
}
517+
513518
BLERemoteService *lastRemoteService = NULL;
514519
unsigned char remoteServiceIndex = 0;
515520
unsigned char remoteCharacteristicIndex = 0;
@@ -548,7 +553,7 @@ void BLECentralRole::begin(){
548553
this->_remoteCharacteristicInfo[remoteCharacteristicIndex].characteristic = (BLERemoteCharacteristic *)remoteAttribute;
549554
this->_remoteCharacteristicInfo[remoteCharacteristicIndex].service = lastRemoteService;
550555
this->_remoteCharacteristicInfo[remoteCharacteristicIndex].uuid = nordicUUID;
551-
556+
552557
memset(&this->_remoteCharacteristicInfo[remoteCharacteristicIndex].properties, 0, sizeof(this->_remoteCharacteristicInfo[remoteCharacteristicIndex].properties));
553558
this->_remoteCharacteristicInfo[remoteCharacteristicIndex].valueHandle = 0;
554559

@@ -824,6 +829,7 @@ void BLECentralRole::poll(ble_evt_t *bleEvt){
824829
(bleEvt->evt.gattc_evt.params.char_disc_rsp.chars[i].uuid.uuid == this->_remoteCharacteristicInfo[j].uuid.uuid)) {
825830
this->_remoteCharacteristicInfo[j].properties = bleEvt->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props;
826831
this->_remoteCharacteristicInfo[j].valueHandle = bleEvt->evt.gattc_evt.params.char_disc_rsp.chars[i].handle_value;
832+
this->_remoteCharacteristicInfo[j].connectionIndex[periphIndex] = 1;
827833
}
828834
}
829835

@@ -1258,7 +1264,9 @@ bool BLECentralRole::readRemoteCharacteristic(BLERemoteCharacteristic& character
12581264
this->_remoteRequestInProgress = true;
12591265
for(int currentPeripheral = 0; currentPeripheral < _allowedPeripherals; currentPeripheral++)
12601266
if(this->_connectionHandle[currentPeripheral] != BLE_CONN_HANDLE_INVALID)
1261-
success = (sd_ble_gattc_read(this->_connectionHandle[currentPeripheral], this->_remoteCharacteristicInfo[i].valueHandle, 0) == NRF_SUCCESS);
1267+
// check if this characteristic belongs to this peripheral
1268+
if(this->_remoteCharacteristicInfo[i].connectionIndex[currentPeripheral])
1269+
success = (sd_ble_gattc_read(this->_connectionHandle[currentPeripheral], this->_remoteCharacteristicInfo[i].valueHandle, 0) == NRF_SUCCESS);
12621270
}
12631271
break;
12641272
}
@@ -1288,12 +1296,11 @@ bool BLECentralRole::canWriteRemoteCharacteristic(BLERemoteCharacteristic& chara
12881296

12891297
bool BLECentralRole::writeRemoteCharacteristic(BLERemoteCharacteristic& characteristic, const unsigned char value[], unsigned char length) {
12901298
bool success = false;
1291-
12921299
for (int i = 0; i < this->_numRemoteCharacteristics; i++) {
1300+
12931301
if (this->_remoteCharacteristicInfo[i].characteristic == &characteristic) {
12941302
if (this->_remoteCharacteristicInfo[i].valueHandle &&
12951303
(this->_remoteCharacteristicInfo[i].properties.write_wo_resp || this->_remoteCharacteristicInfo[i].properties.write)) {
1296-
12971304
ble_gattc_write_params_t writeParams;
12981305

12991306
writeParams.write_op = (this->_remoteCharacteristicInfo[i].properties.write) ? BLE_GATT_OP_WRITE_REQ : BLE_GATT_OP_WRITE_CMD;
@@ -1303,14 +1310,19 @@ bool BLECentralRole::writeRemoteCharacteristic(BLERemoteCharacteristic& characte
13031310
writeParams.p_value = (uint8_t*)value;
13041311

13051312
this->_remoteRequestInProgress = true;
1306-
13071313
for(int currentPeripheral = 0; currentPeripheral < _allowedPeripherals; currentPeripheral++)
1308-
if(this->_connectionHandle[currentPeripheral] != BLE_CONN_HANDLE_INVALID)
1309-
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
1314+
if(this->_connectionHandle[currentPeripheral] != BLE_CONN_HANDLE_INVALID)
1315+
1316+
// check if this characteristic belongs to this peripheral
1317+
if(this->_remoteCharacteristicInfo[i].connectionIndex[currentPeripheral]){
1318+
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
1319+
}
13101320
}
13111321
break;
13121322
}
1313-
}
1323+
1324+
1325+
}
13141326

13151327
return success;
13161328
}
@@ -1352,7 +1364,9 @@ bool BLECentralRole::subscribeRemoteCharacteristic(BLERemoteCharacteristic& char
13521364

13531365
for(int currentPeripheral = 0; currentPeripheral < _allowedPeripherals; currentPeripheral++)
13541366
if(this->_connectionHandle[currentPeripheral] != BLE_CONN_HANDLE_INVALID)
1355-
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
1367+
// check if this characteristic belongs to this peripheral
1368+
if(this->_remoteCharacteristicInfo[i].connectionIndex[currentPeripheral])
1369+
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
13561370
}
13571371
break;
13581372
}
@@ -1387,7 +1401,9 @@ bool BLECentralRole::unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& cha
13871401

13881402
for(int currentPeripheral = 0; currentPeripheral < _allowedPeripherals; currentPeripheral++)
13891403
if(this->_connectionHandle[currentPeripheral] != BLE_CONN_HANDLE_INVALID)
1390-
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
1404+
// check if this characteristic belongs to this peripheral
1405+
if(this->_remoteCharacteristicInfo[i].connectionIndex[currentPeripheral])
1406+
success = (sd_ble_gattc_write(this->_connectionHandle[currentPeripheral], &writeParams) == NRF_SUCCESS);
13911407
}
13921408
break;
13931409
}

libraries/BLE/BLECentralRole.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class BLECentralRole : public BLECharacteristicValueChangeListener,
134134
BLERemoteCharacteristic* characteristic;
135135
BLERemoteService* service;
136136

137+
bool connectionIndex[7];
137138
ble_uuid_t uuid;
138139
ble_gatt_char_props_t properties;
139140
uint16_t valueHandle;

0 commit comments

Comments
 (0)