struct gt_gadget_get_data {
const char *name;
- const char **attrs;
+ int attrs[USBG_GADGET_ATTR_MAX];
int opts;
};
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 <gadget_name> [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;
}
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;
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);
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 <gadget> [udc] \n"
+ "Enable gadget. If udc has not been specified, default one is used.\n"
"\n"
"Options:\n"
- " -u=<udc>, --udc=<udc>\tDisable gadget which is active at given udc\n"
" -h, --help\tPrint this help\n",
program_name);
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,
#include <stdio.h>
+#include <usbg/usbg.h>
+
#include "gadget.h"
#include "backend.h"
#include "common.h"
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;