diff options
| author | 2024-08-20 13:32:09 -0700 | |
|---|---|---|
| committer | 2024-08-23 06:05:14 +0000 | |
| commit | 1ef1ee91ff435b3c3cfa5879cf44a73e7e77f866 (patch) | |
| tree | e3fe2adc1d1a378d7bac0820ab309c122267733d | |
| parent | 2098b15ec2b642b69f93b59298f765c72e7c7a09 (diff) | |
stack::l2cap Introduce l2cap interface and mocks
Bug: 343808590
Test: m .
Flag: EXEMPT, Mechanical Refactor
Change-Id: I95d8bef15b2a709589dbd3dad7eaf7fba25b038e
| -rw-r--r-- | system/stack/include/l2c_api.h | 253 | ||||
| -rw-r--r-- | system/stack/include/l2cap_interface.h | 925 | ||||
| -rw-r--r-- | system/stack/include/l2cap_types.h | 171 | ||||
| -rw-r--r-- | system/stack/l2cap/l2cap_api.cc | 257 | ||||
| -rw-r--r-- | system/test/Android.bp | 7 | ||||
| -rw-r--r-- | system/test/mock/mock_stack_l2cap_interface.cc | 33 | ||||
| -rw-r--r-- | system/test/mock/mock_stack_l2cap_interface.h | 135 |
7 files changed, 1638 insertions, 143 deletions
diff --git a/system/stack/include/l2c_api.h b/system/stack/include/l2c_api.h index 97af8ac5f0..cdfd68a7ba 100644 --- a/system/stack/include/l2c_api.h +++ b/system/stack/include/l2c_api.h @@ -21,8 +21,7 @@ * this file contains the L2CAP API definitions * ******************************************************************************/ -#ifndef L2C_API_H -#define L2C_API_H +#pragma once #include <bluetooth/log.h> #include <stdbool.h> @@ -30,9 +29,8 @@ #include <cstdint> #include <vector> -#include "hcidefs.h" #include "stack/include/bt_hdr.h" -#include "stack/include/l2cdefs.h" +#include "stack/include/l2cap_interface.h" #include "types/bt_transport.h" #include "types/hci_role.h" #include "types/raw_address.h" @@ -51,25 +49,6 @@ #define L2CAP_FCS_LENGTH 2 -/* result code for L2CA_DataWrite() */ -enum class tL2CAP_DW_RESULT : uint8_t { - FAILED = 0, - SUCCESS = 1, - CONGESTED = 2, -}; - -/* Values for priority parameter to L2CA_SetAclPriority */ -enum tL2CAP_PRIORITY : uint8_t { - L2CAP_PRIORITY_NORMAL = 0, - L2CAP_PRIORITY_HIGH = 1, -}; - -/* Values for priority parameter to L2CA_SetAclLatency */ -enum tL2CAP_LATENCY : uint8_t { - L2CAP_LATENCY_NORMAL = 0, - L2CAP_LATENCY_LOW = 1, -}; - /* Values for priority parameter to L2CA_SetTxPriority */ #define L2CAP_CHNL_PRIORITY_HIGH 0 #define L2CAP_CHNL_PRIORITY_LOW 2 @@ -113,40 +92,6 @@ typedef uint8_t tL2CAP_CHNL_DATA_RATE; * Type Definitions ****************************************************************************/ -struct tL2CAP_FCR_OPTS { -#define L2CAP_FCR_BASIC_MODE 0x00 -#define L2CAP_FCR_ERTM_MODE 0x03 -#define L2CAP_FCR_LE_COC_MODE 0x05 - - uint8_t mode; - - uint8_t tx_win_sz; - uint8_t max_transmit; - uint16_t rtrans_tout; - uint16_t mon_tout; - uint16_t mps; -}; - -/* default options for ERTM mode */ -constexpr tL2CAP_FCR_OPTS kDefaultErtmOptions = { - L2CAP_FCR_ERTM_MODE, - 10, /* Tx window size */ - 20, /* Maximum transmissions before disconnecting */ - 2000, /* Retransmission timeout (2 secs) */ - 12000, /* Monitor timeout (12 secs) */ - 1010 /* MPS segment size */ -}; - -struct FLOW_SPEC { - uint8_t qos_flags; /* TBD */ - uint8_t service_type; /* see below */ - uint32_t token_rate; /* bytes/second */ - uint32_t token_bucket_size; /* bytes */ - uint32_t peak_bandwidth; /* bytes/second */ - uint32_t latency; /* microseconds */ - uint32_t delay_variation; /* microseconds */ -}; - /* Values for service_type */ #define SVC_TYPE_BEST_EFFORT 1 #define SVC_TYPE_GUARANTEED 2 @@ -155,28 +100,6 @@ struct FLOW_SPEC { * parameters are optional, for each parameter there is a boolean to * use to signify its presence or absence. */ -struct tL2CAP_CFG_INFO { - uint16_t result; /* Only used in confirm messages */ - bool mtu_present; - uint16_t mtu; - bool qos_present; - FLOW_SPEC qos; - bool flush_to_present; - uint16_t flush_to; - bool fcr_present; - tL2CAP_FCR_OPTS fcr; - bool fcs_present; /* Optionally bypasses FCS checks */ - uint8_t fcs; /* '0' if desire is to bypass FCS, otherwise '1' */ - bool ext_flow_spec_present; - tHCI_EXT_FLOW_SPEC ext_flow_spec; - uint16_t flags; /* bit 0: 0-no continuation, 1-continuation */ -}; - -/* LE credit based L2CAP connection parameters */ -constexpr uint16_t L2CAP_LE_MIN_MTU = 23; // Minimum SDU size -constexpr uint16_t L2CAP_LE_MIN_MPS = 23; -constexpr uint16_t L2CAP_LE_MAX_MPS = 65533; -constexpr uint16_t L2CAP_LE_CREDIT_MAX = 65535; constexpr uint16_t L2CAP_LE_CREDIT_THRESHOLD = 64; // This is initial amout of credits we send, and amount to which we increase @@ -187,23 +110,6 @@ uint16_t L2CA_LeCreditDefault(); // reach default value. uint16_t L2CA_LeCreditThreshold(); -// Max number of CIDs in the L2CAP CREDIT BASED CONNECTION REQUEST -constexpr uint8_t L2CAP_CREDIT_BASED_MAX_CIDS = 5; - -/* Define a structure to hold the configuration parameter for LE L2CAP - * connection oriented channels. - */ -constexpr uint16_t kDefaultL2capMtu = 100; -constexpr uint16_t kDefaultL2capMps = 100; - -struct tL2CAP_LE_CFG_INFO { - uint16_t result{L2CAP_LE_RESULT_CONN_OK}; /* Only used in confirm messages */ - uint16_t mtu{kDefaultL2capMtu}; - uint16_t mps{kDefaultL2capMps}; - uint16_t credits{L2CA_LeCreditDefault()}; - uint8_t number_of_channels{L2CAP_CREDIT_BASED_MAX_CIDS}; -}; - /********************************* * Callback Functions Prototypes *********************************/ @@ -321,35 +227,6 @@ typedef void(tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB)(const RawAddress& bdaddr, bool is_local_cfg, tL2CAP_LE_CFG_INFO* p_cfg); -/* Define the structure that applications use to register with - * L2CAP. This structure includes callback functions. All functions - * MUST be provided, with the exception of the "connect pending" - * callback and "congestion status" callback. - */ -struct tL2CAP_APPL_INFO { - tL2CA_CONNECT_IND_CB* pL2CA_ConnectInd_Cb; - tL2CA_CONNECT_CFM_CB* pL2CA_ConnectCfm_Cb; - tL2CA_CONFIG_IND_CB* pL2CA_ConfigInd_Cb; - tL2CA_CONFIG_CFM_CB* pL2CA_ConfigCfm_Cb; - tL2CA_DISCONNECT_IND_CB* pL2CA_DisconnectInd_Cb; - tL2CA_DISCONNECT_CFM_CB* pL2CA_DisconnectCfm_Cb; - tL2CA_DATA_IND_CB* pL2CA_DataInd_Cb; - tL2CA_CONGESTION_STATUS_CB* pL2CA_CongestionStatus_Cb; - tL2CA_TX_COMPLETE_CB* pL2CA_TxComplete_Cb; - tL2CA_ERROR_CB* pL2CA_Error_Cb; - tL2CA_CREDIT_BASED_CONNECT_IND_CB* pL2CA_CreditBasedConnectInd_Cb; - tL2CA_CREDIT_BASED_CONNECT_CFM_CB* pL2CA_CreditBasedConnectCfm_Cb; - tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB* pL2CA_CreditBasedReconfigCompleted_Cb; - tL2CA_CREDIT_BASED_COLLISION_IND_CB* pL2CA_CreditBasedCollisionInd_Cb; -}; - -/* Define the structure that applications use to create or accept - * connections with enhanced retransmission mode. - */ -struct tL2CAP_ERTM_INFO { - uint8_t preferred_mode; -}; - /***************************************************************************** * External Function Declarations ****************************************************************************/ @@ -730,17 +607,6 @@ typedef void(tL2CA_FIXED_DATA_CB)(uint16_t, const RawAddress&, BT_HDR*); */ typedef void(tL2CA_FIXED_CONGESTION_STATUS_CB)(const RawAddress&, bool); -/* Fixed channel registration info (the callback addresses and channel config) - */ -struct tL2CAP_FIXED_CHNL_REG { - tL2CA_FIXED_CHNL_CB* pL2CA_FixedConn_Cb; - tL2CA_FIXED_DATA_CB* pL2CA_FixedData_Cb; - tL2CA_FIXED_CONGESTION_STATUS_CB* pL2CA_FixedCong_Cb; - - uint16_t default_idle_tout; - tL2CA_TX_COMPLETE_CB* pL2CA_FixedTxComplete_Cb; /* fixed channel tx complete callback */ -}; - /******************************************************************************* * * Function L2CA_RegisterFixedChannel @@ -921,11 +787,112 @@ void L2CA_SetMediaStreamChannel(uint16_t local_media_cid, bool status); *******************************************************************************/ [[nodiscard]] bool L2CA_isMediaChannel(uint16_t handle, uint16_t channel_id, bool is_local_cid); -namespace fmt { -template <> -struct formatter<tL2CAP_LATENCY> : enum_formatter<tL2CAP_LATENCY> {}; -template <> -struct formatter<tL2CAP_PRIORITY> : enum_formatter<tL2CAP_PRIORITY> {}; -} // namespace fmt +namespace bluetooth { +namespace stack { +namespace l2cap { + +class Impl : public Interface { +public: + virtual ~Impl() = default; + + // Lifecycle methods to register BR/EDR l2cap services + [[nodiscard]] uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, + bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info, + uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level) override; + [[nodiscard]] uint16_t L2CA_RegisterWithSecurity(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, + bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info, + uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level) override; + void L2CA_Deregister(uint16_t psm) override; + + // Lifecycle methods to register BLE l2cap services + [[nodiscard]] uint16_t L2CA_AllocateLePSM(void) override; + void L2CA_FreeLePSM(uint16_t psm) override; + + [[nodiscard]] uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, + uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg) override; + void L2CA_DeregisterLECoc(uint16_t psm) override; + + // Methods used for both BR/EDR and BLE + [[nodiscard]] bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, + tBT_TRANSPORT transport) override; + [[nodiscard]] bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout, + tBT_TRANSPORT transport) override; + [[nodiscard]] bool L2CA_GetRemoteChannelId(uint16_t lcid, uint16_t* rcid) override; + + // Connection methods to configure and connect to peer over BR/EDR ACL + [[nodiscard]] uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& bd_addr) override; + [[nodiscard]] uint16_t L2CA_ConnectReqWithSecurity(uint16_t psm, const RawAddress& bd_addr, + uint16_t sec_level) override; + [[nodiscard]] bool L2CA_SetAclLatency(const RawAddress& bd_addr, tL2CAP_LATENCY latency) override; + [[nodiscard]] bool L2CA_UseLatencyMode(const RawAddress& bd_addr, bool use_latency_mode) override; + [[nodiscard]] bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat, + uint8_t* p_chnl_mask) override; + [[nodiscard]] bool L2CA_SetAclPriority(const RawAddress& bd_addr, + tL2CAP_PRIORITY priority) override; + void L2CA_SetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, + uint16_t cont_num, uint16_t timeout) override; + void L2CA_AdjustConnectionIntervals(uint16_t* min_interval, uint16_t* max_interval, + uint16_t floor_interval) override; + void L2CA_SetEcosystemBaseInterval(uint32_t base_interval) override; + + [[nodiscard]] bool L2CA_SubrateRequest(const RawAddress& bd_addr, uint16_t subrate_min, + uint16_t subrate_max, uint16_t max_latency, + uint16_t cont_num, uint16_t timeout) override; + [[nodiscard]] uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush) override; + [[nodiscard]] bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority) override; + [[nodiscard]] bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable) override; + + // Connection methods to configure and connect to peer over BLE ACL + [[nodiscard]] uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& bd_addr, + tL2CAP_LE_CFG_INFO* p_cfg, + uint16_t sec_level) override; + [[nodiscard]] std::vector<uint16_t> L2CA_ConnectCreditBasedReq( + uint16_t psm, const RawAddress& bd_addr, tL2CAP_LE_CFG_INFO* p_cfg) override; + [[nodiscard]] bool L2CA_ConnectCreditBasedRsp(const RawAddress& bd_addr, uint8_t id, + std::vector<uint16_t>& accepted_lcids, + uint16_t result, + tL2CAP_LE_CFG_INFO* p_cfg) override; + [[nodiscard]] uint16_t L2CA_GetPeerLECocCredit(const RawAddress& bd_addr, uint16_t lcid) override; + [[nodiscard]] bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr, + std::vector<uint16_t>& lcids, + tL2CAP_LE_CFG_INFO* p_cfg) override; + [[nodiscard]] bool L2CA_UpdateBleConnParams(const RawAddress& bd_addr, uint16_t min_int, + uint16_t max_int, uint16_t latency, uint16_t timeout, + uint16_t min_ce_len, uint16_t max_ce_len) override; + void L2CA_LockBleConnParamsForServiceDiscovery(const RawAddress& bd_addr, bool lock) override; + void L2CA_LockBleConnParamsForProfileConnection(const RawAddress& bd_addr, bool lock) override; + [[nodiscard]] tHCI_ROLE L2CA_GetBleConnRole(const RawAddress& bd_addr) override; + [[nodiscard]] bool L2CA_SetLeGattTimeout(const RawAddress& bd_addr, uint16_t idle_tout) override; + [[nodiscard]] bool L2CA_MarkLeLinkAsActive(const RawAddress& bd_addr) override; + [[nodiscard]] bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg) override; + // Method to consolidate two BLE addresses into a single device + void L2CA_Consolidate(const RawAddress& identity_addr, const RawAddress& rpa) override; + + // Disconnect methods an active connection for both BR/EDR and BLE + [[nodiscard]] bool L2CA_DisconnectReq(uint16_t cid) override; + [[nodiscard]] bool L2CA_DisconnectLECocReq(uint16_t cid) override; + + // Data write methods for both BR/EDR and BLE + [[nodiscard]] tL2CAP_DW_RESULT L2CA_DataWrite(uint16_t cid, BT_HDR* p_data) override; + [[nodiscard]] tL2CAP_DW_RESULT L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data) override; + + // Fixed channel methods + [[nodiscard]] bool L2CA_RegisterFixedChannel(uint16_t fixed_cid, + tL2CAP_FIXED_CHNL_REG* p_freg) override; + [[nodiscard]] bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr) override; + [[nodiscard]] tL2CAP_DW_RESULT L2CA_SendFixedChnlData(uint16_t fixed_cid, + const RawAddress& bd_addr, + BT_HDR* p_buf) override; + [[nodiscard]] bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr) override; + + // Media methods + void L2CA_SetMediaStreamChannel(uint16_t local_media_cid, bool status) override; + [[nodiscard]] bool L2CA_isMediaChannel(uint16_t handle, uint16_t channel_id, + bool is_local_cid) override; +}; -#endif /* L2C_API_H */ +} // namespace l2cap +} // namespace stack +} // namespace bluetooth diff --git a/system/stack/include/l2cap_interface.h b/system/stack/include/l2cap_interface.h new file mode 100644 index 0000000000..7f0cf88392 --- /dev/null +++ b/system/stack/include/l2cap_interface.h @@ -0,0 +1,925 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ + +#pragma once + +#include <bluetooth/log.h> +#include <stdbool.h> + +#include <cstdint> +#include <vector> + +#include "stack/include/bt_hdr.h" +#include "stack/include/l2cap_types.h" +#include "types/bt_transport.h" +#include "types/hci_role.h" +#include "types/raw_address.h" + +/* result code for L2CA_DataWrite() */ +enum class tL2CAP_DW_RESULT : uint8_t { + FAILED = 0, + SUCCESS = 1, + CONGESTED = 2, +}; + +/********************************* + * Callback Functions Prototypes + *********************************/ + +/* Connection indication callback prototype. Parameters are + * BD Address of remote + * Local CID assigned to the connection + * PSM that the remote wants to connect to + * Identifier that the remote sent + */ +typedef void(tL2CA_CONNECT_IND_CB)(const RawAddress&, uint16_t, uint16_t, uint8_t); + +/* Connection confirmation callback prototype. Parameters are + * Local CID + * Result - 0 = connected + * If there is an error, tL2CA_ERROR_CB is invoked + */ +typedef void(tL2CA_CONNECT_CFM_CB)(uint16_t, uint16_t); + +/* Configuration indication callback prototype. Parameters are + * Local CID assigned to the connection + * Pointer to configuration info + */ +typedef void(tL2CA_CONFIG_IND_CB)(uint16_t, tL2CAP_CFG_INFO*); + +/* Configuration confirm callback prototype. Parameters are + * Local CID assigned to the connection + * Initiator (1 for local, 0 for remote) + * Initial config from remote + * If there is an error, tL2CA_ERROR_CB is invoked + */ +typedef void(tL2CA_CONFIG_CFM_CB)(uint16_t, uint16_t, tL2CAP_CFG_INFO*); + +/* Disconnect indication callback prototype. Parameters are + * Local CID + * Boolean whether upper layer should ack this + */ +typedef void(tL2CA_DISCONNECT_IND_CB)(uint16_t, bool); + +/* Disconnect confirm callback prototype. Parameters are + * Local CID + * Result + */ +typedef void(tL2CA_DISCONNECT_CFM_CB)(uint16_t, uint16_t); + +/* Disconnect confirm callback prototype. Parameters are + * Local CID + * Result + */ +typedef void(tL2CA_DATA_IND_CB)(uint16_t, BT_HDR*); + +/* Congestion status callback protype. This callback is optional. If + * an application tries to send data when the transmit queue is full, + * the data will anyways be dropped. The parameter is: + * Local CID + * true if congested, false if uncongested + */ +typedef void(tL2CA_CONGESTION_STATUS_CB)(uint16_t, bool); + +/* Transmit complete callback protype. This callback is optional. If + * set, L2CAP will call it when packets are sent or flushed. If the + * count is 0xFFFF, it means all packets are sent for that CID (eRTM + * mode only). The parameters are: + * Local CID + * Number of SDUs sent or dropped + */ +typedef void(tL2CA_TX_COMPLETE_CB)(uint16_t, uint16_t); + +/* + * Notify the user when the remote send error result on ConnectRsp or ConfigRsp + * The parameters are: + * Local CID + * Error type (L2CAP_CONN_OTHER_ERROR for ConnectRsp, + * L2CAP_CFG_FAILED_NO_REASON for ConfigRsp) + */ +typedef void(tL2CA_ERROR_CB)(uint16_t, uint16_t); + +/* Create credit based connection request callback prototype. Parameters are + * BD Address of remote + * Vector of allocated local cids to accept + * PSM + * Peer MTU + * Identifier that the remote sent + */ +typedef void(tL2CA_CREDIT_BASED_CONNECT_IND_CB)(const RawAddress& bdaddr, + std::vector<uint16_t>& lcids, uint16_t psm, + uint16_t peer_mtu, uint8_t identifier); + +/* Collision Indication callback prototype. Used to notify upper layer that + * remote devices sent Credit Based Connection Request but it was rejected due + * to ongoing local request. Upper layer might want to sent another request when + * local request is completed. Parameters are: + * BD Address of remote + */ +typedef void(tL2CA_CREDIT_BASED_COLLISION_IND_CB)(const RawAddress& bdaddr); + +/* Credit based connection confirmation callback prototype. Parameters are + * BD Address of remote + * Connected Local CIDs + * Peer MTU + * Result - 0 = connected, non-zero means CID is not connected + */ +typedef void(tL2CA_CREDIT_BASED_CONNECT_CFM_CB)(const RawAddress& bdaddr, uint16_t lcid, + uint16_t peer_mtu, uint16_t result); + +/* Credit based reconfiguration confirm callback prototype. Parameters are + * BD Address of remote + * Local CID assigned to the connection + * Flag indicating if this is local or peer configuration + * Pointer to configuration info + */ +typedef void(tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB)(const RawAddress& bdaddr, uint16_t lcid, + bool is_local_cfg, + tL2CAP_LE_CFG_INFO* p_cfg); + +/* Define the structure that applications use to register with + * L2CAP. This structure includes callback functions. All functions + * MUST be provided, with the exception of the "connect pending" + * callback and "congestion status" callback. + */ +struct tL2CAP_APPL_INFO { + tL2CA_CONNECT_IND_CB* pL2CA_ConnectInd_Cb; + tL2CA_CONNECT_CFM_CB* pL2CA_ConnectCfm_Cb; + tL2CA_CONFIG_IND_CB* pL2CA_ConfigInd_Cb; + tL2CA_CONFIG_CFM_CB* pL2CA_ConfigCfm_Cb; + tL2CA_DISCONNECT_IND_CB* pL2CA_DisconnectInd_Cb; + tL2CA_DISCONNECT_CFM_CB* pL2CA_DisconnectCfm_Cb; + tL2CA_DATA_IND_CB* pL2CA_DataInd_Cb; + tL2CA_CONGESTION_STATUS_CB* pL2CA_CongestionStatus_Cb; + tL2CA_TX_COMPLETE_CB* pL2CA_TxComplete_Cb; + tL2CA_ERROR_CB* pL2CA_Error_Cb; + tL2CA_CREDIT_BASED_CONNECT_IND_CB* pL2CA_CreditBasedConnectInd_Cb; + tL2CA_CREDIT_BASED_CONNECT_CFM_CB* pL2CA_CreditBasedConnectCfm_Cb; + tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB* pL2CA_CreditBasedReconfigCompleted_Cb; + tL2CA_CREDIT_BASED_COLLISION_IND_CB* pL2CA_CreditBasedCollisionInd_Cb; +}; + +/******************************************************************************* + * + * Fixed Channel callback prototypes + * + ******************************************************************************/ + +/* Fixed channel connected and disconnected. Parameters are + * channel + * BD Address of remote + * true if channel is connected, false if disconnected + * Reason for connection failure + * transport : physical transport, BR/EDR or LE + */ +typedef void(tL2CA_FIXED_CHNL_CB)(uint16_t, const RawAddress&, bool, uint16_t, tBT_TRANSPORT); + +/* Signalling data received. Parameters are + * channel + * BD Address of remote + * Pointer to buffer with data + */ +typedef void(tL2CA_FIXED_DATA_CB)(uint16_t, const RawAddress&, BT_HDR*); + +/* Congestion status callback protype. This callback is optional. If + * an application tries to send data when the transmit queue is full, + * the data will anyways be dropped. The parameter is: + * remote BD_ADDR + * true if congested, false if uncongested + */ +typedef void(tL2CA_FIXED_CONGESTION_STATUS_CB)(const RawAddress&, bool); + +/* Fixed channel registration info (the callback addresses and channel config) + */ +struct tL2CAP_FIXED_CHNL_REG { + tL2CA_FIXED_CHNL_CB* pL2CA_FixedConn_Cb; + tL2CA_FIXED_DATA_CB* pL2CA_FixedData_Cb; + tL2CA_FIXED_CONGESTION_STATUS_CB* pL2CA_FixedCong_Cb; + + uint16_t default_idle_tout; + tL2CA_TX_COMPLETE_CB* pL2CA_FixedTxComplete_Cb; /* fixed channel tx complete callback */ +}; + +/******************************************************************************* + * + * Fixed Channel callback prototypes + * + ******************************************************************************/ + +/* Fixed channel connected and disconnected. Parameters are + * channel + * BD Address of remote + * true if channel is connected, false if disconnected + * Reason for connection failure + * transport : physical transport, BR/EDR or LE + */ +typedef void(tL2CA_FIXED_CHNL_CB)(uint16_t, const RawAddress&, bool, uint16_t, tBT_TRANSPORT); + +/* Signalling data received. Parameters are + * channel + * BD Address of remote + * Pointer to buffer with data + */ +typedef void(tL2CA_FIXED_DATA_CB)(uint16_t, const RawAddress&, BT_HDR*); + +/* Congestion status callback protype. This callback is optional. If + * an application tries to send data when the transmit queue is full, + * the data will anyways be dropped. The parameter is: + * remote BD_ADDR + * true if congested, false if uncongested + */ +typedef void(tL2CA_FIXED_CONGESTION_STATUS_CB)(const RawAddress&, bool); + +/* Fixed channel registration info (the callback addresses and channel config) + */ +namespace bluetooth { +namespace stack { +namespace l2cap { + +class Interface { +public: + virtual ~Interface() = default; + + /******************************************************************************* + ** + ** Function L2CA_Register + ** + ** Description Register for L2CAP a PSM service. + ** + ** Parameters: psm: L2cap PSM service to register + ** p_cb_info: Set of l2cap callbacks + ** enable_snoop: Enable to disable snooping on this PSM + ** p_ertm_info: + ** my_mtu: + ** required_remote_mtu: + ** sec_level: Security requirements for connection + ** + ** Returns PSM to use or zero if error. Typically, the PSM returned + ** is the same as was passed in, but for an outgoing-only + ** connection to a dynamic PSM, a "virtual" PSM is returned + ** and should be used in the calls to L2CA_ConnectReq() and + ** BTM_SetSecurityLevel(). + ** + ******************************************************************************/ + virtual uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop, + tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu, + uint16_t required_remote_mtu, uint16_t sec_level) = 0; + virtual uint16_t L2CA_RegisterWithSecurity(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, + bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info, + uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level) = 0; + + /******************************************************************************* + ** + ** Function L2CA_Deregister + ** + ** Description Other layers call this function to deregister for L2CAP + ** services. + ** + ** Parameters: psm: L2cap PSM value to deregister + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_Deregister(uint16_t psm) = 0; + + /******************************************************************************* + ** + ** Function L2CA_AllocateLePSM + ** + ** Description Find an unused LE PSM for an L2CAP service. + ** + ** Returns LE_PSM to use if success. Otherwise returns 0. + ** + ******************************************************************************/ + virtual uint16_t L2CA_AllocateLePSM(void) = 0; + + /******************************************************************************* + ** + ** Function L2CA_FreeLePSM + ** + ** Description Free an assigned LE PSM. + ** + ** Parameters: psm: L2cap PSM value to free. + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_FreeLePSM(uint16_t psm) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ConnectReq + ** + ** Description Create an L2CAP connection to a target device requesting + ** the PSM service. + ** Note that the connection is not established at this time, + ** but connection establishment gets started. The callback + ** will be invoked when connection establishes or fails. + ** + ** Parameters: psm: L2cap PSM on remote to request connection. + ** bd_addr: Remote address of peer connection device. + ** sec_level: Security requirements for connection. + ** + ** Returns Local CID of the connection, or 0 if it failed to + ** start + ** + ******************************************************************************/ + virtual uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr) = 0; + virtual uint16_t L2CA_ConnectReqWithSecurity(uint16_t psm, const RawAddress& p_bd_addr, + uint16_t sec_level) = 0; + + /******************************************************************************* + ** + ** Function L2CA_RegisterLECoc + ** + ** Description Register for L2CAP Connection Oriented Channel. + ** + ** Parameters: psm: L2cap PSM service to register + ** p_cb_info: Set of l2cap callbacks + ** sec_level: Security requirements for connection + ** cfg: Le configuration info. + ** + ** Returns PSM to use or zero if error. Typically, the PSM returned + ** is the same as was passed in, but for an outgoing-only + ** connection to a dynamic PSM, a "virtual" PSM is returned + ** and should be used in the calls to L2CA_ConnectLECocReq() + ** and BTM_SetSecurityLevel(). + ** + ******************************************************************************/ + virtual uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, + uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_DeregisterLECoc + ** + ** Description Other layers call this function to deregister for L2CAP + ** Connection Oriented Channel. + ** + ** Parameters: psm: L2cap PSM service to deregister + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_DeregisterLECoc(uint16_t psm) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ConnectLECocReq + ** + ** Description Higher layers call this function to create an L2CAP LE + ** COC. Note that the connection is not established at this + ** time, but connection establishment gets started. The + ** callback will be invoked when connection establishes or + ** fails. + ** + ** Parameters: psm: L2cap PSM service to register + ** bd_addr: Peer bluetooth device address + ** p_cfg: Peer le configuration info + ** sec_level: Security requirements for connection + ** + ** Returns the CID of the connection, or 0 if it failed to start + ** + ******************************************************************************/ + virtual uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr, + tL2CAP_LE_CFG_INFO* p_cfg, uint16_t sec_level) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ConnectCreditBasedReq + ** + ** Description With this function L2CAP will initiate setup of up to 5 + ** credit based connections for given psm using provided + ** configuration. L2CAP will notify user on the connection + ** result, by calling pL2CA_CreditBasedConnectCfm_Cb for + ** each cid with a result. + ** + ** Parameters: psm: PSM of peer service for connection + ** bd_addr: Peer bluetooth device address + ** p_cfg: Peer le configuration info + ** + ** Returns Local cids allocated for the connection + ** + ******************************************************************************/ + virtual std::vector<uint16_t> L2CA_ConnectCreditBasedReq(uint16_t psm, + const RawAddress& p_bd_addr, + tL2CAP_LE_CFG_INFO* p_cfg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_GetPeerLECocCredit + ** + ** Description Get peers current credit for LE Connection Oriented + ** Channel. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** lcid: Local l2cap channel id + ** + ** Returns Number of the peer current credit + ** + ******************************************************************************/ + virtual uint16_t L2CA_GetPeerLECocCredit(const RawAddress& bd_addr, uint16_t lcid) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ReconfigCreditBasedConnsReq + ** + ** Description Start reconfigure procedure on Connection Oriented + ** Channel. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** lcids: Local channel ids for reconfiguration + ** p_cfg: Peer le configuration info + ** + ** Returns true if peer is connected + ** + ******************************************************************************/ + virtual bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr, + std::vector<uint16_t>& lcids, + tL2CAP_LE_CFG_INFO* p_cfg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ConnectCreditBasedRsp + ** + ** Description Response for the pL2CA_CreditBasedConnectInd_Cb which is + ** the indication for peer requesting credit based + ** connection. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** id: + ** accepted_lcids: + ** result: + ** p_cfg: Peer le configuration info + ** + ** Returns true if peer is connected false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id, + std::vector<uint16_t>& accepted_lcids, uint16_t result, + tL2CAP_LE_CFG_INFO* p_cfg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetIdleTimeoutByBdAddr + ** + ** Description Higher layers call this function to set the idle timeout + ** for a connection. The "idle timeout" is the amount of + *time + ** that a connection can remain up with no L2CAP channels on + ** it. A timeout of zero means that the connection will be + ** torn down immediately when the last channel is removed. + ** A timeout of 0xFFFF means no timeout. Values are in + ** seconds. A bd_addr is the remote BD address. If + ** bd_addr = RawAddress::kAny, then the idle timeouts for + ** all active l2cap links will be changed. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** timeout: Timeout value for ACL link + ** transport: Transport to set timeout (BR/EDR or BLE) + ** + ** Returns true if command succeeded, false if failed + ** + ** NOTE This timeout applies to all logical channels active on + *the + ** ACL link. + ******************************************************************************/ + virtual bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout, + tBT_TRANSPORT transport) = 0; + + /******************************************************************************* + ** + ** Function L2CA_UseLatencyMode + ** + ** Description Sets use latency mode for an ACL channel. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** use_latency_mode: Enable or disable latency mode + ** + ** Returns true if command succeeded, false if failed + ** + ******************************************************************************/ + virtual bool L2CA_UseLatencyMode(const RawAddress& bd_addr, bool use_latency_mode) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetAclPriority + ** + ** Description Sets the transmission priority for an ACL channel. + ** (For initial implementation only two values are valid. + ** L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH). + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** priority: Priority for ACL to peer + ** + ** Returns true if command succeeded, false if failed + ** + ******************************************************************************/ + virtual bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetAclLatency + ** + ** Description Sets the transmission latency for a channel. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** latency: Latency value for the ACL link + ** + ** Returns true if command succeeded, false if failed + ** + ******************************************************************************/ + virtual bool L2CA_SetAclLatency(const RawAddress& bd_addr, tL2CAP_LATENCY latency) = 0; + + /******************************************************************************* + ** + ** Function L2CA_GetPeerFeatures + ** + ** Description Request peer features and fixed channel map + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** p_ext_feat: Peer features + ** p_chnl_mask: Peer fixed channel map + ** + ** Returns true if command succeeded, false if failed + ** + ******************************************************************************/ + virtual bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat, + uint8_t* p_chnl_mask) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetLeGattTimeout + ** + ** Description Higher layers call this function to set the idle timeout + ** for a fixed channel. The "idle timeout" is the amount of + ** time that a connection can remain up with no L2CAP + ** channels on it. A timeout of zero means that the + ** connection will be torn down immediately when the last + ** channel is removed. A timeout of 0xFFFF means no timeout. + ** Values are in seconds. A bd_addr is the remote BD + *address. + ** If bd_addr = RawAddress::kAny, then the idle timeouts for + ** all active l2cap links will be changed. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** idle_tout: Idle timeout for GATT BLE connection + ** + ** Returns true if command succeeded, false if failed + ** + ******************************************************************************/ + virtual bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout) = 0; + + virtual bool L2CA_MarkLeLinkAsActive(const RawAddress& rem_bda) = 0; + + virtual bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int, + uint16_t max_int, uint16_t latency, uint16_t timeout, + uint16_t min_ce_len, uint16_t max_ce_len) = 0; + + /******************************************************************************* + ** + ** Function L2CA_LockBleConnParamsForServiceDiscovery + ** + ** Description: When called with lock=true, LE connection parameters will + ** be locked on fastest value, and we won't accept request + ** to change it from remote. When called with lock=false, + ** parameters are relaxed. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** lock: Determines fast or relaxed parameters + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_LockBleConnParamsForServiceDiscovery(const RawAddress& rem_bda, bool lock) = 0; + + /******************************************************************************* + ** + ** Function L2CA_LockBleConnParamsForProfileConnection + ** + ** Description: When called with lock=true, LE connection parameters will + ** be locked on fastest value, and we won't accept request + ** to change it from remote. When called with lock=false, + ** parameters are relaxed. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** lock: Determines fast or relaxed parameters + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_LockBleConnParamsForProfileConnection(const RawAddress& rem_bda, bool lock) = 0; + + /******************************************************************************* + ** + ** Function L2CA_Consolidate + ** + ** Description This function consolidates two addresses. + ** + ** Parameters: identity_addr: Identity address of peer + ** rpa: Resolvable Private Address of peer + ** + ** Returns void + ** + ******************************************************************************/ + virtual void L2CA_Consolidate(const RawAddress& identity_addr, const RawAddress& rpa) = 0; + virtual tHCI_ROLE L2CA_GetBleConnRole(const RawAddress& bd_addr) = 0; + + /******************************************************************************* + ** + ** Function L2CA_IsLinkEstablished + ** + ** Description Check if a BR/EDR or BLE link to the remote device is + ** established. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** transport: Transport to check (BR/EDR or BLE) + ** + ** Returns true if peer is connected false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SubrateRequest + ** + ** Description BLE Subrate request. + ** + ** Parameters: bd_addr: Peer bluetooth device address + ** Power subrating parameters + ** + ** Return value: true if update started + ** + ******************************************************************************/ + virtual bool L2CA_SubrateRequest(const RawAddress& rem_bda, uint16_t subrate_min, + uint16_t subrate_max, uint16_t max_latency, uint16_t cont_num, + uint16_t timeout) = 0; + + /******************************************************************************* + ** + ** Function L2CA_GetPeerLECocConfig + ** + ** Description Request peer configuration for LE Connection Oriented + ** Channel. + ** + ** Parameters: cid: Local channel id of L2CAP connection + ** peer_cfg: Peer LE CoC configuration + ** + ** Return value: true if peer is connected + ** + ******************************************************************************/ + virtual bool L2CA_GetPeerLECocConfig(uint16_t cid, tL2CAP_LE_CFG_INFO* peer_cfg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_DisconnectReq + ** + ** Description Higher layers call this function to disconnect a channel. + ** + ** Parameters: cid: Local channel id of L2CAP connection + ** + ** Returns true if disconnect sent, else false + ** + ******************************************************************************/ + virtual bool L2CA_DisconnectReq(uint16_t cid) = 0; + virtual bool L2CA_DisconnectLECocReq(uint16_t cid) = 0; + + /******************************************************************************* + ** + ** Function L2CA_DataWrite + ** + ** Description Higher layers call this function to write data. + ** + ** Parameters: cid: Local channel id of L2CAP connection + ** p_data: Data to write to peer + ** + ** Returns L2CAP_DW_SUCCESS, if data accepted, else false + ** L2CAP_DW_CONGESTED, if data accepted and the channel is + ** congested + ** L2CAP_DW_FAILED, if error + ** + ******************************************************************************/ + virtual tL2CAP_DW_RESULT L2CA_DataWrite(uint16_t cid, BT_HDR* p_data) = 0; + virtual tL2CAP_DW_RESULT L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data) = 0; + + /******************************************************************************* + ** + ** Function L2CA_GetRemoteChannelId + ** + ** Description Given a local channel identifier, |lcid|, this function + ** returns the bound remote channel identifier, |rcid|. If + ** |lcid| is not known or is invalid, this function returns + ** false and does not modify the value pointed at by |rcid|. + ** |rcid| may be NULL. + ** + ** Parameters: cid: Local channel id of L2CAP connection + ** rcid: Remote channel id of L2CAP connection + ** + ** Returns true if remote cid exists, false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_GetRemoteChannelId(uint16_t cid, uint16_t* rcid) = 0; + + /******************************************************************************* + ** + ** Function L2CA_FlushChannel + ** + ** Description This function flushes none, some or all buffers queued up + ** for xmission for a particular CID. If called with + ** L2CAP_FLUSH_CHANS_GET (0), it simply returns the number + ** of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL + ** (0xffff) flushes all buffers. All other values specifies + ** the maximum buffers to flush. + ** + ** Parameters: lcid: Local channel id of L2CAP connection + ** num_to_flush: Number of buffers to flush or + ** L2CAP_FLUSH_CHANS_ALL + ** + ** Returns Number of buffers left queued for that CID + ** + ******************************************************************************/ + virtual uint16_t L2CA_FlushChannel(uint16_t cid, uint16_t num_to_flush) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetTxPriority + ** + ** Description Sets the transmission priority for a channel. (FCR Mode) + ** + ** Parameters: cid: Local channel id of L2CAP connection + ** priority: L2CAP channel priority + ** + ** Returns true if a valid channel, else false + ** + ******************************************************************************/ + virtual bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority) = 0; + + /******************************************************************************* + * + * Function L2CA_SetChnlFlushability + * + * Description Higher layers call this function to set a channels + * flushability flags + * + ** Parameters: cid: Local channel id of L2CAP connection + ** is_flushable: Set or clear flushability flag for channel + * Returns true if CID found, else false + * + ******************************************************************************/ + virtual bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable) = 0; + + /******************************************************************************* + ** + ** Function L2CA_RegisterFixedChannel + ** + ** Description Register a fixed channel. + ** + ** Parameters: fixed_cid: Fixed Channel # + ** p_freg: Channel Callbacks and config + ** + ** Return value: true if registered OK, false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_RegisterFixedChannel(uint16_t fixed_cid, tL2CAP_FIXED_CHNL_REG* p_freg) = 0; + + /******************************************************************************* + ** + ** Function L2CA_ConnectFixedChnl + ** + ** Description Connect an fixed signalling channel to a remote device. + ** + ** Parameters: fixed_cid: Fixed CID + ** bd_addr: BD Address of remote + ** + ** Return value: true if connection started, false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SendFixedChnlData + ** + ** Description Write data on a fixed signalling channel. + ** + ** Parameters: fixed_cid: Fixed CID + ** bd_addr: BD Address of remote + ** p_buf: Pointer to data buffer + ** + ** Return value L2CAP_DW_SUCCESS, if data accepted + ** L2CAP_DW_FAILED, if error + ** + ******************************************************************************/ + virtual tL2CAP_DW_RESULT L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda, + BT_HDR* p_buf) = 0; + + /******************************************************************************* + ** + ** Function L2CA_RemoveFixedChnl + ** + ** Description Remove a fixed channel to a remote device. + ** + ** Parameters: fixed_cid: Fixed CID + ** bd_addr: Mac address of remote + ** + ** Return value: true if channel removed, false otherwise + ** + ******************************************************************************/ + virtual bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda) = 0; + + /******************************************************************************* + ** + ** Function L2CA_AdjustConnectionIntervals + ** + ** Description Adjust connection intervals + ** + ** Parameters: Connection intervals + ** + ** Return value: void + ** + ******************************************************************************/ + virtual void L2CA_AdjustConnectionIntervals(uint16_t* min_interval, uint16_t* max_interval, + uint16_t floor_interval) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetEcosystemBaseInterval + ** + ** Description Sets the base ecosystem interval + ** + ** Parameters: Base interval + ** + ** Return value: void + ** + ******************************************************************************/ + virtual void L2CA_SetEcosystemBaseInterval(uint32_t base_interval) = 0; + + /******************************************************************************* + * + * Function L2CA_SetDefaultSubrate + * + * Description BLE Set Default Subrate. + * + * Parameters: Subrate parameters + * + * Return value: void + * + ******************************************************************************/ + virtual void L2CA_SetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max, + uint16_t max_latency, uint16_t cont_num, + uint16_t timeout) = 0; + + /******************************************************************************* + ** + ** Function L2CA_SetMediaStreamChannel + ** + ** Description This function is called to set/reset the ccb of active + ** media streaming channel + ** + ** Parameters: local_media_cid: The local cid provided to A2DP to be + ** used for streaming + ** status: The status of media streaming on this channel + ** + ** Returns void + ** + *******************************************************************************/ + virtual void L2CA_SetMediaStreamChannel(uint16_t local_media_cid, bool status) = 0; + + /******************************************************************************* + ** + ** Function L2CA_isMediaChannel + ** + ** Description This function returns if the channel id passed as + ** parameter is an A2DP streaming channel + ** + ** Parameters: handle: Connection handle with the remote device + ** channel_id: Channel ID + ** is_local_cid: Signifies if the channel id passed is local + ** cid or remote cid (true if local, remote otherwise) + ** + ** Returns bool + ** + *******************************************************************************/ + virtual bool L2CA_isMediaChannel(uint16_t handle, uint16_t channel_id, bool is_local_cid) = 0; +}; + +Interface& get_interface(); + +} // namespace l2cap +} // namespace stack +} // namespace bluetooth diff --git a/system/stack/include/l2cap_types.h b/system/stack/include/l2cap_types.h new file mode 100644 index 0000000000..f2448769ea --- /dev/null +++ b/system/stack/include/l2cap_types.h @@ -0,0 +1,171 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ + +#pragma once + +#include <bluetooth/log.h> +#include <stdbool.h> + +#include <cstdint> + +#include "stack/include/hcidefs.h" +#include "stack/include/l2cdefs.h" + +/* Validity check for PSM. PSM values must be odd. Also, all PSM values must + * be assigned such that the least significant bit of the most sigificant + * octet equals zero. + */ +#define L2C_INVALID_PSM(psm) (((psm) & 0x0101) != 0x0001) +#define L2C_IS_VALID_PSM(psm) (((psm) & 0x0101) == 0x0001) +#define L2C_IS_VALID_LE_PSM(psm) (((psm) > 0x0000) && ((psm) < 0x0100)) + +/* Define the minimum offset that L2CAP needs in a buffer. This is made up of + * HCI type(1), len(2), handle(2), L2CAP len(2) and CID(2) => 9 + */ +#define L2CAP_MIN_OFFSET 13 /* plus control(2), SDU length(2) */ + +#define L2CAP_LCC_SDU_LENGTH 2 +#define L2CAP_LCC_OFFSET (L2CAP_MIN_OFFSET + L2CAP_LCC_SDU_LENGTH) /* plus SDU length(2) */ + +#define L2CAP_FCS_LENGTH 2 + +/***************************************************************************** + * Type Definitions + ****************************************************************************/ + +/* Define the structure that applications use to create or accept + * connections with enhanced retransmission mode. + */ +struct tL2CAP_ERTM_INFO { + uint8_t preferred_mode; +}; + +/* Values for priority parameter to L2CA_SetAclPriority */ +enum tL2CAP_PRIORITY : uint8_t { + L2CAP_PRIORITY_NORMAL = 0, + L2CAP_PRIORITY_HIGH = 1, +}; + +/* Values for priority parameter to L2CA_SetAclLatency */ +enum tL2CAP_LATENCY : uint8_t { + L2CAP_LATENCY_NORMAL = 0, + L2CAP_LATENCY_LOW = 1, +}; + +#define L2CAP_NO_IDLE_TIMEOUT 0xFFFF + +/* L2CA_FlushChannel num_to_flush definitions */ +#define L2CAP_FLUSH_CHANS_ALL 0xffff +#define L2CAP_FLUSH_CHANS_GET 0x0000 + +/* Values for priority parameter to L2CA_SetTxPriority */ +#define L2CAP_CHNL_PRIORITY_HIGH 0 +#define L2CAP_CHNL_PRIORITY_LOW 2 + +typedef uint8_t tL2CAP_CHNL_PRIORITY; + +typedef struct { +#define L2CAP_FCR_BASIC_MODE 0x00 +#define L2CAP_FCR_ERTM_MODE 0x03 +#define L2CAP_FCR_LE_COC_MODE 0x05 + + uint8_t mode; + + uint8_t tx_win_sz; + uint8_t max_transmit; + uint16_t rtrans_tout; + uint16_t mon_tout; + uint16_t mps; +} tL2CAP_FCR_OPTS; + +/* default options for ERTM mode */ +constexpr tL2CAP_FCR_OPTS kDefaultErtmOptions = { + L2CAP_FCR_ERTM_MODE, + 10, /* Tx window size */ + 20, /* Maximum transmissions before disconnecting */ + 2000, /* Retransmission timeout (2 secs) */ + 12000, /* Monitor timeout (12 secs) */ + 1010 /* MPS segment size */ +}; + +typedef struct { + uint8_t qos_flags; /* TBD */ + uint8_t service_type; /* see below */ + uint32_t token_rate; /* bytes/second */ + uint32_t token_bucket_size; /* bytes */ + uint32_t peak_bandwidth; /* bytes/second */ + uint32_t latency; /* microseconds */ + uint32_t delay_variation; /* microseconds */ +} FLOW_SPEC; + +/* Define a structure to hold the configuration parameters. Since the + * parameters are optional, for each parameter there is a boolean to + * use to signify its presence or absence. + */ +typedef struct { + uint16_t result; /* Only used in confirm messages */ + bool mtu_present; + uint16_t mtu; + bool qos_present; + FLOW_SPEC qos; + bool flush_to_present; + uint16_t flush_to; + bool fcr_present; + tL2CAP_FCR_OPTS fcr; + bool fcs_present; /* Optionally bypasses FCS checks */ + uint8_t fcs; /* '0' if desire is to bypass FCS, otherwise '1' */ + bool ext_flow_spec_present; + tHCI_EXT_FLOW_SPEC ext_flow_spec; + uint16_t flags; /* bit 0: 0-no continuation, 1-continuation */ +} tL2CAP_CFG_INFO; + +/* Define a structure to hold the configuration parameter for LE L2CAP + * connection oriented channels. + */ +constexpr uint16_t kDefaultL2capMtu = 100; +constexpr uint16_t kDefaultL2capMps = 100; + +// This is initial amount of credits we send, and amount to which we increase +// credits once they fall below threshold +uint16_t L2CA_LeCreditDefault(); + +// If credit count on remote fall below this value, we send back credits to +// reach default value. +uint16_t L2CA_LeCreditThreshold(); + +// Max number of CIDs in the L2CAP CREDIT BASED CONNECTION REQUEST +constexpr uint8_t L2CAP_CREDIT_BASED_MAX_CIDS = 5; + +struct tL2CAP_LE_CFG_INFO { + uint16_t result{L2CAP_LE_RESULT_CONN_OK}; /* Only used in confirm messages */ + uint16_t mtu{kDefaultL2capMtu}; + uint16_t mps{kDefaultL2capMps}; + uint16_t credits{L2CA_LeCreditDefault()}; + uint8_t number_of_channels{L2CAP_CREDIT_BASED_MAX_CIDS}; +}; + +/* LE credit based L2CAP connection parameters */ +constexpr uint16_t L2CAP_LE_MIN_MTU = 23; // Minimum SDU size +constexpr uint16_t L2CAP_LE_MIN_MPS = 23; +constexpr uint16_t L2CAP_LE_MAX_MPS = 65533; +constexpr uint16_t L2CAP_LE_CREDIT_MAX = 65535; + +namespace fmt { +template <> +struct formatter<tL2CAP_LATENCY> : enum_formatter<tL2CAP_LATENCY> {}; +template <> +struct formatter<tL2CAP_PRIORITY> : enum_formatter<tL2CAP_PRIORITY> {}; +} // namespace fmt diff --git a/system/stack/l2cap/l2cap_api.cc b/system/stack/l2cap/l2cap_api.cc new file mode 100644 index 0000000000..f110aa3290 --- /dev/null +++ b/system/stack/l2cap/l2cap_api.cc @@ -0,0 +1,257 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ + +#include "stack/include/l2c_api.h" +#include "stack/include/l2cap_interface.h" + +static bluetooth::stack::l2cap::Interface* interface_; + +bluetooth::stack::l2cap::Interface& bluetooth::stack::l2cap::get_interface() { return *interface_; } + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_Register( + uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop, + tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level) { + return ::L2CA_Register(psm, p_cb_info, enable_snoop, p_ertm_info, my_mtu, required_remote_mtu, + sec_level); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_RegisterWithSecurity( + uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop, + tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level) { + return ::L2CA_RegisterWithSecurity(psm, p_cb_info, enable_snoop, p_ertm_info, my_mtu, + required_remote_mtu, sec_level); +} + +void bluetooth::stack::l2cap::Impl::L2CA_Deregister(uint16_t psm) { ::L2CA_Deregister(psm); } + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_AllocateLePSM(void) { + return ::L2CA_AllocateLePSM(); +} + +void bluetooth::stack::l2cap::Impl::L2CA_FreeLePSM(uint16_t psm) { return ::L2CA_FreeLePSM(psm); } + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_ConnectReq(uint16_t psm, + const RawAddress& p_bd_addr) { + return ::L2CA_ConnectReq(psm, p_bd_addr); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_ConnectReqWithSecurity( + uint16_t psm, const RawAddress& p_bd_addr, uint16_t sec_level) { + return ::L2CA_ConnectReqWithSecurity(psm, p_bd_addr, sec_level); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_RegisterLECoc( + uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, uint16_t sec_level, + tL2CAP_LE_CFG_INFO cfg) { + return ::L2CA_RegisterLECoc(psm, p_cb_info, sec_level, cfg); +} + +void bluetooth::stack::l2cap::Impl::L2CA_DeregisterLECoc(uint16_t psm) { + ::L2CA_DeregisterLECoc(psm); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_ConnectLECocReq( + uint16_t psm, const RawAddress& p_bd_addr, tL2CAP_LE_CFG_INFO* p_cfg, uint16_t sec_level) { + return ::L2CA_ConnectLECocReq(psm, p_bd_addr, p_cfg, sec_level); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_GetPeerLECocConfig( + uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg) { + return ::L2CA_GetPeerLECocConfig(lcid, peer_cfg); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_GetPeerLECocCredit( + const RawAddress& bd_addr, uint16_t lcid) { + return ::L2CA_GetPeerLECocCredit(bd_addr, lcid); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_ReconfigCreditBasedConnsReq( + const RawAddress& bd_addr, std::vector<uint16_t>& lcids, tL2CAP_LE_CFG_INFO* p_cfg) { + return ::L2CA_ReconfigCreditBasedConnsReq(bd_addr, lcids, p_cfg); +} + +[[nodiscard]] std::vector<uint16_t> bluetooth::stack::l2cap::Impl::L2CA_ConnectCreditBasedReq( + uint16_t psm, const RawAddress& p_bd_addr, tL2CAP_LE_CFG_INFO* p_cfg) { + return ::L2CA_ConnectCreditBasedReq(psm, p_bd_addr, p_cfg); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_ConnectCreditBasedRsp( + const RawAddress& p_bd_addr, uint8_t id, std::vector<uint16_t>& accepted_lcids, + uint16_t result, tL2CAP_LE_CFG_INFO* p_cfg) { + return ::L2CA_ConnectCreditBasedRsp(p_bd_addr, id, accepted_lcids, result, p_cfg); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_DisconnectReq(uint16_t cid) { + return ::L2CA_DisconnectReq(cid); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_DisconnectLECocReq(uint16_t cid) { + return ::L2CA_DisconnectLECocReq(cid); +} + +[[nodiscard]] tL2CAP_DW_RESULT bluetooth::stack::l2cap::Impl::L2CA_DataWrite(uint16_t cid, + BT_HDR* p_data) { + return ::L2CA_DataWrite(cid, p_data); +} + +[[nodiscard]] tL2CAP_DW_RESULT bluetooth::stack::l2cap::Impl::L2CA_LECocDataWrite(uint16_t cid, + BT_HDR* p_data) { + return ::L2CA_LECocDataWrite(cid, p_data); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetIdleTimeoutByBdAddr( + const RawAddress& bd_addr, uint16_t timeout, tBT_TRANSPORT transport) { + return ::L2CA_SetIdleTimeoutByBdAddr(bd_addr, timeout, transport); +} + +[[nodiscard]] uint16_t bluetooth::stack::l2cap::Impl::L2CA_FlushChannel(uint16_t lcid, + uint16_t num_to_flush) { + return ::L2CA_FlushChannel(lcid, num_to_flush); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_UseLatencyMode(const RawAddress& bd_addr, + bool use_latency_mode) { + return ::L2CA_UseLatencyMode(bd_addr, use_latency_mode); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetAclPriority(const RawAddress& bd_addr, + tL2CAP_PRIORITY priority) { + return ::L2CA_SetAclPriority(bd_addr, priority); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetAclLatency(const RawAddress& bd_addr, + tL2CAP_LATENCY latency) { + return ::L2CA_SetAclLatency(bd_addr, latency); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetTxPriority( + uint16_t cid, tL2CAP_CHNL_PRIORITY priority) { + return ::L2CA_SetTxPriority(cid, priority); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetChnlFlushability(uint16_t cid, + bool is_flushable) { + return ::L2CA_SetChnlFlushability(cid, is_flushable); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_GetPeerFeatures(const RawAddress& bd_addr, + uint32_t* p_ext_feat, + uint8_t* p_chnl_mask) { + return ::L2CA_GetPeerFeatures(bd_addr, p_ext_feat, p_chnl_mask); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_RegisterFixedChannel( + uint16_t fixed_cid, tL2CAP_FIXED_CHNL_REG* p_freg) { + return ::L2CA_RegisterFixedChannel(fixed_cid, p_freg); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_ConnectFixedChnl(uint16_t fixed_cid, + const RawAddress& bd_addr) { + return ::L2CA_ConnectFixedChnl(fixed_cid, bd_addr); +} + +[[nodiscard]] tL2CAP_DW_RESULT bluetooth::stack::l2cap::Impl::L2CA_SendFixedChnlData( + uint16_t fixed_cid, const RawAddress& rem_bda, BT_HDR* p_buf) { + return ::L2CA_SendFixedChnlData(fixed_cid, rem_bda, p_buf); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_RemoveFixedChnl(uint16_t fixed_cid, + const RawAddress& rem_bda) { + return ::L2CA_RemoveFixedChnl(fixed_cid, rem_bda); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SetLeGattTimeout(const RawAddress& rem_bda, + uint16_t idle_tout) { + return ::L2CA_SetLeGattTimeout(rem_bda, idle_tout); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_MarkLeLinkAsActive( + const RawAddress& rem_bda) { + return ::L2CA_MarkLeLinkAsActive(rem_bda); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_UpdateBleConnParams( + const RawAddress& rem_bda, uint16_t min_int, uint16_t max_int, uint16_t latency, + uint16_t timeout, uint16_t min_ce_len, uint16_t max_ce_len) { + return ::L2CA_UpdateBleConnParams(rem_bda, min_int, max_int, latency, timeout, min_ce_len, + max_ce_len); +} + +void bluetooth::stack::l2cap::Impl::L2CA_LockBleConnParamsForServiceDiscovery( + const RawAddress& rem_bda, bool lock) { + ::L2CA_LockBleConnParamsForServiceDiscovery(rem_bda, lock); +} + +void bluetooth::stack::l2cap::Impl::L2CA_LockBleConnParamsForProfileConnection( + const RawAddress& rem_bda, bool lock) { + ::L2CA_LockBleConnParamsForProfileConnection(rem_bda, lock); +} + +void bluetooth::stack::l2cap::Impl::L2CA_Consolidate(const RawAddress& identity_addr, + const RawAddress& rpa) { + ::L2CA_Consolidate(identity_addr, rpa); +} + +[[nodiscard]] tHCI_ROLE bluetooth::stack::l2cap::Impl::L2CA_GetBleConnRole( + const RawAddress& bd_addr) { + return ::L2CA_GetBleConnRole(bd_addr); +} + +void bluetooth::stack::l2cap::Impl::L2CA_AdjustConnectionIntervals(uint16_t* min_interval, + uint16_t* max_interval, + uint16_t floor_interval) { + ::L2CA_AdjustConnectionIntervals(min_interval, max_interval, floor_interval); +} + +void bluetooth::stack::l2cap::Impl::L2CA_SetEcosystemBaseInterval(uint32_t base_interval) { + ::L2CA_SetEcosystemBaseInterval(base_interval); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_IsLinkEstablished(const RawAddress& bd_addr, + tBT_TRANSPORT transport) { + return ::L2CA_IsLinkEstablished(bd_addr, transport); +} + +void bluetooth::stack::l2cap::Impl::L2CA_SetDefaultSubrate(uint16_t subrate_min, + uint16_t subrate_max, + uint16_t max_latency, uint16_t cont_num, + uint16_t timeout) { + ::L2CA_SetDefaultSubrate(subrate_min, subrate_max, max_latency, cont_num, timeout); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_SubrateRequest( + const RawAddress& rem_bda, uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, + uint16_t cont_num, uint16_t timeout) { + return ::L2CA_SubrateRequest(rem_bda, subrate_min, subrate_max, max_latency, cont_num, timeout); +} + +void bluetooth::stack::l2cap::Impl::L2CA_SetMediaStreamChannel(uint16_t local_media_cid, + bool status) { + ::L2CA_SetMediaStreamChannel(local_media_cid, status); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_isMediaChannel(uint16_t handle, + uint16_t channel_id, + bool is_local_cid) { + return ::L2CA_isMediaChannel(handle, channel_id, is_local_cid); +} + +[[nodiscard]] bool bluetooth::stack::l2cap::Impl::L2CA_GetRemoteChannelId(uint16_t lcid, + uint16_t* rcid) { + return ::L2CA_GetRemoteChannelId(lcid, rcid); +} diff --git a/system/test/Android.bp b/system/test/Android.bp index fe52ab99d1..2970d10640 100644 --- a/system/test/Android.bp +++ b/system/test/Android.bp @@ -277,6 +277,13 @@ filegroup { } filegroup { + name: "TestMockStackL2capInterface", + srcs: [ + "mock/mock_stack_l2cap_interface.cc", + ], +} + +filegroup { name: "TestMockStackRfcomm", srcs: [ "mock/mock_stack_rfcomm*.cc", diff --git a/system/test/mock/mock_stack_l2cap_interface.cc b/system/test/mock/mock_stack_l2cap_interface.cc new file mode 100644 index 0000000000..3373c815f8 --- /dev/null +++ b/system/test/mock/mock_stack_l2cap_interface.cc @@ -0,0 +1,33 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ + +#include "test/mock/mock_stack_l2cap_interface.h" + +#include "stack/include/l2cap_interface.h" + +namespace { +bluetooth::testing::stack::l2cap::Mock mock_l2cap_interface; +bluetooth::stack::l2cap::Interface* interface_ = &mock_l2cap_interface; +} // namespace + +void bluetooth::testing::stack::l2cap::reset_interface() { interface_ = &mock_l2cap_interface; } + +void bluetooth::testing::stack::l2cap::set_interface( + bluetooth::stack::l2cap::Interface* interface) { + interface_ = interface; +} + +bluetooth::stack::l2cap::Interface& bluetooth::stack::l2cap::get_interface() { return *interface_; } diff --git a/system/test/mock/mock_stack_l2cap_interface.h b/system/test/mock/mock_stack_l2cap_interface.h new file mode 100644 index 0000000000..f2560a6612 --- /dev/null +++ b/system/test/mock/mock_stack_l2cap_interface.h @@ -0,0 +1,135 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ + +#pragma once + +#include <gmock/gmock.h> + +#include <vector> + +#include "stack/include/l2cap_interface.h" + +namespace bluetooth { +namespace testing { +namespace stack { +namespace l2cap { + +class Mock : public ::bluetooth::stack::l2cap::Interface { +public: + ~Mock() = default; + + MOCK_METHOD(uint16_t, L2CA_Register, + (uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop, + tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level)); + MOCK_METHOD(uint16_t, L2CA_RegisterWithSecurity, + (uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop, + tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu, uint16_t required_remote_mtu, + uint16_t sec_level)); + MOCK_METHOD(void, L2CA_Deregister, (uint16_t psm)); + + MOCK_METHOD(uint16_t, L2CA_AllocateLePSM, ()); + MOCK_METHOD(void, L2CA_FreeLePSM, (uint16_t psm)); + + MOCK_METHOD(uint16_t, L2CA_RegisterLECoc, + (uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, uint16_t sec_level, + tL2CAP_LE_CFG_INFO cfg)); + MOCK_METHOD(void, L2CA_DeregisterLECoc, (uint16_t psm)); + + // Methods used for, both BR/EDR and BLE + MOCK_METHOD(bool, L2CA_IsLinkEstablished, (const RawAddress& bd_addr, tBT_TRANSPORT transport)); + MOCK_METHOD(bool, L2CA_SetIdleTimeoutByBdAddr, + (const RawAddress& bd_addr, uint16_t timeout, tBT_TRANSPORT transport)); + MOCK_METHOD(bool, L2CA_GetRemoteChannelId, (uint16_t lcid, uint16_t* rcid)); + + MOCK_METHOD(uint16_t, L2CA_ConnectReq, (uint16_t psm, const RawAddress& bd_addr)); + + MOCK_METHOD(uint16_t, L2CA_ConnectReqWithSecurity, + (uint16_t psm, const RawAddress& bd_addr, uint16_t sec_level)); + MOCK_METHOD(bool, L2CA_SetAclLatency, (const RawAddress& bd_addr, tL2CAP_LATENCY latency)); + MOCK_METHOD(bool, L2CA_UseLatencyMode, (const RawAddress& bd_addr, bool use_latency_mode)); + MOCK_METHOD(bool, L2CA_GetPeerFeatures, + (const RawAddress& bd_addr, uint32_t* p_ext_feat, uint8_t* p_chnl_mask)); + MOCK_METHOD(bool, L2CA_SetAclPriority, (const RawAddress& bd_addr, tL2CAP_PRIORITY priority)); + MOCK_METHOD(void, L2CA_SetDefaultSubrate, + (uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, uint16_t cont_num, + uint16_t timeout)); + MOCK_METHOD(void, L2CA_AdjustConnectionIntervals, + (uint16_t* min_interval, uint16_t* max_interval, uint16_t floor_interval)); + MOCK_METHOD(void, L2CA_SetEcosystemBaseInterval, (uint32_t base_interval)); + + MOCK_METHOD(bool, L2CA_SubrateRequest, + (const RawAddress& bd_addr, uint16_t subrate_min, uint16_t subrate_max, + uint16_t max_latency, uint16_t cont_num, uint16_t timeout)); + MOCK_METHOD(uint16_t, L2CA_FlushChannel, (uint16_t lcid, uint16_t num_to_flush)); + MOCK_METHOD(bool, L2CA_SetTxPriority, (uint16_t cid, tL2CAP_CHNL_PRIORITY priority)); + MOCK_METHOD(bool, L2CA_SetChnlFlushability, (uint16_t cid, bool is_flushable)); + + MOCK_METHOD(uint16_t, L2CA_ConnectLECocReq, + (uint16_t psm, const RawAddress& bd_addr, tL2CAP_LE_CFG_INFO* p_cfg, + uint16_t sec_level)); + MOCK_METHOD(std::vector<uint16_t>, L2CA_ConnectCreditBasedReq, + (uint16_t psm, const RawAddress& bd_addr, tL2CAP_LE_CFG_INFO* p_cfg)); + MOCK_METHOD(bool, L2CA_ConnectCreditBasedRsp, + (const RawAddress& bd_addr, uint8_t id, std::vector<uint16_t>& accepted_lcids, + uint16_t result, tL2CAP_LE_CFG_INFO* p_cfg)); + MOCK_METHOD(uint16_t, L2CA_GetPeerLECocCredit, (const RawAddress& bd_addr, uint16_t lcid)); + + MOCK_METHOD(bool, L2CA_ReconfigCreditBasedConnsReq, + (const RawAddress& bd_addr, std::vector<uint16_t>& lcids, tL2CAP_LE_CFG_INFO* p_cfg)); + + MOCK_METHOD(bool, L2CA_UpdateBleConnParams, + (const RawAddress& bd_addr, uint16_t min_int, uint16_t max_int, uint16_t latency, + uint16_t timeout, uint16_t min_ce_len, uint16_t max_ce_len)); + MOCK_METHOD(void, L2CA_LockBleConnParamsForServiceDiscovery, + (const RawAddress& bd_addr, bool lock)); + MOCK_METHOD(void, L2CA_LockBleConnParamsForProfileConnection, + (const RawAddress& bd_addr, bool lock)); + MOCK_METHOD(tHCI_ROLE, L2CA_GetBleConnRole, (const RawAddress& bd_addr)); + MOCK_METHOD(bool, L2CA_SetLeGattTimeout, (const RawAddress& bd_addr, uint16_t idle_tout)); + MOCK_METHOD(bool, L2CA_MarkLeLinkAsActive, (const RawAddress& bd_addr)); + MOCK_METHOD(bool, L2CA_GetPeerLECocConfig, (uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg)); + MOCK_METHOD(uint16_t, L2CA_LeCreditDefault, ()); + MOCK_METHOD(uint16_t, L2CA_LeCreditThreshold, ()); + + MOCK_METHOD(void, L2CA_Consolidate, (const RawAddress& identity_addr, const RawAddress& rpa)); + + // Disconnect methods an active connection for both BR/EDR and BLE + MOCK_METHOD(bool, L2CA_DisconnectReq, (uint16_t cid)); + MOCK_METHOD(bool, L2CA_DisconnectLECocReq, (uint16_t cid)); + + // Data write methods for both BR/EDR and BLE + MOCK_METHOD(tL2CAP_DW_RESULT, L2CA_DataWrite, (uint16_t cid, BT_HDR* p_data)); + MOCK_METHOD(tL2CAP_DW_RESULT, L2CA_LECocDataWrite, (uint16_t cid, BT_HDR* p_data)); + + MOCK_METHOD(bool, L2CA_RegisterFixedChannel, (uint16_t fixed_cid, tL2CAP_FIXED_CHNL_REG* p_freg)); + MOCK_METHOD(bool, L2CA_ConnectFixedChnl, (uint16_t fixed_cid, const RawAddress& bd_addr)); + MOCK_METHOD(tL2CAP_DW_RESULT, L2CA_SendFixedChnlData, + (uint16_t fixed_cid, const RawAddress& bd_addr, BT_HDR* p_buf)); + MOCK_METHOD(bool, L2CA_RemoveFixedChnl, (uint16_t fixed_cid, const RawAddress& bd_addr)); + + // Media methods + MOCK_METHOD(void, L2CA_SetMediaStreamChannel, (uint16_t local_media_cid, bool status)); + MOCK_METHOD(bool, L2CA_isMediaChannel, (uint16_t handle, uint16_t channel_id, bool is_local_cid)); +}; + +void reset_interface(); +void set_interface(bluetooth::stack::l2cap::Interface* interface_); + +} // namespace l2cap +} // namespace stack +} // namespace testing +} // namespace bluetooth |