2021-10-21 00:46:24 -04:00
|
|
|
/*
|
2024-01-02 05:08:35 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-10-21 00:46:24 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2024-01-23 06:09:57 -05:00
|
|
|
#include <fcntl.h>
|
2023-11-24 01:56:08 -05:00
|
|
|
#include "esp_err.h"
|
2021-10-21 00:46:24 -04:00
|
|
|
#include "esp_rom_sys.h"
|
|
|
|
#include "esp_vfs_cdcacm.h"
|
2024-01-23 06:09:57 -05:00
|
|
|
#include "esp_private/esp_vfs_cdcacm.h"
|
|
|
|
#include "driver/esp_private/usb_serial_jtag_vfs.h"
|
|
|
|
#include "driver/esp_private/uart_vfs.h"
|
2021-10-21 00:46:24 -04:00
|
|
|
#include "esp_private/usb_console.h"
|
2023-11-24 01:56:08 -05:00
|
|
|
#include "esp_vfs_console.h"
|
2021-10-21 00:46:24 -04:00
|
|
|
#include "sdkconfig.h"
|
2023-11-24 01:56:08 -05:00
|
|
|
#include "esp_private/startup_internal.h"
|
2021-10-21 00:46:24 -04:00
|
|
|
|
|
|
|
#define STRINGIFY(s) STRINGIFY2(s)
|
|
|
|
#define STRINGIFY2(s) #s
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is to concentrate all the vfs(UART, USB_SERIAL_JTAG, CDCACM) console into one single file.
|
2023-11-24 01:56:08 -05:00
|
|
|
* Get the vfs information from their component (i.e. uart_vfs.c),
|
2021-10-21 00:46:24 -04:00
|
|
|
* which can help us to output some string to two different ports(i.e both through uart and usb_serial_jtag).
|
|
|
|
* Usually, we set a port as primary and another as secondary. For primary, it is used for all the features supported by each vfs implementation,
|
|
|
|
* while the secondary is only used for output.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fd_primary;
|
|
|
|
int fd_secondary;
|
|
|
|
} vfs_console_context_t;
|
|
|
|
|
|
|
|
#if CONFIG_VFS_SUPPORT_IO
|
|
|
|
|
|
|
|
// Secondary register part.
|
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
2023-11-24 01:56:08 -05:00
|
|
|
const static esp_vfs_t *secondary_vfs = NULL;
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif // Secondary part
|
|
|
|
|
2023-11-24 01:56:08 -05:00
|
|
|
const static esp_vfs_t *primary_vfs = NULL;
|
2021-10-21 00:46:24 -04:00
|
|
|
|
2024-01-23 06:09:57 -05:00
|
|
|
static vfs_console_context_t vfs_console = {0};
|
2021-10-21 00:46:24 -04:00
|
|
|
|
|
|
|
int console_open(const char * path, int flags, int mode)
|
|
|
|
{
|
|
|
|
// Primary port open
|
|
|
|
#if CONFIG_ESP_CONSOLE_UART
|
2024-01-23 06:09:57 -05:00
|
|
|
vfs_console.fd_primary = open("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
|
2021-10-21 00:46:24 -04:00
|
|
|
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
2024-01-23 06:09:57 -05:00
|
|
|
vfs_console.fd_primary = open("/dev/usbserjtag", flags, mode);
|
2021-10-21 00:46:24 -04:00
|
|
|
#elif CONFIG_ESP_CONSOLE_USB_CDC
|
2024-01-23 06:09:57 -05:00
|
|
|
vfs_console.fd_primary = open("/dev/cdcacm", flags, mode);
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Secondary port open
|
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
2024-01-23 06:09:57 -05:00
|
|
|
vfs_console.fd_secondary = open("/dev/secondary", flags, mode);
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t console_write(int fd, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
|
2024-01-23 06:09:57 -05:00
|
|
|
write(vfs_console.fd_primary, data, size);
|
2021-10-21 00:46:24 -04:00
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
2024-01-23 06:09:57 -05:00
|
|
|
write(vfs_console.fd_secondary, data, size);
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int console_fstat(int fd, struct stat * st)
|
|
|
|
{
|
2024-05-24 06:17:00 -04:00
|
|
|
return fstat(vfs_console.fd_primary, st);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_close(int fd)
|
|
|
|
{
|
|
|
|
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
|
2024-01-23 06:09:57 -05:00
|
|
|
close(vfs_console.fd_primary);
|
2021-10-21 00:46:24 -04:00
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
2024-01-23 06:09:57 -05:00
|
|
|
close(vfs_console.fd_secondary);
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t console_read(int fd, void * dst, size_t size)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return read(vfs_console.fd_primary, dst, size);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_fcntl(int fd, int cmd, int arg)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return fcntl(vfs_console.fd_primary, cmd, arg);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_fsync(int fd)
|
|
|
|
{
|
2024-04-11 06:38:35 -04:00
|
|
|
const int ret_val = fsync(vfs_console.fd_primary);
|
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
|
|
|
(void)fsync(vfs_console.fd_secondary);
|
|
|
|
#endif
|
|
|
|
return ret_val;
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_VFS_SUPPORT_DIR
|
|
|
|
int console_access(const char *path, int amode)
|
|
|
|
{
|
|
|
|
// currently only UART support DIR.
|
2024-01-23 06:09:57 -05:00
|
|
|
return access("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), amode);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
#endif // CONFIG_VFS_SUPPORT_DIR
|
|
|
|
|
|
|
|
#ifdef CONFIG_VFS_SUPPORT_SELECT
|
|
|
|
static esp_err_t console_start_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
2024-01-23 06:09:57 -05:00
|
|
|
esp_vfs_select_sem_t select_sem, void **end_select_args)
|
2021-10-21 00:46:24 -04:00
|
|
|
{
|
2023-05-22 10:23:35 -04:00
|
|
|
// start_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
|
2024-01-23 06:09:57 -05:00
|
|
|
if (primary_vfs->start_select) {
|
|
|
|
return primary_vfs->start_select(nfds, readfds, writefds, exceptfds, select_sem, end_select_args);
|
2023-05-22 10:23:35 -04:00
|
|
|
}
|
2024-01-23 06:09:57 -05:00
|
|
|
|
2023-05-22 10:23:35 -04:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t console_end_select(void *end_select_args)
|
|
|
|
{
|
2023-05-22 10:23:35 -04:00
|
|
|
// end_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
|
2024-01-23 06:09:57 -05:00
|
|
|
if (primary_vfs->end_select) {
|
|
|
|
return primary_vfs->end_select(end_select_args);
|
2023-05-22 10:23:35 -04:00
|
|
|
}
|
2024-01-23 06:09:57 -05:00
|
|
|
|
2023-05-22 10:23:35 -04:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_VFS_SUPPORT_SELECT
|
|
|
|
|
|
|
|
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
|
|
|
|
|
|
|
|
int console_tcsetattr(int fd, int optional_actions, const struct termios *p)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return tcsetattr(vfs_console.fd_primary, optional_actions, p);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_tcgetattr(int fd, struct termios *p)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return tcgetattr(vfs_console.fd_primary, p);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_tcdrain(int fd)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return tcdrain(vfs_console.fd_primary);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int console_tcflush(int fd, int select)
|
|
|
|
{
|
2024-01-23 06:09:57 -05:00
|
|
|
return tcflush(vfs_console.fd_primary, select);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
#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
|
|
|
|
.access = &console_access,
|
|
|
|
#endif // CONFIG_VFS_SUPPORT_DIR
|
|
|
|
#ifdef CONFIG_VFS_SUPPORT_SELECT
|
|
|
|
.start_select = &console_start_select,
|
|
|
|
.end_select = &console_end_select,
|
|
|
|
#endif // CONFIG_VFS_SUPPORT_SELECT
|
|
|
|
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
|
|
|
|
.tcsetattr = &console_tcsetattr,
|
|
|
|
.tcgetattr = &console_tcgetattr,
|
|
|
|
.tcdrain = &console_tcdrain,
|
|
|
|
.tcflush = &console_tcflush,
|
|
|
|
#endif // CONFIG_VFS_SUPPORT_TERMIOS
|
|
|
|
};
|
|
|
|
|
2023-11-24 01:56:08 -05:00
|
|
|
static esp_err_t esp_vfs_dev_console_register(void)
|
2021-10-21 00:46:24 -04:00
|
|
|
{
|
2023-08-09 04:11:54 -04:00
|
|
|
return esp_vfs_register(ESP_VFS_DEV_CONSOLE, &vfs, NULL);
|
2021-10-21 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_vfs_console_register(void)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
2024-01-23 06:09:57 -05:00
|
|
|
// Primary vfs part.
|
|
|
|
#if CONFIG_ESP_CONSOLE_UART
|
|
|
|
primary_vfs = esp_vfs_uart_get_vfs();
|
|
|
|
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
primary_vfs = esp_vfs_usb_serial_jtag_get_vfs();
|
2021-10-21 00:46:24 -04:00
|
|
|
#elif CONFIG_ESP_CONSOLE_USB_CDC
|
2023-11-24 01:56:08 -05:00
|
|
|
primary_vfs = esp_vfs_cdcacm_get_vfs();
|
2021-10-21 00:46:24 -04:00
|
|
|
err = esp_usb_console_init();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2024-01-23 06:09:57 -05:00
|
|
|
err = esp_vfs_dev_cdcacm_register();
|
2021-10-21 00:46:24 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2024-01-23 06:09:57 -05:00
|
|
|
#endif
|
2021-10-21 00:46:24 -04:00
|
|
|
|
2024-01-23 06:09:57 -05:00
|
|
|
// Secondary vfs part.
|
2021-10-21 00:46:24 -04:00
|
|
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
2024-01-23 06:09:57 -05:00
|
|
|
secondary_vfs = esp_vfs_usb_serial_jtag_get_vfs();
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif
|
2024-01-23 06:09:57 -05:00
|
|
|
|
2021-10-21 00:46:24 -04:00
|
|
|
err = esp_vfs_dev_console_register();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-11-24 01:56:08 -05:00
|
|
|
ESP_SYSTEM_INIT_FN(init_vfs_console, CORE, BIT(0), 114)
|
|
|
|
{
|
|
|
|
return esp_vfs_console_register();
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:46:24 -04:00
|
|
|
#endif // CONFIG_VFS_SUPPORT_IO
|
2023-11-24 01:56:08 -05:00
|
|
|
|
|
|
|
void esp_vfs_include_console_register(void)
|
|
|
|
{
|
|
|
|
// Linker hook function, exists to make the linker examine this file
|
|
|
|
}
|