Skip to content

Commit 0eac6a0

Browse files
sgbihusys_maker
authored and
sys_maker
committed
Jira 794 Inconsistent writeInt() result with different Peripherals, git 385
Root cause: - As a Central, the write() operation can produce two different commands to a Peripheral depending on the Characteristic. - One command is a simple write without aknowledgement from Peripheral and the other requires a complete handshake for each write operation. - Root cause of the issue was that the function, BLECharacteristicImp::write(), did not check the characteristic property to determine if the caller requires a response. It just treats all calls do not require a response. Feature added: - For write with respond from Peripheral, it is now a blocking call (it also satisfied Arduino's request). Modifications: 1. libraries/CurieBLE/src/internal/BLECallbacks.cpp: - ble_on_write_no_rsp_complete: CB to process Peripheral Response to complete a write operation. 2. libraries/CurieBLE/src/internal/BLECallbacks.h: - Function definition added. 3. libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp: - In function write(), check the characteristic property to determine whether the call requires a response. - Added a flag to signal the waiting on Peripheral Response for a write operation. - Write with response is now a blocking call. 4. BLECharacteristicImp.h: - Prototyping.
1 parent 40b0c43 commit 0eac6a0

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

libraries/CurieBLE/src/internal/BLECallbacks.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,9 @@ void ble_central_device_found(const bt_addr_le_t *addr,
223223
ad, len);
224224
}
225225

226+
void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
227+
const void *data)
228+
{
229+
BLECharacteristicImp::writeResponseReceived(conn, err, data);
230+
}
226231

libraries/CurieBLE/src/internal/BLECallbacks.h

+3
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,8 @@ uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
7474
const void *data,
7575
uint16_t length);
7676

77+
void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
78+
const void *data);
79+
7780
#endif
7881

libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp

+32-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
bt_uuid_16_t BLECharacteristicImp::_gatt_chrc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CHRC_VAL};
2929
bt_uuid_16_t BLECharacteristicImp::_gatt_ccc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CCC_VAL};
30+
volatile bool BLECharacteristicImp::_gattc_writing = false;
3031

3132
BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,
3233
unsigned char properties,
@@ -637,13 +638,20 @@ bool BLECharacteristicImp::read()
637638
return _reading;
638639
}
639640

641+
void BLECharacteristicImp::writeResponseReceived(struct bt_conn *conn,
642+
uint8_t err,
643+
const void *data)
644+
{
645+
_gattc_writing = false;
646+
}
647+
640648
bool BLECharacteristicImp::write(const unsigned char value[],
641649
uint16_t length)
642650
{
643651
int retval = 0;
644652
bt_conn_t* conn = NULL;
645653

646-
if (true == BLEUtils::isLocalBLE(_ble_device))
654+
if (true == BLEUtils::isLocalBLE(_ble_device) || true == _gattc_writing)
647655
{
648656
// GATT server can't write
649657
return false;
@@ -655,12 +663,29 @@ bool BLECharacteristicImp::write(const unsigned char value[],
655663
return false;
656664
}
657665

658-
// Send read request
659-
retval = bt_gatt_write_without_response(conn,
660-
_value_handle,
661-
value,
662-
length,
663-
false);
666+
// Send write request
667+
if (_gatt_chrc.properties | BT_GATT_CHRC_WRITE_WITHOUT_RESP)
668+
{
669+
retval = bt_gatt_write_without_response(conn,
670+
_value_handle,
671+
value,
672+
length,
673+
false);
674+
}
675+
else if (_gatt_chrc.properties | BT_GATT_CHRC_WRITE)
676+
{
677+
_gattc_writing = true;
678+
retval = bt_gatt_write(conn,
679+
_value_handle,
680+
0,
681+
value,
682+
length,
683+
ble_on_write_no_rsp_complete);
684+
while (_gattc_writing)
685+
{
686+
delay(2);
687+
}
688+
}
664689
bt_conn_unref(conn);
665690
return (0 == retval);
666691
}

libraries/CurieBLE/src/internal/BLECharacteristicImp.h

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class BLECharacteristicImp: public BLEAttribute{
168168
*/
169169
bool write(const unsigned char value[],
170170
uint16_t length);
171+
172+
static void writeResponseReceived(struct bt_conn *conn,
173+
uint8_t err,
174+
const void *data);
171175

172176
int descriptorCount() const;
173177
uint8_t discoverResponseProc(bt_conn_t *conn,
@@ -325,6 +329,7 @@ class BLECharacteristicImp: public BLEAttribute{
325329
bool _subscribed;
326330

327331
bool _reading;
332+
static volatile bool _gattc_writing;
328333
bt_gatt_read_params_t _read_params; // GATT read parameter
329334

330335
typedef LinkNode<BLEDescriptorImp *> BLEDescriptorLinkNodeHeader;

0 commit comments

Comments
 (0)