Skip to content

Commit d33b028

Browse files
committed
BLE: Implement Generic GattClient reset logic.
1 parent 29988d5 commit d33b028

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

features/FEATURE_BLE/ble/generic/GenericGattClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class GenericGattClient : public GattClient {
137137
pal::GattClient* const _pal_client;
138138
ServiceDiscovery::TerminationCallback_t _termination_callback;
139139
mutable ProcedureControlBlock* control_blocks;
140+
bool _is_reseting;
140141
};
141142

142143
}

features/FEATURE_BLE/source/generic/GenericGattClient.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ struct GenericGattClient::ProcedureControlBlock {
7474
*/
7575
virtual void handle_timeout_error(GenericGattClient* client) = 0;
7676

77+
/**
78+
* Function called when the procedure is aborted
79+
*/
80+
virtual void abort(GenericGattClient *client) = 0;
81+
7782
procedure_type_t type;
7883
Gap::Handle_t connection_handle;
7984
ProcedureControlBlock* next;
@@ -111,6 +116,10 @@ struct GenericGattClient::DiscoveryControlBlock : public ProcedureControlBlock {
111116
terminate(client);
112117
}
113118

119+
virtual void abort(GenericGattClient *client) {
120+
terminate(client);
121+
}
122+
114123
virtual void handle(GenericGattClient* client, const AttServerMessage& message) {
115124
// if end of discovery has been requested, ends it immediately
116125
if (done) {
@@ -436,6 +445,19 @@ struct GenericGattClient::ReadControlBlock : public ProcedureControlBlock {
436445
terminate(client, response);
437446
}
438447

448+
virtual void abort(GenericGattClient *client) {
449+
GattReadCallbackParams response = {
450+
connection_handle,
451+
attribute_handle,
452+
offset,
453+
0, // size of 0
454+
NULL, // no data
455+
BLE_ERROR_INVALID_STATE,
456+
457+
};
458+
terminate(client, response);
459+
}
460+
439461
void terminate(GenericGattClient* client, const GattReadCallbackParams& response) {
440462
client->remove_control_block(this);
441463
client->processReadResponse(&response);
@@ -617,6 +639,17 @@ struct GenericGattClient::WriteControlBlock : public ProcedureControlBlock {
617639
terminate(client, response);
618640
}
619641

642+
virtual void abort(GenericGattClient *client) {
643+
GattWriteCallbackParams response = {
644+
connection_handle,
645+
attribute_handle,
646+
GattWriteCallbackParams::OP_WRITE_REQ,
647+
BLE_ERROR_INVALID_STATE,
648+
0x00
649+
};
650+
terminate(client, response);
651+
}
652+
620653
void terminate(GenericGattClient* client, const GattWriteCallbackParams& response) {
621654
client->remove_control_block(this);
622655
client->processWriteResponse(&response);
@@ -814,6 +847,10 @@ struct GenericGattClient::DescriptorDiscoveryControlBlock : public ProcedureCont
814847
terminate(client, BLE_ERROR_UNSPECIFIED);
815848
}
816849

850+
virtual void abort(GenericGattClient *client) {
851+
terminate(client, BLE_ERROR_INVALID_STATE);
852+
}
853+
817854
virtual void handle(GenericGattClient* client, const AttServerMessage& message) {
818855
if (done) {
819856
terminate(client, BLE_ERROR_NONE);
@@ -892,7 +929,8 @@ struct GenericGattClient::DescriptorDiscoveryControlBlock : public ProcedureCont
892929
GenericGattClient::GenericGattClient(pal::GattClient* pal_client) :
893930
_pal_client(pal_client),
894931
_termination_callback(),
895-
control_blocks(NULL) {
932+
control_blocks(NULL),
933+
_is_reseting(false) {
896934
_pal_client->when_server_message_received(
897935
mbed::callback(this, &GenericGattClient::on_server_message_received)
898936
);
@@ -909,7 +947,7 @@ ble_error_t GenericGattClient::launchServiceDiscovery(
909947
const UUID& matching_characteristic_uuid
910948
) {
911949
// verify that there is no other procedures going on this connection
912-
if (get_control_block(connection_handle)) {
950+
if (_is_reseting || get_control_block(connection_handle)) {
913951
return BLE_ERROR_INVALID_STATE;
914952
}
915953

@@ -988,7 +1026,7 @@ ble_error_t GenericGattClient::read(
9881026
uint16_t offset) const
9891027
{
9901028
// verify that there is no other procedures going on this connection
991-
if (get_control_block(connection_handle)) {
1029+
if (_is_reseting || get_control_block(connection_handle)) {
9921030
return BLE_ERROR_INVALID_STATE;
9931031
}
9941032

@@ -1032,7 +1070,7 @@ ble_error_t GenericGattClient::write(
10321070
const uint8_t* value
10331071
) const {
10341072
// verify that there is no other procedures going on this connection
1035-
if (get_control_block(connection_handle)) {
1073+
if (_is_reseting || get_control_block(connection_handle)) {
10361074
return BLE_ERROR_INVALID_STATE;
10371075
}
10381076

@@ -1111,7 +1149,7 @@ ble_error_t GenericGattClient::discoverCharacteristicDescriptors(
11111149
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
11121150
) {
11131151
// verify that there is no other procedures going on this connection
1114-
if (get_control_block(characteristic.getConnectionHandle())) {
1152+
if (_is_reseting || get_control_block(characteristic.getConnectionHandle())) {
11151153
return BLE_ERROR_INVALID_STATE;
11161154
}
11171155

@@ -1186,7 +1224,17 @@ void GenericGattClient::terminateCharacteristicDescriptorDiscovery(
11861224
}
11871225

11881226
ble_error_t GenericGattClient::reset(void) {
1189-
return BLE_ERROR_NOT_IMPLEMENTED;
1227+
1228+
// _is_reseting prevent executions of new procedure while the instance resets.
1229+
// otherwise new procedures can be launched from callbacks generated by the
1230+
// reset.
1231+
_is_reseting = true;
1232+
while (control_blocks) {
1233+
control_blocks->abort(this);
1234+
}
1235+
_is_reseting = false;
1236+
1237+
return BLE_ERROR_NONE;
11901238
}
11911239

11921240
void GenericGattClient::on_termination(Gap::Handle_t connection_handle) {

0 commit comments

Comments
 (0)