From 7a2a18232dce2505edf852f00ec423c06340ace6 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Tue, 8 Feb 2022 22:44:48 +0100 Subject: [PATCH] Add list serial devices feature For convenience, add a --list-devices option which lists the available serial devices. --- README.md | 1 + man/tio.1 | 5 +++++ src/bash-completion/tio.in | 5 +++++ src/include/tio/options.h | 1 + src/include/tio/tty.h | 1 + src/main.c | 9 ++++++++- src/options.c | 14 +++++++++++++- src/tty.c | 21 +++++++++++++++++++++ 8 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a75052b..4f8174c 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ The command-line interface is straightforward as reflected in the output from -n, --no-autoconnect Disable automatic connect -e, --local-echo Do local echo -t, --timestamp Timestamp lines + -i, --list-devices List available serial devices -l, --log Log to file -m, --map Map special characters -v, --version Display version diff --git a/man/tio.1 b/man/tio.1 index d1f1e2a..5a0d5b3 100644 --- a/man/tio.1 +++ b/man/tio.1 @@ -61,6 +61,11 @@ Enable local echo. Timestamp lines. +.TP +.BR \-t ", " \-\-list\-devices + +List available serial devices. + .TP .BR \-l ", " "\-\-log " \fI diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index 22c8080..9c468ea 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -21,6 +21,7 @@ _tio() -m --map \ -v --version \ -t --timestamp \ + -i --list-devices \ -h --help" # Complete the arguments to the options. @@ -70,6 +71,10 @@ _tio() COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 ;; + -i | --list-devices) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; -v | --version) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 diff --git a/src/include/tio/options.h b/src/include/tio/options.h index 1ae1974..c28ba6b 100644 --- a/src/include/tio/options.h +++ b/src/include/tio/options.h @@ -41,6 +41,7 @@ struct option_t bool log; bool local_echo; bool timestamp; + bool list_devices; const char *log_filename; const char *map; }; diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 44a80a4..b1fdef7 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -48,5 +48,6 @@ void stdin_restore(void); void tty_configure(void); int tty_connect(void); void tty_wait_for_device(void); +void list_serial_devices(void); #endif diff --git a/src/main.c b/src/main.c index 83c060a..6e9b9d0 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { - int status; + int status = 0; /* Install error exit handler */ atexit(&error_exit); @@ -39,6 +39,13 @@ int main(int argc, char *argv[]) /* Parse options */ parse_options(argc, argv); + /* List available serial devices */ + if (option.list_devices) + { + list_serial_devices(); + return status; + } + /* Configure tty device */ tty_configure(); diff --git a/src/options.c b/src/options.c index aace521..86128b2 100644 --- a/src/options.c +++ b/src/options.c @@ -47,6 +47,7 @@ struct option_t option = .log = false, .local_echo = false, .timestamp = false, + .list_devices = false, .log_filename = "", .map = "" }; @@ -65,6 +66,7 @@ void print_help(char *argv[]) printf(" -n, --no-autoconnect Disable automatic connect\n"); printf(" -e, --local-echo Do local echo\n"); printf(" -t, --timestamp Timestamp lines\n"); + printf(" -i, --list-devices List available serial devices\n"); printf(" -l, --log Log to file\n"); printf(" -m, --map Map special characters\n"); printf(" -v, --version Display version\n"); @@ -115,6 +117,7 @@ void parse_options(int argc, char *argv[]) {"no-autoconnect", no_argument, 0, 'n'}, {"local-echo", no_argument, 0, 'e'}, {"timestamp", no_argument, 0, 't'}, + {"list-devices", no_argument, 0, 'i'}, {"log", required_argument, 0, 'l'}, {"map", required_argument, 0, 'm'}, {"version", no_argument, 0, 'v'}, @@ -126,7 +129,7 @@ void parse_options(int argc, char *argv[]) int option_index = 0; /* Parse argument using getopt_long */ - c = getopt_long(argc, argv, "b:d:f:s:p:o:netl:m:vh", long_options, &option_index); + c = getopt_long(argc, argv, "b:d:f:s:p:o:netil:m:vh", long_options, &option_index); /* Detect the end of the options */ if (c == -1) @@ -180,6 +183,10 @@ void parse_options(int argc, char *argv[]) option.timestamp = true; break; + case 'i': + option.list_devices = true; + break; + case 'l': option.log = true; option.log_filename = optarg; @@ -214,6 +221,11 @@ void parse_options(int argc, char *argv[]) } } + if (option.list_devices) + { + return; + } + /* Assume first non-option is the tty device name */ if (optind < argc) option.tty_device = argv[optind++]; diff --git a/src/tty.c b/src/tty.c index 8e1190a..975eea2 100644 --- a/src/tty.c +++ b/src/tty.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "config.h" #include "tio/tty.h" #include "tio/print.h" @@ -48,6 +49,8 @@ extern int setspeed2(int fd, int baudrate); #endif +#define PATH_SERIAL_DEVICES "/dev/serial/by-id/" + static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old; static unsigned long rx_total = 0, tx_total = 0; static bool connected = false; @@ -841,3 +844,21 @@ error_read: error_open: return TIO_ERROR; } + +void list_serial_devices(void) +{ + DIR *d; + struct dirent *dir; + d = opendir(PATH_SERIAL_DEVICES); + + if (d) + { + while ((dir = readdir(d)) != NULL) + { + if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, ".."))) + printf("%s%s\n", PATH_SERIAL_DEVICES, dir->d_name); + } + + closedir(d); + } +}