return 0;
}
-static void gt_parse_gadget_create(const Command *cmd, int argc, char **argv,
- ExecutableCommand *exec, void *data)
+static int gt_parse_gadget_attrs(struct gt_setting *attrs, int *attr_val, char **str_val)
{
- struct gt_gadget_create_data *dt;
- int ind;
- int c;
- int avaible_opts = GT_FORCE | GT_HELP;
- struct gt_setting *setting, *attrs = NULL;
+ struct gt_setting *setting;
+ int attr_id;
_Bool iter;
int i;
- dt = zalloc(sizeof(*dt));
- if (dt == NULL)
- goto out;
-
- ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
- if (ind < 0 || dt->opts & GT_HELP)
- goto out;
-
- if (ind == argc)
- goto out;
-
- dt->name = argv[ind++];
-
- c = gt_parse_setting_list(&attrs, argc - ind, argv + ind);
- if (c < 0)
- goto out;
-
- for (i = 0; i < USBG_GADGET_ATTR_MAX; i++)
- dt->attr_val[i] = -1;
- memset(dt->str_val, 0, sizeof(dt->str_val));
-
for (setting = attrs; setting->variable; setting++) {
- int attr_id;
-
iter = TRUE;
attr_id = usbg_lookup_gadget_attr(setting->variable);
fprintf(stderr, "Invalid value '%s' for attribute '%s'\n",
setting->value, setting->variable);
- goto out;
+ return -1;
}
- dt->attr_val[attr_id] = val;
+ attr_val[attr_id] = val;
iter = FALSE;
}
for (i = 0; iter && i < GT_GADGET_STRS_COUNT; i++) {
if (streq(setting->variable, gadget_strs[i].name)) {
- dt->str_val[i] = setting->value;
+ str_val[i] = setting->value;
iter = FALSE;
break;
}
if (iter) {
fprintf(stderr, "Unknown attribute passed: %s\n", setting->variable);
- goto out;
+ return -1;
}
}
+ return 0;
+}
+
+static void gt_parse_gadget_create(const Command *cmd, int argc, char **argv,
+ ExecutableCommand *exec, void *data)
+{
+ struct gt_gadget_create_data *dt;
+ int ind;
+ int c;
+ int avaible_opts = GT_FORCE | GT_HELP;
+ struct gt_setting *attrs = NULL;
+ int i;
+
+ dt = zalloc(sizeof(*dt));
+ if (dt == NULL)
+ goto out;
+
+ ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+ if (ind < 0 || dt->opts & GT_HELP)
+ goto out;
+
+ if (ind == argc)
+ goto out;
+
+ dt->name = argv[ind++];
+
+ c = gt_parse_setting_list(&attrs, argc - ind, argv + ind);
+ if (c < 0)
+ goto out;
+
+ for (i = 0; i < USBG_GADGET_ATTR_MAX; i++)
+ dt->attr_val[i] = -1;
+ memset(dt->str_val, 0, sizeof(dt->str_val));
+
+ gt_parse_gadget_attrs(attrs, dt->attr_val, dt->str_val);
+
executable_command_set(exec, GET_EXECUTABLE(create),
(void *)dt, gt_gadget_create_destructor);
if (data == NULL)
return;
dt = (struct gt_gadget_set_data *)data;
- gt_setting_list_cleanup(dt->attrs);
free(dt);
}
int tmp;
int ind;
int avaible_opts = GT_HELP;
+ struct gt_setting *attrs;
+ int i;
dt = zalloc(sizeof(*dt));
if (dt == NULL)
goto out;
dt->name = argv[ind++];
- tmp = gt_parse_setting_list(&dt->attrs, argc - ind, argv + ind);
+ tmp = gt_parse_setting_list(&attrs, argc - ind, argv + ind);
if (tmp < 0)
goto out;
+ for (i = 0; i < USBG_GADGET_ATTR_MAX; i++)
+ dt->attr_val[i] = -1;
+ memset(dt->str_val, 0, sizeof(dt->str_val));
+
+ gt_parse_gadget_attrs(attrs, dt->attr_val, dt->str_val);
executable_command_set(exec, GET_EXECUTABLE(set), (void *)dt,
gt_gadget_set_destructor);
return;
return ret;
}
+static int set_func(void *data)
+{
+ struct gt_gadget_set_data *dt;
+ int i;
+ usbg_gadget *g;
+ int ret;
+
+ dt = (struct gt_gadget_set_data *)data;
+
+ g = usbg_get_gadget(backend_ctx.libusbg_state, dt->name);
+ if (!g) {
+ fprintf(stderr, "Error on get gadget\n");
+ return -1;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(dt->attr_val); ++i) {
+ if (dt->attr_val[i] < 0)
+ continue;
+
+ ret = usbg_set_gadget_attr(g, i, dt->attr_val[i]);
+ if (ret != USBG_SUCCESS) {
+ fprintf(stderr, "Unable to set attribute %s: %s\n",
+ usbg_get_gadget_attr_str(i),
+ usbg_strerror(ret));
+ return -1;
+ }
+ }
+
+ for (i = 0; i < G_N_ELEMENTS(dt->str_val); i++) {
+ if (dt->str_val[i] == NULL)
+ continue;
+
+ ret = gadget_strs[i].set_fn(g, LANG_US_ENG, dt->str_val[i]);
+ if (ret != USBG_SUCCESS) {
+ fprintf(stderr, "Unable to set string %s: %s\n",
+ gadget_strs[i].name,
+ usbg_strerror(ret));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
struct gt_gadget_backend gt_gadget_backend_libusbg = {
.create = create_func,
.rm = rm_func,
.get = get_func,
- .set = NULL,
+ .set = set_func,
.enable = enable_func,
.disable = disable_func,
.gadget = gadget_func,
static int set_func(void *data)
{
struct gt_gadget_set_data *dt;
- struct gt_setting *ptr;
+ int i;
dt = (struct gt_gadget_set_data *)data;
printf("Gadget set called successfully. Not implemented.\n");
printf("name = %s", dt->name);
- ptr = dt->attrs;
- while (ptr->variable) {
- printf(", %s = %s", ptr->variable, ptr->value);
- ptr++;
+
+ for (i = 0; i < ARRAY_SIZE(dt->attr_val); ++i) {
+ if (dt->attr_val[i] >= 0) {
+ printf(", %s = %d", usbg_get_gadget_attr_str(i),
+ dt->attr_val[i]);
+ }
}
+
+ for (i = 0; i < ARRAY_SIZE(dt->str_val); ++i) {
+ if (dt->str_val[i] != NULL) {
+ printf(", %s = %s", gadget_strs[i].name,
+ dt->str_val[i]);
+ }
+ }
+
putchar('\n');
return 0;
}