2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
32 int (*set_fn)(usbg_gadget *, int, const char *);
34 { "product", usbg_set_gadget_product },
35 { "manufacturer", usbg_set_gadget_manufacturer },
36 { "serialnumber", usbg_set_gadget_product },
39 struct gt_gadget_create_data {
41 struct gt_setting *attrs;
45 static void gt_gadget_create_destructor(void *data)
47 struct gt_gadget_create_data *dt;
51 dt = (struct gt_gadget_create_data *)data;
52 gt_setting_list_cleanup(dt->attrs);
56 static char *attr_type_get(usbg_gadget_attr a)
60 case B_DEVICE_SUB_CLASS:
61 case B_DEVICE_PROTOCOL:
62 case B_MAX_PACKET_SIZE_0:
76 static int attr_val_in_range(usbg_gadget_attr a, unsigned long int val)
78 char *type = attr_type_get(a);
84 return val <= UINT8_MAX;
85 else if (type[0] == 'q')
86 return val <= UINT16_MAX;
92 static int gt_gadget_create_func(void *data)
94 struct gt_gadget_create_data *dt;
95 struct gt_setting *setting;
96 int attr_val[USBG_GADGET_ATTR_MAX];
97 char *str_val[G_N_ELEMENTS(strs)];
102 for (i = 0; i < G_N_ELEMENTS(attr_val); i++)
104 memset(str_val, 0, sizeof(str_val));
106 dt = (struct gt_gadget_create_data *)data;
108 for (setting = dt->attrs; setting->variable; setting++) {
113 attr_id = usbg_lookup_gadget_attr(setting->variable);
115 unsigned long int val;
119 val = strtoul(setting->value, &endptr, 0);
121 || *setting->value == 0
122 || (endptr && *endptr != 0)
123 || attr_val_in_range(attr_id, val) == 0) {
125 fprintf(stderr, "Invalid value '%s' for attribute '%s'\n",
126 setting->value, setting->variable);
130 attr_val[attr_id] = val;
133 for (i = 0; iter && i < G_N_ELEMENTS(strs); i++) {
134 if (streq(setting->variable, strs[i].name)) {
135 str_val[i] = setting->value;
142 fprintf(stderr, "Unknown attribute passed: %s\n", setting->variable);
147 if (backend_ctx.backend == GT_BACKEND_GADGETD) {
155 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
156 for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++) {
157 if (attr_val[i] == -1)
160 g_variant_builder_add(b, "{sv}",
161 usbg_get_gadget_attr_str(i),
162 g_variant_new(attr_type_get(i), attr_val[i]));
164 gattrs = g_variant_builder_end(b);
166 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
167 for (i = 0; i < G_N_ELEMENTS(strs); i++) {
168 if (str_val[i] == NULL)
171 g_variant_builder_add(b, "{sv}",
173 g_variant_new("s", str_val[i]));
175 gstrs = g_variant_builder_end(b);
177 v = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
180 "org.usb.device.GadgetManager",
182 g_variant_new("(s@a{sv}@a{sv})",
187 G_DBUS_CALL_FLAGS_NONE,
193 fprintf(stderr, "Unable to create gadget: %s\n", err->message);
200 } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
203 r = usbg_create_gadget(backend_ctx.libusbg_state,
208 if (r != USBG_SUCCESS) {
209 fprintf(stderr, "Unable to create gadget %s: %s\n",
210 dt->name, usbg_strerror(r));
214 for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++) {
215 if (attr_val[i] == -1)
218 r = usbg_set_gadget_attr(g, i, attr_val[i]);
219 if (r != USBG_SUCCESS) {
220 fprintf(stderr, "Unable to set attribute %s: %s\n",
221 usbg_get_gadget_attr_str(i),
227 for (i = 0; i < G_N_ELEMENTS(str_val); i++) {
228 if (str_val[i] == NULL)
231 r = strs[i].set_fn(g, LANG_US_ENG, str_val[i]);
232 if (r != USBG_SUCCESS) {
233 fprintf(stderr, "Unable to set string %s: %s\n",
243 usbg_rm_gadget(g, USBG_RM_RECURSE);
250 static int gt_gadget_create_help(void *data)
254 printf("usage: %s create NAME [attr=value]...\n"
255 "Create new gadget of specified name, attributes and language strings.\n"
260 for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++)
261 printf(" %s\n", usbg_get_gadget_attr_str(i));
263 printf("Device strings (en_US locale):\n");
265 for (i = 0; i < G_N_ELEMENTS(strs); i++)
266 printf(" %s\n", strs[i].name);
271 static void gt_parse_gadget_create(const Command *cmd, int argc, char **argv,
272 ExecutableCommand *exec, void *data)
274 struct gt_gadget_create_data *dt;
277 int avaible_opts = GT_FORCE;
279 dt = zalloc(sizeof(*dt));
283 ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
290 dt->name = argv[ind++];
292 c = gt_parse_setting_list(&dt->attrs, argc - ind, argv + ind);
296 executable_command_set(exec, gt_gadget_create_func,
297 (void *)dt, gt_gadget_create_destructor);
300 gt_gadget_create_destructor((void *)dt);
301 executable_command_set(exec, cmd->printHelp, data, NULL);
304 struct gt_gadget_rm_data {
309 static int gt_gadget_rm_help(void *data)
311 printf("Gadget rm help.\n");
315 static int gt_gadget_rm_func(void *data)
317 struct gt_gadget_rm_data *dt;
319 dt = (struct gt_gadget_rm_data *)data;
320 printf("Gadget rm called successfully. Not implemented.\n");
321 printf("name = %s, force = %d, recursive = %d\n",
322 dt->name, !!(dt->opts & GT_FORCE), !!(dt->opts & GT_RECURSIVE));
326 static void gt_parse_gadget_rm(const Command *cmd, int argc, char **argv,
327 ExecutableCommand *exec, void *data)
329 struct gt_gadget_rm_data *dt;
331 int avaible_opts = GT_FORCE | GT_RECURSIVE;
333 dt = zalloc(sizeof(*dt));
337 ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
343 dt->name = argv[ind];
344 executable_command_set(exec, gt_gadget_rm_func, (void *)dt,
349 executable_command_set(exec, cmd->printHelp, data, NULL);
352 struct gt_gadget_get_data {
357 static void gt_gadget_get_destructor(void *data)
359 struct gt_gadget_get_data *dt;
363 dt = (struct gt_gadget_get_data *)data;
369 static int gt_gadget_get_func(void *data)
371 struct gt_gadget_get_data *dt;
374 dt = (struct gt_gadget_get_data *)data;
375 printf("Gadget get called successfully. Not implemented yet.\n");
376 printf("name = %s, attrs = ", dt->name);
380 printf("%s, ", *ptr);
388 static int gt_gadget_get_help(void *data)
390 printf("Gadget get help.\n");
394 static void gt_parse_gadget_get(const Command *cmd, int argc, char **argv,
395 ExecutableCommand *exec, void * data)
397 struct gt_gadget_get_data *dt = NULL;
403 dt = zalloc(sizeof(*dt));
408 dt->attrs = calloc(argc, sizeof(char *));
409 if (dt->attrs == NULL)
413 for (i = 0; i < argc; i++) {
414 dt->attrs[i] = argv[i];
417 executable_command_set(exec, gt_gadget_get_func, (void *)dt,
418 gt_gadget_get_destructor);
422 gt_gadget_get_destructor((void *)dt);
423 executable_command_set(exec, cmd->printHelp, data, NULL);
426 struct gt_gadget_set_data {
428 struct gt_setting *attrs;
431 static void gt_gadget_set_destructor(void *data)
433 struct gt_gadget_set_data *dt;
437 dt = (struct gt_gadget_set_data *)data;
438 gt_setting_list_cleanup(dt->attrs);
442 static int gt_gadget_set_func(void *data)
444 struct gt_gadget_set_data *dt;
445 struct gt_setting *ptr;
447 dt = (struct gt_gadget_set_data *)data;
448 printf("Gadget set called successfully. Not implemented.\n");
449 printf("name = %s", dt->name);
451 while (ptr->variable) {
452 printf(", %s = %s", ptr->variable, ptr->value);
459 static int gt_gadget_set_help(void *data)
461 printf("Gadget set help.\n");
465 static void gt_parse_gadget_set(const Command *cmd, int argc, char **argv,
466 ExecutableCommand *exec, void * data)
468 struct gt_gadget_set_data *dt = NULL;
474 dt = zalloc(sizeof(*dt));
479 tmp = gt_parse_setting_list(&dt->attrs, argc-1, argv+1);
483 executable_command_set(exec, gt_gadget_set_func, (void *)dt,
484 gt_gadget_set_destructor);
487 gt_gadget_set_destructor((void *)dt);
488 executable_command_set(exec, cmd->printHelp, data, NULL);
491 struct gt_gadget_enable_data {
496 static int gt_gadget_enable_func(void *data)
498 struct gt_gadget_enable_data *dt;
500 dt = (struct gt_gadget_enable_data *)data;
501 printf("Gadget enable called successfully. Not implemented.\n");
503 printf("gadget = %s, ", dt->gadget);
505 printf("udc = %s", dt->udc);
510 static int gt_gadget_enable_help(void *data)
512 printf("Gadget enable help.\n");
516 static void gt_parse_gadget_enable(const Command *cmd, int argc,
517 char **argv, ExecutableCommand *exec, void * data)
519 struct gt_gadget_enable_data *dt;
521 struct option opts[] = {
522 {"gadget", required_argument, 0, 'g'},
523 {"udc", required_argument, 0, 'u'},
527 dt = zalloc(sizeof(*dt));
534 c = getopt_long(argc, argv, "g:u:", opts, &opt_index);
549 executable_command_set(exec, gt_gadget_enable_func, (void *)dt, free);
553 executable_command_set(exec, cmd->printHelp, data, NULL);
556 struct gt_gadget_disable_data {
561 static int gt_gadget_disable_func(void *data)
563 struct gt_gadget_disable_data *dt;
565 dt = (struct gt_gadget_disable_data *)data;
566 printf("Gadget disable called successfully. Not implemented.\n");
568 printf("gadget = %s, ", dt->gadget);
570 printf("udc = %s", dt->udc);
575 static int gt_gadget_disable_help(void *data)
577 printf("Gadget disable help.\n");
581 static void gt_parse_gadget_disable(const Command *cmd, int argc,
582 char **argv, ExecutableCommand *exec, void * data)
584 struct gt_gadget_disable_data *dt;
586 struct option opts[] = {
587 {"udc", required_argument, 0, 'u'},
591 dt = zalloc(sizeof(*dt));
598 c = getopt_long(argc, argv, "u:", opts, &opt_index);
610 if (optind < argc - 1 || (dt->udc && optind < argc)) {
611 printf("Too many arguments\n");
615 dt->gadget = argv[optind];
616 executable_command_set(exec, gt_gadget_disable_func, (void *)dt, free);
620 executable_command_set(exec, cmd->printHelp, data, NULL);
623 struct gt_gadget_gadget_data {
628 static int gt_gadget_gadget_func(void *data)
630 struct gt_gadget_gadget_data *dt;
632 dt = (struct gt_gadget_gadget_data *)data;
633 printf("Gadget gadget called successfully. Not implemented.\n");
635 printf("name = %s, ", dt->name);
636 printf("recursive = %d, verbose = %d\n",
637 !!(dt->opts & GT_RECURSIVE), !!(dt->opts & GT_VERBOSE));
641 static int gt_gadget_gadget_help(void *data)
643 printf("Gadget gadget help.\n");
647 static void gt_parse_gadget_gadget(const Command *cmd, int argc,
648 char **argv, ExecutableCommand *exec, void * data)
650 struct gt_gadget_gadget_data *dt;
652 int avaible_opts = GT_RECURSIVE | GT_VERBOSE;
654 dt = zalloc(sizeof(*dt));
658 ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
662 switch (argc - ind) {
666 dt->name = argv[ind];
672 executable_command_set(exec, gt_gadget_gadget_func, (void *)dt,
677 executable_command_set(exec, cmd->printHelp, data, NULL);
680 struct gt_gadget_load_data {
682 const char *gadget_name;
688 static int gt_gadget_load_func(void *data)
690 struct gt_gadget_load_data *dt;
692 dt = (struct gt_gadget_load_data *)data;
693 printf("Gadget load called successfully. Not implemented.\n");
695 printf("name = %s, ", dt->name);
697 printf("gadget = %s, ", dt->gadget_name);
699 printf("file %s, ", dt->file);
701 printf("path = %s, ", dt->path);
703 printf("off = %d, stdin = %d\n",
704 !!(dt->opts & GT_OFF), !!(dt->opts & GT_STDIN));
709 static int gt_gadget_load_help(void *data)
711 printf("Gadget load help\n");
715 static void gt_parse_gadget_load(const Command *cmd, int argc,
716 char **argv, ExecutableCommand *exec, void * data)
719 struct gt_gadget_load_data *dt;
720 struct option opts[] = {
721 {"off", no_argument, 0, 'o'},
722 {"file", required_argument, 0, 1},
723 {"stdin", no_argument, 0, 2},
724 {"path", required_argument, 0, 3},
728 dt = zalloc(sizeof(*dt));
735 c = getopt_long(argc, argv, "o", opts, &opt_index);
743 if (dt->path || dt->opts & GT_STDIN)
748 if (dt->path || dt->file)
750 dt->opts |= GT_STDIN;
753 if (dt->file || dt->opts & GT_STDIN)
762 if (optind == argc || optind < argc - 2)
765 dt->name = argv[optind++];
766 if (dt->opts & GT_STDIN || dt->file) {
767 dt->gadget_name = dt->name;
774 dt->gadget_name = argv[optind++];
776 executable_command_set(exec, gt_gadget_load_func, (void *)dt, free);
780 executable_command_set(exec, cmd->printHelp, data, NULL);
783 struct gt_gadget_save_data {
788 struct gt_setting *attrs;
792 static void gt_gadget_save_destructor(void *data)
794 struct gt_gadget_save_data *dt;
798 dt = (struct gt_gadget_save_data *)data;
799 gt_setting_list_cleanup(dt->attrs);
803 static int gt_gadget_save_func(void *data)
805 struct gt_gadget_save_data *dt;
806 struct gt_setting *ptr;
808 dt = (struct gt_gadget_save_data *)data;
809 printf("Gadget save called successfully. Not implemented\n");
811 printf("gadget = %s, ", dt->gadget);
813 printf("name = %s, ", dt->name);
815 printf("file = %s, ", dt->file);
817 printf("path = %s, ", dt->path);
818 printf("force = %d, stdout = %d",
819 !!(dt->opts & GT_FORCE), !!(dt->opts & GT_STDOUT));
822 while (ptr->variable) {
823 printf(", %s = %s", ptr->variable, ptr->value);
831 static int gt_gadget_save_help(void *data)
833 printf("Gadget save help.\n");
837 static void gt_parse_gadget_save(const Command *cmd, int argc,
838 char **argv, ExecutableCommand *exec, void * data)
841 struct gt_gadget_save_data *dt;
842 struct option opts[] = {
843 {"force", no_argument, 0, 'f'},
844 {"file", required_argument, 0, 1},
845 {"stdout", no_argument, 0, 2},
846 {"path", required_argument, 0, 3},
850 dt = zalloc(sizeof(*dt));
857 c = getopt_long(argc, argv, "f", opts, &opt_index);
862 dt->opts |= GT_FORCE;
865 if (dt->path || dt->opts & GT_STDOUT)
870 if (dt->file || dt->path)
872 dt->opts |= GT_STDOUT;
875 if (dt->file || dt->opts & GT_STDOUT)
887 dt->gadget = argv[optind++];
891 && !(dt->opts & GT_STDOUT)
892 && strchr(argv[optind], '=') == NULL)
893 dt->name = argv[optind++];
895 c = gt_parse_setting_list(&dt->attrs, argc - optind,
900 executable_command_set(exec, gt_gadget_save_func, (void *)dt,
901 gt_gadget_save_destructor);
905 gt_gadget_save_destructor((void *)dt);
906 executable_command_set(exec, cmd->printHelp, data, NULL);
909 struct gt_gadget_template_data {
914 static int gt_gadget_template_func(void *data)
916 struct gt_gadget_template_data *dt;
918 dt = (struct gt_gadget_template_data *)data;
919 printf("Gadget template called successfully. Not implemented.\n");
921 printf("name = %s, ", dt->name);
922 printf("verbose = %d, recursive = %d\n",
923 !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
927 static int gt_gadget_template_help(void *data)
929 printf("Gadget template help.\n");
933 static void gt_parse_gadget_template(const Command *cmd, int argc,
934 char **argv, ExecutableCommand *exec, void * data)
937 struct gt_gadget_template_data *dt;
938 int avaible_opts = GT_VERBOSE | GT_RECURSIVE;
940 dt = zalloc(sizeof(*dt));
943 ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
951 dt->name = argv[ind];
953 executable_command_set(exec, gt_gadget_template_func, (void *)dt,
959 executable_command_set(exec, cmd->printHelp, data, NULL);
962 static int gt_gadget_template_rm_func(void *data)
967 printf("Gadget template rm called successfully. Not implemented.\n");
968 printf("name = %s\n", dt);
972 static int gt_gadget_template_rm_help(void *data)
974 printf("Gadget template rm help.\n");
978 static void gt_parse_gadget_template_rm(const Command *cmd, int argc,
979 char **argv, ExecutableCommand *exec, void * data)
981 const char *dt = NULL;
987 executable_command_set(exec, gt_gadget_template_rm_func, (void *)dt,
992 executable_command_set(exec, cmd->printHelp, data, NULL);
995 struct gt_gadget_template_set_data {
997 struct gt_setting *attr;
1000 static void gt_gadget_template_set_destructor(void *data)
1002 struct gt_gadget_template_set_data *dt;
1006 dt = (struct gt_gadget_template_set_data *)data;
1007 gt_setting_list_cleanup(dt->attr);
1011 static int gt_gadget_template_set_func(void *data)
1013 struct gt_gadget_template_set_data *dt;
1014 struct gt_setting *ptr;
1016 dt = (struct gt_gadget_template_set_data *)data;
1017 printf("Gadget template set called successfully. Not implemened.\n");
1018 printf("name = %s", dt->name);
1020 while (ptr->variable) {
1021 printf(", %s = %s", ptr->variable, ptr->value);
1029 static int gt_gadget_template_set_help(void *data)
1031 printf("Gadget template set help.\n");
1035 static void gt_parse_gadget_template_set(const Command *cmd, int argc,
1036 char **argv, ExecutableCommand *exec, void * data)
1039 struct gt_gadget_template_set_data *dt;
1041 dt = zalloc(sizeof(*dt));
1049 tmp = gt_parse_setting_list(&dt->attr, argc-1, argv+1);
1053 executable_command_set(exec, gt_gadget_template_set_func, (void *)dt,
1054 gt_gadget_template_set_destructor);
1058 gt_gadget_template_set_destructor((void *)dt);
1059 executable_command_set(exec, cmd->printHelp, data, NULL);
1062 struct gt_gadget_template_get_data {
1067 static void gt_gadget_template_get_destructor(void *data)
1069 struct gt_gadget_template_get_data *dt;
1073 dt = (struct gt_gadget_template_get_data *)data;
1078 static int gt_gadget_template_get_func(void *data)
1080 struct gt_gadget_template_get_data *dt;
1083 dt = (struct gt_gadget_template_get_data *)data;
1084 printf("Gadget template get called successfully. Not implemented.\n");
1085 printf("name = %s, attr = ", dt->name);
1088 printf("%s, ", *ptr);
1096 static int gt_gadget_template_get_help(void *data)
1098 printf("Gadget template get help.\n");
1102 static void gt_parse_gadget_template_get(const Command *cmd, int argc,
1103 char **argv, ExecutableCommand *exec, void * data)
1106 struct gt_gadget_template_get_data *dt;
1108 dt = zalloc(sizeof(*dt));
1116 dt->attr = calloc(argc, sizeof(char *));
1117 if (dt->attr == NULL)
1122 for (i = 0; i < argc; i++)
1123 dt->attr[i] = argv[i];
1125 executable_command_set(exec, gt_gadget_template_get_func, (void *)dt,
1126 gt_gadget_template_get_destructor);
1130 gt_gadget_template_get_destructor((void *)dt);
1131 executable_command_set(exec, cmd->printHelp, data, NULL);
1134 const Command *get_gadget_template_children(const Command *cmd)
1136 static Command commands[] = {
1137 {"get", NEXT, gt_parse_gadget_template_get, NULL,
1138 gt_gadget_template_get_help},
1139 {"set", NEXT, gt_parse_gadget_template_set, NULL,
1140 gt_gadget_template_set_help},
1141 {"rm", NEXT, gt_parse_gadget_template_rm, NULL,
1142 gt_gadget_template_rm_help},
1143 {NULL, AGAIN, gt_parse_gadget_template, NULL,
1144 gt_gadget_template_help},
1151 const Command *get_gadget_children(const Command *cmd)
1153 static Command commands[] = {
1154 {"create", NEXT, gt_parse_gadget_create, NULL,
1155 gt_gadget_create_help},
1156 {"rm", NEXT, gt_parse_gadget_rm, NULL, gt_gadget_rm_help},
1157 {"get", NEXT, gt_parse_gadget_get, NULL, gt_gadget_get_help},
1158 {"set", NEXT, gt_parse_gadget_set, NULL, gt_gadget_set_help},
1159 {"enable", NEXT, gt_parse_gadget_enable, NULL,
1160 gt_gadget_enable_help},
1161 {"disable", NEXT, gt_parse_gadget_disable, NULL,
1162 gt_gadget_disable_help},
1163 {"gadget", NEXT, gt_parse_gadget_gadget, NULL,
1164 gt_gadget_gadget_help},
1165 {"template", NEXT, command_parse, get_gadget_template_children,
1166 gt_gadget_template_help},
1167 {"load", NEXT, gt_parse_gadget_load, NULL,
1168 gt_gadget_load_help},
1169 {"save", NEXT, gt_parse_gadget_save, NULL,
1170 gt_gadget_save_help},
1177 int gt_gadget_help(void *data)
1179 printf("Gadget help function\n");