BLE: Introduce new disconnection API.

pull/8738/head
Vincent Coubard 2018-11-16 10:10:34 +00:00
parent 7d7ccea827
commit da7d4a0c12
6 changed files with 164 additions and 20 deletions

View File

@ -326,6 +326,8 @@ public:
*/
void onConnectionComplete(const ble::ConnectionCompleteEvent &event) { }
void onDisconnection(const ble::DisconnectionEvent &event) { }
/**
* Function invoked when the current transmitter and receiver PHY have
* been read for a given connection.
@ -708,6 +710,24 @@ public:
*/
virtual ble_error_t cancelConnect();
/**
* Initiate a disconnection procedure.
*
* Once the disconnection procedure has completed a
* DisconnectionCallbackParams_t, the event is emitted to handlers that
* have been registered with onDisconnection().
*
* @param[in] reason Reason of the disconnection transmitted to the peer.
* @param[in] connectionHandle Handle of the connection to end.
*
* @return BLE_ERROR_NONE if the disconnection procedure successfully
* started.
*/
virtual ble_error_t disconnect(
ble::connection_handle_t connectionHandle,
ble::local_disconnection_reason_t reason
);
/**
* Read the PHY used by the transmitter and the receiver on a connection.
*

View File

@ -552,6 +552,28 @@ private:
const address_t &peerAddress;
};
struct DisconnectionEvent {
DisconnectionEvent(
connection_handle_t connectionHandle,
const disconnection_reason_t &reason
) : connectionHandle(connectionHandle), reason(reason) { }
connection_handle_t getConnectionHandle() const
{
return connectionHandle;
}
const disconnection_reason_t &getReason() const
{
return reason;
}
private:
ble::connection_handle_t connectionHandle;
ble::disconnection_reason_t reason;
};
} // namespace ble
#endif //BLE_GAP_EVENTS_H

View File

@ -555,6 +555,110 @@ struct connection_role_t :SafeEnum<connection_role_t, uint8_t> {
explicit connection_role_t(uint8_t raw_value) : SafeEnum(raw_value) { }
};
/**
* Enumeration of disconnection reasons that should be transmited to the peer.
*/
struct local_disconnection_reason_t : SafeEnum<local_disconnection_reason_t, uint8_t> {
enum type {
/**
* GAP or GATT failed to authenticate the peer.
*/
AUTHENTICATION_FAILURE = 0x05,
AUTHENTICATION_FAILLURE = 0x05,
/**
* Connection terminated by the user.
*/
USER_TERMINATION = 0x13,
REMOTE_USER_TERMINATED_CONNECTION = 0x13,
/**
* Connection termination due to low resources.
*/
LOW_RESOURCES = 0x14,
REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES = 0x14,
/**
* Connection termination due to power off.
*/
POWER_OFF = 0x15,
REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF = 0x15,
/**
* Remote feature not supported
*/
UNSUPPORTED_REMOTE_FEATURE = 0x1A,
/**
* Not possible to pai with a unit key.
*/
PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = 0x29,
/**
* Connection parameters were unacceptable.
*/
CONN_INTERVAL_UNACCEPTABLE = 0x3B,
UNACCEPTABLE_CONNECTION_PARAMETERS = 0x3B
};
/**
* Construct a new instance of disconnection_reason_t.
*/
local_disconnection_reason_t(type value) : SafeEnum(value) { }
};
/**
* Enumeration of disconnection reasons received in a disconnection event.
*/
struct disconnection_reason_t : SafeEnum<disconnection_reason_t, uint8_t> {
enum type {
/**
* GAP or GATT failed to authenticate the peer.
*/
AUTHENTICATION_FAILURE = 0x05,
/**
* The connection timed out.
*/
CONNECTION_TIMEOUT = 0x08,
/**
* Connection terminated by the user.
*/
REMOTE_USER_TERMINATED_CONNECTION = 0x13,
/**
* Remote device terminated connection due to low resources.
*/
REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14,
/**
* Remote device terminated connection due to power off.
*/
REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF = 0x15,
/**
* Indicate that the local user or the internal
* Bluetooth subsystem terminated the connection.
*/
LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
/**
* Connection parameters were unacceptable.
*/
CONN_INTERVAL_UNACCEPTABLE = 0x3B
};
/**
* Construct a new instance of disconnection_reason_t.
*/
disconnection_reason_t(type value) : SafeEnum(value) { }
};
} // namespace ble
#endif //BLE_GAP_TYPES_H

View File

@ -216,26 +216,7 @@ struct hci_error_code_t : SafeEnum<hci_error_code_t, uint8_t> {
};
/**
* Reasons which can be used to end a connection.
*/
struct disconnection_reason_t : SafeEnum<disconnection_reason_t, uint8_t> {
enum type {
AUTHENTICATION_FAILLURE = 0x05,
REMOTE_USER_TERMINATED_CONNECTION = 0x13,
REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES = 0x14,
REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF = 0x15,
UNSUPPORTED_REMOTE_FEATURE = 0x1A,
PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = 0x29,
UNACCEPTABLE_CONNECTION_PARAMETERS = 0x3B
};
/**
* Construct a new disconnection_reason_t instance.
*/
disconnection_reason_t(type value) :
SafeEnum<disconnection_reason_t, uint8_t>(value) { }
};
typedef ble::local_disconnection_reason_t disconnection_reason_t;
typedef ble::advertising_filter_policy_t advertising_filter_policy_t;

View File

@ -271,6 +271,14 @@ ble_error_t Gap::cancelConnect() {
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t Gap::disconnect(
ble::connection_handle_t connectionHandle,
ble::local_disconnection_reason_t reason
) {
// Forward to the old implementation for now.
return disconnect(connectionHandle, (Gap::DisconnectionReason_t) reason.value());
}
ble_error_t Gap::readPhy(ble::connection_handle_t connection)
{
return BLE_ERROR_NOT_IMPLEMENTED;

View File

@ -1228,6 +1228,15 @@ void GenericGap::processDisconnectionEvent(
);
}
if (_eventHandler) {
_eventHandler->onDisconnection(
DisconnectionEvent(
handle,
(ble::disconnection_reason_t::type) reason
)
);
}
::Gap::processDisconnectionEvent(
handle,
reason