Skip to content

Jira 794 Inconsistent writeInt() result with different Peripherals, g… #414

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions libraries/CurieBLE/src/internal/BLECallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,9 @@ void ble_central_device_found(const bt_addr_le_t *addr,
ad, len);
}

void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the empty function? Let's delete it, if not need, or add a comment explaining WHY there is an empty function (e.g. a TODO or something)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that empty function neither. Let me check it out more. It should be made better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see those two functions _bt_gatt_write and bt_gatt_write in the gatt.c. This try to schedule the write request with response.

const void *data)
{
BLECharacteristicImp::writeResponseReceived(conn, err, data);
}

3 changes: 3 additions & 0 deletions libraries/CurieBLE/src/internal/BLECallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
const void *data,
uint16_t length);

void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
const void *data);

#endif

39 changes: 32 additions & 7 deletions libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

bt_uuid_16_t BLECharacteristicImp::_gatt_chrc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CHRC_VAL};
bt_uuid_16_t BLECharacteristicImp::_gatt_ccc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CCC_VAL};
volatile bool BLECharacteristicImp::_gattc_writing = false;

BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,
unsigned char properties,
Expand Down Expand Up @@ -637,13 +638,20 @@ bool BLECharacteristicImp::read()
return _reading;
}

void BLECharacteristicImp::writeResponseReceived(struct bt_conn *conn,
uint8_t err,
const void *data)
{
_gattc_writing = false;
}

bool BLECharacteristicImp::write(const unsigned char value[],
uint16_t length)
{
int retval = 0;
bt_conn_t* conn = NULL;

if (true == BLEUtils::isLocalBLE(_ble_device))
if (true == BLEUtils::isLocalBLE(_ble_device) || true == _gattc_writing)
{
// GATT server can't write
return false;
Expand All @@ -655,12 +663,29 @@ bool BLECharacteristicImp::write(const unsigned char value[],
return false;
}

// Send read request
retval = bt_gatt_write_without_response(conn,
_value_handle,
value,
length,
false);
// Send write request
if (_gatt_chrc.properties | BT_GATT_CHRC_WRITE_WITHOUT_RESP)
{
retval = bt_gatt_write_without_response(conn,
_value_handle,
value,
length,
false);
}
else if (_gatt_chrc.properties | BT_GATT_CHRC_WRITE)
{
_gattc_writing = true;
retval = bt_gatt_write(conn,
_value_handle,
0,
value,
length,
ble_on_write_no_rsp_complete);
while (_gattc_writing)
{
delay(2);
}
}
bt_conn_unref(conn);
return (0 == retval);
}
Expand Down
5 changes: 5 additions & 0 deletions libraries/CurieBLE/src/internal/BLECharacteristicImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class BLECharacteristicImp: public BLEAttribute{
*/
bool write(const unsigned char value[],
uint16_t length);

static void writeResponseReceived(struct bt_conn *conn,
uint8_t err,
const void *data);

int descriptorCount() const;
uint8_t discoverResponseProc(bt_conn_t *conn,
Expand Down Expand Up @@ -325,6 +329,7 @@ class BLECharacteristicImp: public BLEAttribute{
bool _subscribed;

bool _reading;
static volatile bool _gattc_writing;
bt_gatt_read_params_t _read_params; // GATT read parameter

typedef LinkNode<BLEDescriptorImp *> BLEDescriptorLinkNodeHeader;
Expand Down