function: Introduce new backends mechanism in function subcommand
authorPawel Szewczyk <p.szewczyk@samsung.com>
Mon, 15 Jun 2015 15:00:17 +0000 (17:00 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Tue, 7 Jul 2015 11:56:30 +0000 (13:56 +0200)
Move each backend implementation to separate file and use backend
structure to select functions to be executed. Placeholders for not
implemented functions are provided by additional backend instance.

This commit do it only with function subcommand.

Change-Id: If8a3ac3e5296a22308a6b0ab86d7d7c4719b9f71
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/base/include/backend.h
source/base/src/backend.c
source/config/src/configuration.c
source/function/CMakeLists.txt
source/function/include/function.h
source/function/src/function.c
source/function/src/function_gadgetd.c [new file with mode: 0644]
source/function/src/function_libusbg.c [new file with mode: 0644]
source/function/src/function_not_implemented.c [new file with mode: 0644]
source/gadget/src/gadget.c

index 52da209..66ba0b4 100644 (file)
@@ -42,8 +42,14 @@ enum gt_backend_type {
        GT_BACKEND_LIBUSBG,
 };
 
+struct gt_backend {
+       struct gt_function_backend *function;
+};
+
 struct gt_backend_ctx {
-       enum gt_backend_type backend;
+       enum gt_backend_type backend_type;
+
+       struct gt_backend *backend;
 
        union {
                usbg_state *libusbg_state;
@@ -51,6 +57,9 @@ struct gt_backend_ctx {
        };
 };
 
+extern struct gt_backend gt_backend_libusbg;
+extern struct gt_backend gt_backend_gadgetd;
+
 extern struct gt_backend_ctx backend_ctx;
 
 #endif /* __GADGET_TOOL_BACKEND_H__ */
index 3c67d65..44d4c66 100644 (file)
 #include <gio/gio.h>
 
 #include "backend.h"
+#include "function.h"
 
 struct gt_backend_ctx backend_ctx = {
-       .backend = GT_BACKEND_AUTO,
+       .backend_type = GT_BACKEND_AUTO,
+};
+
+struct gt_backend gt_backend_libusbg = {
+       .function = &gt_function_backend_libusbg,
+};
+
+struct gt_backend gt_backend_gadgetd = {
+       .function = &gt_function_backend_gadgetd,
 };
 
 int gt_backend_init(const char *program_name, enum gt_option_flags flags)
 {
-       enum gt_backend_type backend;
+       enum gt_backend_type backend_type;
        GError *err = NULL;
 
        if (strcmp(program_name, "gt") == 0)
-               backend = GT_BACKEND_LIBUSBG;
+               backend_type = GT_BACKEND_LIBUSBG;
        else if (strcmp(program_name, "gadgetctl") == 0)
-               backend = GT_BACKEND_GADGETD;
+               backend_type = GT_BACKEND_GADGETD;
        else
-               backend = GT_BACKEND_AUTO;
+               backend_type = GT_BACKEND_AUTO;
 
-       if (backend == GT_BACKEND_GADGETD || backend == GT_BACKEND_AUTO) {
+       if (backend_type == GT_BACKEND_GADGETD || backend_type == GT_BACKEND_AUTO) {
                GDBusConnection *conn;
 
+               backend_ctx.backend = &gt_backend_gadgetd;
+
 #if ! GLIB_CHECK_VERSION(2, 36, 0)
                g_type_init();
 #endif
@@ -69,31 +80,33 @@ int gt_backend_init(const char *program_name, enum gt_option_flags flags)
                         * This message could be probably shown in verbose
                         * mode (which we don't have yet).
                         */
-                       fprintf(stderr, "Unable to initialize gadgetd backend\n");
+                       fprintf(stderr, "Unable to initialize gadgetd backend_type\n");
                        goto out_gadgetd;
                }
 
-               backend_ctx.backend = GT_BACKEND_GADGETD;
+               backend_ctx.backend_type = GT_BACKEND_GADGETD;
                backend_ctx.gadgetd_conn = conn;
                return 0;
 
        }
 
 out_gadgetd:
-       if (err && backend == GT_BACKEND_GADGETD)
+       if (err && backend_type == GT_BACKEND_GADGETD)
                return -1;
 
-       if (backend == GT_BACKEND_LIBUSBG || backend == GT_BACKEND_AUTO) {
+       if (backend_type == GT_BACKEND_LIBUSBG || backend_type == GT_BACKEND_AUTO) {
                usbg_state *s = NULL;
                int r;
 
+               backend_ctx.backend = &gt_backend_libusbg;
+
                r = usbg_init("/sys/kernel/config", &s);
                if (r != USBG_SUCCESS) {
-                       fprintf(stderr, "Unable to initialize libusbg backend: %s\n", usbg_strerror(r));
+                       fprintf(stderr, "Unable to initialize libusbg backend_type: %s\n", usbg_strerror(r));
                        goto out_libusbg;
                }
 
-               backend_ctx.backend = GT_BACKEND_LIBUSBG;
+               backend_ctx.backend_type = GT_BACKEND_LIBUSBG;
                backend_ctx.libusbg_state = s;
                return 0;
        }
index 063a092..a0f38de 100644 (file)
@@ -60,7 +60,7 @@ static int gt_config_create_func(void *data)
        dt = (struct gt_config_create_data *)data;
 
        /* TODO implement -f option */
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
+       if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
                GVariant *gret;
                GError *error = NULL;
                _cleanup_g_free_ gchar *path = NULL;
@@ -442,7 +442,7 @@ static int gt_config_add_func(void *data)
 
        dt = (struct gt_config_add_del_data *)data;
 
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
+       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;
@@ -535,7 +535,7 @@ static int gt_config_add_func(void *data)
                g_variant_get(gret, "(b)", &function_added);
                g_variant_unref(gret);
 
-       } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
+       } else if (backend_ctx.backend_type == GT_BACKEND_LIBUSBG) {
                int usbg_ret = USBG_SUCCESS;
                usbg_function_type f_type;
                usbg_gadget *g;
index ac0fefb..a88af61 100644 (file)
@@ -3,6 +3,9 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
 
 SET( FUNCTION_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/src/function.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/function_libusbg.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/function_gadgetd.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/function_not_implemented.c
        )
 
-add_library(function STATIC ${FUNCTION_SRC} )
\ No newline at end of file
+add_library(function STATIC ${FUNCTION_SRC} )
index 273051e..a51fd21 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_function_backend {
+       /**
+        * Create a function
+        */
+       int (*create)(void *);
+       /**
+        * Remove a function
+        */
+       int (*rm)(void *);
+       /**
+        * List all known types of functions
+        */
+       int (*list_types)(void *);
+       /**
+        * Get attributes of a function
+        */
+       int (*get)(void *);
+       /**
+        * Set attributes of a function
+        */
+       int (*set)(void *);
+       /**
+        * Show functions
+        */
+       int (*func)(void *);
+       /**
+        * Load function from file
+        */
+       int (*load)(void *);
+       /**
+        * Save function to file
+        */
+       int (*save)(void *);
+       /**
+        * Function template
+        */
+       int (*template_default)(void *);
+       /**
+        * Get template attributes
+        */
+       int (*template_get)(void *);
+       /**
+        * Set template atributes
+        */
+       int (*template_set)(void *);
+       /**
+        * Remove template
+        */
+       int (*template_rm)(void *);
+};
+
+struct gt_func_create_data {
+       const char *gadget;
+       const char *type;
+       const char *name;
+       int opts;
+       struct gt_setting *attrs;
+};
+
+struct gt_func_rm_data {
+       const char *gadget;
+       const char *type;
+       const char *name;
+       int opts;
+};
+
+struct gt_func_get_data {
+       const char *gadget;
+       const char *type;
+       const char *name;
+       const char **attrs;
+};
+
+struct gt_func_set_data {
+       const char *gadget;
+       const char *type;
+       const char *name;
+       struct gt_setting *attrs;
+};
+
+struct gt_func_func_data {
+       const char *gadget;
+       const char *type;
+       const char *name;
+       int opts;
+};
+
+struct gt_func_load_data {
+       const char *name;
+       const char *gadget;
+       const char *func;
+       const char *file;
+       const char *path;
+       int opts;
+};
+
+struct gt_func_save_data {
+       const char *gadget;
+       const char *func;
+       const char *name;
+       const char *file;
+       const char *path;
+       int opts;
+       struct gt_setting *attrs;
+};
+
+struct gt_func_template_data {
+       const char *name;
+       int opts;
+};
+
+struct gt_func_template_get_data {
+       const char *name;
+       const char **attrs;
+};
+
+struct gt_func_template_set_data {
+       const char *name;
+       struct gt_setting *attrs;
+};
+
+/**
  * @brief Gets the next possible commands after func
  * @param[in] cmd actual command (should be func)
  * @return Pointer to table with all children of cmd
@@ -37,4 +163,8 @@ const Command *gt_func_get_children(const Command *cmd);
  */
 int gt_func_help(void *data);
 
+extern struct gt_function_backend gt_function_backend_libusbg;
+extern struct gt_function_backend gt_function_backend_gadgetd;
+extern struct gt_function_backend gt_function_backend_not_implemented;
+
 #endif //__GADGET_TOOL_FUNCTION_FUNCTION_H__
index 915f7e5..7a1d426 100644 (file)
 #include <string.h>
 #include <unistd.h>
 #include <getopt.h>
-#include <glib.h>
-#include <gio/gio.h>
 
 #include "function.h"
 #include "common.h"
 #include "parser.h"
 #include "backend.h"
-#include <gio/gio.h>
 
-struct gt_func_create_data {
-       const char *gadget;
-       const char *type;
-       const char *name;
-       int opts;
-       struct gt_setting *attrs;
-};
+#define GET_EXECUTABLE(func) \
+       (backend_ctx.backend->function->func ? \
+        backend_ctx.backend->function->func : \
+        gt_function_backend_not_implemented.func)
 
 static void gt_func_create_destructor(void *data)
 {
@@ -48,96 +42,6 @@ static void gt_func_create_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_create_func(void *data)
-{
-       struct gt_func_create_data *dt;
-
-       dt = (struct gt_func_create_data *)data;
-
-       if (dt->attrs->variable) {
-               /* TODO add support for attributes */
-               printf("Attributes are not supported now\n");
-               return -1;
-       }
-
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
-               gchar *gadget_objpath = NULL;
-               GError *err = NULL;
-               GVariant *v;
-
-               v = 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,
-                                               &err);
-               if (err) {
-                       fprintf(stderr, "Unable to find gadget %s: %s\n", dt->gadget, err->message);
-                       return -1;
-               }
-
-               g_variant_get(v, "(o)", &gadget_objpath);
-               g_variant_unref(v);
-
-               v = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                               "org.usb.gadgetd",
-                                               gadget_objpath,
-                                               "org.usb.device.Gadget.FunctionManager",
-                                               "CreateFunction",
-                                               g_variant_new("(ss)", dt->name, dt->type),
-                                               NULL,
-                                               G_DBUS_CALL_FLAGS_NONE,
-                                               -1,
-                                               NULL,
-                                               &err);
-               if (err) {
-                       fprintf(stderr, "Unable to create function: %s\n", err->message);
-                       g_free(gadget_objpath);
-                       return -1;
-               }
-
-               g_free(gadget_objpath);
-               g_variant_unref(v);
-               return 0;
-
-       } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
-               usbg_gadget *g;
-               usbg_function_type f_type;
-               usbg_function *f;
-               int r;
-
-               f_type = usbg_lookup_function_type(dt->type);
-               if (f_type < 0) {
-                       fprintf(stderr, "Unable to find function %s: %s\n",
-                               dt->type, usbg_strerror(f_type));
-                       return -1;
-               }
-
-               g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
-               if (!g) {
-                       fprintf(stderr, "Unable to find gadget %s\n",
-                               dt->gadget);
-                       return -1;
-               }
-
-               r = usbg_create_function(g, f_type, dt->name, NULL, &f);
-               if (r < 0) {
-                       fprintf(stderr, "Unable to create function: %s\n",
-                               usbg_strerror(r));
-                       return -1;
-               }
-
-               return 0;
-       }
-
-       return -1;
-}
-
 static int gt_func_create_help(void *data)
 {
        printf("usage: %s func create GADGET_NAME FUNCTION_TYPE FUNCTION_NAME\n"
@@ -173,8 +77,8 @@ static void gt_parse_func_create(const Command *cmd, int argc,
        if (tmp < 0)
                goto out;
 
-       executable_command_set(exec, gt_func_create_func, (void *)dt,
-                       gt_func_create_destructor);
+       executable_command_set(exec, GET_EXECUTABLE(create),
+                       (void *)dt, gt_func_create_destructor);
 
        return;
 out:
@@ -182,13 +86,6 @@ out:
        executable_command_set(exec, gt_func_create_help, data, NULL);
 }
 
-struct gt_func_rm_data {
-       const char *gadget;
-       const char *type;
-       const char *name;
-       int opts;
-};
-
 static void gt_func_rm_destructor(void *data)
 {
        struct gt_func_rm_data *dt;
@@ -200,18 +97,6 @@ static void gt_func_rm_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_rm_func(void *data)
-{
-       struct gt_func_rm_data *dt;
-
-       dt = (struct gt_func_rm_data *)data;
-       printf("Func rm called successfully. Not implemented.\n");
-       printf("gadget=%s, type=%s, name=%s, recursive=%d, force=%d\n",
-               dt->gadget, dt->type, dt->name, !!(dt->opts & GT_RECURSIVE),
-               !!(dt->opts & GT_FORCE));
-
-       return 0;
-}
 
 static int gt_func_rm_help(void *data)
 {
@@ -244,8 +129,8 @@ static void gt_parse_func_rm(const Command *cmd, int argc,
        if (ind != argc)
                goto out;
 
-       executable_command_set(exec, gt_func_rm_func, (void *)dt,
-                       gt_func_rm_destructor);
+       executable_command_set(exec, GET_EXECUTABLE(rm),
+                       (void *)dt, gt_func_rm_destructor);
 
        return;
 out:
@@ -253,55 +138,6 @@ out:
        executable_command_set(exec, gt_func_rm_help, data, NULL);
 }
 
-
-static int gt_func_list_types_func(void *data)
-{
-       int i;
-
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
-               GError *err = NULL;
-               GVariantIter *iter;
-               GVariant *v;
-               gchar *s;
-
-               v = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
-                                               "org.usb.gadgetd",
-                                               "/org/usb/Gadget",
-                                               "org.usb.device.GadgetManager",
-                                               "ListAvailableFunctions",
-                                               NULL,
-                                               NULL,
-                                               G_DBUS_CALL_FLAGS_NONE,
-                                               -1,
-                                               NULL,
-                                               &err);
-
-               if (err) {
-                       fprintf(stderr, "Unable to get function list: %s\n", err->message);
-                       return -1;
-               }
-
-               printf("Discovered functions:\n");
-               g_variant_get(v, "(as)", &iter);
-               while (g_variant_iter_loop(iter, "s", &s))
-                      printf("  %s\n", s);
-
-               g_variant_iter_free(iter);
-               g_variant_unref(v);
-
-               return 0;
-
-       } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
-               printf("Functions known by library:\n");
-               for (i = USBG_FUNCTION_TYPE_MIN; i < USBG_FUNCTION_TYPE_MAX; i++)
-                       printf("  %s\n", usbg_get_function_type_str(i));
-
-               return 0;
-       }
-
-       return -1;
-}
-
 static int gt_func_list_types_help(void *data)
 {
        printf("%s func list-types\n"
@@ -314,19 +150,13 @@ static void gt_parse_func_list_types(const Command *cmd, int argc,
                char **argv, ExecutableCommand *exec, void * data)
 {
        if (argc == 0)
-               executable_command_set(exec, gt_func_list_types_func, NULL, NULL);
+               executable_command_set(exec, GET_EXECUTABLE(list_types),
+                               NULL, NULL);
        else
                executable_command_set(exec, gt_func_list_types_help, NULL, NULL);
 
 }
 
-struct gt_func_get_data {
-       const char *gadget;
-       const char *type;
-       const char *name;
-       const char **attrs;
-};
-
 static void gt_func_get_destructor(void *data)
 {
        struct gt_func_get_data *dt;
@@ -338,22 +168,6 @@ static void gt_func_get_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_get_func(void *data)
-{
-       struct gt_func_get_data *dt;
-       const char **ptr;
-
-       dt = (struct gt_func_get_data *)data;
-       printf("Func get called successfully. Not implemented.\n");
-       printf("gadget=%s, type=%s, name=%s, attrs=",
-               dt->gadget, dt->type, dt->name);
-
-       for (ptr = dt->attrs; *ptr; ptr++)
-               printf("%s, ", *ptr);
-       putchar('\n');
-
-       return 0;
-}
 
 static int gt_func_get_help(void *data)
 {
@@ -388,7 +202,7 @@ static void gt_parse_func_get(const Command *cmd, int argc,
        for (i = 0; argv[i]; i++)
                dt->attrs[i] = argv[i];
 
-       executable_command_set(exec, gt_func_get_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(get), (void *)dt,
                        gt_func_get_destructor);
 
        return;
@@ -397,13 +211,6 @@ out:
        executable_command_set(exec, gt_func_get_help, data, NULL);
 }
 
-struct gt_func_set_data {
-       const char *gadget;
-       const char *type;
-       const char *name;
-       struct gt_setting *attrs;
-};
-
 static void gt_func_set_destructor(void *data)
 {
        struct gt_func_set_data *dt;
@@ -415,22 +222,6 @@ static void gt_func_set_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_set_func(void *data)
-{
-       struct gt_func_set_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_func_set_data *)data;
-       printf("Func set called successfully. Not implemented.\n");
-       printf("gadget=%s, type=%s, name=%s", dt->gadget, dt->type, dt->name);
-
-       for (ptr = dt->attrs; ptr->variable; ptr++)
-               printf(", %s=%s", ptr->variable, ptr->value);
-       putchar('\n');
-
-       return 0;
-}
-
 static int gt_func_set_help(void *data)
 {
        printf("Func set help.\n");
@@ -461,7 +252,7 @@ static void gt_parse_func_set(const Command *cmd, int argc,
        if (tmp < 0)
                goto out;
 
-       executable_command_set(exec, gt_func_set_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(set), (void *)dt,
                        gt_func_set_destructor);
 
        return;
@@ -470,12 +261,6 @@ out:
        executable_command_set(exec, gt_func_get_help, data, NULL);
 }
 
-struct gt_func_func_data {
-       const char *gadget;
-       const char *type;
-       const char *name;
-       int opts;
-};
 
 static void gt_func_func_destructor(void *data)
 {
@@ -487,22 +272,6 @@ static void gt_func_func_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_func_func(void *data)
-{
-       struct gt_func_func_data *dt;
-
-       dt = (struct gt_func_func_data *)data;
-       printf("Func func called successfully. Not implemented.\n");
-       printf("gadget=%s", dt->gadget);
-       if (dt->type)
-               printf(", type=%s", dt->type);
-       if (dt->name)
-               printf(", name=%s", dt->name);
-       printf(", verbose=%d\n", !!(dt->opts & GT_VERBOSE));
-
-       return 0;
-}
-
 static int gt_func_func_help(void *data)
 {
        printf("usage: %s func COMMAND\n"
@@ -549,7 +318,7 @@ static void gt_parse_func_func(const Command *cmd, int argc,
                dt->name = argv[ind++];
        }
 
-       executable_command_set(exec, gt_func_func_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(func), (void *)dt,
                        gt_func_func_destructor);
        return;
 out:
@@ -557,37 +326,6 @@ out:
        executable_command_set(exec, gt_func_func_help, data, NULL);
 }
 
-struct gt_func_load_data {
-       const char *name;
-       const char *gadget;
-       const char *func;
-       const char *file;
-       const char *path;
-       int opts;
-};
-
-static int gt_func_load_func(void *data)
-{
-       struct gt_func_load_data *dt;
-
-       dt = (struct gt_func_load_data *)data;
-       printf("Func load called succesfully. Not implemented.\n");
-       if (dt->name)
-               printf("name=%s, ", dt->name);
-       if (dt->gadget)
-               printf("gadget=%s, ", dt->gadget);
-       if (dt->func)
-               printf("func=%s, ", dt->func);
-       if (dt->file)
-               printf("file=%s, ", dt->file);
-       if (dt->path)
-               printf("path=%s, ", dt->path);
-       printf("force=%d, stdin=%d\n", !!(dt->opts & GT_FORCE),
-                       !!(dt->opts & GT_STDIN));
-
-       return 0;
-}
-
 static int gt_func_load_help(void *data)
 {
        printf("Func load help.\n");
@@ -659,23 +397,14 @@ static void gt_parse_func_load(const Command *cmd, int argc,
        if (optind < argc)
                dt->func = argv[optind];
 
-       executable_command_set(exec, gt_func_load_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(load),
+                       (void *)dt, free);
        return;
 out:
        free(dt);
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_func_save_data {
-       const char *gadget;
-       const char *func;
-       const char *name;
-       const char *file;
-       const char *path;
-       int opts;
-       struct gt_setting *attrs;
-};
-
 static void gt_func_save_destructor(void *data)
 {
        struct gt_func_save_data *dt;
@@ -688,34 +417,6 @@ static void gt_func_save_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_save_func(void *data)
-{
-       struct gt_func_save_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_func_save_data *)data;
-
-       printf("Func save called successfully. Not implemented.\n");
-       if (dt->gadget)
-               printf("gadget=%s, ", dt->gadget);
-       if (dt->func)
-               printf("func=%s, ", dt->func);
-       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));
-
-       for (ptr = dt->attrs; ptr->variable; ptr++)
-               printf(", %s=%s", ptr->variable, ptr->value);
-       putchar('\n');
-
-       return 0;
-}
-
 static int gt_func_save_help(void *data)
 {
        printf("Func save help.\n");
@@ -786,7 +487,7 @@ static void gt_parse_func_save(const Command *cmd, int argc,
        if (c < 0)
                goto out;
 
-       executable_command_set(exec, gt_func_save_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(save), (void *)dt,
                        gt_func_save_destructor);
 
        return;
@@ -801,24 +502,6 @@ int gt_func_help(void *data)
        return -1;
 }
 
-struct gt_func_template_data {
-       const char *name;
-       int opts;
-};
-
-static int gt_func_template_func(void *data)
-{
-       struct gt_func_template_data *dt;
-
-       dt = (struct gt_func_template_data *)data;
-       printf("Func template called successfully. Not implemented.\n");
-       if (dt->name)
-               printf("name=%s, ", dt->name);
-       printf("verbose=%d\n", !!(dt->opts & GT_VERBOSE));
-
-       return 0;
-}
-
 static int gt_func_template_help(void *data)
 {
        printf("Func template help.\n");
@@ -845,18 +528,14 @@ static void gt_parse_func_template(const Command *cmd, int argc,
 
        dt->name = argv[ind++];
 
-       executable_command_set(exec, gt_func_template_func, (void *)dt, free);
+       executable_command_set(exec, GET_EXECUTABLE(template_default),
+                       (void *)dt, free);
        return;
 out:
        free(dt);
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_func_template_get_data {
-       const char *name;
-       const char **attrs;
-};
-
 static void gt_func_template_get_destructor(void *data)
 {
        struct gt_func_template_get_data *dt;
@@ -868,21 +547,6 @@ static void gt_func_template_get_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_template_get_func(void *data)
-{
-       struct gt_func_template_get_data *dt;
-       const char **ptr;
-
-       dt = (struct gt_func_template_get_data *)data;
-       printf("Func template get called successfully. Not implemented.\n");
-       printf("name=%s, attrs=", dt->name);
-       for (ptr = dt->attrs; *ptr; ptr++)
-               printf("%s, ", *ptr);
-       putchar('\n');
-
-       return 0;
-}
-
 static int gt_func_template_get_help(void *data)
 {
        printf("Func template get help called successfully.\n");
@@ -912,8 +576,8 @@ static void gt_parse_func_template_get(const Command *cmd, int argc,
        for (i = 0; argv[i]; i++)
                dt->attrs[i] = argv[i];
 
-       executable_command_set(exec, gt_func_template_get_func, (void *)dt,
-                       gt_func_template_get_destructor);
+       executable_command_set(exec, GET_EXECUTABLE(template_get),
+                       (void *)dt, gt_func_template_get_destructor);
        return;
 
 out:
@@ -921,11 +585,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-struct gt_func_template_set_data {
-       const char *name;
-       struct gt_setting *attrs;
-};
-
 static void gt_func_template_set_destructor(void *data)
 {
        struct gt_func_template_set_data *dt;
@@ -937,22 +596,6 @@ static void gt_func_template_set_destructor(void *data)
        free(dt);
 }
 
-static int gt_func_template_set_func(void *data)
-{
-       struct gt_func_template_set_data *dt;
-       struct gt_setting *ptr;
-
-       dt = (struct gt_func_template_set_data *)data;
-       printf("Func template set called successfully. Not implemented.\n");
-       printf("name=%s", dt->name);
-
-       for (ptr = dt->attrs; ptr->variable; ptr++)
-               printf(", %s=%s", ptr->variable, ptr->value);
-       putchar('\n');
-
-       return 0;
-}
-
 static int gt_func_template_set_help(void *data)
 {
        printf("Func template set help.\n");
@@ -977,7 +620,7 @@ static void gt_parse_func_template_set(const Command *cmd, int argc,
        if (tmp < 0)
                goto out;
 
-       executable_command_set(exec, gt_func_template_set_func, (void *)dt,
+       executable_command_set(exec, GET_EXECUTABLE(template_set), (void *)dt,
                        gt_func_template_set_destructor);
        return;
 
@@ -986,17 +629,6 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
-static int gt_func_template_rm_func(void *data)
-{
-       const char *dt;
-
-       dt = (const char *)data;
-       printf("Func template rm called successfully. Not implemented.\n");
-       printf("name=%s\n", dt);
-
-       return 0;
-}
-
 static int gt_func_template_rm_help(void *data)
 {
        printf("Func template rm help.\n");
@@ -1013,7 +645,7 @@ static void gt_parse_func_template_rm(const Command *cmd, int argc,
 
        name = argv[0];
 
-       executable_command_set(exec, gt_func_template_rm_func, (void *)name, NULL);
+       executable_command_set(exec, GET_EXECUTABLE(template_rm), (void *)name, NULL);
 
        return;
 out:
diff --git a/source/function/src/function_gadgetd.c b/source/function/src/function_gadgetd.c
new file mode 100644 (file)
index 0000000..0617399
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2015 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 <glib.h>
+#include <gio/gio.h>
+
+#include "parser.h"
+#include "backend.h"
+#include "function.h"
+
+static int create_func(void *data)
+{
+       struct gt_func_create_data *dt;
+       gchar *gadget_objpath = NULL;
+       GError *err = NULL;
+       GVariant *v;
+
+       dt = (struct gt_func_create_data *)data;
+
+       if (dt->attrs->variable) {
+               /* TODO add support for attributes */
+               printf("Attributes are not supported now\n");
+               return -1;
+       }
+
+       v = 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,
+                                       &err);
+       if (err) {
+               fprintf(stderr, "Unable to find gadget %s: %s\n", dt->gadget, err->message);
+               return -1;
+       }
+
+       g_variant_get(v, "(o)", &gadget_objpath);
+       g_variant_unref(v);
+
+       v = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                       "org.usb.gadgetd",
+                                       gadget_objpath,
+                                       "org.usb.device.Gadget.FunctionManager",
+                                       "CreateFunction",
+                                       g_variant_new("(ss)", dt->name, dt->type),
+                                       NULL,
+                                       G_DBUS_CALL_FLAGS_NONE,
+                                       -1,
+                                       NULL,
+                                       &err);
+       if (err) {
+               fprintf(stderr, "Unable to create function: %s\n", err->message);
+               g_free(gadget_objpath);
+               return -1;
+       }
+
+       g_free(gadget_objpath);
+       g_variant_unref(v);
+
+       return 0;
+}
+
+static int list_types_func(void *data)
+{
+       GError *err = NULL;
+       GVariantIter *iter;
+       GVariant *v;
+       gchar *s;
+
+       v = g_dbus_connection_call_sync(backend_ctx.gadgetd_conn,
+                                       "org.usb.gadgetd",
+                                       "/org/usb/Gadget",
+                                       "org.usb.device.GadgetManager",
+                                       "ListAvailableFunctions",
+                                       NULL,
+                                       NULL,
+                                       G_DBUS_CALL_FLAGS_NONE,
+                                       -1,
+                                       NULL,
+                                       &err);
+
+       if (err) {
+               fprintf(stderr, "Unable to get function list: %s\n", err->message);
+               return -1;
+       }
+
+       printf("Discovered functions:\n");
+       g_variant_get(v, "(as)", &iter);
+       while (g_variant_iter_loop(iter, "s", &s))
+              printf("  %s\n", s);
+
+       g_variant_iter_free(iter);
+       g_variant_unref(v);
+
+       return 0;
+}
+
+struct gt_function_backend gt_function_backend_gadgetd = {
+       .create = create_func,
+       .rm = NULL,
+       .list_types = list_types_func,
+       .get = NULL,
+       .set = NULL,
+       .func = NULL,
+       .load = NULL,
+       .save = NULL,
+       .template_default = NULL,
+       .template_get = NULL,
+       .template_set = NULL,
+       .template_rm = NULL,
+};
diff --git a/source/function/src/function_libusbg.c b/source/function/src/function_libusbg.c
new file mode 100644 (file)
index 0000000..d9df233
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2015 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 <usbg/usbg.h>
+
+#include "function.h"
+#include "common.h"
+#include "backend.h"
+
+static int create_func(void *data)
+{
+       struct gt_func_create_data *dt;
+       usbg_gadget *g;
+       usbg_function_type f_type;
+       usbg_function *f;
+       int r;
+
+       dt = (struct gt_func_create_data *)data;
+
+       if (dt->attrs->variable) {
+               /* TODO add support for attributes */
+               printf("Attributes are not supported now\n");
+               return -1;
+       }
+
+
+       f_type = usbg_lookup_function_type(dt->type);
+       if (f_type < 0) {
+               fprintf(stderr, "Unable to find function %s: %s\n",
+                       dt->type, usbg_strerror(f_type));
+               return -1;
+       }
+
+       g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
+       if (!g) {
+               fprintf(stderr, "Unable to find gadget %s\n",
+                       dt->gadget);
+               return -1;
+       }
+
+       r = usbg_create_function(g, f_type, dt->name, NULL, &f);
+       if (r < 0) {
+               fprintf(stderr, "Unable to create function: %s\n",
+                       usbg_strerror(r));
+               return -1;
+       }
+
+       return 0;
+}
+
+static int list_types_func(void *data)
+{
+       int i;
+       printf("Functions known by library:\n");
+       for (i = USBG_FUNCTION_TYPE_MIN; i < USBG_FUNCTION_TYPE_MAX; i++)
+               printf("  %s\n", usbg_get_function_type_str(i));
+
+       return 0;
+}
+
+struct gt_function_backend gt_function_backend_libusbg = {
+       .create = create_func,
+       .rm = NULL,
+       .list_types = list_types_func,
+       .get = NULL,
+       .set = NULL,
+       .func = NULL,
+       .load = NULL,
+       .save = NULL,
+       .template_default = NULL,
+       .template_get = NULL,
+       .template_set = NULL,
+       .template_rm = NULL,
+};
diff --git a/source/function/src/function_not_implemented.c b/source/function/src/function_not_implemented.c
new file mode 100644 (file)
index 0000000..7ba93f4
--- /dev/null
@@ -0,0 +1,228 @@
+/*
+ * Copyright (c) 2015 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 "function.h"
+#include "common.h"
+#include "parser.h"
+
+static int create_func(void *data)
+{
+       struct gt_func_create_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_func_create_data *)data;
+       printf("Func create called successfully. Not implemented.\n");
+       printf("gadget=%s, type=%s, name=%s, force=%d",
+                       dt->gadget, dt->type, dt->name, !!(dt->opts & GT_FORCE));
+
+       for (ptr = dt->attrs; ptr->variable; ptr++)
+               printf(", %s=%s", ptr->variable, ptr->value);
+
+       putchar('\n');
+
+       return 0;
+}
+
+static int list_types_func(void *data)
+{
+       printf("Func list-typed called successfuly. Not implemented yet.\n");
+       return 0;
+}
+
+static int rm_func(void *data)
+{
+       struct gt_func_rm_data *dt;
+
+       dt = (struct gt_func_rm_data *)data;
+       printf("Func rm called successfully. Not implemented.\n");
+       printf("gadget=%s, type=%s, name=%s, recursive=%d, force=%d\n",
+               dt->gadget, dt->type, dt->name, !!(dt->opts & GT_RECURSIVE),
+               !!(dt->opts & GT_FORCE));
+
+       return 0;
+}
+
+static int get_func(void *data)
+{
+       struct gt_func_get_data *dt;
+       const char **ptr;
+
+       dt = (struct gt_func_get_data *)data;
+       printf("Func get called successfully. Not implemented.\n");
+       printf("gadget=%s, type=%s, name=%s, attrs=",
+               dt->gadget, dt->type, dt->name);
+
+       for (ptr = dt->attrs; *ptr; ptr++)
+               printf("%s, ", *ptr);
+       putchar('\n');
+
+       return 0;
+}
+
+static int set_func(void *data)
+{
+       struct gt_func_set_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_func_set_data *)data;
+       printf("Func set called successfully. Not implemented.\n");
+       printf("gadget=%s, type=%s, name=%s", dt->gadget, dt->type, dt->name);
+
+       for (ptr = dt->attrs; ptr->variable; ptr++)
+               printf(", %s=%s", ptr->variable, ptr->value);
+       putchar('\n');
+
+       return 0;
+}
+
+static int func_func(void *data)
+{
+       struct gt_func_func_data *dt;
+
+       dt = (struct gt_func_func_data *)data;
+       printf("Func func called successfully. Not implemented.\n");
+       printf("gadget=%s", dt->gadget);
+       if (dt->type)
+               printf(", type=%s", dt->type);
+       if (dt->name)
+               printf(", name=%s", dt->name);
+       printf(", verbose=%d\n", !!(dt->opts & GT_VERBOSE));
+
+       return 0;
+}
+
+static int load_func(void *data)
+{
+       struct gt_func_load_data *dt;
+
+       dt = (struct gt_func_load_data *)data;
+       printf("Func load called succesfully. Not implemented.\n");
+       if (dt->name)
+               printf("name=%s, ", dt->name);
+       if (dt->gadget)
+               printf("gadget=%s, ", dt->gadget);
+       if (dt->func)
+               printf("func=%s, ", dt->func);
+       if (dt->file)
+               printf("file=%s, ", dt->file);
+       if (dt->path)
+               printf("path=%s, ", dt->path);
+       printf("force=%d, stdin=%d\n", !!(dt->opts & GT_FORCE),
+                       !!(dt->opts & GT_STDIN));
+
+       return 0;
+}
+
+static int save_func(void *data)
+{
+       struct gt_func_save_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_func_save_data *)data;
+
+       printf("Func save called successfully. Not implemented.\n");
+       if (dt->gadget)
+               printf("gadget=%s, ", dt->gadget);
+       if (dt->func)
+               printf("func=%s, ", dt->func);
+       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));
+
+       for (ptr = dt->attrs; ptr->variable; ptr++)
+               printf(", %s=%s", ptr->variable, ptr->value);
+       putchar('\n');
+
+       return 0;
+}
+
+static int template_func(void *data)
+{
+       struct gt_func_template_data *dt;
+
+       dt = (struct gt_func_template_data *)data;
+       printf("Func template called successfully. Not implemented.\n");
+       if (dt->name)
+               printf("name=%s, ", dt->name);
+       printf("verbose=%d\n", !!(dt->opts & GT_VERBOSE));
+
+       return 0;
+}
+
+static int template_get_func(void *data)
+{
+       struct gt_func_template_get_data *dt;
+       const char **ptr;
+
+       dt = (struct gt_func_template_get_data *)data;
+       printf("Func template get called successfully. Not implemented.\n");
+       printf("name=%s, attrs=", dt->name);
+       for (ptr = dt->attrs; *ptr; ptr++)
+               printf("%s, ", *ptr);
+       putchar('\n');
+
+       return 0;
+}
+
+static int template_set_func(void *data)
+{
+       struct gt_func_template_set_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_func_template_set_data *)data;
+       printf("Func template set called successfully. Not implemented.\n");
+       printf("name=%s", dt->name);
+
+       for (ptr = dt->attrs; ptr->variable; ptr++)
+               printf(", %s=%s", ptr->variable, ptr->value);
+       putchar('\n');
+
+       return 0;
+}
+
+static int template_rm_func(void *data)
+{
+       const char *dt;
+
+       dt = (const char *)data;
+       printf("Func template rm called successfully. Not implemented.\n");
+       printf("name=%s\n", dt);
+
+       return 0;
+}
+
+struct gt_function_backend gt_function_backend_not_implemented = {
+       .create = create_func,
+       .rm = rm_func,
+       .list_types = list_types_func,
+       .get = get_func,
+       .set = set_func,
+       .func = func_func,
+       .load = load_func,
+       .save = save_func,
+       .template_default = template_func,
+       .template_get = template_get_func,
+       .template_set = template_set_func,
+       .template_rm = template_rm_func,
+       .create = create_func,
+};
index 385425a..0d6fb47 100644 (file)
@@ -144,7 +144,7 @@ static int gt_gadget_create_func(void *data)
                }
        }
 
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
+       if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
 
                GVariantBuilder *b;
                GVariant *gattrs;
@@ -197,7 +197,7 @@ static int gt_gadget_create_func(void *data)
                g_variant_unref(v);
                return 0;
 
-       } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
+       } else if (backend_ctx.backend_type == GT_BACKEND_LIBUSBG) {
                usbg_gadget *g;
 
                r = usbg_create_gadget(backend_ctx.libusbg_state,
@@ -499,7 +499,7 @@ static int gt_gadget_enable_func(void *data)
 
        dt = (struct gt_gadget_enable_data *)data;
 
-       if (backend_ctx.backend == GT_BACKEND_GADGETD) {
+       if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
                /* TODO add support for enabling well known UDC */
                GVariant *gret;
                GError *error = NULL;
@@ -596,7 +596,7 @@ out:
                        return -1;
                }
 
-       } else if (backend_ctx.backend == GT_BACKEND_LIBUSBG) {
+       } else if (backend_ctx.backend_type == GT_BACKEND_LIBUSBG) {
                usbg_gadget *g;
                usbg_udc *udc = NULL;
                int usbg_ret;