Add list serial devices feature

For convenience, add a --list-devices option which lists the available
serial devices.
This commit is contained in:
Martin Lund 2022-02-08 22:44:48 +01:00
parent 6310f28d2c
commit 7a2a18232d
8 changed files with 55 additions and 2 deletions

View file

@ -29,6 +29,7 @@ The command-line interface is straightforward as reflected in the output from
-n, --no-autoconnect Disable automatic connect -n, --no-autoconnect Disable automatic connect
-e, --local-echo Do local echo -e, --local-echo Do local echo
-t, --timestamp Timestamp lines -t, --timestamp Timestamp lines
-i, --list-devices List available serial devices
-l, --log <filename> Log to file -l, --log <filename> Log to file
-m, --map <flags> Map special characters -m, --map <flags> Map special characters
-v, --version Display version -v, --version Display version

View file

@ -61,6 +61,11 @@ Enable local echo.
Timestamp lines. Timestamp lines.
.TP
.BR \-t ", " \-\-list\-devices
List available serial devices.
.TP .TP
.BR \-l ", " "\-\-log " \fI<filename> .BR \-l ", " "\-\-log " \fI<filename>

View file

@ -21,6 +21,7 @@ _tio()
-m --map \ -m --map \
-v --version \ -v --version \
-t --timestamp \ -t --timestamp \
-i --list-devices \
-h --help" -h --help"
# Complete the arguments to the options. # Complete the arguments to the options.
@ -70,6 +71,10 @@ _tio()
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0 return 0
;; ;;
-i | --list-devices)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
-v | --version) -v | --version)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0 return 0

View file

@ -41,6 +41,7 @@ struct option_t
bool log; bool log;
bool local_echo; bool local_echo;
bool timestamp; bool timestamp;
bool list_devices;
const char *log_filename; const char *log_filename;
const char *map; const char *map;
}; };

View file

@ -48,5 +48,6 @@ void stdin_restore(void);
void tty_configure(void); void tty_configure(void);
int tty_connect(void); int tty_connect(void);
void tty_wait_for_device(void); void tty_wait_for_device(void);
void list_serial_devices(void);
#endif #endif

View file

@ -31,7 +31,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int status; int status = 0;
/* Install error exit handler */ /* Install error exit handler */
atexit(&error_exit); atexit(&error_exit);
@ -39,6 +39,13 @@ int main(int argc, char *argv[])
/* Parse options */ /* Parse options */
parse_options(argc, argv); parse_options(argc, argv);
/* List available serial devices */
if (option.list_devices)
{
list_serial_devices();
return status;
}
/* Configure tty device */ /* Configure tty device */
tty_configure(); tty_configure();

View file

@ -47,6 +47,7 @@ struct option_t option =
.log = false, .log = false,
.local_echo = false, .local_echo = false,
.timestamp = false, .timestamp = false,
.list_devices = false,
.log_filename = "", .log_filename = "",
.map = "" .map = ""
}; };
@ -65,6 +66,7 @@ void print_help(char *argv[])
printf(" -n, --no-autoconnect Disable automatic connect\n"); printf(" -n, --no-autoconnect Disable automatic connect\n");
printf(" -e, --local-echo Do local echo\n"); printf(" -e, --local-echo Do local echo\n");
printf(" -t, --timestamp Timestamp lines\n"); printf(" -t, --timestamp Timestamp lines\n");
printf(" -i, --list-devices List available serial devices\n");
printf(" -l, --log <filename> Log to file\n"); printf(" -l, --log <filename> Log to file\n");
printf(" -m, --map <flags> Map special characters\n"); printf(" -m, --map <flags> Map special characters\n");
printf(" -v, --version Display version\n"); printf(" -v, --version Display version\n");
@ -115,6 +117,7 @@ void parse_options(int argc, char *argv[])
{"no-autoconnect", no_argument, 0, 'n'}, {"no-autoconnect", no_argument, 0, 'n'},
{"local-echo", no_argument, 0, 'e'}, {"local-echo", no_argument, 0, 'e'},
{"timestamp", no_argument, 0, 't'}, {"timestamp", no_argument, 0, 't'},
{"list-devices", no_argument, 0, 'i'},
{"log", required_argument, 0, 'l'}, {"log", required_argument, 0, 'l'},
{"map", required_argument, 0, 'm'}, {"map", required_argument, 0, 'm'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
@ -126,7 +129,7 @@ void parse_options(int argc, char *argv[])
int option_index = 0; int option_index = 0;
/* Parse argument using getopt_long */ /* 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 */ /* Detect the end of the options */
if (c == -1) if (c == -1)
@ -180,6 +183,10 @@ void parse_options(int argc, char *argv[])
option.timestamp = true; option.timestamp = true;
break; break;
case 'i':
option.list_devices = true;
break;
case 'l': case 'l':
option.log = true; option.log = true;
option.log_filename = optarg; 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 */ /* Assume first non-option is the tty device name */
if (optind < argc) if (optind < argc)
option.tty_device = argv[optind++]; option.tty_device = argv[optind++];

View file

@ -36,6 +36,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <dirent.h>
#include "config.h" #include "config.h"
#include "tio/tty.h" #include "tio/tty.h"
#include "tio/print.h" #include "tio/print.h"
@ -48,6 +49,8 @@
extern int setspeed2(int fd, int baudrate); extern int setspeed2(int fd, int baudrate);
#endif #endif
#define PATH_SERIAL_DEVICES "/dev/serial/by-id/"
static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old; static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old;
static unsigned long rx_total = 0, tx_total = 0; static unsigned long rx_total = 0, tx_total = 0;
static bool connected = false; static bool connected = false;
@ -841,3 +844,21 @@ error_read:
error_open: error_open:
return TIO_ERROR; 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);
}
}