Skip to content

Commit

Permalink
Merge branch 'feature/switch_to_new_vfs_api_v5.4' into 'release/v5.4'
Browse files Browse the repository at this point in the history
feat(storage): switch to new VFS API (v5.4)

See merge request espressif/esp-idf!35282
  • Loading branch information
Jiang Jiang Jian committed Nov 29, 2024
2 parents 34a35b9 + def8b9b commit da9e87a
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern "C" {
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_uart_get_vfs(void);
const esp_vfs_fs_ops_t *esp_vfs_uart_get_vfs(void);

#ifdef __cplusplus
}
Expand Down
41 changes: 29 additions & 12 deletions components/esp_driver_uart/src/uart_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,38 +985,55 @@ static int uart_tcflush(int fd, int select)
}
#endif // CONFIG_VFS_SUPPORT_TERMIOS

static const esp_vfs_t uart_vfs = {
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &uart_write,
.open = &uart_open,
.fstat = &uart_fstat,
.close = &uart_close,
.read = &uart_read,
.fcntl = &uart_fcntl,
.fsync = &uart_fsync,
#ifdef CONFIG_VFS_SUPPORT_DIR
static const esp_vfs_dir_ops_t s_vfs_uart_dir = {
.access = &uart_access,
};
#endif // CONFIG_VFS_SUPPORT_DIR

#ifdef CONFIG_VFS_SUPPORT_SELECT
static const esp_vfs_select_ops_t s_vfs_uart_select = {
.start_select = &uart_start_select,
.end_select = &uart_end_select,
};
#endif // CONFIG_VFS_SUPPORT_SELECT

#ifdef CONFIG_VFS_SUPPORT_TERMIOS
static const esp_vfs_termios_ops_t s_vfs_uart_termios = {
.tcsetattr = &uart_tcsetattr,
.tcgetattr = &uart_tcgetattr,
.tcdrain = &uart_tcdrain,
.tcflush = &uart_tcflush,
};
#endif // CONFIG_VFS_SUPPORT_TERMIOS

static const esp_vfs_fs_ops_t s_vfs_uart = {
.write = &uart_write,
.open = &uart_open,
.fstat = &uart_fstat,
.close = &uart_close,
.read = &uart_read,
.fcntl = &uart_fcntl,
.fsync = &uart_fsync,
#ifdef CONFIG_VFS_SUPPORT_DIR
.dir = &s_vfs_uart_dir,
#endif // CONFIG_VFS_SUPPORT_DIR
#ifdef CONFIG_VFS_SUPPORT_SELECT
.select = &s_vfs_uart_select,
#endif // CONFIG_VFS_SUPPORT_SELECT
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
.termios = &s_vfs_uart_termios,
#endif // CONFIG_VFS_SUPPORT_TERMIOS
};

const esp_vfs_t *esp_vfs_uart_get_vfs(void)
const esp_vfs_fs_ops_t *esp_vfs_uart_get_vfs(void)
{
return &uart_vfs;
return &s_vfs_uart;
}

void uart_vfs_dev_register(void)
{
ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &uart_vfs, NULL));
ESP_ERROR_CHECK(esp_vfs_register_fs("/dev/uart", &s_vfs_uart, ESP_VFS_FLAG_STATIC, NULL));
}

int uart_vfs_dev_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern "C" {
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_usb_serial_jtag_get_vfs(void);
const esp_vfs_fs_ops_t *esp_vfs_usb_serial_jtag_get_vfs(void);

#ifdef __cplusplus
}
Expand Down
36 changes: 24 additions & 12 deletions components/esp_driver_usb_serial_jtag/src/usb_serial_jtag_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,36 +571,48 @@ void usb_serial_jtag_vfs_set_rx_line_endings(esp_line_endings_t mode)
s_ctx.rx_mode = mode;
}

static const esp_vfs_t usj_vfs = {
.flags = ESP_VFS_FLAG_DEFAULT,
#ifdef CONFIG_VFS_SUPPORT_SELECT
static const esp_vfs_select_ops_t s_vfs_jtag_select = {
.start_select = &usb_serial_jtag_start_select,
.end_select = &usb_serial_jtag_end_select,
};
#endif // CONFIG_VFS_SUPPORT_SELECT
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
static const esp_vfs_termios_ops_t s_vfs_jtag_termios = {
.tcsetattr = &usb_serial_jtag_tcsetattr,
.tcgetattr = &usb_serial_jtag_tcgetattr,
.tcdrain = &usb_serial_jtag_tcdrain,
.tcflush = &usb_serial_jtag_tcflush,
};
#endif // CONFIG_VFS_SUPPORT_TERMIOS

static const esp_vfs_fs_ops_t s_vfs_jtag = {
.write = &usb_serial_jtag_write,
.open = &usb_serial_jtag_open,
.fstat = &usb_serial_jtag_fstat,
.close = &usb_serial_jtag_close,
.read = &usb_serial_jtag_read,
.fcntl = &usb_serial_jtag_fcntl,
.fsync = &usb_serial_jtag_fsync,

#ifdef CONFIG_VFS_SUPPORT_SELECT
.start_select = &usb_serial_jtag_start_select,
.end_select = &usb_serial_jtag_end_select,
.select = &s_vfs_jtag_select,
#endif // CONFIG_VFS_SUPPORT_SELECT

#ifdef CONFIG_VFS_SUPPORT_TERMIOS
.tcsetattr = &usb_serial_jtag_tcsetattr,
.tcgetattr = &usb_serial_jtag_tcgetattr,
.tcdrain = &usb_serial_jtag_tcdrain,
.tcflush = &usb_serial_jtag_tcflush,
.termios = &s_vfs_jtag_termios,
#endif // CONFIG_VFS_SUPPORT_TERMIOS
};

const esp_vfs_t* esp_vfs_usb_serial_jtag_get_vfs(void)
const esp_vfs_fs_ops_t* esp_vfs_usb_serial_jtag_get_vfs(void)
{
return &usj_vfs;
return &s_vfs_jtag;
}

esp_err_t usb_serial_jtag_vfs_register(void)
{
// "/dev/usb_serial_jtag" unfortunately is too long for vfs
return esp_vfs_register("/dev/usbserjtag", &usj_vfs, NULL);
return esp_vfs_register_fs("/dev/usbserjtag", &s_vfs_jtag, ESP_VFS_FLAG_STATIC, NULL);
}

#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
Expand All @@ -615,7 +627,7 @@ ESP_SYSTEM_INIT_FN(init_vfs_usj, CORE, BIT(0), 111)
ESP_SYSTEM_INIT_FN(init_vfs_usj_sec, CORE, BIT(0), 112)
{
// "/dev/seccondary_usb_serial_jtag" unfortunately is too long for vfs
esp_vfs_register("/dev/secondary", &usj_vfs, NULL);
esp_vfs_register_fs("/dev/secondary", &s_vfs_jtag, ESP_VFS_FLAG_STATIC, NULL);
return ESP_OK;
}
#endif
Expand Down
34 changes: 19 additions & 15 deletions components/esp_netif/vfs_l2tap/esp_vfs_l2tap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -580,8 +580,25 @@ static esp_err_t l2tap_end_select(void *end_select_args)

return ret;
}

static const esp_vfs_select_ops_t s_vfs_l2tap_select = {
.start_select = &l2tap_start_select,
.end_select = &l2tap_end_select,
};
#endif //CONFIG_VFS_SUPPORT_SELECT

static const esp_vfs_fs_ops_t s_vfs_l2tap = {
.write = &l2tap_write,
.open = &l2tap_open,
.close = &l2tap_close,
.read = &l2tap_read,
.fcntl = &l2tap_fcntl,
.ioctl = &l2tap_ioctl,
#ifdef CONFIG_VFS_SUPPORT_SELECT
.select = &s_vfs_l2tap_select,
#endif // CONFIG_VFS_SUPPORT_SELECT
};

esp_err_t esp_vfs_l2tap_intf_register(l2tap_vfs_config_t *config)
{
l2tap_vfs_config_t def_config = L2TAP_VFS_CONFIG_DEFAULT();
Expand All @@ -593,20 +610,7 @@ esp_err_t esp_vfs_l2tap_intf_register(l2tap_vfs_config_t *config)

ESP_RETURN_ON_FALSE(!s_is_registered, ESP_ERR_INVALID_STATE, TAG, "vfs is already registered");
s_is_registered = true;
esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &l2tap_write,
.open = &l2tap_open,
.close = &l2tap_close,
.read = &l2tap_read,
.fcntl = &l2tap_fcntl,
.ioctl = &l2tap_ioctl,
#ifdef CONFIG_VFS_SUPPORT_SELECT
.start_select = &l2tap_start_select,
.end_select = &l2tap_end_select,
#endif // CONFIG_VFS_SUPPORT_SELECT
};
ESP_RETURN_ON_ERROR(esp_vfs_register(config->base_path, &vfs, NULL), TAG, "vfs register error");
ESP_RETURN_ON_ERROR(esp_vfs_register_fs(config->base_path, &s_vfs_l2tap, ESP_VFS_FLAG_STATIC, NULL), TAG, "vfs register error");

return ESP_OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern "C" {
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_cdcacm_get_vfs(void);
const esp_vfs_fs_ops_t *esp_vfs_cdcacm_get_vfs(void);

#ifdef __cplusplus
}
Expand Down
11 changes: 5 additions & 6 deletions components/esp_vfs_console/vfs_cdcacm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -300,8 +300,7 @@ void esp_vfs_dev_cdcacm_set_rx_line_endings(esp_line_endings_t mode)
s_rx_mode = mode;
}

static const esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_DEFAULT,
static const esp_vfs_fs_ops_t s_cdcacm_vfs = {
.write = &cdcacm_write,
.open = &cdcacm_open,
.fstat = &cdcacm_fstat,
Expand All @@ -311,12 +310,12 @@ static const esp_vfs_t vfs = {
.fsync = &cdcacm_fsync
};

const esp_vfs_t *esp_vfs_cdcacm_get_vfs(void)
const esp_vfs_fs_ops_t *esp_vfs_cdcacm_get_vfs(void)
{
return &vfs;
return &s_cdcacm_vfs;
}

esp_err_t esp_vfs_dev_cdcacm_register(void)
{
return esp_vfs_register("/dev/cdcacm", &vfs, NULL);
return esp_vfs_register_fs("/dev/cdcacm", &s_cdcacm_vfs, ESP_VFS_FLAG_STATIC, NULL);
}
53 changes: 37 additions & 16 deletions components/esp_vfs_console/vfs_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "sdkconfig.h"
#include "esp_private/startup_internal.h"
#include "esp_vfs_null.h"
#include "esp_private/nullfs.h"

#define STRINGIFY(s) STRINGIFY2(s)
#define STRINGIFY2(s) #s
Expand All @@ -37,10 +38,10 @@ typedef struct {

// Secondary register part.
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
const static esp_vfs_t *secondary_vfs = NULL;
const static esp_vfs_fs_ops_t *secondary_vfs = NULL;
#endif // Secondary part

const static esp_vfs_t *primary_vfs = NULL;
const static esp_vfs_fs_ops_t *primary_vfs = NULL;

static vfs_console_context_t vfs_console = {0};

Expand Down Expand Up @@ -121,8 +122,8 @@ static esp_err_t console_start_select(int nfds, fd_set *readfds, fd_set *writefd
esp_vfs_select_sem_t select_sem, void **end_select_args)
{
// start_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
if (primary_vfs->start_select) {
return primary_vfs->start_select(nfds, readfds, writefds, exceptfds, select_sem, end_select_args);
if (primary_vfs->select->start_select) {
return primary_vfs->select->start_select(nfds, readfds, writefds, exceptfds, select_sem, end_select_args);
}

return ESP_ERR_NOT_SUPPORTED;
Expand All @@ -131,8 +132,8 @@ static esp_err_t console_start_select(int nfds, fd_set *readfds, fd_set *writefd
esp_err_t console_end_select(void *end_select_args)
{
// end_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
if (primary_vfs->end_select) {
return primary_vfs->end_select(end_select_args);
if (primary_vfs->select->end_select) {
return primary_vfs->select->end_select(end_select_args);
}

return ESP_ERR_NOT_SUPPORTED;
Expand Down Expand Up @@ -163,33 +164,53 @@ int console_tcflush(int fd, int select)
}
#endif // CONFIG_VFS_SUPPORT_TERMIOS

static const esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &console_write,
.open = &console_open,
.fstat = &console_fstat,
.close = &console_close,
.read = &console_read,
.fcntl = &console_fcntl,
.fsync = &console_fsync,
#ifdef CONFIG_VFS_SUPPORT_DIR
static const esp_vfs_dir_ops_t s_vfs_console_dir = {
.access = &console_access,
};
#endif // CONFIG_VFS_SUPPORT_DIR

#ifdef CONFIG_VFS_SUPPORT_SELECT
static const esp_vfs_select_ops_t s_vfs_console_select = {
.start_select = &console_start_select,
.end_select = &console_end_select,
};
#endif // CONFIG_VFS_SUPPORT_SELECT

#ifdef CONFIG_VFS_SUPPORT_TERMIOS
static const esp_vfs_termios_ops_t s_vfs_console_termios = {
.tcsetattr = &console_tcsetattr,
.tcgetattr = &console_tcgetattr,
.tcdrain = &console_tcdrain,
.tcflush = &console_tcflush,
};
#endif // CONFIG_VFS_SUPPORT_TERMIOS

static const esp_vfs_fs_ops_t s_vfs_console = {
.write = &console_write,
.open = &console_open,
.fstat = &console_fstat,
.close = &console_close,
.read = &console_read,
.fcntl = &console_fcntl,
.fsync = &console_fsync,

#ifdef CONFIG_VFS_SUPPORT_DIR
.dir = &s_vfs_console_dir,
#endif // CONFIG_VFS_SUPPORT_DIR

#ifdef CONFIG_VFS_SUPPORT_SELECT
.select = &s_vfs_console_select,
#endif // CONFIG_VFS_SUPPORT_SELECT

#ifdef CONFIG_VFS_SUPPORT_TERMIOS
.termios = &s_vfs_console_termios,
#endif // CONFIG_VFS_SUPPORT_TERMIOS
};

static esp_err_t esp_vfs_dev_console_register(void)
{
return esp_vfs_register(ESP_VFS_DEV_CONSOLE, &vfs, NULL);
return esp_vfs_register_fs(ESP_VFS_DEV_CONSOLE, &s_vfs_console, ESP_VFS_FLAG_STATIC, NULL);
}

esp_err_t esp_vfs_console_register(void)
Expand Down
Loading

0 comments on commit da9e87a

Please sign in to comment.