Added esp_spp_vfs_unregister() to free memory allocated by esp_spp_vfs_register()

pull/11369/head
xiongweichao 2022-04-26 17:24:51 +08:00 zatwierdzone przez BOT
rodzic 5d4edd8218
commit 67341a31b7
4 zmienionych plików z 124 dodań i 22 usunięć

Wyświetl plik

@ -205,9 +205,28 @@ esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data)
esp_err_t esp_spp_vfs_register(void)
{
btc_msg_t msg;
btc_spp_args_t arg;
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
return btc_spp_vfs_register();
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_SPP;
msg.act = BTC_SPP_ACT_VFS_REGISTER;
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
esp_err_t esp_spp_vfs_unregister(void)
{
btc_msg_t msg;
btc_spp_args_t arg;
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_SPP;
msg.act = BTC_SPP_ACT_VFS_UNREGISTER;
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
#endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE

Wyświetl plik

@ -77,6 +77,8 @@ typedef enum {
ESP_SPP_WRITE_EVT = 33, /*!< When SPP write operation completes, the event comes, only for ESP_SPP_MODE_CB */
ESP_SPP_SRV_OPEN_EVT = 34, /*!< When SPP Server connection open, the event comes */
ESP_SPP_SRV_STOP_EVT = 35, /*!< When SPP server stopped, the event comes */
ESP_SPP_VFS_REGISTER_EVT = 36, /*!< When SPP VFS register, the event comes */
ESP_SPP_VFS_UNREGISTER_EVT = 37, /*!< When SPP VFS unregister, the event comes */
} esp_spp_cb_event_t;
@ -195,6 +197,20 @@ typedef union {
uint32_t handle; /*!< The connection handle */
bool cong; /*!< TRUE, congested. FALSE, uncongested */
} cong; /*!< SPP callback param of ESP_SPP_CONG_EVT */
/**
* @brief ESP_SPP_VFS_REGISTER_EVT
*/
struct spp_vfs_register_evt_param {
esp_spp_status_t status; /*!< status */
} vfs_register; /*!< SPP callback param of ESP_SPP_VFS_REGISTER_EVT */
/**
* @brief ESP_SPP_VFS_UNREGISTER_EVT
*/
struct spp_vfs_unregister_evt_param {
esp_spp_status_t status; /*!< status */
} vfs_unregister; /*!< SPP callback param of ESP_SPP_VFS_UNREGISTER_EVT */
} esp_spp_cb_param_t; /*!< SPP callback parameter union type */
/**
@ -358,6 +374,8 @@ esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data);
/**
* @brief This function is used to register VFS.
* For now, SPP only supports write, read and close.
* When the operation is completed, the callback function will be called with ESP_SPP_VFS_REGISTER_EVT.
* This function must be called after esp_spp_init()/esp_spp_enhanced_init() successful and before esp_spp_deinit().
*
* @return
* - ESP_OK: success
@ -365,6 +383,17 @@ esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data);
*/
esp_err_t esp_spp_vfs_register(void);
/**
* @brief This function is used to unregister VFS.
* When the operation is completed, the callback function will be called with ESP_SPP_VFS_UNREGISTER_EVT.
* This function must be called after esp_spp_vfs_register() successful and before esp_spp_deinit().
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_vfs_unregister(void);
#ifdef __cplusplus
}
#endif

Wyświetl plik

@ -39,6 +39,8 @@ typedef enum {
BTC_SPP_ACT_START_SRV,
BTC_SPP_ACT_STOP_SRV,
BTC_SPP_ACT_WRITE,
BTC_SPP_ACT_VFS_REGISTER,
BTC_SPP_ACT_VFS_UNREGISTER,
} btc_spp_act_t;
/* btc_spp_args_t */
@ -96,6 +98,5 @@ void btc_spp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
void btc_spp_arg_deep_free(btc_msg_t *msg);
esp_err_t spp_send_data_to_btc(uint32_t handle, int len, uint8_t *p_data, esp_spp_mode_t spp_mode);
esp_err_t btc_spp_vfs_register(void);
#endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE
#endif ///__BTC_SPP_H__

Wyświetl plik

@ -85,6 +85,9 @@ static spp_local_param_t *spp_local_param_ptr;
#define spp_local_param (*spp_local_param_ptr)
#endif
static void btc_spp_vfs_register(void);
static void btc_spp_vfs_unregister(void);
static void spp_osi_free(void *p)
{
osi_free(p);
@ -523,6 +526,9 @@ static void btc_spp_init(btc_spp_args_t *arg)
ret = ESP_SPP_NO_RESOURCE;
break;
}
if (arg->init.mode == ESP_SPP_MODE_VFS) {
spp_local_param.spp_vfs_id = -1;
}
spp_local_param.spp_mode = arg->init.mode;
spp_local_param.spp_slot_id = 0;
BTA_JvEnable((tBTA_JV_DM_CBACK *)btc_spp_dm_inter_cb);
@ -956,6 +962,12 @@ void btc_spp_call_handler(btc_msg_t *msg)
case BTC_SPP_ACT_WRITE:
btc_spp_write(arg);
break;
case BTC_SPP_ACT_VFS_REGISTER:
btc_spp_vfs_register();
break;
case BTC_SPP_ACT_VFS_UNREGISTER:
btc_spp_vfs_unregister();
break;
default:
BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act);
break;
@ -1256,6 +1268,12 @@ void btc_spp_cb_handler(btc_msg_t *msg)
vEventGroupDelete(spp_local_param.tx_event_group);
spp_local_param.tx_event_group = NULL;
}
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
if (spp_local_param.spp_vfs_id != -1) {
esp_vfs_unregister_with_id(spp_local_param.spp_vfs_id);
spp_local_param.spp_vfs_id = -1;
}
}
#if SPP_DYNAMIC_MEMORY == TRUE
osi_free(spp_local_param_ptr);
spp_local_param_ptr = NULL;
@ -1558,11 +1576,16 @@ static ssize_t spp_vfs_read(int fd, void * dst, size_t size)
return item_size;
}
esp_err_t btc_spp_vfs_register(void)
static void btc_spp_vfs_register(void)
{
esp_spp_status_t ret = ESP_SPP_SUCCESS;
esp_spp_cb_param_t param;
do {
if (!is_spp_init()) {
BTC_TRACE_ERROR("%s SPP have not been init\n", __func__);
return ESP_FAIL;
ret = ESP_SPP_NEED_INIT;
break;
}
esp_vfs_t vfs = {
@ -1578,10 +1601,40 @@ esp_err_t btc_spp_vfs_register(void)
// No FD range is registered here: spp_vfs_id is used to register/unregister
// file descriptors
if (esp_vfs_register_with_id(&vfs, NULL, &spp_local_param.spp_vfs_id) != ESP_OK) {
return ESP_FAIL;
ret = ESP_SPP_FAILURE;
break;
}
} while (0);
param.vfs_register.status = ret;
btc_spp_cb_to_app(ESP_SPP_VFS_REGISTER_EVT, &param);
}
return ESP_OK;
static void btc_spp_vfs_unregister(void)
{
esp_spp_status_t ret = ESP_SPP_SUCCESS;
esp_spp_cb_param_t param;
do {
if (!is_spp_init()) {
BTC_TRACE_ERROR("%s SPP have not been init\n", __func__);
ret = ESP_SPP_NEED_INIT;
break;
}
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
if (spp_local_param.spp_vfs_id != -1) {
if (esp_vfs_unregister_with_id(spp_local_param.spp_vfs_id) != ESP_OK) {
ret = ESP_SPP_FAILURE;
break;
}
spp_local_param.spp_vfs_id = -1;
}
}
} while (0);
param.vfs_unregister.status = ret;
btc_spp_cb_to_app(ESP_SPP_VFS_UNREGISTER_EVT, &param);
}
#endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE