From 9bd8fca82a6e72fcba87ecdaddede5d573e7ab52 Mon Sep 17 00:00:00 2001
From: Pawel Szewczyk
Date: Tue, 14 Jul 2015 13:03:03 +0200
Subject: [PATCH] function: Add func show command at libusbg backend
Change-Id: I1804ef3ff0bc8e1c73d74bf1771c460f0388f044
Signed-off-by: Pawel Szewczyk
---
source/base/include/parser.h | 2 +
source/base/src/parser.c | 8 ++
source/function/include/function.h | 7 ++
source/function/src/function.c | 19 ++--
source/function/src/function_libusbg.c | 140 ++++++++++++++++++++++++-
source/function/src/function_not_implemented.c | 1 +
6 files changed, 169 insertions(+), 8 deletions(-)
diff --git a/source/base/include/parser.h b/source/base/include/parser.h
index cc132af..b264ddb 100644
--- a/source/base/include/parser.h
+++ b/source/base/include/parser.h
@@ -85,6 +85,8 @@ enum gt_option_flags {
GT_STDOUT = 32,
GT_HELP = 64,
GT_QUIET = 128,
+ GT_INSTANCE = 256,
+ GT_TYPE = 512,
};
/**
diff --git a/source/base/src/parser.c b/source/base/src/parser.c
index 1e65d15..c62b42a 100644
--- a/source/base/src/parser.c
+++ b/source/base/src/parser.c
@@ -189,6 +189,8 @@ int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv)
{GT_STDIN, {"stdout", no_argument, 0, 2}},
{GT_HELP, {"help", no_argument, 0, 'h'}},
{GT_QUIET, {"quiet", no_argument, 0, 'q'}},
+ {GT_INSTANCE, {"instance", no_argument, 0, 3}},
+ {GT_TYPE, {"type", no_argument, 0, 4}},
{0, {NULL, 0, 0, 0}}
};
@@ -245,6 +247,12 @@ int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv)
case 'q':
*optmask |= GT_QUIET;
break;
+ case 3:
+ *optmask |= GT_INSTANCE;
+ break;
+ case 4:
+ *optmask |= GT_TYPE;
+ break;
default:
return -1;
}
diff --git a/source/function/include/function.h b/source/function/include/function.h
index 5d6abef..60761db 100644
--- a/source/function/include/function.h
+++ b/source/function/include/function.h
@@ -171,6 +171,13 @@ const Command *gt_func_get_children(const Command *cmd);
*/
int gt_func_help(void *data);
+/**
+ * @brief Print function
+ * @param[in] f Function to be printed
+ * @param[in] opts Options of printing
+ */
+int gt_print_function_libusbg(usbg_function *f, int opts);
+
extern struct gt_function_backend gt_function_backend_libusbg;
extern struct gt_function_backend gt_function_backend_gadgetd;
extern struct gt_function_backend gt_function_backend_not_implemented;
diff --git a/source/function/src/function.c b/source/function/src/function.c
index a754135..5d648f9 100644
--- a/source/function/src/function.c
+++ b/source/function/src/function.c
@@ -324,12 +324,15 @@ static int gt_func_func_help(void *data)
static int gt_func_show_help(void *data)
{
- printf("usage: %s func show [ ]\n"
- "Show functions. If no function was specified, show all functions\n\n",
+ printf("usage: %s func show [type[ [instance]]\n"
+ "Show functions. If no function was specified, show all functions.\n"
+ "If only function type was specified show only functions of given type\n\n",
program_name);
printf("Options:\n"
" -v, --verbose\tShow also attributes\n"
+ " --instance\tShow only function instances (cannot be used with --type)\n"
+ " --type\t\tShow only function types (cannot be used with --instance)\n"
" -h, --help\tPrint this help\n");
return 0;
@@ -340,7 +343,7 @@ static void gt_parse_func_show(const Command *cmd, int argc,
{
struct gt_func_show_data *dt = NULL;
int ind;
- int avaible_opts = GT_VERBOSE | GT_HELP;
+ int avaible_opts = GT_VERBOSE | GT_HELP | GT_INSTANCE | GT_TYPE;
if (argc < 1)
goto out;
@@ -353,12 +356,13 @@ static void gt_parse_func_show(const Command *cmd, int argc,
if (ind < 0 || dt->opts & GT_HELP)
goto out;
+ if (dt->opts & GT_INSTANCE && dt->opts & GT_TYPE)
+ goto out;
dt->gadget = argv[ind++];
+ dt->type = -1;
+ dt->instance = NULL;
if (argc > ind) {
- if (argc - ind != 2)
- goto out;
-
dt->type = usbg_lookup_function_type(argv[ind]);
if (dt->type < 0) {
fprintf(stderr, "Unknown function type: %s", argv[ind]);
@@ -366,7 +370,8 @@ static void gt_parse_func_show(const Command *cmd, int argc,
}
ind++;
- dt->instance = argv[ind++];
+ if (argc > ind)
+ dt->instance = argv[ind++];
}
executable_command_set(exec, GET_EXECUTABLE(show), (void *)dt,
diff --git a/source/function/src/function_libusbg.c b/source/function/src/function_libusbg.c
index f408771..acb6d91 100644
--- a/source/function/src/function_libusbg.c
+++ b/source/function/src/function_libusbg.c
@@ -78,13 +78,151 @@ static int list_types_func(void *data)
return 0;
}
+int gt_print_function_libusbg(usbg_function *f, int opts)
+{
+ const char *instance;
+ usbg_function_type type;
+ int usbg_ret;
+ usbg_function_attrs f_attrs;
+
+ instance = usbg_get_function_instance(f);
+ if (!instance) {
+ fprintf(stderr, "Unable to get function instance name\n");
+ return -1;
+ }
+
+ type = usbg_get_function_type(f);
+
+ if (opts & GT_INSTANCE)
+ printf(" %s\n", instance);
+ else if (opts & GT_TYPE)
+ printf(" %s\n", usbg_get_function_type_str(type));
+ else
+ fprintf(stdout, " %s.%s\n",
+ usbg_get_function_type_str(type), instance);
+
+ usbg_ret = usbg_get_function_attrs(f, &f_attrs);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ return -1;
+ }
+
+ if (!(opts & GT_VERBOSE))
+ return 0;
+
+ switch (f_attrs.header.attrs_type) {
+ case USBG_F_ATTRS_SERIAL:
+ fprintf(stdout, " port_num\t\t%d\n",
+ f_attrs.attrs.serial.port_num);
+ break;
+
+ case USBG_F_ATTRS_NET:
+ {
+ usbg_f_net_attrs *f_net_attrs = &f_attrs.attrs.net;
+
+ fprintf(stdout, " dev_addr\t\t%s\n",
+ ether_ntoa(&f_net_attrs->dev_addr));
+ fprintf(stdout, " host_addr\t\t%s\n",
+ ether_ntoa(&f_net_attrs->host_addr));
+ fprintf(stdout, " ifname\t\t%s\n", f_net_attrs->ifname);
+ fprintf(stdout, " qmult\t\t%d\n", f_net_attrs->qmult);
+ break;
+ }
+
+ case USBG_F_ATTRS_PHONET:
+ fprintf(stdout, " ifname\t\t%s\n", f_attrs.attrs.phonet.ifname);
+ break;
+
+ case USBG_F_ATTRS_FFS:
+ fprintf(stdout, " dev_name\t\t%s\n", f_attrs.attrs.ffs.dev_name);
+ break;
+
+ case USBG_F_ATTRS_MS:
+ {
+ usbg_f_ms_attrs *attrs = &f_attrs.attrs.ms;
+ int i;
+
+ fprintf(stdout, " stall\t\t%d\n", attrs->stall);
+ fprintf(stdout, " nluns\t\t%d\n", attrs->nluns);
+ for (i = 0; i < attrs->nluns; ++i) {
+ fprintf(stdout, " lun %d:\n", attrs->luns[i]->id);
+ fprintf(stdout, " cdrom\t\t%d\n", attrs->luns[i]->cdrom);
+ fprintf(stdout, " ro\t\t%d\n", attrs->luns[i]->ro);
+ fprintf(stdout, " nofua\t\t%d\n", attrs->luns[i]->nofua);
+ fprintf(stdout, " removable\t\t%d\n", attrs->luns[i]->removable);
+ fprintf(stdout, " file\t\t%s\n", attrs->luns[i]->filename);
+ }
+ break;
+ }
+
+ case USBG_F_ATTRS_MIDI:
+ {
+ usbg_f_midi_attrs *attrs = &f_attrs.attrs.midi;
+
+ fprintf(stdout, " index\t\t%d\n", attrs->index);
+ fprintf(stdout, " id\t\t\t%s\n", attrs->id);
+ fprintf(stdout, " in_ports\t\t%d\n", attrs->in_ports);
+ fprintf(stdout, " out_ports\t\t%d\n", attrs->out_ports);
+ fprintf(stdout, " buflen\t\t%d\n", attrs->buflen);
+ fprintf(stdout, " qlen\t\t%d\n", attrs->qlen);
+ break;
+ }
+
+ default:
+ fprintf(stdout, " UNKNOWN\n");
+ }
+
+ usbg_cleanup_function_attrs(&f_attrs);
+ return 0;
+}
+
+static int show_func(void *data)
+{
+ struct gt_func_show_data *dt;
+ usbg_gadget *g;
+ usbg_function *f;
+
+ dt = (struct gt_func_show_data *)data;
+
+ g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
+ if (g == NULL) {
+ fprintf(stderr, "Unable to find gadget %s\n",
+ dt->gadget);
+ return -1;
+ }
+
+ if (dt->instance) {
+ f = usbg_get_function(g, dt->type, dt->instance);
+ if (f == NULL) {
+ fprintf(stderr, "Unable to find function: %s.%s\n",
+ usbg_get_function_type_str(dt->type),
+ dt->instance);
+ return -1;
+ }
+
+ gt_print_function_libusbg(f, dt->opts);
+ } else if (dt->type >= 0) {
+ usbg_for_each_function(f, g) {
+ if (dt->type == usbg_get_function_type(f))
+ gt_print_function_libusbg(f, dt->opts);
+ }
+ } else {
+ usbg_for_each_function(f, g) {
+ gt_print_function_libusbg(f, dt->opts);
+ }
+ }
+
+ return 0;
+}
+
struct gt_function_backend gt_function_backend_libusbg = {
.create = create_func,
.rm = NULL,
.list_types = list_types_func,
.get = NULL,
.set = NULL,
- .show = NULL,
+ .show = show_func,
.load = NULL,
.save = NULL,
.template_default = NULL,
diff --git a/source/function/src/function_not_implemented.c b/source/function/src/function_not_implemented.c
index 2db1bcd..d2c07a2 100644
--- a/source/function/src/function_not_implemented.c
+++ b/source/function/src/function_not_implemented.c
@@ -15,6 +15,7 @@
*/
#include
+#include
#include "function.h"
#include "common.h"
--
2.7.4