From d661e8e3479f4756c7cd7d0da45e2e17d3aa5763 Mon Sep 17 00:00:00 2001
From: Pawel Szewczyk
Date: Mon, 6 Jul 2015 11:57:10 +0200
Subject: [PATCH] gadget: Add gadget get at libusbg backend
Signed-off-by: Pawel Szewczyk
Use new format of passing gadget attrs also in not
implemented backend.
Change-Id: I4d8213f7c66dab9cbbf66aa4d0f9d931e919799b
Signed-off-by: Krzysztof Opasiak
---
source/gadget/include/gadget.h | 2 +-
source/gadget/src/gadget.c | 45 ++++++++++++++++-------
source/gadget/src/gadget_libusbg.c | 58 +++++++++++++++++++++++++++++-
source/gadget/src/gadget_not_implemented.c | 12 +++----
4 files changed, 97 insertions(+), 20 deletions(-)
diff --git a/source/gadget/include/gadget.h b/source/gadget/include/gadget.h
index 4209b81..a0fbb6b 100644
--- a/source/gadget/include/gadget.h
+++ b/source/gadget/include/gadget.h
@@ -104,7 +104,7 @@ struct gt_gadget_rm_data {
struct gt_gadget_get_data {
const char *name;
- const char **attrs;
+ int attrs[USBG_GADGET_ATTR_MAX];
int opts;
};
diff --git a/source/gadget/src/gadget.c b/source/gadget/src/gadget.c
index 7f6a60d..1c5ec29 100644
--- a/source/gadget/src/gadget.c
+++ b/source/gadget/src/gadget.c
@@ -237,13 +237,18 @@ static void gt_gadget_get_destructor(void *data)
return;
dt = (struct gt_gadget_get_data *)data;
- free(dt->attrs);
free(dt);
}
static int gt_gadget_get_help(void *data)
{
- printf("Gadget get help.\n");
+ printf("usage: %s get [attr] ...\n"
+ "Print gadget attributes and their values. "
+ "If attr has not been given, all attributes are printed.\n"
+ "\n"
+ "Options:\n"
+ " -h, --help\tPrint this help\n",
+ program_name);
return -1;
}
@@ -251,9 +256,11 @@ static void gt_parse_gadget_get(const Command *cmd, int argc, char **argv,
ExecutableCommand *exec, void * data)
{
struct gt_gadget_get_data *dt = NULL;
- int i;
+ int iter;
int ind;
int avaible_opts = GT_HELP;
+ int attr_id;
+ int i;
if (argc == 0)
goto out;
@@ -267,13 +274,28 @@ static void gt_parse_gadget_get(const Command *cmd, int argc, char **argv,
goto out;
dt->name = argv[ind++];
- dt->attrs = calloc(argc - ind + 1, sizeof(char *));
- if (dt->attrs == NULL)
- goto out;
- i = 0;
- while (argv[ind])
- dt->attrs[i++] = argv[ind++];
+ for (i = 0; i < USBG_GADGET_ATTR_MAX; ++i)
+ dt->attrs[i] = 0;
+
+ iter = 0;
+ while (argv[ind]) {
+ attr_id = usbg_lookup_gadget_attr(argv[ind]);
+ if (attr_id < 0) {
+ fprintf(stderr, "%s: invalid attribute name\n", argv[ind]);
+ goto out;
+ }
+
+ dt->attrs[attr_id] = 1;
+ ind++;
+ iter = 1;
+ }
+
+ if (!iter) {
+ for (i = 0; i < USBG_GADGET_ATTR_MAX; ++i)
+ dt->attrs[i] = 1;
+ }
+
executable_command_set(exec, GET_EXECUTABLE(get), (void *)dt,
gt_gadget_get_destructor);
@@ -335,11 +357,10 @@ out:
static int gt_gadget_enable_help(void *data)
{
- printf("usage: %s disable [options] [gadget] \n"
- "Remove gadget of specified name\n"
+ printf("usage: %s enable [udc] \n"
+ "Enable gadget. If udc has not been specified, default one is used.\n"
"\n"
"Options:\n"
- " -u=, --udc=\tDisable gadget which is active at given udc\n"
" -h, --help\tPrint this help\n",
program_name);
diff --git a/source/gadget/src/gadget_libusbg.c b/source/gadget/src/gadget_libusbg.c
index 6253527..2d214a1 100644
--- a/source/gadget/src/gadget_libusbg.c
+++ b/source/gadget/src/gadget_libusbg.c
@@ -199,10 +199,66 @@ static int disable_func(void *data)
return 0;
}
+static int print_gadget_attrs(usbg_gadget *g, int *mask) {
+ usbg_gadget_attrs g_attrs;
+ int usbg_ret;
+
+ usbg_ret = usbg_get_gadget_attrs(g, &g_attrs);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ return -1;
+ }
+
+ if (mask[BCD_USB])
+ printf(" bcdUSB\t\t%x.%02x\n",
+ g_attrs.bcdUSB >> 8,
+ g_attrs.bcdUSB & 0x00ff);
+
+ if (mask[B_DEVICE_CLASS])
+ printf(" bDeviceClass\t\t0x%02x\n", g_attrs.bDeviceClass);
+ if (mask[B_DEVICE_SUB_CLASS])
+ printf(" bDeviceSubClass\t0x%02x\n", g_attrs.bDeviceSubClass);
+ if (mask[B_DEVICE_PROTOCOL])
+ printf(" bDeviceProtocol\t0x%02x\n", g_attrs.bDeviceProtocol);
+ if (mask[B_MAX_PACKET_SIZE_0])
+ printf(" bMaxPacketSize0\t%d\n", g_attrs.bMaxPacketSize0);
+ if (mask[ID_VENDOR])
+ printf(" idVendor\t\t0x%04x\n", g_attrs.idVendor);
+ if (mask[ID_PRODUCT])
+ printf(" idProduct\t\t0x%04x\n", g_attrs.idProduct);
+ if (mask[BCD_DEVICE])
+ printf(" bcdDevice\t\t%x.%02x\n",
+ g_attrs.bcdDevice >> 8,
+ g_attrs.bcdDevice & 0x00ff);
+
+ return 0;
+}
+
+static int get_func(void *data)
+{
+ struct gt_gadget_get_data *dt;
+
+ usbg_gadget *g;
+ int ret;
+
+ dt = (struct gt_gadget_get_data *)data;
+
+ g = usbg_get_gadget(backend_ctx.libusbg_state, dt->name);
+ if (g == NULL) {
+ fprintf(stderr, "Gadget '%s' not found\n", dt->name);
+ return -1;
+ }
+
+ ret = print_gadget_attrs(g, dt->attrs);
+
+ return ret;
+}
+
struct gt_gadget_backend gt_gadget_backend_libusbg = {
.create = create_func,
.rm = rm_func,
- .get = NULL,
+ .get = get_func,
.set = NULL,
.enable = enable_func,
.disable = disable_func,
diff --git a/source/gadget/src/gadget_not_implemented.c b/source/gadget/src/gadget_not_implemented.c
index 04ce730..b5edd8a 100644
--- a/source/gadget/src/gadget_not_implemented.c
+++ b/source/gadget/src/gadget_not_implemented.c
@@ -17,6 +17,8 @@
#include
+#include
+
#include "gadget.h"
#include "backend.h"
#include "common.h"
@@ -46,17 +48,15 @@ static int rm_func(void *data)
static int get_func(void *data)
{
struct gt_gadget_get_data *dt;
- const char **ptr;
+ int i;
dt = (struct gt_gadget_get_data *)data;
printf("Gadget get called successfully. Not implemented yet.\n");
printf("name = %s, attrs = ", dt->name);
- ptr = dt->attrs;
- while (*ptr) {
- printf("%s, ", *ptr);
- ptr++;
- }
+ for (i = 0; i < ARRAY_SIZE(dt->attrs); ++i)
+ if (dt->attrs[i] > 0)
+ printf("%s, ", usbg_get_gadget_attr_str(i));
putchar('\n');
return 0;
--
2.7.4