Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 28491b41 authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

stack::l2cap Introduce l2cap interface and mocks am: 1ef1ee91

parents 32c50c63 1ef1ee91
Loading
Loading
Loading
Loading
+110 −143
Original line number Diff line number Diff line
@@ -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
+925 −0

File added.

Preview size limit exceeded, changes collapsed.

+171 −0
Original line number Diff line number Diff line
/*
 * 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
+257 −0

File added.

Preview size limit exceeded, changes collapsed.

+7 −0
Original line number Diff line number Diff line
@@ -276,6 +276,13 @@ filegroup {
    ],
}

filegroup {
    name: "TestMockStackL2capInterface",
    srcs: [
        "mock/mock_stack_l2cap_interface.cc",
    ],
}

filegroup {
    name: "TestMockStackRfcomm",
    srcs: [
Loading