config: Separate config command backends
authorPawel Szewczyk <p.szewczyk@samsung.com>
Wed, 1 Jul 2015 15:42:22 +0000 (17:42 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Tue, 7 Jul 2015 12:26:25 +0000 (14:26 +0200)
Change-Id: I53adcfc32c121710e1ff8baae41d169e6120ae9e
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/base/include/backend.h
source/base/src/backend.c
source/config/CMakeLists.txt
source/config/include/configuration.h
source/config/src/configuration.c
source/config/src/configuration_gadgetd.c [new file with mode: 0644]
source/config/src/configuration_libusbg.c [new file with mode: 0644]
source/config/src/configuration_not_implemented.c [new file with mode: 0644]

index c2d008d..de3024d 100644 (file)
@@ -45,6 +45,7 @@ enum gt_backend_type {
 struct gt_backend {
        struct gt_function_backend *function;
        struct gt_gadget_backend *gadget;
+       struct gt_config_backend *config;
 };
 
 struct gt_backend_ctx {
index 39c2bc3..f672fe6 100644 (file)
@@ -23,6 +23,7 @@
 #include "backend.h"
 #include "function.h"
 #include "gadget.h"
+#include "configuration.h"
 
 struct gt_backend_ctx backend_ctx = {
        .backend_type = GT_BACKEND_AUTO,
@@ -31,11 +32,13 @@ struct gt_backend_ctx backend_ctx = {
 struct gt_backend gt_backend_libusbg = {
        .function = &gt_function_backend_libusbg,
        .gadget = &gt_gadget_backend_libusbg,
+       .config = &gt_config_backend_libusbg,
 };
 
 struct gt_backend gt_backend_gadgetd = {
        .function = &gt_function_backend_gadgetd,
        .gadget = &gt_gadget_backend_gadgetd,
+       .config = &gt_config_backend_gadgetd,
 };
 
 int gt_backend_init(const char *program_name, enum gt_option_flags flags)
index ca55338..ecdaf68 100644 (file)
@@ -3,6 +3,9 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
 
 SET( CONFIG_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration_gadgetd.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration_libusbg.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration_not_implemented.c
        )
 
-add_library(config STATIC ${CONFIG_SRC} )
\ No newline at end of file
+add_library(config STATIC ${CONFIG_SRC} )
index 1497fff..cb4e861 100644 (file)
 #include "command.h"
 
 /**
+ * An interface that backends need to implement. Not implemented functions
+ * should be filled with NULL pointers. For each function the only argument
+ * passed is a pointer to corresponding structure.
+ */
+struct gt_config_backend {
+       /**
+        * Create a configuration
+        */
+       int (*create)(void *);
+       /**
+        * Remove a configuration
+        */
+       int (*rm)(void *);
+       /**
+        * Get configuration attributes
+        */
+       int (*get)(void *);
+       /**
+        * Set configuration attributes
+        */
+       int (*set)(void *);
+       /**
+        * Show configs
+        */
+       int (*config)(void *);
+       /**
+        * Add a function to configuration
+        */
+       int (*add)(void *);
+       /**
+        * Remove function from configuration
+        */
+       int (*del)(void *);
+       /**
+        * Show templates
+        */
+       int (*template_default)(void *);
+       /**
+        * Show templates attributes
+        */
+       int (*template_get)(void *);
+       /**
+        * Set templates attributes
+        */
+       int (*template_set)(void *);
+       /**
+        * Remove template
+        */
+       int (*template_rm)(void *);
+       /**
+        * Load config from file
+        */
+       int (*load)(void *);
+       /**
+        * Save config to file
+        */
+       int (*save)(void *);
+};
+
+struct gt_config_create_data {
+       const char *gadget;
+       int config_id;
+       const char *config_label;
+       int opts;
+       struct gt_setting *attrs;
+};
+
+struct gt_config_rm_data {
+       const char *gadget;
+       const char *config;
+       int opts;
+};
+
+struct gt_config_get_data {
+       const char *gadget;
+       const char *config;
+       const char **attrs;
+};
+
+struct gt_config_set_data {
+       const char *gadget;
+       const char *config;
+       struct gt_setting *attrs;
+};
+
+struct gt_config_config_data {
+       const char *gadget;
+       const char *config;
+       int opts;
+};
+
+struct gt_config_add_del_data {
+       const char *gadget;
+       int   config_id;
+       const char *config_label;
+       const char *type;
+       const char *instance;
+};
+
+struct gt_config_template_data {
+       const char *name;
+       int opts;
+};
+
+struct gt_config_template_get_data {
+       const char *name;
+       const char **attr;
+};
+
+struct gt_config_template_set_data {
+       const char *name;
+       struct gt_setting *attr;
+};
+
+struct gt_config_load_data {
+       const char *name;
+       const char *gadget;
+       const char *config;
+       const char *file;
+       const char *path;
+       int opts;
+};
+
+struct gt_config_save_data {
+       const char *gadget;
+       const char *config;
+       const char *name;
+       const char *file;
+       const char *path;
+       int opts;
+       struct gt_setting *attrs;
+};
+
+/**
  * @brief Gets the next possible commands after config
  * @param[in] cmd actual command (should be config)
  * @return Pointer to table with all children of cmd
@@ -37,4 +171,8 @@ const Command *gt_config_get_children(const Command *cmd);
  */
 int gt_config_help(void *data);
 
+extern struct gt_config_backend gt_config_backend_gadgetd;
+extern struct gt_config_backend gt_config_backend_libusbg;
+extern struct gt_config_backend gt_config_backend_not_implemented;
+
 #endif //__GADGET_TOOL_CONFIGURATION_CONFIGURATION_H__
index a0f38de..55ca5c8 100644 (file)
 #include "configuration.h"
 #include "common.h"
 #include "parser.h"
+#include "backend.h"
 
 #include <errno.h>
-#include <backend.h>
 #include <gio/gio.h>
 
+#define GET_EXECUTABLE(func) \
+       (backend_ctx.backend->config->func ? \
+        backend_ctx.backend->config->func : \
+        gt_config_backend_not_implemented.func)
+
 int gt_config_help(void *data)
 {
        printf("Configuration help function\n");
        return -1;
 }
 
-struct gt_config_create_data {
-       const char *gadget;
-       int config_id;
-       const char *config_label;
-       int opts;
-       struct gt_setting *attrs;
-};
-
 static void gt_config_create_destructor(void *data)
 {
        struct gt_config_create_data *dt;
@@ -53,82 +50,6 @@ static void gt_config_create_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_create_func(void *data)
-{
-       struct gt_config_create_data *dt;
-
-       dt = (struct gt_config_create_data *)data;
-
-       /* TODO implement -f option */
-       if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
-               GVariant *gret;
-               GError *error = NULL;
-               _cleanup_g_free_ gchar *path = NULL;
-               _cleanup_g_free_ gchar *out_config_path = NULL;
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  "/org/usb/Gadget",
-                                                  "org.usb.device.GadgetManager",
-                                                  "FindGadgetByName",
-                                                  g_variant_new ("(s)",
-                                                                 dt->gadget),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-
-               if (error) {
-                       fprintf(stderr, "Failed to get gadget path, %s\n", error->message);
-                       return -1;
-               }
-
-               g_variant_get(gret, "(o)", &path);
-               g_variant_unref(gret);
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  path,
-                                                  "org.usb.device.Gadget.ConfigManager",
-                                                  "CreateConfig",
-                                                  g_variant_new ("(is)",
-                                                                 dt->config_id,
-                                                                 dt->config_label),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-               if (error) {
-                       fprintf(stderr, "Unknown error, %s\n", error->message);
-                       return -1;
-               }
-               g_variant_get(gret, "(o)", &out_config_path);
-               g_variant_unref(gret);
-       } else {
-               int usbg_ret;
-               usbg_gadget *g;
-               usbg_config *c;
-
-               g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
-               if (g == NULL) {
-                       fprintf(stderr, "Cant get gadget by name\n");
-                       return -1;
-               }
-
-               usbg_ret = usbg_create_config(g, dt->config_id, dt->config_label, NULL, NULL, &c);
-               if (usbg_ret != USBG_SUCCESS) {
-                       fprintf(stderr,"Error on config create\n");
-                       fprintf(stderr,"Error: %s: %s\n", usbg_error_name(usbg_ret),
-                               usbg_strerror(usbg_ret));
-                       return -1;
-               }
-       }
-
-       return 0;
-}
-
 static int gt_config_create_help(void *data)
 {
        printf("usage: %s config create GADGET_NAME <cf_label>  <cfg_id> <-f>\n"
@@ -170,7 +91,7 @@ static void gt_parse_config_create(const Command *cmd, int argc, char **argv,
        if (c < 0)
                goto out;
 
-       executable_command_set(exec, gt_config_create_func,
+       executable_command_set(exec, GET_EXECUTABLE(create),
                               (void *)dt, gt_config_create_destructor);
        return;
 
@@ -179,25 +100,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_rm_data {
-       const char *gadget;
-       const char *config;
-       int opts;
-};
-
-static int gt_config_rm_func(void *data)
-{
-       struct gt_config_rm_data *dt;
-
-       dt = (struct gt_config_rm_data *)data;
-       printf("Config rm called successfully. Not implemented.\n");
-       printf("gadget = %s, config = %s, force = %d, recursive = %d\n",
-               dt->gadget, dt->config, !!(dt->opts & GT_FORCE),
-               !!(dt->opts & GT_RECURSIVE));
-
-       return 0;
-}
-
 static int gt_config_rm_help(void *data)
 {
        printf("Config rm help.\n");
@@ -223,7 +125,7 @@ static void gt_parse_config_rm(const Command *cmd, int argc, char **argv,
 
        dt->gadget = argv[ind++];
        dt->config = argv[ind++];
-       executable_command_set(exec, gt_config_rm_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(rm), (void *)dt, free);
 
        return;
 out:
@@ -231,12 +133,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_get_data {
-       const char *gadget;
-       const char *config;
-       const char **attrs;
-};
-
 static void gt_config_get_destructor(void *data)
 {
        struct gt_config_get_data *dt;
@@ -248,24 +144,6 @@ static void gt_config_get_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_get_func(void *data)
-{
-       struct gt_config_get_data *dt;
-       const char **ptr;
-
-       dt = (struct gt_config_get_data *)data;
-       printf("Config get called successfully. Not implemented.\n");
-       printf("gadget = %s, config = %s, attrs = ",
-               dt->gadget, dt->config);
-       ptr = dt->attrs;
-       while (*ptr) {
-               printf("%s, ", *ptr);
-               ptr++;
-       }
-       putchar('\n');
-       return 0;
-}
-
 static int gt_config_get_help(void *data)
 {
        printf("Config get help.\n");
@@ -296,7 +174,7 @@ static void gt_parse_config_get(const Command *cmd, int argc, char **argv,
        for (i = 0; argv[i]; i++)
                dt->attrs[i] = argv[i];
 
-       executable_command_set(exec, gt_config_get_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(get), (void *)dt,
                        gt_config_get_destructor);
        return;
 out:
@@ -304,12 +182,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_set_data {
-       const char *gadget;
-       const char *config;
-       struct gt_setting *attrs;
-};
-
 static void gt_config_set_destructor(void *data)
 {
        struct gt_config_set_data *dt;
@@ -321,24 +193,6 @@ static void gt_config_set_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_set_func(void *data)
-{
-       struct gt_config_set_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_config_set_data *)data;
-       printf("Config set called successfully. Not implemented.\n");
-       printf("gadget = %s, config = %s", dt->gadget, dt->config);
-       ptr = dt->attrs;
-       while (ptr->variable) {
-               printf(", %s = %s", ptr->variable, ptr->value);
-               ptr++;
-       }
-
-       putchar('\n');
-       return 0;
-}
-
 static int gt_config_set_help(void *data)
 {
        printf("Config set help.\n");
@@ -363,7 +217,7 @@ static void gt_parse_config_set(const Command *cmd, int argc, char **argv,
        if (tmp < 0)
                goto out;
 
-       executable_command_set(exec, gt_config_set_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(set), (void *)dt,
                        gt_config_set_destructor);
        return;
 out:
@@ -371,27 +225,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_config_data {
-       const char *gadget;
-       const char *config;
-       int opts;
-};
-
-static int gt_config_config_func(void *data)
-{
-       struct gt_config_config_data *dt;
-
-       dt = (struct gt_config_config_data *)data;
-       printf("Config config called successfully. Not implemented.\n");
-       printf("gadget = %s", dt->gadget);
-       if (dt->config)
-               printf(", config = %s", dt->config);
-       printf(", verbose = %d, recursive = %d\n",
-               !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
-
-       return 0;
-}
-
 static int gt_config_config_help(void *data)
 {
        printf("Config config help.\n");
@@ -420,7 +253,7 @@ static void gt_parse_config_config(const Command *cmd, int argc, char **argv,
        if (ind < argc)
                dt->config = argv[ind++];
 
-       executable_command_set(exec, gt_config_config_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(config), (void *)dt, free);
 
        return;
 out:
@@ -428,167 +261,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_add_del_data {
-       const char *gadget;
-       int   config_id;
-       const char *config_label;
-       const char *type;
-       const char *instance;
-};
-
-static int gt_config_add_func(void *data)
-{
-       struct gt_config_add_del_data *dt;
-
-       dt = (struct gt_config_add_del_data *)data;
-
-       if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
-               _cleanup_g_free_ gchar *gpath = NULL;
-               _cleanup_g_free_ gchar *fpath = NULL;
-               _cleanup_g_free_ gchar *cpath = NULL;
-               GVariant *gret;
-               GError *error = NULL;
-               gboolean function_added;
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  "/org/usb/Gadget",
-                                                  "org.usb.device.GadgetManager",
-                                                  "FindGadgetByName",
-                                                  g_variant_new ("(s)",
-                                                                 dt->gadget),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-
-               if (error) {
-                       fprintf(stderr, "Failed to find gadget, %s\n", error->message);
-                       return -1;
-               }
-
-               g_variant_get(gret, "(o)", &gpath);
-               g_variant_unref(gret);
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  gpath,
-                                                  "org.usb.device.Gadget.FunctionManager",
-                                                  "FindFunctionByName",
-                                                  g_variant_new ("(ss)",
-                                                                 dt->type,
-                                                                 dt->instance),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-
-               if (error) {
-                       fprintf(stderr, "Failed to find function, %s\n", error->message);
-                       return -1;
-               }
-
-               g_variant_get(gret, "(o)", &fpath);
-               g_variant_unref(gret);
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  gpath,
-                                                  "org.usb.device.Gadget.ConfigManager",
-                                                  "FindConfigByName",
-                                                  g_variant_new ("(is)",
-                                                                 dt->config_id,
-                                                                 dt->config_label),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-
-               if (error) {
-                       fprintf(stderr, "Failed to find config, %s\n", error->message);
-                       return -1;
-               }
-
-               g_variant_get(gret, "(o)", &cpath);
-               g_variant_unref(gret);
-
-               gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                                  "org.usb.gadgetd",
-                                                  cpath,
-                                                  "org.usb.device.Gadget.Config",
-                                                  "AttachFunction",
-                                                  g_variant_new ("(o)", fpath),
-                                                  NULL,
-                                                  G_DBUS_CALL_FLAGS_NONE,
-                                                  -1,
-                                                  NULL,
-                                                  &error);
-
-               if (error) {
-                       fprintf(stderr,"Failed to attach function, %s\n", error->message);
-                       return -1;
-               }
-
-               g_variant_get(gret, "(b)", &function_added);
-               g_variant_unref(gret);
-
-       } else if (backend_ctx.backend_type == GT_BACKEND_LIBUSBG) {
-               int usbg_ret = USBG_SUCCESS;
-               usbg_function_type f_type;
-               usbg_gadget *g;
-               usbg_function *f;
-               usbg_config *c;
-               int n;
-               char buff[255];
-               _cleanup_g_free_ gchar *func_name = NULL;
-               const char *cfg_label = NULL;
-
-               cfg_label = dt->config_label;
-
-               /* func_name cant be NULL (libusbg requirement) */
-               n = snprintf(buff, 255, "%s.%s", dt->type, dt->instance);
-               if (n < 0)
-                       return -1;
-               func_name = strdup(buff);
-
-               f_type = usbg_lookup_function_type(dt->type);
-
-               g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
-               if (g == NULL) {
-                       fprintf(stderr, "Unable to get gadget\n");
-                       return -1;
-               }
-
-               f = usbg_get_function(g, f_type, dt->instance);
-               if (f == NULL) {
-                       fprintf(stderr, "Unable to get function\n");
-                       return -1;
-               }
-
-               if (strcmp(dt->config_label, "") == 0)
-                       cfg_label = NULL;
-
-               c = usbg_get_config(g, dt->config_id, cfg_label);
-               if (c == NULL) {
-                       fprintf(stderr, "Unable to get config\n");
-                       return -1;
-               }
-
-               usbg_ret = usbg_add_config_function(c, func_name, f);
-               if (usbg_ret != USBG_SUCCESS) {
-                       fprintf(stderr, "Unable to attach function\n");
-                       fprintf(stderr,"Error: %s: %s\n", usbg_error_name(usbg_ret),
-                               usbg_strerror(usbg_ret));
-                       return -1;
-               }
-       }
-
-       return 0;
-}
-
 static int gt_config_add_help(void *data)
 {
        printf("usage: %s config add GADGET_NAME <cfg_id> <cf_label> <func_type> <func_instance>\n"
@@ -636,7 +308,7 @@ static void gt_parse_config_add(const Command *cmd, int argc, char **argv,
 
        dt->gadget = argv[0];
 
-       executable_command_set(exec, gt_config_add_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(add), (void *)dt, free);
 
        return;
 out:
@@ -644,18 +316,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-static int gt_config_del_func(void *data)
-{
-       struct gt_config_add_del_data *dt;
-
-       dt = (struct gt_config_add_del_data *)data;
-       printf("Config del called successfully. Not implemented.\n");
-       printf("gadget = %s, cfg_label = %s, cfg_id = %d, type = %s, instance = %s\n",
-                       dt->gadget, dt->config_label, dt->config_id, dt->type, dt->instance);
-
-       return 0;
-}
-
 static int gt_config_del_help(void *data)
 {
        printf("Config del help.\n");
@@ -699,7 +359,7 @@ static void gt_parse_config_del(const Command *cmd, int argc, char **argv,
 
        dt->gadget = argv[0];
 
-       executable_command_set(exec, gt_config_del_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(del), (void *)dt, free);
 
        return;
 out:
@@ -707,24 +367,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_template_data {
-       const char *name;
-       int opts;
-};
-
-static int gt_config_template_func(void *data)
-{
-       struct gt_config_template_data *dt;
-
-       dt = (struct gt_config_template_data *)data;
-       printf("Config template called successfully. Not implemented.\n");
-       if (dt->name)
-               printf("name = %s, ", dt->name);
-       printf("verbose = %d, recursive = %d\n",
-               !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
-       return 0;
-}
-
 static int gt_config_template_help(void *data)
 {
        printf("Config template help.\n");
@@ -751,7 +393,7 @@ static void gt_parse_config_template(const Command *cmd, int argc, char **argv,
 
        dt->name = argv[ind];
 
-       executable_command_set(exec, gt_config_template_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(template_default), (void *)dt, free);
 
        return;
 out:
@@ -759,11 +401,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_template_get_data {
-       const char *name;
-       const char **attr;
-};
-
 static void gt_config_template_get_destructor(void *data)
 {
        struct gt_config_template_get_data *dt;
@@ -775,24 +412,6 @@ static void gt_config_template_get_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_template_get_func(void *data)
-{
-       struct gt_config_template_get_data *dt;
-       const char **ptr;
-
-       dt = (struct gt_config_template_get_data *)data;
-       printf("Config template get called successfully. Not implemented.\n");
-       printf("name = %s, attr = ", dt->name);
-       ptr = dt->attr;
-       while (*ptr) {
-               printf("%s, ", *ptr);
-               ptr++;
-       }
-
-       putchar('\n');
-       return 0;
-}
-
 static int gt_config_template_get_help(void *data)
 {
        printf("Config template get help.\n");
@@ -821,7 +440,7 @@ static void gt_parse_config_template_get(const Command *cmd, int argc,
        for (i = 0; argv[i]; i++)
                dt->attr[i] = argv[i];
 
-       executable_command_set(exec, gt_config_template_get_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(template_get), (void *)dt,
                gt_config_template_get_destructor);
 
        return;
@@ -830,11 +449,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_template_set_data {
-       const char *name;
-       struct gt_setting *attr;
-};
-
 static void gt_config_template_set_destructor(void *data)
 {
        struct gt_config_template_set_data *dt;
@@ -846,24 +460,6 @@ static void gt_config_template_set_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_template_set_func(void *data)
-{
-       struct gt_config_template_set_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_config_template_set_data *)data;
-       printf("Config template set called successfully. Not implemened.\n");
-       printf("name = %s", dt->name);
-       ptr = dt->attr;
-       while (ptr->variable) {
-               printf(", %s = %s", ptr->variable, ptr->value);
-               ptr++;
-       }
-
-       putchar('\n');
-       return 0;
-}
-
 static int gt_config_template_set_help(void *data)
 {
        printf("Config template set help.\n");
@@ -888,7 +484,7 @@ static void gt_parse_config_template_set(const Command *cmd, int argc,
        if (tmp < 0)
                goto out;
 
-       executable_command_set(exec, gt_config_template_set_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(template_set), (void *)dt,
                gt_config_template_set_destructor);
 
        return;
@@ -897,16 +493,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-static int gt_config_template_rm_func(void *data)
-{
-       const char *dt;
-
-       dt = (const char *)data;
-       printf("Config template rm called successfully. Not implemented.\n");
-       printf("name = %s\n", dt);
-       return 0;
-}
-
 static int gt_config_template_rm_help(void *data)
 {
        printf("Config template rm help.\n");
@@ -922,7 +508,7 @@ static void gt_parse_config_template_rm(const Command *cmd, int argc,
                goto out;
 
        dt = argv[0];
-       executable_command_set(exec, gt_config_template_rm_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(template_rm), (void *)dt,
                        NULL);
 
        return;
@@ -947,38 +533,6 @@ static const Command *gt_config_template_get_children(const Command *cmd)
        return commands;
 }
 
-struct gt_config_load_data {
-       const char *name;
-       const char *gadget;
-       const char *config;
-       const char *file;
-       const char *path;
-       int opts;
-};
-
-static int gt_config_load_func(void *data)
-{
-       struct gt_config_load_data *dt;
-
-       dt = (struct gt_config_load_data *)data;
-       printf("Config load called successfully. Not implemented.\n");
-       if (dt->name)
-               printf("name = %s, ", dt->name);
-       if (dt->gadget)
-               printf("gadget = %s, ", dt->gadget);
-       if (dt->config)
-               printf("config = %s, ", dt->config);
-       if (dt->file)
-               printf("file = %s, ", dt->file);
-       if (dt->path)
-               printf("path = %s, ", dt->path);
-       printf("recursive = %d, force = %d, stdin = %d\n",
-               !!(dt->opts & GT_RECURSIVE), !!(dt->opts & GT_FORCE),
-               !!(dt->opts & GT_STDIN));
-
-       return 0;
-}
-
 static int gt_config_load_help(void *data)
 {
        printf("Config load help.\n");
@@ -1052,7 +606,7 @@ static void gt_parse_config_load(const Command *cmd, int argc,
        if (optind < argc)
                dt->config = argv[optind++];
 
-       executable_command_set(exec, gt_config_load_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(load), (void *)dt, free);
 
        return;
 out:
@@ -1060,16 +614,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_config_save_data {
-       const char *gadget;
-       const char *config;
-       const char *name;
-       const char *file;
-       const char *path;
-       int opts;
-       struct gt_setting *attrs;
-};
-
 static void gt_config_save_destructor(void *data)
 {
        struct gt_config_save_data *dt;
@@ -1081,38 +625,6 @@ static void gt_config_save_destructor(void *data)
        free(dt);
 }
 
-static int gt_config_save_func(void *data)
-{
-       struct gt_config_save_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_config_save_data *)data;
-       printf("Config save called successfully. Not implemented.\n");
-       if (dt->gadget)
-               printf("gadget=%s, ", dt->gadget);
-       if (dt->config)
-               printf("config=%s, ", dt->config);
-       if (dt->name)
-               printf("name=%s, ", dt->name);
-       if (dt->file)
-               printf("file=%s, ", dt->file);
-       if (dt->path)
-               printf("path=%s, ", dt->path);
-       printf("force=%d, stdout=%d",
-               !!(dt->opts & GT_FORCE), !!(dt->opts & GT_STDOUT));
-
-       ptr = dt->attrs;
-       while (ptr->variable) {
-               printf(", %s = %s", ptr->variable, ptr->value);
-               ptr++;
-       }
-
-       putchar('\n');
-
-
-       return 0;
-}
-
 static int gt_config_save_help(void *data)
 {
        printf("Config save help.\n");
@@ -1183,7 +695,7 @@ static void gt_parse_config_save(const Command *cmd, int argc,
        if (c < 0)
                goto out;
 
-       executable_command_set(exec, gt_config_save_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(save), (void *)dt,
                        gt_config_save_destructor);
 
        return;
diff --git a/source/config/src/configuration_gadgetd.c b/source/config/src/configuration_gadgetd.c
new file mode 100644 (file)
index 0000000..c876df5
--- /dev/null
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "configuration.h"
+#include "common.h"
+#include "parser.h"
+#include "backend.h"
+
+#include <errno.h>
+#include <gio/gio.h>
+
+static int create_func(void *data)
+{
+       struct gt_config_create_data *dt;
+
+       dt = (struct gt_config_create_data *)data;
+
+       /* TODO implement -f option */
+       GVariant *gret;
+       GError *error = NULL;
+       _cleanup_g_free_ gchar *path = NULL;
+       _cleanup_g_free_ gchar *out_config_path = NULL;
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          "/org/usb/Gadget",
+                                          "org.usb.device.GadgetManager",
+                                          "FindGadgetByName",
+                                          g_variant_new ("(s)",
+                                                         dt->gadget),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+
+       if (error) {
+               fprintf(stderr, "Failed to get gadget path, %s\n", error->message);
+               return -1;
+       }
+
+       g_variant_get(gret, "(o)", &path);
+       g_variant_unref(gret);
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          path,
+                                          "org.usb.device.Gadget.ConfigManager",
+                                          "CreateConfig",
+                                          g_variant_new ("(is)",
+                                                         dt->config_id,
+                                                         dt->config_label),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+       if (error) {
+               fprintf(stderr, "Unknown error, %s\n", error->message);
+               return -1;
+       }
+       g_variant_get(gret, "(o)", &out_config_path);
+       g_variant_unref(gret);
+
+       return 0;
+}
+
+static int add_func(void *data)
+{
+       struct gt_config_add_del_data *dt;
+
+       dt = (struct gt_config_add_del_data *)data;
+
+       _cleanup_g_free_ gchar *gpath = NULL;
+       _cleanup_g_free_ gchar *fpath = NULL;
+       _cleanup_g_free_ gchar *cpath = NULL;
+       GVariant *gret;
+       GError *error = NULL;
+       gboolean function_added;
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          "/org/usb/Gadget",
+                                          "org.usb.device.GadgetManager",
+                                          "FindGadgetByName",
+                                          g_variant_new ("(s)",
+                                                         dt->gadget),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+
+       if (error) {
+               fprintf(stderr, "Failed to find gadget, %s\n", error->message);
+               return -1;
+       }
+
+       g_variant_get(gret, "(o)", &gpath);
+       g_variant_unref(gret);
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          gpath,
+                                          "org.usb.device.Gadget.FunctionManager",
+                                          "FindFunctionByName",
+                                          g_variant_new ("(ss)",
+                                                         dt->type,
+                                                         dt->instance),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+
+       if (error) {
+               fprintf(stderr, "Failed to find function, %s\n", error->message);
+               return -1;
+       }
+
+       g_variant_get(gret, "(o)", &fpath);
+       g_variant_unref(gret);
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          gpath,
+                                          "org.usb.device.Gadget.ConfigManager",
+                                          "FindConfigByName",
+                                          g_variant_new ("(is)",
+                                                         dt->config_id,
+                                                         dt->config_label),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+
+       if (error) {
+               fprintf(stderr, "Failed to find config, %s\n", error->message);
+               return -1;
+       }
+
+       g_variant_get(gret, "(o)", &cpath);
+       g_variant_unref(gret);
+
+       gret = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                          "org.usb.gadgetd",
+                                          cpath,
+                                          "org.usb.device.Gadget.Config",
+                                          "AttachFunction",
+                                          g_variant_new ("(o)", fpath),
+                                          NULL,
+                                          G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+
+       if (error) {
+               fprintf(stderr,"Failed to attach function, %s\n", error->message);
+               return -1;
+       }
+
+       g_variant_get(gret, "(b)", &function_added);
+       g_variant_unref(gret);
+
+       return 0;
+}
+
+struct gt_config_backend gt_config_backend_gadgetd = {
+       .create = create_func,
+       .add = add_func,
+       .rm = NULL,
+       .get = NULL,
+       .set = NULL,
+       .config = NULL,
+       .del = NULL,
+       .template_default = NULL,
+       .template_get = NULL,
+       .template_set = NULL,
+       .template_rm = NULL,
+       .load = NULL,
+       .save = NULL,
+};
diff --git a/source/config/src/configuration_libusbg.c b/source/config/src/configuration_libusbg.c
new file mode 100644 (file)
index 0000000..e052e81
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "configuration.h"
+#include "common.h"
+#include "parser.h"
+#include "backend.h"
+
+static int create_func(void *data)
+{
+       struct gt_config_create_data *dt;
+
+       dt = (struct gt_config_create_data *)data;
+
+       /* TODO implement -f option */
+       int usbg_ret;
+       usbg_gadget *g;
+       usbg_config *c;
+
+       g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
+       if (g == NULL) {
+               fprintf(stderr, "Cant get gadget by name\n");
+               return -1;
+       }
+
+       usbg_ret = usbg_create_config(g, dt->config_id, dt->config_label, NULL, NULL, &c);
+       if (usbg_ret != USBG_SUCCESS) {
+               fprintf(stderr,"Error on config create\n");
+               fprintf(stderr,"Error: %s: %s\n", usbg_error_name(usbg_ret),
+                       usbg_strerror(usbg_ret));
+               return -1;
+       }
+
+       return 0;
+}
+
+static int add_func(void *data)
+{
+       struct gt_config_add_del_data *dt;
+
+       int usbg_ret = USBG_SUCCESS;
+       usbg_function_type f_type;
+       usbg_gadget *g;
+       usbg_function *f;
+       usbg_config *c;
+       int n;
+       char buff[255];
+       _cleanup_g_free_ gchar *func_name = NULL;
+       const char *cfg_label = NULL;
+
+       dt = (struct gt_config_add_del_data *)data;
+       cfg_label = dt->config_label;
+
+       /* func_name cant be NULL (libusbg requirement) */
+       n = snprintf(buff, 255, "%s.%s", dt->type, dt->instance);
+       if (n < 0)
+               return -1;
+       func_name = strdup(buff);
+
+       f_type = usbg_lookup_function_type(dt->type);
+
+       g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
+       if (g == NULL) {
+               fprintf(stderr, "Unable to get gadget\n");
+               return -1;
+       }
+
+       f = usbg_get_function(g, f_type, dt->instance);
+       if (f == NULL) {
+               fprintf(stderr, "Unable to get function\n");
+               return -1;
+       }
+
+       if (strcmp(dt->config_label, "") == 0)
+               cfg_label = NULL;
+
+       c = usbg_get_config(g, dt->config_id, cfg_label);
+       if (c == NULL) {
+               fprintf(stderr, "Unable to get config\n");
+               return -1;
+       }
+
+       usbg_ret = usbg_add_config_function(c, func_name, f);
+       if (usbg_ret != USBG_SUCCESS) {
+               fprintf(stderr, "Unable to attach function\n");
+               fprintf(stderr,"Error: %s: %s\n", usbg_error_name(usbg_ret),
+                       usbg_strerror(usbg_ret));
+               return -1;
+       }
+
+       return 0;
+}
+
+struct gt_config_backend gt_config_backend_libusbg = {
+       .create = create_func,
+       .add = add_func,
+       .rm = NULL,
+       .get = NULL,
+       .set = NULL,
+       .config = NULL,
+       .del = NULL,
+       .template_default = NULL,
+       .template_get = NULL,
+       .template_set = NULL,
+       .template_rm = NULL,
+       .load = NULL,
+       .save = NULL,
+};
diff --git a/source/config/src/configuration_not_implemented.c b/source/config/src/configuration_not_implemented.c
new file mode 100644 (file)
index 0000000..c36b9b4
--- /dev/null
@@ -0,0 +1,226 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "configuration.h"
+#include "common.h"
+#include "parser.h"
+#include "backend.h"
+
+static int rm_func(void *data)
+{
+       struct gt_config_rm_data *dt;
+
+       dt = (struct gt_config_rm_data *)data;
+       printf("Config rm called successfully. Not implemented.\n");
+       printf("gadget = %s, config = %s, force = %d, recursive = %d\n",
+               dt->gadget, dt->config, !!(dt->opts & GT_FORCE),
+               !!(dt->opts & GT_RECURSIVE));
+
+       return 0;
+}
+
+static int get_func(void *data)
+{
+       struct gt_config_get_data *dt;
+       const char **ptr;
+
+       dt = (struct gt_config_get_data *)data;
+       printf("Config get called successfully. Not implemented.\n");
+       printf("gadget = %s, config = %s, attrs = ",
+               dt->gadget, dt->config);
+       ptr = dt->attrs;
+       while (*ptr) {
+               printf("%s, ", *ptr);
+               ptr++;
+       }
+       putchar('\n');
+       return 0;
+}
+
+static int set_func(void *data)
+{
+       struct gt_config_set_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_config_set_data *)data;
+       printf("Config set called successfully. Not implemented.\n");
+       printf("gadget = %s, config = %s", dt->gadget, dt->config);
+       ptr = dt->attrs;
+       while (ptr->variable) {
+               printf(", %s = %s", ptr->variable, ptr->value);
+               ptr++;
+       }
+
+       putchar('\n');
+       return 0;
+}
+
+static int config_func(void *data)
+{
+       struct gt_config_config_data *dt;
+
+       dt = (struct gt_config_config_data *)data;
+       printf("Config config called successfully. Not implemented.\n");
+       printf("gadget = %s", dt->gadget);
+       if (dt->config)
+               printf(", config = %s", dt->config);
+       printf(", verbose = %d, recursive = %d\n",
+               !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
+
+       return 0;
+}
+
+static int del_func(void *data)
+{
+       struct gt_config_add_del_data *dt;
+
+       dt = (struct gt_config_add_del_data *)data;
+       printf("Config del called successfully. Not implemented.\n");
+       printf("gadget = %s, cfg_label = %s, cfg_id = %d, type = %s, instance = %s\n",
+                       dt->gadget, dt->config_label, dt->config_id, dt->type, dt->instance);
+
+       return 0;
+}
+
+static int template_func(void *data)
+{
+       struct gt_config_template_data *dt;
+
+       dt = (struct gt_config_template_data *)data;
+       printf("Config template called successfully. Not implemented.\n");
+       if (dt->name)
+               printf("name = %s, ", dt->name);
+       printf("verbose = %d, recursive = %d\n",
+               !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
+       return 0;
+}
+
+static int template_get_func(void *data)
+{
+       struct gt_config_template_get_data *dt;
+       const char **ptr;
+
+       dt = (struct gt_config_template_get_data *)data;
+       printf("Config template get called successfully. Not implemented.\n");
+       printf("name = %s, attr = ", dt->name);
+       ptr = dt->attr;
+       while (*ptr) {
+               printf("%s, ", *ptr);
+               ptr++;
+       }
+
+       putchar('\n');
+       return 0;
+}
+
+static int template_set_func(void *data)
+{
+       struct gt_config_template_set_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_config_template_set_data *)data;
+       printf("Config template set called successfully. Not implemened.\n");
+       printf("name = %s", dt->name);
+       ptr = dt->attr;
+       while (ptr->variable) {
+               printf(", %s = %s", ptr->variable, ptr->value);
+               ptr++;
+       }
+
+       putchar('\n');
+       return 0;
+}
+
+static int template_rm_func(void *data)
+{
+       const char *dt;
+
+       dt = (const char *)data;
+       printf("Config template rm called successfully. Not implemented.\n");
+       printf("name = %s\n", dt);
+       return 0;
+}
+
+static int load_func(void *data)
+{
+       struct gt_config_load_data *dt;
+
+       dt = (struct gt_config_load_data *)data;
+       printf("Config load called successfully. Not implemented.\n");
+       if (dt->name)
+               printf("name = %s, ", dt->name);
+       if (dt->gadget)
+               printf("gadget = %s, ", dt->gadget);
+       if (dt->config)
+               printf("config = %s, ", dt->config);
+       if (dt->file)
+               printf("file = %s, ", dt->file);
+       if (dt->path)
+               printf("path = %s, ", dt->path);
+       printf("recursive = %d, force = %d, stdin = %d\n",
+               !!(dt->opts & GT_RECURSIVE), !!(dt->opts & GT_FORCE),
+               !!(dt->opts & GT_STDIN));
+
+       return 0;
+}
+
+static int save_func(void *data)
+{
+       struct gt_config_save_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_config_save_data *)data;
+       printf("Config save called successfully. Not implemented.\n");
+       if (dt->gadget)
+               printf("gadget=%s, ", dt->gadget);
+       if (dt->config)
+               printf("config=%s, ", dt->config);
+       if (dt->name)
+               printf("name=%s, ", dt->name);
+       if (dt->file)
+               printf("file=%s, ", dt->file);
+       if (dt->path)
+               printf("path=%s, ", dt->path);
+       printf("force=%d, stdout=%d",
+               !!(dt->opts & GT_FORCE), !!(dt->opts & GT_STDOUT));
+
+       ptr = dt->attrs;
+       while (ptr->variable) {
+               printf(", %s = %s", ptr->variable, ptr->value);
+               ptr++;
+       }
+
+       putchar('\n');
+
+
+       return 0;
+}
+
+struct gt_config_backend gt_config_backend_not_implemented = {
+       .rm = rm_func,
+       .get = get_func,
+       .set = set_func,
+       .config = config_func,
+       .del = del_func,
+       .template_default = template_func,
+       .template_get = template_get_func,
+       .template_set = template_set_func,
+       .template_rm = template_rm_func,
+       .load = load_func,
+       .save = save_func,
+};