From 7d961a9e7b82b0b2be9243770c191322014348f7 Mon Sep 17 00:00:00 2001
From: Pawel Szewczyk
Date: Tue, 5 Aug 2014 13:40:16 +0200
Subject: [PATCH] gt: Add gadget get and set comands parsing
Change-Id: I206fca8de0f17f64d444381d3a9b38b5848f7ca9
Signed-off-by: Pawel Szewczyk
---
source/gadget/src/gadget.c | 143 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 141 insertions(+), 2 deletions(-)
diff --git a/source/gadget/src/gadget.c b/source/gadget/src/gadget.c
index c18e75c..c562f30 100644
--- a/source/gadget/src/gadget.c
+++ b/source/gadget/src/gadget.c
@@ -147,14 +147,153 @@ out:
executable_command_set(exec, cmd->printHelp, data, NULL);
}
+struct gt_gadget_get_data {
+ const char *name;
+ const char **attrs;
+};
+
+static void gt_gadget_get_destructor(void *data)
+{
+ struct gt_gadget_get_data *dt;
+
+ if (data == NULL)
+ return;
+ dt = (struct gt_gadget_get_data *)data;
+
+ free(dt->attrs);
+ free(dt);
+}
+
+static int gt_gadget_get_func(void *data)
+{
+ struct gt_gadget_get_data *dt;
+ const char **ptr;
+
+ 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++;
+ }
+
+ putchar('\n');
+ return 0;
+}
+
+static int gt_gadget_get_help(void *data)
+{
+ printf("Gadget get help.\n");
+ return -1;
+}
+
+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;
+
+ if (argc == 0)
+ goto out;
+
+ dt = zalloc(sizeof(*dt));
+ if (dt == NULL)
+ goto out;
+
+ dt->name = argv[0];
+ dt->attrs = calloc(argc, sizeof(char *));
+ if (dt->attrs == NULL)
+ goto out;
+
+ argv++;
+ for (i = 0; i < argc; i++) {
+ dt->attrs[i] = argv[i];
+ }
+
+ executable_command_set(exec, gt_gadget_get_func, (void *)dt,
+ gt_gadget_get_destructor);
+
+ return;
+out:
+ gt_gadget_get_destructor((void *)dt);
+ executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+struct gt_gadget_set_data {
+ const char *name;
+ struct gt_setting *attrs;
+};
+
+static void gt_gadget_set_destructor(void *data)
+{
+ struct gt_gadget_set_data *dt;
+
+ if (data == NULL)
+ return;
+ dt = (struct gt_gadget_set_data *)data;
+ gt_setting_list_cleanup(dt->attrs);
+ free(dt);
+}
+
+static int gt_gadget_set_func(void *data)
+{
+ struct gt_gadget_set_data *dt;
+ struct gt_setting *ptr;
+
+ 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++;
+ }
+ putchar('\n');
+ return 0;
+}
+
+static int gt_gadget_set_help(void *data)
+{
+ printf("Gadget set help.\n");
+ return -1;
+}
+
+static void gt_parse_gadget_set(const Command *cmd, int argc, char **argv,
+ ExecutableCommand *exec, void * data)
+{
+ struct gt_gadget_set_data *dt = NULL;
+ int tmp;
+
+ if (argc < 2)
+ goto out;
+
+ dt = zalloc(sizeof(*dt));
+ if (dt == NULL)
+ goto out;
+
+ dt->name = argv[0];
+ tmp = gt_parse_setting_list(&dt->attrs, argc-1, argv+1);
+ if (tmp < 0)
+ goto out;
+
+ executable_command_set(exec, gt_gadget_set_func, (void *)dt,
+ gt_gadget_set_destructor);
+ return;
+out:
+ gt_gadget_set_destructor((void *)dt);
+ executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
const Command *get_gadget_children(const Command *cmd)
{
static Command commands[] = {
{"create", NEXT, gt_parse_gadget_create, NULL,
gt_gadget_create_help},
{"rm", NEXT, gt_parse_gadget_rm, NULL, gt_gadget_rm_help},
-// {"get", AGAIN, gt_parse_gadget_get, NULL, gt_gadget_get_help},
-// {"set", AGAIN, gt_parse_gadget_set, NULL, gt_gadget_set_help},
+ {"get", NEXT, gt_parse_gadget_get, NULL, gt_gadget_get_help},
+ {"set", NEXT, gt_parse_gadget_set, NULL, gt_gadget_set_help},
// {"enable", AGAIN, gt_parse_gadget_enable, NULL,
// gt_gadget_enable_help},
// {"disable", parse_gadget_disable, NULL, gadget_disable_help_func},
--
2.7.4