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;
};
};
+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__ */
#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 = >_function_backend_libusbg,
+};
+
+struct gt_backend gt_backend_gadgetd = {
+ .function = >_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 = >_backend_gadgetd;
+
#if ! GLIB_CHECK_VERSION(2, 36, 0)
g_type_init();
#endif
* 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 = >_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;
}
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;
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;
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;
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} )
#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
*/
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__
#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)
{
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"
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:
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;
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)
{
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:
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"
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;
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)
{
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;
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;
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");
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;
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)
{
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"
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:
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");
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;
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");
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;
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");
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;
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");
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:
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;
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");
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;
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");
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:
--- /dev/null
+/*
+ * 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,
+};
--- /dev/null
+/*
+ * 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,
+};
--- /dev/null
+/*
+ * 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,
+};
}
}
- if (backend_ctx.backend == GT_BACKEND_GADGETD) {
+ if (backend_ctx.backend_type == GT_BACKEND_GADGETD) {
GVariantBuilder *b;
GVariant *gattrs;
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,
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;
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;