From 67341a31b787394558ce600e02abc4e3734655fd Mon Sep 17 00:00:00 2001 From: xiongweichao Date: Tue, 26 Apr 2022 17:24:51 +0800 Subject: [PATCH] Added esp_spp_vfs_unregister() to free memory allocated by esp_spp_vfs_register() --- .../bt/host/bluedroid/api/esp_spp_api.c | 21 ++++- .../bluedroid/api/include/api/esp_spp_api.h | 29 ++++++ .../btc/profile/std/include/btc_spp.h | 3 +- .../bluedroid/btc/profile/std/spp/btc_spp.c | 93 +++++++++++++++---- 4 files changed, 124 insertions(+), 22 deletions(-) diff --git a/components/bt/host/bluedroid/api/esp_spp_api.c b/components/bt/host/bluedroid/api/esp_spp_api.c index 4d0790d638..432d832abb 100644 --- a/components/bt/host/bluedroid/api/esp_spp_api.c +++ b/components/bt/host/bluedroid/api/esp_spp_api.c @@ -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 diff --git a/components/bt/host/bluedroid/api/include/api/esp_spp_api.h b/components/bt/host/bluedroid/api/include/api/esp_spp_api.h index be82764974..2433199193 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_spp_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_spp_api.h @@ -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 diff --git a/components/bt/host/bluedroid/btc/profile/std/include/btc_spp.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_spp.h index 2a2a3851e5..09f5246629 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_spp.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_spp.h @@ -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__ diff --git a/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c b/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c index 9092981b27..548278ff9f 100644 --- a/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c +++ b/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c @@ -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,30 +1576,65 @@ 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) { - if (!is_spp_init()) { - BTC_TRACE_ERROR("%s SPP have not been init\n", __func__); - return ESP_FAIL; - } + esp_spp_status_t ret = ESP_SPP_SUCCESS; + esp_spp_cb_param_t param; - esp_vfs_t vfs = { - .flags = ESP_VFS_FLAG_DEFAULT, - .write = spp_vfs_write, - .open = NULL, - .fstat = NULL, - .close = spp_vfs_close, - .read = spp_vfs_read, - .fcntl = NULL - }; + do { + if (!is_spp_init()) { + BTC_TRACE_ERROR("%s SPP have not been init\n", __func__); + ret = ESP_SPP_NEED_INIT; + break; + } - // 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; - } + esp_vfs_t vfs = { + .flags = ESP_VFS_FLAG_DEFAULT, + .write = spp_vfs_write, + .open = NULL, + .fstat = NULL, + .close = spp_vfs_close, + .read = spp_vfs_read, + .fcntl = NULL + }; - return ESP_OK; + // 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) { + ret = ESP_SPP_FAILURE; + break; + } + } while (0); + + param.vfs_register.status = ret; + btc_spp_cb_to_app(ESP_SPP_VFS_REGISTER_EVT, ¶m); +} + +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, ¶m); } #endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE