BLE - Devirtualize pal::ConnectionEventMonitor

The event handler has been extracted out of the monitor declaration.
pull/9727/head
Vincent Coubard 2019-02-25 18:53:31 +00:00
parent 71b8d8a2f3
commit 6514bbd1f0
1 changed files with 70 additions and 44 deletions

View File

@ -24,61 +24,87 @@
namespace ble {
namespace pal {
/**
* Implemented by classes that are reacting to connection changes.
* @see ConnectionEventMonitor
*/
template<class Impl>
class ConnectionEventMonitorEventHandler {
Impl* self() {
return static_cast<Impl*>(this);
}
public:
/**
* Inform the Security manager of a new connection. This will create
* or retrieve an existing security manager entry for the connected device.
* Called by GAP.
*
* @param[in] connection Handle to identify the connection.
* @param[in] role indicate if the device is central or peripheral.
* @param[in] peer_address_type type of address.
* @param[in] peer_address Address of the connected device.
* @param[in] local_address_type type of address of the local device.
* @param[in] local_address Address of the local device that was used during connection.
* @param[in] connection_params connection parameters like interval, latency and timeout.
* @param[in] resolved_peer_address resolved address of the peer; may
* be NULL.
*/
void on_connected(
connection_handle_t connection,
::Gap::Role_t role,
ble::peer_address_type_t peer_address_type,
const BLEProtocol::AddressBytes_t peer_address,
BLEProtocol::AddressType_t local_address_type,
const BLEProtocol::AddressBytes_t local_address,
const ::Gap::ConnectionParams_t *connection_params
) {
self()->on_connected_(
connection,
role,
peer_address_type,
peer_address,
local_address_type,
local_address,
connection_params
);
}
/**
* Inform the monitor about a disconnection.
*
* @param[in] connectionHandle Handle to identify the connection.
* @param[in] reason Reason for the disconnection.
*/
void on_disconnected(
connection_handle_t connection,
::Gap::DisconnectionReason_t reason
) {
self()->on_disconnected_(connection, reason);
}
};
/**
* Implemented by classes that need to be notified of connection changes.
* Notification is done by calling functions in the passed in event handler
*/
template<class EventHandler>
class ConnectionEventMonitor {
protected:
ConnectionEventMonitor() : _connection_event_handler(NULL) { }
EventHandler* _connection_event_handler;
public:
/**
* Implemented by classes that are reacting to connection changes.
* @see ConnectionEventMonitor
*/
class EventHandler {
public:
/**
* Inform the Security manager of a new connection. This will create
* or retrieve an existing security manager entry for the connected device.
* Called by GAP.
*
* @param[in] connection Handle to identify the connection.
* @param[in] role indicate if the device is central or peripheral.
* @param[in] peer_address_type type of address.
* @param[in] peer_address Address of the connected device.
* @param[in] local_address_type type of address of the local device.
* @param[in] local_address Address of the local device that was used during connection.
* @param[in] connection_params connection parameters like interval, latency and timeout.
* @param[in] resolved_peer_address resolved address of the peer; may
* be NULL.
*/
virtual void on_connected(
connection_handle_t connection,
::Gap::Role_t role,
ble::peer_address_type_t peer_address_type,
const BLEProtocol::AddressBytes_t peer_address,
BLEProtocol::AddressType_t local_address_type,
const BLEProtocol::AddressBytes_t local_address,
const ::Gap::ConnectionParams_t *connection_params
) = 0;
/**
* Inform the monitor about a disconnection.
*
* @param[in] connectionHandle Handle to identify the connection.
* @param[in] reason Reason for the disconnection.
*/
virtual void on_disconnected(
connection_handle_t connection,
::Gap::DisconnectionReason_t reason
) = 0;
};
/**
* Register a handler for connection events to be used internally and serviced first.
*
* @param[in] connection_event_handler Event handler being registered.
*/
virtual void set_connection_event_handler(EventHandler *connection_event_handler) = 0;
void set_connection_event_handler(EventHandler *connection_event_handler) {
_connection_event_handler = connection_event_handler;
}
};
} // namespace pal