Skip to content

Commit 55ffb17

Browse files
Copy data from read auth callback
1 parent 376fda5 commit 55ffb17

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

connectivity/FEATURE_BLE/include/ble/gatt/GattCharacteristic.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,19 +1672,26 @@ class GattCharacteristic {
16721672
*
16731673
* @attention This function is not meant to be called by user code.
16741674
*
1675-
* @param[in] params Context of the read-auth request; it contains an
1675+
* @param[in,out] params Context of the read-auth request; it contains an
16761676
* out-parameter used as a reply and the handler can fill it with outgoing
1677-
* data.
1677+
* data. The params->data provides a pointer to the data and params->len
1678+
* provides the length of this data. params->len is also used to pass the
1679+
* maximum size of data that the params->data can contain. If you set the
1680+
* params->len to a value larger than the passed in value the read operation
1681+
* will fail.
16781682
*
16791683
* @return A GattAuthCallbackReply_t value indicating whether authorization
16801684
* is granted.
16811685
*
1686+
* @note If the read is approved, the event handler can specify an outgoing
1687+
* value directly with the help of the fields params->data and params->len.
1688+
*
16821689
* @note If the read request is approved and params->data remains nullptr, then
16831690
* the current characteristic value is used in the read response payload.
16841691
*
1685-
* @note If the read is approved, the event handler can specify an outgoing
1686-
* value directly with the help of the fields
1687-
* GattReadAuthCallbackParams::data and GattReadAuthCallbackParams::len.
1692+
* @note The params->len parameter initially contains the maximum length of
1693+
* data that can be returned. Set it to the length of your data but it must
1694+
* not be larger than the original value.
16881695
*/
16891696
GattAuthCallbackReply_t authorizeRead(GattReadAuthCallbackParams *params)
16901697
{

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ uint8_t GattServer::atts_read_cb(
11301130
connId,
11311131
handle,
11321132
offset,
1133-
/* len */ 0,
1133+
/* len */ pAttr->maxLen,
11341134
/* data */ nullptr,
11351135
AUTH_CALLBACK_REPLY_SUCCESS
11361136
};
@@ -1146,8 +1146,27 @@ uint8_t GattServer::atts_read_cb(
11461146
return read_auth_params.authorizationReply & 0xFF;
11471147
}
11481148

1149-
pAttr->pValue = read_auth_params.data;
1150-
*pAttr->pLen = read_auth_params.len;
1149+
/* if new data provided copy into the attribute value buffer */
1150+
if (read_auth_params.data) {
1151+
if (read_auth_params.offset + read_auth_params.len > pAttr->maxLen) {
1152+
tr_error("Read authorisation callback set length larger than maximum attribute length. Cannot copy data");
1153+
1154+
GattReadCallbackParams read_params = {
1155+
connId,
1156+
handle,
1157+
offset,
1158+
read_auth_params.len,
1159+
read_auth_params.data,
1160+
BLE_ERROR_INVALID_PARAM,
1161+
};
1162+
getInstance().handleDataReadEvent(&read_params);
1163+
1164+
return ATT_ERR_UNLIKELY;
1165+
}
1166+
1167+
memcpy(pAttr->pValue + read_auth_params.offset, read_auth_params.data, read_auth_params.len);
1168+
*pAttr->pLen = read_auth_params.len;
1169+
}
11511170
}
11521171

11531172
tr_debug("Read attribute %d on connection %d - value=%s",

0 commit comments

Comments
 (0)