Skip to content

Commit cdc8845

Browse files
copy authorsation callbacks to gattserver
1 parent c28f0d6 commit cdc8845

File tree

2 files changed

+68
-28
lines changed

2 files changed

+68
-28
lines changed

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

+52-25
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,34 @@ ble_error_t GattServer::insert_characteristic_value_attribute(
416416
characteristic->isReadAuthorizationEnabled() ||
417417
characteristic->isWriteAuthorizationEnabled()
418418
) {
419-
if (_auth_char_count >= MBED_CONF_BLE_API_IMPLEMENTATION_MAX_CHARACTERISTIC_AUTHORISATION_COUNT) {
419+
if (_auth_callbacks_count >= MBED_CONF_BLE_API_IMPLEMENTATION_MAX_CHARACTERISTIC_AUTHORISATION_COUNT) {
420420
return BLE_ERROR_NO_MEM;
421421
}
422-
_auth_char[_auth_char_count] = characteristic;
423-
++_auth_char_count;
422+
423+
char_auth_callback *new_cb = (char_auth_callback *) alloc_block(sizeof(char_auth_callback));
424+
425+
if (!new_cb) {
426+
return BLE_ERROR_NO_MEM;
427+
}
428+
429+
new_cb->read_cb = characteristic->readAuthorizationCallback;
430+
new_cb->write_cb = characteristic->writeAuthorizationCallback;
431+
new_cb->handle = characteristic->getValueHandle();
432+
new_cb->update_security = characteristic->getUpdateSecurityRequirement();
433+
new_cb->_next = nullptr;
434+
435+
/* add it to the list */
436+
if (_auth_callbacks) {
437+
char_auth_callback *last_cb = _auth_callbacks;
438+
while (last_cb->_next) {
439+
last_cb = last_cb->_next;
440+
}
441+
last_cb->_next = new_cb;
442+
} else {
443+
_auth_callbacks = new_cb;
444+
};
445+
446+
++_auth_callbacks_count;
424447
}
425448

426449
++attribute_it;
@@ -941,7 +964,8 @@ ble_error_t GattServer::reset(ble::GattServer* server)
941964
currentHandle = 0;
942965
cccd_cnt = 0;
943966

944-
_auth_char_count = 0;
967+
_auth_callbacks_count = 0;
968+
_auth_callbacks = nullptr;
945969

946970
AttsCccRegister(cccd_cnt, (attsCccSet_t *) cccds, cccd_cb);
947971

@@ -978,8 +1002,8 @@ uint8_t GattServer::atts_read_cb(
9781002
attsAttr_t *pAttr
9791003
)
9801004
{
981-
GattCharacteristic *auth_char = getInstance().get_auth_char(handle);
982-
if (auth_char && auth_char->isReadAuthorizationEnabled()) {
1005+
char_auth_callback *auth_cb = getInstance().get_auth_callback(handle);
1006+
if (auth_cb && auth_cb->read_cb) {
9831007
GattReadAuthCallbackParams read_auth_params = {
9841008
connId,
9851009
handle,
@@ -989,9 +1013,10 @@ uint8_t GattServer::atts_read_cb(
9891013
AUTH_CALLBACK_REPLY_SUCCESS
9901014
};
9911015

992-
GattAuthCallbackReply_t ret = auth_char->authorizeRead(&read_auth_params);
993-
if (ret != AUTH_CALLBACK_REPLY_SUCCESS) {
994-
return ret & 0xFF;
1016+
auth_cb->read_cb.call(&read_auth_params);
1017+
1018+
if (read_auth_params.authorizationReply != AUTH_CALLBACK_REPLY_SUCCESS) {
1019+
return read_auth_params.authorizationReply & 0xFF;
9951020
}
9961021

9971022
pAttr->pValue = read_auth_params.data;
@@ -1021,8 +1046,8 @@ uint8_t GattServer::atts_write_cb(
10211046
attsAttr_t *pAttr
10221047
)
10231048
{
1024-
GattCharacteristic* auth_char = getInstance().get_auth_char(handle);
1025-
if (auth_char && auth_char->isWriteAuthorizationEnabled()) {
1049+
char_auth_callback* auth_cb = getInstance().get_auth_callback(handle);
1050+
if (auth_cb && auth_cb->write_cb) {
10261051
GattWriteAuthCallbackParams write_auth_params = {
10271052
connId,
10281053
handle,
@@ -1032,9 +1057,10 @@ uint8_t GattServer::atts_write_cb(
10321057
AUTH_CALLBACK_REPLY_SUCCESS
10331058
};
10341059

1035-
GattAuthCallbackReply_t ret = auth_char->authorizeWrite(&write_auth_params);
1036-
if (ret!= AUTH_CALLBACK_REPLY_SUCCESS) {
1037-
return ret & 0xFF;
1060+
auth_cb->write_cb.call(&write_auth_params);
1061+
1062+
if (write_auth_params.authorizationReply != AUTH_CALLBACK_REPLY_SUCCESS) {
1063+
return write_auth_params.authorizationReply & 0xFF;
10381064
}
10391065
}
10401066

@@ -1329,14 +1355,16 @@ void *GattServer::alloc_block(size_t block_size)
13291355
return block->data;
13301356
}
13311357

1332-
GattCharacteristic *GattServer::get_auth_char(uint16_t value_handle)
1358+
GattServer::char_auth_callback *GattServer::get_auth_callback(uint16_t value_handle)
13331359
{
1334-
for (size_t i = 0; i < _auth_char_count; ++i) {
1335-
if (_auth_char[i]->getValueHandle() == value_handle) {
1336-
return _auth_char[i];
1360+
GattServer::char_auth_callback* current = _auth_callbacks;
1361+
while (current) {
1362+
if (current->handle == value_handle) {
1363+
break;
13371364
}
1365+
current = current->_next;
13381366
}
1339-
return nullptr;
1367+
return current;
13401368
}
13411369

13421370
bool GattServer::get_cccd_index_by_cccd_handle(GattAttribute::Handle_t cccd_handle, uint8_t &idx) const
@@ -1364,13 +1392,12 @@ bool GattServer::is_update_authorized(
13641392
GattAttribute::Handle_t value_handle
13651393
)
13661394
{
1367-
GattCharacteristic *auth_char = get_auth_char(value_handle);
1368-
if (!auth_char) {
1395+
char_auth_callback *auth_cb = get_auth_callback(value_handle);
1396+
if (!auth_cb) {
13691397
return true;
13701398
}
13711399

1372-
att_security_requirement_t sec_req =
1373-
auth_char->getUpdateSecurityRequirement();
1400+
const att_security_requirement_t sec_req = auth_cb->update_security;
13741401

13751402
if (sec_req == att_security_requirement_t::NONE) {
13761403
return true;
@@ -1426,8 +1453,8 @@ GattServer::GattServer() :
14261453
cccd_values(),
14271454
cccd_handles(),
14281455
cccd_cnt(0),
1429-
_auth_char(),
1430-
_auth_char_count(0),
1456+
_auth_callbacks(nullptr),
1457+
_auth_callbacks_count(0),
14311458
generic_access_service(),
14321459
generic_attribute_service(),
14331460
registered_service(nullptr),

connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ class GattServer : public PalSigningMonitor {
185185
GapAdvertisingData::Appearance getAppearance();
186186

187187
#endif // Disabled until reworked and reintroduced to GattServer API
188+
private:
189+
struct char_auth_callback {
190+
/** The registered callback handler for read authorization reply. */
191+
FunctionPointerWithContext<GattReadAuthCallbackParams *> read_cb;
192+
/** The registered callback handler for write authorization reply. */
193+
FunctionPointerWithContext<GattWriteAuthCallbackParams *> write_cb;
194+
/** built in list */
195+
char_auth_callback *_next = nullptr;
196+
/** Characteristic handle the callbacks belong to. */
197+
ble::attribute_handle_t handle = 0;
198+
/** security requirement of update operations */
199+
ble::att_security_requirement_t update_security = ble::att_security_requirement_t::NONE;
200+
};
188201

189202
public:
190203
/**
@@ -274,7 +287,7 @@ class GattServer : public PalSigningMonitor {
274287

275288
void *alloc_block(size_t block_size);
276289

277-
GattCharacteristic *get_auth_char(uint16_t value_handle);
290+
char_auth_callback *get_auth_callback(uint16_t value_handle);
278291

279292
bool get_cccd_index_by_cccd_handle(GattAttribute::Handle_t cccd_handle, uint8_t &idx) const;
280293

@@ -354,8 +367,8 @@ class GattServer : public PalSigningMonitor {
354367
uint16_t cccd_handles[MBED_CONF_BLE_API_IMPLEMENTATION_MAX_CCCD_COUNT];
355368
uint8_t cccd_cnt;
356369

357-
GattCharacteristic *_auth_char[MBED_CONF_BLE_API_IMPLEMENTATION_MAX_CHARACTERISTIC_AUTHORISATION_COUNT];
358-
uint8_t _auth_char_count;
370+
char_auth_callback *_auth_callbacks;
371+
uint8_t _auth_callbacks_count;
359372

360373
struct {
361374
attsGroup_t service;

0 commit comments

Comments
 (0)