Skip to content

Commit bc56500

Browse files
sgbihuSidLeung
authored andcommitted
Jira 795, read() is blocking selectable, git 383
Mods: 1. Add an input parameter to BLECharacteristic::read() for blocking call selection. 2. By default, read() is a blocking call. File changes: 1. libraries/CurieBLE/src/BLECharacteristic.cpp: - Method read() added blocking selection. 2. libraries/CurieBLE/src/BLECharacteristic.h: - Prototype for read(), default is blocking. 3. libraries/CurieBLE/src/internal/BLECallbacks.cpp: - Added parameter checking in profile_read_rsp_process(). 4. libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp - The implementation of read() added waiting for resp if blocking is selected. 5. libraries/CurieBLE/src/internal/BLECharacteristicImp.h: - prototyping.
1 parent ee08a03 commit bc56500

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

Diff for: libraries/CurieBLE/src/BLECharacteristic.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,14 @@ bool BLECharacteristic::canUnsubscribe()
367367
return retVar;
368368
}
369369

370-
bool BLECharacteristic::read()
370+
bool BLECharacteristic::read(bool blocked)
371371
{
372372
bool retVar = false;
373373
BLECharacteristicImp *characteristicImp = getImplementation();
374374

375375
if (NULL != characteristicImp)
376376
{
377-
retVar = characteristicImp->read();
377+
retVar = characteristicImp->read(blocked);
378378
}
379379
return retVar;
380380
}

Diff for: libraries/CurieBLE/src/BLECharacteristic.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,9 @@ class BLECharacteristic: public BLEAttributeWithValue
320320
* @return bool true - Success, false - Failed
321321
*
322322
* @note Only for GATT client. Schedule read request to the GATT server
323+
* Arduino requests to have read, by default, be blocking.
323324
*/
324-
virtual bool read();
325+
virtual bool read(bool blocked = true);
325326

326327
/**
327328
* @brief Write the charcteristic value

Diff for: libraries/CurieBLE/src/internal/BLECallbacks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ uint8_t profile_read_rsp_process(bt_conn_t *conn,
145145
const void *data,
146146
uint16_t length)
147147
{
148-
if (NULL == data)
148+
if (NULL == data && 0 != length)
149149
{
150150
return BT_GATT_ITER_STOP;
151151
}

Diff for: libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,10 @@ bt_uuid_t* BLECharacteristicImp::getClientCharacteristicConfigUuid(void)
595595
return (bt_uuid_t*) &_gatt_ccc_uuid;
596596
}
597597

598-
bool BLECharacteristicImp::read()
598+
bool BLECharacteristicImp::read(bool blocked)
599599
{
600600
int retval = 0;
601+
bool ret_bool = false;
601602
bt_conn_t* conn = NULL;
602603

603604
if (true == BLEUtils::isLocalBLE(_ble_device))
@@ -631,12 +632,23 @@ bool BLECharacteristicImp::read()
631632

632633
// Send read request
633634
retval = bt_gatt_read(conn, &_read_params);
634-
bt_conn_unref(conn);
635635
if (0 == retval)
636636
{
637637
_reading = true;
638+
ret_bool = true;
639+
640+
// Block the call
641+
if (blocked == true)
642+
{
643+
while (_reading == true && ret_bool)
644+
{
645+
delay(5);
646+
ret_bool = _ble_device.connected();
647+
}
648+
}
638649
}
639-
return _reading;
650+
bt_conn_unref(conn);
651+
return ret_bool;
640652
}
641653

642654
void BLECharacteristicImp::writeResponseReceived(struct bt_conn *conn,

Diff for: libraries/CurieBLE/src/internal/BLECharacteristicImp.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ class BLECharacteristicImp: public BLEAttribute{
146146
/**
147147
* @brief Schedule the read request to read the characteristic in peripheral
148148
*
149-
* @param[in] none
149+
* @param[in] blocked Flag the call is blocked or un-blocked
150150
*
151151
* @return bool Indicate the success or error
152152
*
153-
* @note Only for central device
153+
* @note Only for GATT client
154+
* Default it is block call as per Arduino request
154155
*/
155-
bool read();
156+
bool read(bool blocked = true);
156157

157158
/**
158159
* @brief Schedule the write request to update the characteristic in peripheral
159160
*
160-
* @param[in] peripheral The peripheral device that want to be updated
161161
* @param[in] value New value to set, as a byte array. Data is stored in internal copy.
162162
* @param[in] length Length, in bytes, of valid data in the array to write.
163163
* Must not exceed maxLength set for this characteristic.
@@ -328,7 +328,7 @@ class BLECharacteristicImp: public BLEAttribute{
328328
bt_gatt_subscribe_params_t _sub_params;
329329
bool _subscribed;
330330

331-
bool _reading;
331+
volatile bool _reading;
332332
static volatile bool _gattc_writing;
333333
bt_gatt_read_params_t _read_params; // GATT read parameter
334334

0 commit comments

Comments
 (0)