Skip to content

Commit a7b9490

Browse files
authored
Merge pull request #9537 from paul-szczepanek-arm/mtu-events
BLE: Add MTU events
2 parents 1554b0c + d801ed3 commit a7b9490

File tree

19 files changed

+415
-82
lines changed

19 files changed

+415
-82
lines changed

features/FEATURE_BLE/ble/GattClient.h

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@
8383
*/
8484
class GattClient {
8585
public:
86+
87+
/**
88+
* Definition of the general handler of GattClient related events.
89+
*/
90+
struct EventHandler {
91+
/**
92+
* Function invoked when the connections changes the ATT_MTU which controls
93+
* the maximum size of an attribute that can be read in a single L2CAP packet
94+
* which might be fragmented across multiple packets.
95+
*
96+
* @param connectionHandle The handle of the connection that changed the size.
97+
* @param attMtuSize
98+
*/
99+
virtual void onAttMtuChange(
100+
ble::connection_handle_t connectionHandle,
101+
uint16_t attMtuSize
102+
)
103+
{
104+
}
105+
};
106+
107+
/**
108+
* Assign the event handler implementation that will be used by the
109+
* module to signal events back to the application.
110+
*
111+
* @param handler Application implementation of an EventHandler.
112+
*/
113+
void setEventHandler(EventHandler *handler)
114+
{
115+
eventHandler = handler;
116+
}
117+
86118
/**
87119
* Attribute read event handler.
88120
*
@@ -635,6 +667,27 @@ class GattClient {
635667
(void) characteristic;
636668
}
637669

670+
/**
671+
* Trigger MTU negotiation. This might result in a Gap event onAttMtuChange
672+
* being called if MTU changes.
673+
*
674+
* @note This does not guarantee a change in MTU size. If size remains
675+
* unchanged no event will be generated.
676+
*
677+
* @param connection Connection on which the MTU is to be negotiated.
678+
*
679+
* @return BLE_ERROR_NONE if the procedure has been launched successfully
680+
* otherwise an appropriate error.
681+
*/
682+
virtual ble_error_t negotiateAttMtu(
683+
ble::connection_handle_t connection
684+
) {
685+
/* Requesting action from porter(s): override this API if this
686+
capability is supported. */
687+
(void) connection;
688+
return BLE_ERROR_NOT_IMPLEMENTED;
689+
}
690+
638691
/**
639692
* Register an handler for Handle Value Notification/Indication events.
640693
*
@@ -742,7 +795,7 @@ class GattClient {
742795
}
743796

744797
protected:
745-
GattClient()
798+
GattClient() : eventHandler(NULL)
746799
{
747800
/* Empty */
748801
}
@@ -794,6 +847,11 @@ class GattClient {
794847
}
795848

796849
protected:
850+
/**
851+
* Event handler provided by the application.
852+
*/
853+
EventHandler *eventHandler;
854+
797855
/**
798856
* Callchain containing all registered event handlers for data read
799857
* events.

features/FEATURE_BLE/ble/GattServer.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,38 @@
8787
*/
8888
class GattServer {
8989
public:
90+
91+
/**
92+
* Definition of the general handler of GattServer related events.
93+
*/
94+
struct EventHandler {
95+
/**
96+
* Function invoked when the connections changes the ATT_MTU which controls
97+
* the maximum size of an attribute that can be read in a single L2CAP packet
98+
* which might be fragmented across multiple packets.
99+
*
100+
* @param connectionHandle The handle of the connection that changed the size.
101+
* @param attMtuSize
102+
*/
103+
virtual void onAttMtuChange(
104+
ble::connection_handle_t connectionHandle,
105+
uint16_t attMtuSize
106+
)
107+
{
108+
}
109+
};
110+
111+
/**
112+
* Assign the event handler implementation that will be used by the
113+
* module to signal events back to the application.
114+
*
115+
* @param handler Application implementation of an EventHandler.
116+
*/
117+
void setEventHandler(EventHandler *handler)
118+
{
119+
eventHandler = handler;
120+
}
121+
90122
/**
91123
* Event handler invoked when the server has sent data to a client.
92124
*
@@ -166,6 +198,7 @@ class GattServer {
166198
GattServer() :
167199
serviceCount(0),
168200
characteristicCount(0),
201+
eventHandler(NULL),
169202
dataSentCallChain(),
170203
dataWrittenCallChain(),
171204
dataReadCallChain(),
@@ -778,6 +811,11 @@ class GattServer {
778811
}
779812

780813
protected:
814+
/**
815+
* Event handler provided by the application.
816+
*/
817+
EventHandler *eventHandler;
818+
781819
/**
782820
* The total number of services added to the ATT table.
783821
*/

features/FEATURE_BLE/ble/gap/Gap.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ class Gap {
492492
{
493493
}
494494

495+
/**
496+
* Function invoked when the connections changes the maximum number of octets
497+
* that can be sent or received by the controller in a single packet. A single
498+
* L2CAP packet can be fragmented across many such packets.
499+
*
500+
* @note This only triggers if controller supports data length extension and
501+
* negotiated data length is longer than the default 23.
502+
*
503+
* @param connectionHandle The handle of the connection that changed the size.
504+
* @param txSize Number of octets we can send on this connection in a single packet.
505+
* @param rxSize Number of octets we can receive on this connection in a single packet.
506+
*/
507+
virtual void onDataLengthChange(
508+
connection_handle_t connectionHandle,
509+
uint16_t txSize,
510+
uint16_t rxSize
511+
)
512+
{
513+
}
514+
495515
protected:
496516
/**
497517
* Prevent polymorphic deletion and avoid unnecessary virtual destructor

features/FEATURE_BLE/ble/generic/GenericGap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ class GenericGap :
627627
phy_t rx_phy
628628
);
629629

630+
virtual void on_data_length_change(
631+
connection_handle_t connection_handle,
632+
uint16_t tx_size,
633+
uint16_t rx_size
634+
);
635+
630636
virtual void on_phy_update_complete(
631637
pal::hci_error_code_t hci_status,
632638
Handle_t connection_handle,

features/FEATURE_BLE/ble/generic/GenericGattClient.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,23 @@ namespace generic {
3333
* @attention: Not part of the public interface of BLE API.
3434
*/
3535
class GenericGattClient : public GattClient,
36-
public pal::SigningEventMonitor {
36+
public pal::SigningEventMonitor,
37+
public pal::GattClient::EventHandler {
3738
public:
39+
40+
/**
41+
* @see pal::GattClient::EventHandler::on_att_mtu_change
42+
*/
43+
virtual void on_att_mtu_change(
44+
ble::connection_handle_t connection_handle,
45+
uint16_t att_mtu_size
46+
)
47+
{
48+
if (eventHandler) {
49+
eventHandler->onAttMtuChange(connection_handle, att_mtu_size);
50+
}
51+
}
52+
3853
/**
3954
* Create a GenericGattClient from a pal::GattClient
4055
*/
@@ -111,6 +126,13 @@ class GenericGattClient : public GattClient,
111126
const DiscoveredCharacteristic& characteristic
112127
);
113128

129+
/**
130+
* @see GattClient::negotiateAttMtu
131+
*/
132+
virtual ble_error_t negotiateAttMtu(
133+
connection_handle_t connection
134+
);
135+
114136
/**
115137
* @see GattClient::reset
116138
*/
@@ -121,6 +143,14 @@ class GenericGattClient : public GattClient,
121143
*/
122144
virtual void set_signing_event_handler(pal::SigningEventMonitor::EventHandler *signing_event_handler);
123145

146+
/**
147+
* Return the user registered event handler.
148+
* @return User registered event handler or NULL if none is present.
149+
*/
150+
::GattClient::EventHandler* getEventHandler() {
151+
return eventHandler;
152+
}
153+
124154
private:
125155
struct ProcedureControlBlock;
126156
struct DiscoveryControlBlock;

features/FEATURE_BLE/ble/pal/PalGap.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ struct Gap {
4444
ble::phy_t rx_phy
4545
) = 0;
4646

47+
/**
48+
* @copydoc Gap::EventHandler::onDataLengthChange
49+
*/
50+
virtual void on_data_length_change(
51+
connection_handle_t connection_handle,
52+
uint16_t tx_size,
53+
uint16_t rx_size
54+
) = 0;
55+
4756
/**
4857
* @copydoc Gap::EventHandler::onPhyUpdateComplete
4958
*/

features/FEATURE_BLE/ble/pal/PalGattClient.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,25 @@ namespace pal {
5555
* the class AttClientToGattClientAdapter
5656
*/
5757
class GattClient {
58-
5958
public:
59+
/**
60+
* Definition of the general handler of GattClient related events.
61+
*/
62+
struct EventHandler {
63+
/**
64+
* Function invoked when the connections changes the ATT_MTU which controls
65+
* the maximum size of an attribute that can be read in a single L2CAP packet
66+
* which might be fragmented across multiple packets.
67+
*
68+
* @param connectionHandle The handle of the connection that changed the size.
69+
* @param attMtuSize
70+
*/
71+
virtual void on_att_mtu_change(
72+
ble::connection_handle_t connection_handle,
73+
uint16_t att_mtu_size
74+
) = 0;
75+
};
76+
6077
/**
6178
* Initialisation of the instance. An implementation can use this function
6279
* to initialise the subsystems needed to realize the operations of this
@@ -582,8 +599,27 @@ class GattClient {
582599
_transaction_timeout_cb = cb;
583600
}
584601

602+
/**
603+
* Sets the event handler that us called by the PAL porters to notify the stack of events
604+
* which will in turn be passed onto the user application when appropriate.
605+
*
606+
* @param event_handler The new event handler interface implementation.
607+
*/
608+
void set_event_handler(EventHandler* event_handler) {
609+
_event_handler = event_handler;
610+
}
611+
612+
/**
613+
* Get the currently registered event handler.
614+
*
615+
* @return Currently registered event handler. NULL if no event handler is present.
616+
*/
617+
EventHandler* get_event_handler() {
618+
return _event_handler;
619+
}
620+
585621
protected:
586-
GattClient() { }
622+
GattClient() : _event_handler(NULL) { }
587623

588624
virtual ~GattClient() { }
589625

@@ -621,6 +657,8 @@ class GattClient {
621657
}
622658

623659
private:
660+
EventHandler* _event_handler;
661+
624662
/**
625663
* Callback called when the client receive a message from the server.
626664
*/

features/FEATURE_BLE/source/generic/GenericGap.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,17 @@ void GenericGap::on_read_phy(
860860
}
861861
}
862862

863+
void GenericGap::on_data_length_change(
864+
Handle_t connection_handle,
865+
uint16_t tx_size,
866+
uint16_t rx_size
867+
)
868+
{
869+
if (_eventHandler) {
870+
_eventHandler->onDataLengthChange(connection_handle, tx_size, rx_size);
871+
}
872+
}
873+
863874
void GenericGap::on_phy_update_complete(
864875
pal::hci_error_code_t hci_status,
865876
Handle_t connection_handle,

features/FEATURE_BLE/source/generic/GenericGattClient.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,12 @@ void GenericGattClient::terminateCharacteristicDescriptorDiscovery(
12591259

12601260
}
12611261

1262+
ble_error_t GenericGattClient::negotiateAttMtu(
1263+
connection_handle_t connection
1264+
) {
1265+
return _pal_client->exchange_mtu(connection);
1266+
}
1267+
12621268
ble_error_t GenericGattClient::reset(void) {
12631269

12641270
// _is_reseting prevent executions of new procedure while the instance resets.
@@ -1274,7 +1280,7 @@ ble_error_t GenericGattClient::reset(void) {
12741280
}
12751281

12761282
void GenericGattClient::set_signing_event_handler(
1277-
EventHandler *signing_event_handler
1283+
pal::SigningEventMonitor::EventHandler *signing_event_handler
12781284
) {
12791285
_signing_event_handler = signing_event_handler;
12801286
}

features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ class BLE : public ::BLEInstanceBase {
107107
*/
108108
virtual generic::GenericGattClient &getGattClient();
109109

110+
/**
111+
* Get the PAL Gatt Client.
112+
*
113+
* @return PAL Gatt Client.
114+
*/
115+
pal::AttClientToGattClientAdapter &getPalGattClient();
116+
110117
/**
111118
* @see BLEInstanceBase::getSecurityManager
112119
*/

features/FEATURE_BLE/targets/TARGET_CORDIO/CordioGattServer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class GattServer : public ::GattServer,
6262
*/
6363
static GattServer &getInstance();
6464

65+
::GattServer::EventHandler* getEventHandler() {
66+
return eventHandler;
67+
}
68+
6569
/**
6670
* Initialize the GattServer and add mandatory services (generic access and
6771
* generic attribute service).

0 commit comments

Comments
 (0)