Skip to content

Commit

Permalink
Fix #82 by fixing UUID comparison between 16-bit and 128-bit UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosun committed Jan 9, 2018
1 parent 209c96f commit a5be092
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,42 @@ unsigned char LBLEUuid::equals(const LBLEUuid &rhs) const

bool LBLEUuid::operator<(const LBLEUuid &rhs) const
{
const bool rhs16 = rhs.is16Bit();
const bool rhs32 = bt_uuid_is_uuid32(&rhs.uuid_data);

if(is16Bit())
{
if(rhs16) {
return (uuid_data.uuid16 < rhs.uuid_data.uuid16);
}

// 16-bit is always smaller than 32/128
return true;
}
else if(bt_uuid_is_uuid32(&uuid_data))
{
if(rhs32) {
return (uuid_data.uuid32 < rhs.uuid_data.uuid32);
}

// 32-bit is larger than 16-bit
if(rhs16) {
return false;
}

// 128-bit case
return true;
}
else
{
if(rhs16) {
return false;
}

if(rhs32) {
return false;
}

// 128-bit: compare from MSB to LSB
for(int i = 0; i < 16; ++i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,14 @@ int LBLEClient::writeCharacteristic(const LBLEUuid& uuid, const LBLEValueBuffer&
auto found = m_characteristics.find(uuid);
if(found == m_characteristics.end())
{
pr_debug("cannot find characteristics");
return 0;
}

// value buffer too big
if(value.size() > MAXIMUM_WRITE_SIZE)
{
pr_debug("write value too long");
return 0;
}

Expand All @@ -1031,27 +1033,34 @@ int LBLEClient::writeCharacteristic(const LBLEUuid& uuid, const LBLEValueBuffer&
req.attribute_value_length = value.size();
req.att_req = (bt_att_write_req_t*)&reqBuf[0];
req.att_req->opcode = BT_ATT_OPCODE_WRITE_REQUEST;
// the "attribute_handle" field requires a "value handle" actually.
pr_debug("write_charc %s with value handle=%d", found->first.toString().c_str(), found->second);
req.att_req->attribute_handle = found->second;
memcpy(req.att_req->attribute_value, &value[0], value.size());

bool done = waitAndProcessEvent(
// start read request
[&]()
{
bt_gattc_write_charc(m_connection, &req);
bt_status_t writeResult = bt_gattc_write_charc(m_connection, &req);
pr_debug("write result = 0x%x", writeResult);
},
// wait for event...
BT_GATTC_WRITE_CHARC,
// and parse event result in bt task context
[this](bt_msg_type_t msg, bt_status_t, void* buf)
{
const bt_gattc_write_rsp_t *pWriteResp = (bt_gattc_write_rsp_t*)buf;
if(BT_GATTC_WRITE_CHARC != msg || pWriteResp->connection_handle != m_connection)
{
if(BT_GATTC_WRITE_CHARC != msg) {
// not for our request
pr_debug("got wrong messge");
return;
}

const bt_gattc_write_rsp_t *pWriteResp = (bt_gattc_write_rsp_t*)buf;
if(pWriteResp->connection_handle != this->m_connection) {
pr_debug("got wrong handle=%d (%d)", pWriteResp->connection_handle, this->m_connection);
}

do{
// check error case
if(BT_ATT_OPCODE_ERROR_RESPONSE == pWriteResp->att_rsp->opcode)
Expand Down

0 comments on commit a5be092

Please sign in to comment.