Skip to content

Commit

Permalink
Merge branch 'bugfix/spp_add_ertm_option_master' into 'master'
Browse files Browse the repository at this point in the history
bt: Added esp_spp_enhanced_init() API to indicate whether to enable L2CAP ERTM

Closes BT-2411

See merge request espressif/esp-idf!21126
  • Loading branch information
wmy-espressif committed Nov 29, 2022
2 parents f2dfe2b + c497d7d commit 4b2ac6c
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 74 deletions.
9 changes: 0 additions & 9 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ config BT_SPP_ENABLED
help
This enables the Serial Port Profile

config BT_SPP_SEND_BUF_DEFAULT
int "SPP default send buffer size"
depends on BT_SPP_ENABLED
range 100 10000
default 4000
help
Sets the default send buffer size for new SPP channels. Setting a smaller
default SNDBUF size can save some memory, but may decrease performance.

config BT_L2CAP_ENABLED
bool "BT L2CAP"
depends on BT_CLASSIC_ENABLED
Expand Down
22 changes: 21 additions & 1 deletion components/bt/host/bluedroid/api/esp_spp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,36 @@ esp_err_t esp_spp_register_callback(esp_spp_cb_t callback)


esp_err_t esp_spp_init(esp_spp_mode_t mode)
{
esp_spp_cfg_t bt_spp_cfg = {
.mode = mode,
.enable_l2cap_ertm = true,
.tx_buffer_size = ESP_SPP_MAX_TX_BUFFER_SIZE,
};

return esp_spp_enhanced_init(&bt_spp_cfg);
}

esp_err_t esp_spp_enhanced_init(const esp_spp_cfg_t *cfg)
{
btc_msg_t msg;
btc_spp_args_t arg;
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

if (cfg->mode == ESP_SPP_MODE_VFS && (cfg->tx_buffer_size < ESP_SPP_MIN_TX_BUFFER_SIZE ||
cfg->tx_buffer_size > ESP_SPP_MAX_TX_BUFFER_SIZE)) {
LOG_WARN("Invalid tx buffer size");
return ESP_ERR_INVALID_ARG;
}

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_SPP;
msg.act = BTC_SPP_ACT_INIT;

arg.init.mode = mode;
arg.init.mode = cfg->mode;
arg.init.enable_l2cap_ertm = cfg->enable_l2cap_ertm;
arg.init.tx_buffer_size = cfg->tx_buffer_size;

return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

Expand Down
80 changes: 59 additions & 21 deletions components/bt/host/bluedroid/api/include/api/esp_spp_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
extern "C" {
#endif

typedef enum {
ESP_SPP_SUCCESS = 0, /*!< Successful operation. */
ESP_SPP_FAILURE, /*!< Generic failure. */
ESP_SPP_BUSY, /*!< Temporarily can not handle this request. */
ESP_SPP_NO_DATA, /*!< No data */
ESP_SPP_NO_RESOURCE, /*!< No more resource */
ESP_SPP_NEED_INIT, /*!< SPP module shall init first */
ESP_SPP_NEED_DEINIT, /*!< SPP module shall deinit first */
ESP_SPP_NO_CONNECTION, /*!< Connection may have been closed */
ESP_SPP_NO_SERVER, /*!< No SPP server */
} esp_spp_status_t;
#define ESP_SPP_MAX_MTU (3*330) /*!< SPP max MTU */
#define ESP_SPP_MAX_SCN 31 /*!< SPP max SCN */
#define ESP_SPP_MIN_TX_BUFFER_SIZE 100 /*!< SPP min tx buffer */
#define ESP_SPP_MAX_TX_BUFFER_SIZE (ESP_SPP_MAX_MTU * 10) /*!< SPP max tx buffer size */

/**
* @brief SPP default configuration
*/
#define BT_SPP_DEFAULT_CONFIG() { \
.mode = ESP_SPP_MODE_VFS, \
.enable_l2cap_ertm = true, \
.tx_buffer_size = ESP_SPP_MAX_TX_BUFFER_SIZE, \
}

/* Security Setting Mask
Use these three mask mode:
Expand All @@ -41,6 +43,18 @@ Use these three mask mode:
#define ESP_SPP_SEC_IN_16_DIGITS 0x4000 /*!< Min 16 digit for pin code relate to BTA_SEC_IN_16_DIGITS in bta/bta_api.h*/
typedef uint16_t esp_spp_sec_t;

typedef enum {
ESP_SPP_SUCCESS = 0, /*!< Successful operation. */
ESP_SPP_FAILURE, /*!< Generic failure. */
ESP_SPP_BUSY, /*!< Temporarily can not handle this request. */
ESP_SPP_NO_DATA, /*!< No data */
ESP_SPP_NO_RESOURCE, /*!< No more resource */
ESP_SPP_NEED_INIT, /*!< SPP module shall init first */
ESP_SPP_NEED_DEINIT, /*!< SPP module shall deinit first */
ESP_SPP_NO_CONNECTION, /*!< Connection may have been closed */
ESP_SPP_NO_SERVER, /*!< No SPP server */
} esp_spp_status_t;

typedef enum {
ESP_SPP_ROLE_MASTER = 0, /*!< Role: master */
ESP_SPP_ROLE_SLAVE = 1, /*!< Role: slave */
Expand All @@ -51,8 +65,15 @@ typedef enum {
ESP_SPP_MODE_VFS = 1, /*!< Use VFS to write/read data */
} esp_spp_mode_t;

#define ESP_SPP_MAX_MTU (3*330) /*!< SPP max MTU */
#define ESP_SPP_MAX_SCN 31 /*!< SPP max SCN */
/**
* @brief SPP configuration parameters
*/
typedef struct {
esp_spp_mode_t mode; /*!< Choose the mode of SPP, ESP_SPP_MODE_CB or ESP_SPP_MODE_VFS. */
bool enable_l2cap_ertm; /*!< Enable/disable Logical Link Control and Adaptation Layer Protocol enhanced retransmission mode. */
uint16_t tx_buffer_size; /*!< Tx buffer size for a new SPP channel. A smaller setting can save memory, but may incur a decrease in throughput. Only for ESP_SPP_MODE_VFS mode. */
} esp_spp_cfg_t;

/**
* @brief SPP callback function events
*/
Expand Down Expand Up @@ -221,14 +242,31 @@ esp_err_t esp_spp_register_callback(esp_spp_cb_t callback);
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_init(esp_spp_mode_t mode);
esp_err_t esp_spp_init(esp_spp_mode_t mode) __attribute__((deprecated("Please use esp_spp_enhanced_init")));


/**
* @brief This function is called to init SPP module.
* When the operation is completed, the callback function will be called with ESP_SPP_INIT_EVT.
* This function should be called after esp_bluedroid_enable() completes successfully.
*
* @param[in] cfg: SPP configuration.
*
* @note The member variable enable_l2cap_etrm in esp_spp_cfg_t can affect all L2CAP channel
* configurations of the upper layer RFCOMM protocol.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_enhanced_init(const esp_spp_cfg_t *cfg);

/**
* @brief This function is called to uninit SPP module.
* The operation will close all active SPP connection first, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback function will be called with ESP_SPP_UNINIT_EVT.
* This function should be called after esp_spp_init() completes successfully.
* This function should be called after esp_spp_init()/esp_spp_enhanced_init() completes successfully.
*
* @return
* - ESP_OK: success
Expand All @@ -240,7 +278,7 @@ esp_err_t esp_spp_deinit(void);
/**
* @brief This function is called to performs service discovery for the services provided by the given peer device.
* When the operation is completed, the callback function will be called with ESP_SPP_DISCOVERY_COMP_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
Expand All @@ -254,7 +292,7 @@ esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr);
* @brief This function makes an SPP connection to a remote BD Address.
* When the connection is initiated or failed to initiate, the callback is called with ESP_SPP_CL_INIT_EVT.
* When the connection is established or failed, the callback is called with ESP_SPP_OPEN_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @param[in] sec_mask: Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only.
* @param[in] role: Master or slave.
Expand All @@ -270,7 +308,7 @@ esp_err_t esp_spp_connect(esp_spp_sec_t sec_mask, esp_spp_role_t role, uint8_t r
/**
* @brief This function closes an SPP connection.
* When the operation is completed, the callback function will be called with ESP_SPP_CLOSE_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @param[in] handle: The connection handle.
*
Expand All @@ -285,7 +323,7 @@ esp_err_t esp_spp_disconnect(uint32_t handle);
* SPP connection request from a remote Bluetooth device.
* When the server is started successfully, the callback is called with ESP_SPP_START_EVT.
* When the connection is established, the callback is called with ESP_SPP_SRV_OPEN_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @param[in] sec_mask: Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only.
* @param[in] role: Master or slave.
Expand All @@ -304,7 +342,7 @@ esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask, esp_spp_role_t role, uint8_t
* The operation will close all active SPP connection first, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback is called with ESP_SPP_SRV_STOP_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @return
* - ESP_OK: success
Expand All @@ -318,7 +356,7 @@ esp_err_t esp_spp_stop_srv(void);
* The operation will close all active SPP connection first on the specific SPP server, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback is called with ESP_SPP_SRV_STOP_EVT.
* This function must be called after esp_spp_init() successful and before esp_spp_deinit().
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @param[in] scn: Server channel number.
*
Expand Down
13 changes: 13 additions & 0 deletions components/bt/host/bluedroid/bta/include/bta/bta_jv_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,19 @@ extern tBTA_JV_STATUS BTA_JvL2capWriteFixed(UINT16 channel, BD_ADDR *addr, UINT3
#endif /* BTA_JV_L2CAP_INCLUDED */

#if BTA_JV_RFCOMM_INCLUDED
/*******************************************************************************
**
** Function BTA_JvRfcommConfig
**
** Description This function is to configure RFCOMM.
**
**
** Returns BTA_JV_SUCCESS, if the request is being processed.
** BTA_JV_FAILURE, otherwise.
**
*******************************************************************************/
extern tBTA_JV_STATUS BTA_JvRfcommConfig(BOOLEAN enable_l2cap_ertm);

/*******************************************************************************
**
** Function BTA_JvRfcommConnect
Expand Down
16 changes: 16 additions & 0 deletions components/bt/host/bluedroid/bta/jv/bta_jv_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,22 @@ static void bta_jv_port_event_cl_cback(UINT32 code, UINT16 port_handle)
}
}

/*******************************************************************************
**
** Function bta_jv_rfcomm_config
**
** Description Configure RFCOMM
**
** Returns void
**
*******************************************************************************/
void bta_jv_rfcomm_config(tBTA_JV_MSG *p_data)
{
APPL_TRACE_DEBUG("%s enable_l2cap_ertm:%d", __func__, p_data->rfcomm_config.enable_l2cap_ertm);

PORT_SetL2capErtm(p_data->rfcomm_config.enable_l2cap_ertm);
}

/*******************************************************************************
**
** Function bta_jv_rfcomm_connect
Expand Down
27 changes: 27 additions & 0 deletions components/bt/host/bluedroid/bta/jv/bta_jv_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,33 @@ tBTA_JV_STATUS BTA_JvL2capWriteFixed(UINT16 channel, BD_ADDR *addr, UINT32 req_i
#endif /* BTA_JV_L2CAP_INCLUDED */

#if BTA_JV_RFCOMM_INCLUDED
/*******************************************************************************
**
** Function BTA_JvRfcommConfig
**
** Description This function is to configure RFCOMM.
**
** Returns BTA_JV_SUCCESS, if the request is being processed.
** BTA_JV_FAILURE, otherwise.
**
*******************************************************************************/
tBTA_JV_STATUS BTA_JvRfcommConfig(BOOLEAN enable_l2cap_ertm)
{
tBTA_JV_STATUS status = BTA_JV_FAILURE;
tBTA_JV_API_RFCOMM_CONFIG *p_msg;

APPL_TRACE_API( "%s", __func__);

if ((p_msg = (tBTA_JV_API_RFCOMM_CONFIG *)osi_malloc(sizeof(tBTA_JV_API_RFCOMM_CONFIG))) != NULL) {
p_msg->hdr.event = BTA_JV_API_RFCOMM_CONFIG_EVT;
p_msg->enable_l2cap_ertm = enable_l2cap_ertm;
bta_sys_sendmsg(p_msg);
status = BTA_JV_SUCCESS;
}

return (status);
}

/*******************************************************************************
**
** Function BTA_JvRfcommConnect
Expand Down
3 changes: 2 additions & 1 deletion components/bt/host/bluedroid/bta/jv/bta_jv_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ const tBTA_JV_ACTION bta_jv_action[] = {
bta_jv_l2cap_write, /* BTA_JV_API_L2CAP_WRITE_EVT */
#endif /* BTA_JV_L2CAP_INCLUDED */
#if BTA_JV_RFCOMM_INCLUDED
bta_jv_rfcomm_config, /* BTA_JV_API_RFCOMM_CONFIG_EVT */
bta_jv_rfcomm_connect, /* BTA_JV_API_RFCOMM_CONNECT_EVT */
bta_jv_rfcomm_close, /* BTA_JV_API_RFCOMM_CLOSE_EVT */
bta_jv_rfcomm_start_server, /* BTA_JV_API_RFCOMM_START_SERVER_EVT */
bta_jv_rfcomm_stop_server, /* BTA_JV_API_RFCOMM_STOP_SERVER_EVT */
bta_jv_rfcomm_read, /* BTA_JV_API_RFCOMM_READ_EVT */
bta_jv_rfcomm_write, /* BTA_JV_API_RFCOMM_WRITE_EVT */
bta_jv_rfcomm_flow_control, /* BTA_JV_API_RFCOMM_FLOW_CONTROL_EVT */
#endif /* BTA_JV_RFCOMM_INCLUDED */
#endif /* BTA_JV_RFCOMM_INCLUDED */
bta_jv_set_pm_profile, /* BTA_JV_API_SET_PM_PROFILE_EVT */
bta_jv_change_pm_state, /* BTA_JV_API_PM_STATE_CHANGE_EVT */
#if BTA_JV_L2CAP_INCLUDED
Expand Down
11 changes: 10 additions & 1 deletion components/bt/host/bluedroid/bta/jv/include/bta_jv_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum {
BTA_JV_API_L2CAP_WRITE_EVT,
#endif /* BTA_JV_L2CAP_INCLUDED */
#if BTA_JV_RFCOMM_INCLUDED
BTA_JV_API_RFCOMM_CONFIG_EVT,
BTA_JV_API_RFCOMM_CONNECT_EVT,
BTA_JV_API_RFCOMM_CLOSE_EVT,
BTA_JV_API_RFCOMM_START_SERVER_EVT,
Expand All @@ -83,7 +84,7 @@ enum {

/* data type for BTA_JV_API_ENABLE_EVT */
typedef struct {
BT_HDR hdr;
BT_HDR hdr;
tBTA_JV_DM_CBACK *p_cback;
} tBTA_JV_API_ENABLE;

Expand Down Expand Up @@ -257,6 +258,12 @@ typedef struct {
#endif /* BTA_JV_L2CAP_INCLUDED */

#if BTA_JV_RFCOMM_INCLUDED
/* data type for BTA_JV_API_RFCOMM_CONFIG_EVT */
typedef struct {
BT_HDR hdr;
BOOLEAN enable_l2cap_ertm;
} tBTA_JV_API_RFCOMM_CONFIG;

/* data type for BTA_JV_API_RFCOMM_CONNECT_EVT */
typedef struct {
BT_HDR hdr;
Expand Down Expand Up @@ -392,6 +399,7 @@ typedef union {
tBTA_JV_API_L2CAP_WRITE_FIXED l2cap_write_fixed;
#endif /* BTA_JV_L2CAP_INCLUDED */
#if BTA_JV_RFCOMM_INCLUDED
tBTA_JV_API_RFCOMM_CONFIG rfcomm_config;
tBTA_JV_API_RFCOMM_CONNECT rfcomm_connect;
tBTA_JV_API_RFCOMM_READ rfcomm_read;
tBTA_JV_API_RFCOMM_WRITE rfcomm_write;
Expand Down Expand Up @@ -463,6 +471,7 @@ extern void bta_jv_l2cap_read (tBTA_JV_MSG *p_data);
extern void bta_jv_l2cap_write (tBTA_JV_MSG *p_data);
#endif /* BTA_JV_L2CAP_INCLUDED */
#if BTA_JV_RFCOMM_INCLUDED
extern void bta_jv_rfcomm_config (tBTA_JV_MSG *p_data);
extern void bta_jv_rfcomm_connect (tBTA_JV_MSG *p_data);
extern void bta_jv_rfcomm_close (tBTA_JV_MSG *p_data);
extern void bta_jv_rfcomm_start_server (tBTA_JV_MSG *p_data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typedef union {
//BTC_SPP_ACT_INIT
struct init_arg {
esp_spp_mode_t mode;
bool enable_l2cap_ertm;
UINT16 tx_buffer_size;
} init;
//BTC_SPP_ACT_UNINIT
struct uninit_arg {
Expand Down
Loading

0 comments on commit 4b2ac6c

Please sign in to comment.