Skip to content

Commit 0155bce

Browse files
allow zero length attribute reads
1 parent 17bf709 commit 0155bce

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ uint8_t GattServer::atts_read_cb(
11241124
attsAttr_t *pAttr
11251125
)
11261126
{
1127+
uint8_t err = ATT_SUCCESS;
1128+
11271129
char_auth_callback *auth_cb = getInstance().get_auth_callback(handle);
11281130
if (auth_cb && auth_cb->read_cb) {
11291131
GattReadAuthCallbackParams read_auth_params = {
@@ -1148,25 +1150,21 @@ uint8_t GattServer::atts_read_cb(
11481150

11491151
/* if new data provided copy into the attribute value buffer */
11501152
if (read_auth_params.data) {
1151-
if (read_auth_params.len > pAttr->maxLen || offset >= read_auth_params.len) {
1152-
tr_error("Read authorisation callback set length larger than maximum attribute length "
1153-
"or current offset is beyond new length. Cannot copy data");
11541153

1155-
GattReadCallbackParams read_params = {
1156-
connId,
1157-
handle,
1158-
offset,
1159-
read_auth_params.len,
1160-
read_auth_params.data,
1161-
BLE_ERROR_INVALID_PARAM,
1162-
};
1163-
getInstance().handleDataReadEvent(&read_params);
1164-
1165-
return ATT_ERR_UNLIKELY;
1154+
if (read_auth_params.len > pAttr->maxLen) {
1155+
tr_error("Read authorisation callback set length larger than maximum attribute length, "
1156+
"cannot copy data");
1157+
err = ATT_ERR_UNLIKELY;
11661158
}
11671159

11681160
memcpy(pAttr->pValue, read_auth_params.data, read_auth_params.len);
11691161
*pAttr->pLen = read_auth_params.len;
1162+
1163+
if (read_auth_params.len < offset) {
1164+
tr_warning("Read authorisation callback shortened data beyond current offset, "
1165+
"current read will fail");
1166+
err = ATT_ERR_OFFSET;
1167+
}
11701168
}
11711169
}
11721170

@@ -1181,11 +1179,11 @@ uint8_t GattServer::atts_read_cb(
11811179
offset,
11821180
*pAttr->pLen,
11831181
pAttr->pValue,
1184-
/* status */ BLE_ERROR_NONE,
1182+
/* status */ (err == ATT_SUCCESS) ? BLE_ERROR_NONE : BLE_ERROR_PARAM_OUT_OF_RANGE
11851183
};
11861184
getInstance().handleDataReadEvent(&read_params);
11871185

1188-
return ATT_SUCCESS;
1186+
return err;
11891187
}
11901188

11911189
uint8_t GattServer::atts_write_cb(

0 commit comments

Comments
 (0)