mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add list serial devices feature
For convenience, add a --list-devices option which lists the available serial devices.
This commit is contained in:
parent
6310f28d2c
commit
7a2a18232d
8 changed files with 55 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ struct option_t
|
|||
bool log;
|
||||
bool local_echo;
|
||||
bool timestamp;
|
||||
bool list_devices;
|
||||
const char *log_filename;
|
||||
const char *map;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <filename> Log to file\n");
|
||||
printf(" -m, --map <flags> 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++];
|
||||
|
|
|
|||
21
src/tty.c
21
src/tty.c
|
|
@ -36,6 +36,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue