From: Gerhard Sittig Date: Thu, 16 Mar 2017 11:16:58 +0000 (+0100) Subject: show: print list of available serial ports X-Git-Url: https://sigrok.org/gitweb/?p=sigrok-cli.git;a=commitdiff_plain;h=56fc0d6dbca2d46ff2a79275ef6307c4e38bf6a1 show: print list of available serial ports Introduce the --list-serial command line option, which prints the list of serial ports detected by the sigrok serial.c abstraction. The code was taken from Carl-Fredrik Sundström's message to the mailing list, with minor style fixes applied, and the option renamed to explicitly mention its "list" nature, not suggesting it would be a parameter to setup. --- diff --git a/main.c b/main.c index 8e4ac3a..829492f 100644 --- a/main.c +++ b/main.c @@ -292,6 +292,8 @@ int main(int argc, char **argv) set_options(); else if (opt_samples || opt_time || opt_frames || opt_continuous) run_session(); + else if (opt_list_serial) + show_serial_ports(); else show_help(); diff --git a/options.c b/options.c index 3ec4414..d69ed73 100644 --- a/options.c +++ b/options.c @@ -52,6 +52,7 @@ gchar *opt_frames = NULL; gboolean opt_continuous = FALSE; gchar *opt_get = NULL; gboolean opt_set = FALSE; +gboolean opt_list_serial = FALSE; /* * Defines a callback function that generates an error if an @@ -158,6 +159,7 @@ static const GOptionEntry optargs[] = { "Sample continuously", NULL}, {"get", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_get, "Get device options only", NULL}, {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL}, + {"list-serial", 0, 0, G_OPTION_ARG_NONE, &opt_list_serial, "List available serial ports", NULL}, {NULL, 0, 0, 0, NULL, NULL, NULL} }; diff --git a/show.c b/show.c index 090be4a..c2517dd 100644 --- a/show.c +++ b/show.c @@ -1055,3 +1055,25 @@ void show_transform(void) } g_strfreev(tok); } + +static void print_serial_port(gpointer data, gpointer user_data) +{ + struct sr_serial_port *port; + + port = (void *)data; + (void)user_data; + printf("\t%s\t%s\n", port->name, port->description); +} + +void show_serial_ports(void) +{ + GSList *serial_ports; + + serial_ports = sr_serial_list(NULL); + if (!serial_ports) + return; + + printf("Available serial ports:\n"); + g_slist_foreach(serial_ports, print_serial_port, NULL); + g_slist_free_full(serial_ports, (GDestroyNotify)sr_serial_free); +} diff --git a/sigrok-cli.h b/sigrok-cli.h index 37746e3..7def741 100644 --- a/sigrok-cli.h +++ b/sigrok-cli.h @@ -52,6 +52,7 @@ void show_pd_detail(void); void show_input(void); void show_output(void); void show_transform(void); +void show_serial_ports(void); /* device.c */ GSList *device_scan(void); @@ -139,6 +140,7 @@ extern gchar *opt_frames; extern gboolean opt_continuous; extern gchar *opt_get; extern gboolean opt_set; +extern gboolean opt_list_serial; int parse_options(int argc, char **argv); void show_help(void);