diff --git a/features/FEATURE_BLE/ble/Gap.h b/features/FEATURE_BLE/ble/Gap.h index 8576b7c880..46cc61716a 100644 --- a/features/FEATURE_BLE/ble/Gap.h +++ b/features/FEATURE_BLE/ble/Gap.h @@ -25,6 +25,7 @@ #include "ble/gap/ConnectionParameters.h" #include "ble/gap/ScanParameters.h" #include "ble/gap/AdvertisingParameters.h" +#include "ble/gap/Events.h" // leagacy gap headers #include "ble/GapAdvertisingData.h" @@ -1162,128 +1163,6 @@ public: * Definition of the general handler of Gap related events. */ struct EventHandler { - - /** Event generated when an advertising packet is seen during passive scanning - * or a scan response is received during active scanning. - */ - struct AdvertisingReportEvent { - /** Create a advertising report event. - * - * @param type Type of advertising used. - * @param peerAddressType Peer address type of advertiser. - * @param peerAddress Peer address of advertiser. - * @param primaryPhy PHY used on the primary channels. - * @param secondaryPhy PHY used on secondary channels. - * @param SID Set identification number. - * @param txPower Transmission power reported by the packet. - * @param rssi Measured signal strength. - * @param periodicInterval Interval of periodic advertising. - * @param directAddressType Directed advertising target address type. - * @param directAddress Directed advertising target address. - * @param advertisingData Advertising payload. - */ - AdvertisingReportEvent( - const AdvertisingEventType_t &type, - const PeerAddressType_t &peerAddressType, - const ble::address_t &peerAddress, - const Phy_t &primaryPhy, - const Phy_t &secondaryPhy, - ble::advertising_sid_t SID, - ble::advertising_power_t txPower, - ble::rssi_t rssi, - UnitPeriodicInterval_t periodicInterval, - const PeerAddressType_t &directAddressType, - const ble::address_t &directAddress, - const mbed::Span &advertisingData - ) : - type(type), - peerAddressType(peerAddressType), - peerAddress(peerAddress), - primaryPhy(primaryPhy), - secondaryPhy(secondaryPhy), - SID(SID), - txPower(txPower), - rssi(rssi), - periodicInterval(periodicInterval), - directAddressType(directAddressType), - directAddress(directAddress), - advertisingData(advertisingData) { } - - /** Get event type. */ - const AdvertisingEventType_t &getType() const { - return type; - } - - /** Get peer address type. */ - const PeerAddressType_t &getPeerAddressType() const { - return peerAddressType; - } - - /** Get peer address. */ - const ble::address_t &getPeerAddress() const { - return peerAddress; - } - - /** Get primary PHY. */ - const Phy_t &getPrimaryPhy() const { - return primaryPhy; - } - - /** Get secondary PHY. */ - const Phy_t &getSecondaryPhy() const { - return secondaryPhy; - } - - /** Get advertising set identifier. */ - ble::advertising_sid_t getSID() const { - return SID; - } - - /** Get TX power. */ - ble::advertising_power_t getTxPower() const { - return txPower; - } - - /** Get received signal strength. */ - ble::rssi_t getRssi() const { - return rssi; - } - - /** Get interval. */ - UnitPeriodicInterval_t getPeriodicInterval() const { - return periodicInterval; - } - - /** Get target address type in directed advertising. */ - const PeerAddressType_t &getDirectAddressType() const { - return directAddressType; - } - - /** Get target address in directed advertising. */ - const ble::address_t &getDirectAddress() const { - return directAddress; - } - - /** Get payload. */ - const mbed::Span &getAdvertisingData() const { - return advertisingData; - } - - private: - AdvertisingEventType_t type; - PeerAddressType_t peerAddressType; - ble::address_t const &peerAddress; - Phy_t primaryPhy; - Phy_t secondaryPhy; - ble::advertising_sid_t SID; - ble::advertising_power_t txPower; - ble::rssi_t rssi; - UnitPeriodicInterval_t periodicInterval; - PeerAddressType_t directAddressType; - const ble::address_t &directAddress; - mbed::Span advertisingData; - }; - /** Called when scanning reads an advertising packet during passive scan or receives * a scan response during an active scan. * @@ -1295,117 +1174,6 @@ public: (void) event; } - /** Event generated when a connection initiation end (successfully or not). - */ - struct ConnectionCompleteEvent { - /** Create a connection complete event. - * - * @param success BLE_ERROR_NONE if connection succeeded. - * @param connectionHandle Connection handle if successful. - * @param ownRole Role of the local device. - * @param peerAddressType Peer address type. - * @param peerAddress Peer address. - * @param localResolvablePrivateAddress Local address type if privacy enabled. - * @param peerResolvablePrivateAddress Peer address type if privacy enabled. - * @param connectionInterval Connection interval. - * @param connectionLatency Connection latency in events. - * @param supervisionTimeout Supervision timeout. - * @param masterClockAccuracy Peer clock accuracy in parts per million. - */ - ConnectionCompleteEvent( - ble_error_t status, - Handle_t connectionHandle, - Role_t ownRole, - const PeerAddressType_t &peerAddressType, - const ble::address_t &peerAddress, - const ble::address_t &localResolvablePrivateAddress, - const ble::address_t &peerResolvablePrivateAddress, - UnitConnInterval_t connectionInterval, - UnitSlaveLatency_t connectionLatency, - UnitSupervisionTimeout_t supervisionTimeout, - uint16_t masterClockAccuracy - ) : - status(status), - connectionHandle(connectionHandle), - ownRole(ownRole), - peerAddressType(peerAddressType), - peerAddress(peerAddress), - localResolvablePrivateAddress(localResolvablePrivateAddress), - peerResolvablePrivateAddress(peerResolvablePrivateAddress), - connectionInterval(connectionInterval), - connectionLatency(connectionLatency), - supervisionTimeout(supervisionTimeout), - masterClockAccuracy(masterClockAccuracy) { } - - /** Get connection complete event status. */ - ble_error_t getStatus() const { - return status; - } - - /** Get connection handle (valid only when successful). */ - Handle_t getConnectionHandle() const { - return connectionHandle; - } - - /** Get own role. */ - Role_t getOwnRole() const { - return ownRole; - } - - /** Get peer address type. */ - const PeerAddressType_t &getPeerAddressType() const { - return peerAddressType; - } - - /** Get peer address. */ - const ble::address_t &getPeerAddress() const { - return peerAddress; - } - - /** Get get local resolvable random address if privacy is used. */ - const ble::address_t &getLocalResolvablePrivateAddress() const { - return localResolvablePrivateAddress; - } - - /** Get peer resolvable private address if privacy is used. */ - const ble::address_t &getPeerResolvablePrivateAddress() const { - return peerResolvablePrivateAddress; - } - - /** Get connection interval. */ - UnitConnInterval_t getConnectionInterval() const { - return connectionInterval; - } - - /** Get connection latency. */ - UnitSlaveLatency_t getConnectionLatency() const { - return connectionLatency; - } - - /** Get supervision timeout. */ - UnitSupervisionTimeout_t getSupervisionTimeout() const { - return supervisionTimeout; - } - - /** Get clock accuracy in parts per million. */ - uint16_t getMasterClockAccuracy() const { - return masterClockAccuracy; - } - - private: - ble_error_t status; - Handle_t connectionHandle; - Role_t ownRole; - PeerAddressType_t peerAddressType; - const ble::address_t &peerAddress; - const ble::address_t &localResolvablePrivateAddress; - const ble::address_t &peerResolvablePrivateAddress; - UnitConnInterval_t connectionInterval; - UnitSlaveLatency_t connectionLatency; - UnitSupervisionTimeout_t supervisionTimeout; - uint16_t masterClockAccuracy; - }; - /** Called when connection attempt ends. * * @param event Connection event @see ConnectionCompleteEvent_t for details. @@ -1416,90 +1184,6 @@ public: (void)event; } - /** Event generated when we first receive a periodic advertisement. - */ - struct PeriodicAdvertisingSyncEstablishedEvent { - /** Create advertising sync event. - * - * @param success BLE_ERROR_NONE if synchronisation was achieved. - * @param syncHandle Advertising sync handle. - * @param sid Advertising set identifier. - * @param peerAddressType Peer address type. - * @param peerAddress Peer address. - * @param peerPhy PHY used for advertisements. - * @param advertisingInterval Periodic advertising interval. - * @param masterClockAccuracy Peer clock accuracy in parts per million. - */ - PeriodicAdvertisingSyncEstablishedEvent( - ble_error_t status, - ble::periodic_sync_handle_t syncHandle, - ble::advertising_sid_t sid, - const PeerAddressType_t &peerAddressType, - const ble::address_t &peerAddress, - const Phy_t &peerPhy, - uint16_t advertisingInterval, - const ble::clock_accuracy_t &peerClockAccuracy - ) : - status(status), - syncHandle(syncHandle), - sid(sid), - peerAddressType(peerAddressType), - peerAddress(peerAddress), - peerPhy(peerPhy), - advertisingInterval(advertisingInterval), - peerClockAccuracy(peerClockAccuracy) { } - - /** Get sync establishment status. */ - ble_error_t getStatus() const { - return status; - } - - /** Get periodic advertising sync handle. */ - ble::periodic_sync_handle_t getSyncHandle() const { - return syncHandle; - } - - /** Get advertising set identifier. */ - ble::advertising_sid_t getSid() const { - return sid; - } - - /** Get peer address type. */ - const PeerAddressType_t &getPeerAddressType() const { - return peerAddressType; - } - - /** Get peer address. */ - const ble::address_t &getPeerAddress() const { - return peerAddress; - } - - /** Get PHY used. */ - const Phy_t &getPeerPhy() const { - return peerPhy; - } - - /** Get interval. */ - uint16_t getAdvertisingInterval() const { - return advertisingInterval; - } - - /** Get clock accuracy in parts per million. */ - const ble::clock_accuracy_t &getPeerClockAccuracy() const { - return peerClockAccuracy; - } - - private: - ble_error_t status; - ble::periodic_sync_handle_t syncHandle; - ble::advertising_sid_t sid; - PeerAddressType_t peerAddressType; - const ble::address_t& peerAddress; - Phy_t peerPhy; - uint16_t advertisingInterval; - ble::clock_accuracy_t peerClockAccuracy; - }; - /** Called when first advertising packet in periodic advertising is received. * * @param event Periodic advertising sync event @see PeriodicAdvertisingSyncEstablishedEvent. @@ -1510,63 +1194,6 @@ public: (void) event; } - /** Event generated when periodic advertising packet is received. - */ - struct PeriodicAdvertisingReportEvent { - /** Create periodic advertising report event. - * - * @param syncHandle Periodic advertising sync handle - * @param txPower TX power. - * @param rssi Received signal strength. - * @param dataStatus Status to indicate the completeness of the payload. - * @param payload Periodic advertisement payload. - */ - PeriodicAdvertisingReportEvent( - ble::periodic_sync_handle_t syncHandle, - ble::advertising_power_t txPower, - ble::rssi_t rssi, - ble::advertising_data_status_t dataStatus, - const mbed::Span &payload - ) : - syncHandle(syncHandle), - txPower(txPower), - rssi(rssi), - dataStatus(dataStatus), - payload(payload) { } - - /** Get periodic advertising sync handle. */ - ble::periodic_sync_handle_t getSyncHandle() const { - return syncHandle; - } - - /** Get TX power as reported by the advertising packet. */ - ble::advertising_power_t getTxPower() const { - return txPower; - } - - /** Get received signal strength. */ - ble::rssi_t getRssi() const { - return rssi; - } - - /** Get data completeness status. */ - const ble::advertising_data_status_t &getDataStatus() const { - return dataStatus; - } - - /** Get payload. */ - const mbed::Span &getPayload() const { - return payload; - } - - private: - ble::periodic_sync_handle_t syncHandle; - ble::advertising_power_t txPower; - ble::rssi_t rssi; - ble::advertising_data_status_t dataStatus; - mbed::Span payload; - }; - /** Called when a periodic advertising packet is received. * * @param event Periodic advertisement event. @@ -1577,89 +1204,16 @@ public: (void) event; } - /** Event generated when periodic advertising sync is lost. - */ - struct PeriodicAdvertisingSyncLoss { - /** Create periodic advertising sync loss event. - * - * @param syncHandle Periodic advertising sync handle. - */ - PeriodicAdvertisingSyncLoss( - ble::periodic_sync_handle_t syncHandle - ) : - syncHandle(syncHandle) { } - - /** Get periodic sync handle. */ - ble::periodic_sync_handle_t getSyncHandle() const { - return syncHandle; - } - - private: - ble::periodic_sync_handle_t syncHandle; - }; - virtual void onPeriodicAdvertisingSyncLoss( const PeriodicAdvertisingSyncLoss &event ) { (void) event; } - /** Event generated when scan times out. - */ - struct ScanTimeoutEvent { }; - /** Called when scan times out. */ virtual void onScanTimeout(const ScanTimeoutEvent &) { } - /** Event produced when advertising ends. - */ - struct AdvertisingEndEvent_t { - /** Create advertising end event. - * - * @param advHandle Advertising set handle. - * @param connection Connection handle. - * @param completed_events Number of events created during before advertising end. - * @param connected True if connection has been established. - */ - AdvertisingEndEvent_t( - AdvHandle_t advHandle, - Handle_t connection, - uint8_t completed_events, - bool connected - ) : - advHandle(advHandle), - connection(connection), - completed_events(completed_events), - connected(connected) { } - - /** Get advertising handle. */ - AdvHandle_t getAdvHandle() const { - return advHandle; - } - - /** Get connection handle (valid only if connected successfully). */ - Handle_t getConnection() const { - return connection; - } - - /** Get how many events advertising created. */ - uint8_t getCompleted_events() const { - return completed_events; - } - - /** Has the advertising ended with a connection. */ - bool isConnected() const { - return connected; - } - - private: - AdvHandle_t advHandle; - Handle_t connection; - uint8_t completed_events; - bool connected; - }; - /** Called when advertising ends. * * @param event Advertising end event: @see AdvertisingEndEvent_t for details. @@ -1670,45 +1224,6 @@ public: (void) event; } - /** Event produced when a peer requests a scan response from the advertiser. - */ - struct ScanRequestEvent_t { - /** Create scan request event. - * - * @param advHandle Advertising handle. - * @param peerAddressType Peer address type. - * @param peerAddress Peer address. - */ - ScanRequestEvent_t( - AdvHandle_t advHandle, - const PeerAddressType_t &peerAddressType, - const ble::address_t &peerAddress - ) : - advHandle(advHandle), - peerAddressType(peerAddressType), - peerAddress(peerAddress) { } - - /** Get advertising handle. */ - AdvHandle_t getAdvHandle() const { - return advHandle; - } - - /** Get peer address type. */ - const PeerAddressType_t &getPeerAddressType() const { - return peerAddressType; - } - - /** Get peer address. */ - const ble::address_t &getPeerAddress() const { - return peerAddress; - } - - private: - AdvHandle_t advHandle; - PeerAddressType_t peerAddressType; - const ble::address_t &peerAddress; - }; - /** Called when a scanning device request a scan response. * * @param event Scan request event: @see ScanRequestEvent_t for details. diff --git a/features/FEATURE_BLE/ble/gap/Events.h b/features/FEATURE_BLE/ble/gap/Events.h new file mode 100644 index 0000000000..9a7de3e445 --- /dev/null +++ b/features/FEATURE_BLE/ble/gap/Events.h @@ -0,0 +1,509 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018-2018 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +#ifndef BLE_GAP_EVENTS_H +#define BLE_GAP_EVENTS_H + +#include "ble/blecommon.h" +#include "ble/BLETypes.h" +#include "platform/Span.h" + +/** Event generated when an advertising packet is seen during passive scanning + * or a scan response is received during active scanning. + */ +struct AdvertisingReportEvent { + /** Create a advertising report event. + * + * @param type Type of advertising used. + * @param peerAddressType Peer address type of advertiser. + * @param peerAddress Peer address of advertiser. + * @param primaryPhy PHY used on the primary channels. + * @param secondaryPhy PHY used on secondary channels. + * @param SID Set identification number. + * @param txPower Transmission power reported by the packet. + * @param rssi Measured signal strength. + * @param periodicInterval Interval of periodic advertising. + * @param directAddressType Directed advertising target address type. + * @param directAddress Directed advertising target address. + * @param advertisingData Advertising payload. + */ + AdvertisingReportEvent( + const ble::advertising_event_t &type, + const ble::peer_address_type_t &peerAddressType, + const ble::address_t &peerAddress, + const ble::phy_t &primaryPhy, + const ble::phy_t &secondaryPhy, + ble::advertising_sid_t SID, + ble::advertising_power_t txPower, + ble::rssi_t rssi, + ble::unit_periodic_interval_t periodicInterval, + const ble::peer_address_type_t &directAddressType, + const ble::address_t &directAddress, + const mbed::Span &advertisingData + ) : + type(type), + peerAddressType(peerAddressType), + peerAddress(peerAddress), + primaryPhy(primaryPhy), + secondaryPhy(secondaryPhy), + SID(SID), + txPower(txPower), + rssi(rssi), + periodicInterval(periodicInterval), + directAddressType(directAddressType), + directAddress(directAddress), + advertisingData(advertisingData) { } + + /** Get event type. */ + const ble::advertising_event_t &getType() const { + return type; + } + + /** Get peer address type. */ + const ble::peer_address_type_t &getPeerAddressType() const { + return peerAddressType; + } + + /** Get peer address. */ + const ble::address_t &getPeerAddress() const { + return peerAddress; + } + + /** Get primary PHY. */ + const ble::phy_t &getPrimaryPhy() const { + return primaryPhy; + } + + /** Get secondary PHY. */ + const ble::phy_t &getSecondaryPhy() const { + return secondaryPhy; + } + + /** Get advertising set identifier. */ + ble::advertising_sid_t getSID() const { + return SID; + } + + /** Get TX power. */ + ble::advertising_power_t getTxPower() const { + return txPower; + } + + /** Get received signal strength. */ + ble::rssi_t getRssi() const { + return rssi; + } + + /** Get interval. */ + ble::unit_periodic_interval_t getPeriodicInterval() const { + return periodicInterval; + } + + /** Get target address type in directed advertising. */ + const ble::peer_address_type_t &getDirectAddressType() const { + return directAddressType; + } + + /** Get target address in directed advertising. */ + const ble::address_t &getDirectAddress() const { + return directAddress; + } + + /** Get payload. */ + const mbed::Span &getAdvertisingData() const { + return advertisingData; + } + +private: + ble::advertising_event_t type; + ble::peer_address_type_t peerAddressType; + ble::address_t const &peerAddress; + ble::phy_t primaryPhy; + ble::phy_t secondaryPhy; + ble::advertising_sid_t SID; + ble::advertising_power_t txPower; + ble::rssi_t rssi; + ble::unit_periodic_interval_t periodicInterval; + ble::peer_address_type_t directAddressType; + const ble::address_t &directAddress; + mbed::Span advertisingData; +}; + +/** Event generated when a connection initiation end (successfully or not). + */ +struct ConnectionCompleteEvent { + /** Create a connection complete event. + * + * @param success BLE_ERROR_NONE if connection succeeded. + * @param connectionHandle Connection handle if successful. + * @param ownRole Role of the local device. + * @param peerAddressType Peer address type. + * @param peerAddress Peer address. + * @param localResolvablePrivateAddress Local address type if privacy enabled. + * @param peerResolvablePrivateAddress Peer address type if privacy enabled. + * @param connectionInterval Connection interval. + * @param connectionLatency Connection latency in events. + * @param supervisionTimeout Supervision timeout. + * @param masterClockAccuracy Peer clock accuracy in parts per million. + */ + ConnectionCompleteEvent( + ble_error_t status, + ble::connection_handle_t connectionHandle, + ble::connection_role_t ownRole, + const ble::peer_address_type_t &peerAddressType, + const ble::address_t &peerAddress, + const ble::address_t &localResolvablePrivateAddress, + const ble::address_t &peerResolvablePrivateAddress, + ble::unit_conn_interval_t connectionInterval, + ble::unit_slave_latency_t connectionLatency, + ble::unit_supervision_timeout_t supervisionTimeout, + uint16_t masterClockAccuracy + ) : + status(status), + connectionHandle(connectionHandle), + ownRole(ownRole), + peerAddressType(peerAddressType), + peerAddress(peerAddress), + localResolvablePrivateAddress(localResolvablePrivateAddress), + peerResolvablePrivateAddress(peerResolvablePrivateAddress), + connectionInterval(connectionInterval), + connectionLatency(connectionLatency), + supervisionTimeout(supervisionTimeout), + masterClockAccuracy(masterClockAccuracy) { } + + /** Get connection complete event status. */ + ble_error_t getStatus() const { + return status; + } + + /** Get connection handle (valid only when successful). */ + ble::connection_handle_t getConnectionHandle() const { + return connectionHandle; + } + + /** Get own role. */ + ble::connection_role_t getOwnRole() const { + return ownRole; + } + + /** Get peer address type. */ + const ble::peer_address_type_t &getPeerAddressType() const { + return peerAddressType; + } + + /** Get peer address. */ + const ble::address_t &getPeerAddress() const { + return peerAddress; + } + + /** Get get local resolvable random address if privacy is used. */ + const ble::address_t &getLocalResolvablePrivateAddress() const { + return localResolvablePrivateAddress; + } + + /** Get peer resolvable private address if privacy is used. */ + const ble::address_t &getPeerResolvablePrivateAddress() const { + return peerResolvablePrivateAddress; + } + + /** Get connection interval. */ + ble::unit_conn_interval_t getConnectionInterval() const { + return connectionInterval; + } + + /** Get connection latency. */ + ble::unit_slave_latency_t getConnectionLatency() const { + return connectionLatency; + } + + /** Get supervision timeout. */ + ble::unit_supervision_timeout_t getSupervisionTimeout() const { + return supervisionTimeout; + } + + /** Get clock accuracy in parts per million. */ + uint16_t getMasterClockAccuracy() const { + return masterClockAccuracy; + } + +private: + ble_error_t status; + ble::connection_handle_t connectionHandle; + ble::connection_role_t ownRole; + ble::peer_address_type_t peerAddressType; + const ble::address_t &peerAddress; + const ble::address_t &localResolvablePrivateAddress; + const ble::address_t &peerResolvablePrivateAddress; + ble::unit_conn_interval_t connectionInterval; + ble::unit_slave_latency_t connectionLatency; + ble::unit_supervision_timeout_t supervisionTimeout; + uint16_t masterClockAccuracy; +}; + +/** Event generated when we first receive a periodic advertisement. + */ +struct PeriodicAdvertisingSyncEstablishedEvent { + /** Create advertising sync event. + * + * @param success BLE_ERROR_NONE if synchronisation was achieved. + * @param syncHandle Advertising sync handle. + * @param sid Advertising set identifier. + * @param peerAddressType Peer address type. + * @param peerAddress Peer address. + * @param peerPhy PHY used for advertisements. + * @param advertisingInterval Periodic advertising interval. + * @param masterClockAccuracy Peer clock accuracy in parts per million. + */ + PeriodicAdvertisingSyncEstablishedEvent( + ble_error_t status, + ble::periodic_sync_handle_t syncHandle, + ble::advertising_sid_t sid, + const ble::peer_address_type_t &peerAddressType, + const ble::address_t &peerAddress, + const ble::phy_t &peerPhy, + uint16_t advertisingInterval, + const ble::clock_accuracy_t &peerClockAccuracy + ) : + status(status), + syncHandle(syncHandle), + sid(sid), + peerAddressType(peerAddressType), + peerAddress(peerAddress), + peerPhy(peerPhy), + advertisingInterval(advertisingInterval), + peerClockAccuracy(peerClockAccuracy) { } + + /** Get sync establishment status. */ + ble_error_t getStatus() const { + return status; + } + + /** Get periodic advertising sync handle. */ + ble::periodic_sync_handle_t getSyncHandle() const { + return syncHandle; + } + + /** Get advertising set identifier. */ + ble::advertising_sid_t getSid() const { + return sid; + } + + /** Get peer address type. */ + const ble::peer_address_type_t &getPeerAddressType() const { + return peerAddressType; + } + + /** Get peer address. */ + const ble::address_t &getPeerAddress() const { + return peerAddress; + } + + /** Get PHY used. */ + const ble::phy_t &getPeerPhy() const { + return peerPhy; + } + + /** Get interval. */ + uint16_t getAdvertisingInterval() const { + return advertisingInterval; + } + + /** Get clock accuracy in parts per million. */ + const ble::clock_accuracy_t &getPeerClockAccuracy() const { + return peerClockAccuracy; + } + +private: + ble_error_t status; + ble::periodic_sync_handle_t syncHandle; + ble::advertising_sid_t sid; + ble::peer_address_type_t peerAddressType; + const ble::address_t& peerAddress; + ble::phy_t peerPhy; + uint16_t advertisingInterval; + ble::clock_accuracy_t peerClockAccuracy; +}; + +/** Event generated when periodic advertising packet is received. + */ +struct PeriodicAdvertisingReportEvent { + /** Create periodic advertising report event. + * + * @param syncHandle Periodic advertising sync handle + * @param txPower TX power. + * @param rssi Received signal strength. + * @param dataStatus Status to indicate the completeness of the payload. + * @param payload Periodic advertisement payload. + */ + PeriodicAdvertisingReportEvent( + ble::periodic_sync_handle_t syncHandle, + ble::advertising_power_t txPower, + ble::rssi_t rssi, + ble::advertising_data_status_t dataStatus, + const mbed::Span &payload + ) : + syncHandle(syncHandle), + txPower(txPower), + rssi(rssi), + dataStatus(dataStatus), + payload(payload) { } + + /** Get periodic advertising sync handle. */ + ble::periodic_sync_handle_t getSyncHandle() const { + return syncHandle; + } + + /** Get TX power as reported by the advertising packet. */ + ble::advertising_power_t getTxPower() const { + return txPower; + } + + /** Get received signal strength. */ + ble::rssi_t getRssi() const { + return rssi; + } + + /** Get data completeness status. */ + const ble::advertising_data_status_t &getDataStatus() const { + return dataStatus; + } + + /** Get payload. */ + const mbed::Span &getPayload() const { + return payload; + } + +private: + ble::periodic_sync_handle_t syncHandle; + ble::advertising_power_t txPower; + ble::rssi_t rssi; + ble::advertising_data_status_t dataStatus; + mbed::Span payload; +}; + +/** Event generated when periodic advertising sync is lost. + */ +struct PeriodicAdvertisingSyncLoss { + /** Create periodic advertising sync loss event. + * + * @param syncHandle Periodic advertising sync handle. + */ + PeriodicAdvertisingSyncLoss( + ble::periodic_sync_handle_t syncHandle + ) : + syncHandle(syncHandle) { } + + /** Get periodic sync handle. */ + ble::periodic_sync_handle_t getSyncHandle() const { + return syncHandle; + } + +private: + ble::periodic_sync_handle_t syncHandle; +}; + +/** Event generated when scan times out. + */ +struct ScanTimeoutEvent { }; + +/** Event produced when advertising ends. + */ +struct AdvertisingEndEvent_t { + /** Create advertising end event. + * + * @param advHandle Advertising set handle. + * @param connection Connection handle. + * @param completed_events Number of events created during before advertising end. + * @param connected True if connection has been established. + */ + AdvertisingEndEvent_t( + ble::advertising_handle_t advHandle, + ble::connection_handle_t connection, + uint8_t completed_events, + bool connected + ) : + advHandle(advHandle), + connection(connection), + completed_events(completed_events), + connected(connected) { } + + /** Get advertising handle. */ + ble::advertising_handle_t getAdvHandle() const { + return advHandle; + } + + /** Get connection handle (valid only if connected successfully). */ + ble::connection_handle_t getConnection() const { + return connection; + } + + /** Get how many events advertising created. */ + uint8_t getCompleted_events() const { + return completed_events; + } + + /** Has the advertising ended with a connection. */ + bool isConnected() const { + return connected; + } + +private: + ble::advertising_handle_t advHandle; + ble::connection_handle_t connection; + uint8_t completed_events; + bool connected; +}; + +/** Event produced when a peer requests a scan response from the advertiser. + */ +struct ScanRequestEvent_t { + /** Create scan request event. + * + * @param advHandle Advertising handle. + * @param peerAddressType Peer address type. + * @param peerAddress Peer address. + */ + ScanRequestEvent_t( + ble::advertising_handle_t advHandle, + const ble::peer_address_type_t &peerAddressType, + const ble::address_t &peerAddress + ) : + advHandle(advHandle), + peerAddressType(peerAddressType), + peerAddress(peerAddress) { } + + /** Get advertising handle. */ + ble::advertising_handle_t getAdvHandle() const { + return advHandle; + } + + /** Get peer address type. */ + const ble::peer_address_type_t &getPeerAddressType() const { + return peerAddressType; + } + + /** Get peer address. */ + const ble::address_t &getPeerAddress() const { + return peerAddress; + } + +private: + ble::advertising_handle_t advHandle; + ble::peer_address_type_t peerAddressType; + const ble::address_t &peerAddress; +}; + +#endif //BLE_GAP_EVENTS_H diff --git a/features/FEATURE_BLE/source/generic/GenericGap.cpp b/features/FEATURE_BLE/source/generic/GenericGap.cpp index e313782923..2cf940633e 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.cpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.cpp @@ -1227,7 +1227,7 @@ void GenericGap::on_scan_timeout() return; } - _eventHandler->onScanTimeout(::Gap::EventHandler::ScanTimeoutEvent()); + _eventHandler->onScanTimeout(ScanTimeoutEvent()); } void GenericGap::process_scan_timeout() @@ -2190,10 +2190,10 @@ void GenericGap::on_enhanced_connection_complete( } _eventHandler->onConnectionComplete( - Gap::EventHandler::ConnectionCompleteEvent( + ConnectionCompleteEvent( (status == pal::hci_error_code_t::SUCCESS) ? BLE_ERROR_NONE : BLE_ERROR_INTERNAL_STACK_FAILURE, (ble::connection_handle_t)connection_handle, - (Gap::Role_t)own_role.value(), + own_role, (ble::peer_address_type_t::type)peer_address_type.value(), peer_address, local_resolvable_private_address, @@ -2227,7 +2227,7 @@ void GenericGap::on_extended_advertising_report( } _eventHandler->onAdvertisingReport( - ::Gap::EventHandler::AdvertisingReportEvent( + AdvertisingReportEvent( event_type, (PeerAddressType_t::type)(address_type ? address_type->value() : PeerAddressType_t::ANONYMOUS), (BLEProtocol::AddressBytes_t&)address, @@ -2260,7 +2260,7 @@ void GenericGap::on_periodic_advertising_sync_established( } _eventHandler->onPeriodicAdvertisingSyncEstablished( - ::Gap::EventHandler::PeriodicAdvertisingSyncEstablishedEvent( + PeriodicAdvertisingSyncEstablishedEvent( (error == pal::hci_error_code_t::SUCCESS) ? BLE_ERROR_NONE : BLE_ERROR_INTERNAL_STACK_FAILURE, sync_handle, advertising_sid, @@ -2288,7 +2288,7 @@ void GenericGap::on_periodic_advertising_report( } _eventHandler->onPeriodicAdvertisingReportEvent( - ::Gap::EventHandler::PeriodicAdvertisingReportEvent( + PeriodicAdvertisingReportEvent( sync_handle, tx_power, rssi, @@ -2305,7 +2305,7 @@ void GenericGap::on_periodic_advertising_sync_loss(pal::sync_handle_t sync_handl } _eventHandler->onPeriodicAdvertisingSyncLoss( - ::Gap::EventHandler::PeriodicAdvertisingSyncLoss(sync_handle) + PeriodicAdvertisingSyncLoss(sync_handle) ); } @@ -2323,7 +2323,7 @@ void GenericGap::on_advertising_set_terminated( } _eventHandler->onAdvertisingEnd( - ::Gap::EventHandler::AdvertisingEndEvent_t( + AdvertisingEndEvent_t( advertising_handle, connection_handle, number_of_completed_extended_advertising_events, @@ -2343,7 +2343,7 @@ void GenericGap::on_scan_request_received( } _eventHandler->onScanRequest( - ::Gap::EventHandler::ScanRequestEvent_t( + ScanRequestEvent_t( advertising_handle, (ble::peer_address_type_t::type) scanner_address_type.value(), address