Skip to content

BLE: Add MTU events #9537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion features/FEATURE_BLE/ble/GattClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@
*/
class GattClient {
public:

/**
* Definition of the general handler of GattClient related events.
*/
struct EventHandler {
/**
* Function invoked when the connections changes the ATT_MTU which controls
* the maximum size of an attribute that can be read in a single L2CAP packet
* which might be fragmented across multiple packets.
*
* @param connectionHandle The handle of the connection that changed the size.
* @param attMtuSize
*/
virtual void onAttMtuChange(
ble::connection_handle_t connectionHandle,
uint16_t attMtuSize
)
{
}
};

/**
* Assign the event handler implementation that will be used by the
* module to signal events back to the application.
*
* @param handler Application implementation of an EventHandler.
*/
void setEventHandler(EventHandler *handler)
{
eventHandler = handler;
}

/**
* Attribute read event handler.
*
Expand Down Expand Up @@ -635,6 +667,27 @@ class GattClient {
(void) characteristic;
}

/**
* Trigger MTU negotiation. This might result in a Gap event onAttMtuChange
* being called if MTU changes.
*
* @note This does not guarantee a change in MTU size. If size remains
* unchanged no event will be generated.
*
* @param connection Connection on which the MTU is to be negotiated.
*
* @return BLE_ERROR_NONE if the procedure has been launched successfully
* otherwise an appropriate error.
*/
virtual ble_error_t negotiateAttMtu(
ble::connection_handle_t connection
) {
/* Requesting action from porter(s): override this API if this
capability is supported. */
(void) connection;
return BLE_ERROR_NOT_IMPLEMENTED;
}

/**
* Register an handler for Handle Value Notification/Indication events.
*
Expand Down Expand Up @@ -742,7 +795,7 @@ class GattClient {
}

protected:
GattClient()
GattClient() : eventHandler(NULL)
{
/* Empty */
}
Expand Down Expand Up @@ -794,6 +847,11 @@ class GattClient {
}

protected:
/**
* Event handler provided by the application.
*/
EventHandler *eventHandler;

/**
* Callchain containing all registered event handlers for data read
* events.
Expand Down
38 changes: 38 additions & 0 deletions features/FEATURE_BLE/ble/GattServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@
*/
class GattServer {
public:

/**
* Definition of the general handler of GattServer related events.
*/
struct EventHandler {
/**
* Function invoked when the connections changes the ATT_MTU which controls
* the maximum size of an attribute that can be read in a single L2CAP packet
* which might be fragmented across multiple packets.
*
* @param connectionHandle The handle of the connection that changed the size.
* @param attMtuSize
*/
virtual void onAttMtuChange(
ble::connection_handle_t connectionHandle,
uint16_t attMtuSize
)
{
}
};

/**
* Assign the event handler implementation that will be used by the
* module to signal events back to the application.
*
* @param handler Application implementation of an EventHandler.
*/
void setEventHandler(EventHandler *handler)
{
eventHandler = handler;
}

/**
* Event handler invoked when the server has sent data to a client.
*
Expand Down Expand Up @@ -166,6 +198,7 @@ class GattServer {
GattServer() :
serviceCount(0),
characteristicCount(0),
eventHandler(NULL),
dataSentCallChain(),
dataWrittenCallChain(),
dataReadCallChain(),
Expand Down Expand Up @@ -778,6 +811,11 @@ class GattServer {
}

protected:
/**
* Event handler provided by the application.
*/
EventHandler *eventHandler;

/**
* The total number of services added to the ATT table.
*/
Expand Down
20 changes: 20 additions & 0 deletions features/FEATURE_BLE/ble/gap/Gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,26 @@ class Gap {
{
}

/**
* Function invoked when the connections changes the maximum number of octets
* that can be sent or received by the controller in a single packet. A single
* L2CAP packet can be fragmented across many such packets.
*
* @note This only triggers if controller supports data length extension and
* negotiated data length is longer than the default 23.
*
* @param connectionHandle The handle of the connection that changed the size.
* @param txSize Number of octets we can send on this connection in a single packet.
* @param rxSize Number of octets we can receive on this connection in a single packet.
*/
virtual void onDataLengthChange(
connection_handle_t connectionHandle,
uint16_t txSize,
uint16_t rxSize
)
{
}

protected:
/**
* Prevent polymorphic deletion and avoid unnecessary virtual destructor
Expand Down
6 changes: 6 additions & 0 deletions features/FEATURE_BLE/ble/generic/GenericGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,12 @@ class GenericGap :
phy_t rx_phy
);

virtual void on_data_length_change(
connection_handle_t connection_handle,
uint16_t tx_size,
uint16_t rx_size
);

virtual void on_phy_update_complete(
pal::hci_error_code_t hci_status,
Handle_t connection_handle,
Expand Down
32 changes: 31 additions & 1 deletion features/FEATURE_BLE/ble/generic/GenericGattClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,23 @@ namespace generic {
* @attention: Not part of the public interface of BLE API.
*/
class GenericGattClient : public GattClient,
public pal::SigningEventMonitor {
public pal::SigningEventMonitor,
public pal::GattClient::EventHandler {
public:

/**
* @see pal::GattClient::EventHandler::on_att_mtu_change
*/
virtual void on_att_mtu_change(
ble::connection_handle_t connection_handle,
uint16_t att_mtu_size
)
{
if (eventHandler) {
eventHandler->onAttMtuChange(connection_handle, att_mtu_size);
}
}

/**
* Create a GenericGattClient from a pal::GattClient
*/
Expand Down Expand Up @@ -111,6 +126,13 @@ class GenericGattClient : public GattClient,
const DiscoveredCharacteristic& characteristic
);

/**
* @see GattClient::negotiateAttMtu
*/
virtual ble_error_t negotiateAttMtu(
connection_handle_t connection
);

/**
* @see GattClient::reset
*/
Expand All @@ -121,6 +143,14 @@ class GenericGattClient : public GattClient,
*/
virtual void set_signing_event_handler(pal::SigningEventMonitor::EventHandler *signing_event_handler);

/**
* Return the user registered event handler.
* @return User registered event handler or NULL if none is present.
*/
::GattClient::EventHandler* getEventHandler() {
return eventHandler;
}

private:
struct ProcedureControlBlock;
struct DiscoveryControlBlock;
Expand Down
9 changes: 9 additions & 0 deletions features/FEATURE_BLE/ble/pal/PalGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ struct Gap {
ble::phy_t rx_phy
) = 0;

/**
* @copydoc Gap::EventHandler::onDataLengthChange
*/
virtual void on_data_length_change(
connection_handle_t connection_handle,
uint16_t tx_size,
uint16_t rx_size
) = 0;

/**
* @copydoc Gap::EventHandler::onPhyUpdateComplete
*/
Expand Down
42 changes: 40 additions & 2 deletions features/FEATURE_BLE/ble/pal/PalGattClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,25 @@ namespace pal {
* the class AttClientToGattClientAdapter
*/
class GattClient {

public:
/**
* Definition of the general handler of GattClient related events.
*/
struct EventHandler {
/**
* Function invoked when the connections changes the ATT_MTU which controls
* the maximum size of an attribute that can be read in a single L2CAP packet
* which might be fragmented across multiple packets.
*
* @param connectionHandle The handle of the connection that changed the size.
* @param attMtuSize
*/
virtual void on_att_mtu_change(
ble::connection_handle_t connection_handle,
uint16_t att_mtu_size
) = 0;
};

/**
* Initialisation of the instance. An implementation can use this function
* to initialise the subsystems needed to realize the operations of this
Expand Down Expand Up @@ -582,8 +599,27 @@ class GattClient {
_transaction_timeout_cb = cb;
}

/**
* Sets the event handler that us called by the PAL porters to notify the stack of events
* which will in turn be passed onto the user application when appropriate.
*
* @param event_handler The new event handler interface implementation.
*/
void set_event_handler(EventHandler* event_handler) {
_event_handler = event_handler;
}

/**
* Get the currently registered event handler.
*
* @return Currently registered event handler. NULL if no event handler is present.
*/
EventHandler* get_event_handler() {
return _event_handler;
}

protected:
GattClient() { }
GattClient() : _event_handler(NULL) { }

virtual ~GattClient() { }

Expand Down Expand Up @@ -621,6 +657,8 @@ class GattClient {
}

private:
EventHandler* _event_handler;

/**
* Callback called when the client receive a message from the server.
*/
Expand Down
11 changes: 11 additions & 0 deletions features/FEATURE_BLE/source/generic/GenericGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,17 @@ void GenericGap::on_read_phy(
}
}

void GenericGap::on_data_length_change(
Handle_t connection_handle,
uint16_t tx_size,
uint16_t rx_size
)
{
if (_eventHandler) {
_eventHandler->onDataLengthChange(connection_handle, tx_size, rx_size);
}
}

void GenericGap::on_phy_update_complete(
pal::hci_error_code_t hci_status,
Handle_t connection_handle,
Expand Down
8 changes: 7 additions & 1 deletion features/FEATURE_BLE/source/generic/GenericGattClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,12 @@ void GenericGattClient::terminateCharacteristicDescriptorDiscovery(

}

ble_error_t GenericGattClient::negotiateAttMtu(
connection_handle_t connection
) {
return _pal_client->exchange_mtu(connection);
}

ble_error_t GenericGattClient::reset(void) {

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

void GenericGattClient::set_signing_event_handler(
EventHandler *signing_event_handler
pal::SigningEventMonitor::EventHandler *signing_event_handler
) {
_signing_event_handler = signing_event_handler;
}
Expand Down
7 changes: 7 additions & 0 deletions features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ class BLE : public ::BLEInstanceBase {
*/
virtual generic::GenericGattClient &getGattClient();

/**
* Get the PAL Gatt Client.
*
* @return PAL Gatt Client.
*/
pal::AttClientToGattClientAdapter &getPalGattClient();

/**
* @see BLEInstanceBase::getSecurityManager
*/
Expand Down
4 changes: 4 additions & 0 deletions features/FEATURE_BLE/targets/TARGET_CORDIO/CordioGattServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class GattServer : public ::GattServer,
*/
static GattServer &getInstance();

::GattServer::EventHandler* getEventHandler() {
return eventHandler;
}

/**
* Initialize the GattServer and add mandatory services (generic access and
* generic attribute service).
Expand Down
Loading