backend: Introduce notion of different backends
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Sun, 28 Sep 2014 23:38:04 +0000 (01:38 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 30 Sep 2014 16:05:26 +0000 (18:05 +0200)
Backend is currently selected using argv[0] name:

 - gt selects libusbg (direct) backend
 - gadgetctl selects gadgetd (d-bus) backend

Any other program name will trigger autodetection
(d-bus then direct).

Change-Id: Ie8459416b11110afb6a09d1855c3b26b595e7067
Signed-off-by: Karol Lewandowski <k.lewandowsk@samsung.com>
source/base/CMakeLists.txt
source/base/include/backend.h [new file with mode: 0644]
source/base/src/backend.c [new file with mode: 0644]
source/main.c

index 612a037..e64d7cf 100644 (file)
@@ -6,6 +6,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include
                        ${CMAKE_SOURCE_DIR}/settings/include )
 
 SET( BASE_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/backend.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src/command.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src/executable_command.c
diff --git a/source/base/include/backend.h b/source/base/include/backend.h
new file mode 100644 (file)
index 0000000..52da209
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014 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.
+ */
+
+/**
+ * @file backend.h
+ * @brief Declaration of methods related to different backends available
+ */
+
+#ifndef __GADGET_TOOL_BACKEND_H__
+#define __GADGET_TOOL_BACKEND_H__
+
+#include <usbg/usbg.h>
+#include <gio/gio.h>
+#include "parser.h"
+
+/**
+ * @brief Initialize global backend type according to supplied program name and opts.
+ * @param[in] program_name program invocation name
+ * @param[in] flags program flags
+ */
+int gt_backend_init(const char *program_name, enum gt_option_flags flags);
+
+/**
+ * Backend type
+ */
+enum gt_backend_type {
+       GT_BACKEND_AUTO = 0,
+       GT_BACKEND_GADGETD,
+       GT_BACKEND_LIBUSBG,
+};
+
+struct gt_backend_ctx {
+       enum gt_backend_type backend;
+
+       union {
+               usbg_state *libusbg_state;
+               GDBusConnection *gadgetd_conn;
+       };
+};
+
+extern struct gt_backend_ctx backend_ctx;
+
+#endif /* __GADGET_TOOL_BACKEND_H__ */
diff --git a/source/base/src/backend.c b/source/base/src/backend.c
new file mode 100644 (file)
index 0000000..3c67d65
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2014 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 <string.h>
+#include <usbg/usbg.h>
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "backend.h"
+
+struct gt_backend_ctx backend_ctx = {
+       .backend = GT_BACKEND_AUTO,
+};
+
+int gt_backend_init(const char *program_name, enum gt_option_flags flags)
+{
+       enum gt_backend_type backend;
+       GError *err = NULL;
+
+       if (strcmp(program_name, "gt") == 0)
+               backend = GT_BACKEND_LIBUSBG;
+       else if (strcmp(program_name, "gadgetctl") == 0)
+               backend = GT_BACKEND_GADGETD;
+       else
+               backend = GT_BACKEND_AUTO;
+
+       if (backend == GT_BACKEND_GADGETD || backend == GT_BACKEND_AUTO) {
+               GDBusConnection *conn;
+
+#if ! GLIB_CHECK_VERSION(2, 36, 0)
+               g_type_init();
+#endif
+
+               conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               if (err) {
+                       fprintf(stderr, "Unable to connect to d-bus: %s\n", err->message);
+                       goto out_gadgetd;
+               }
+
+               g_dbus_connection_call_sync(conn,
+                                           "org.usb.gadgetd",
+                                           "/",
+                                           "org.freedesktop.DBus.Peer",
+                                           "Ping",
+                                           NULL,
+                                           NULL,
+                                           G_DBUS_CALL_FLAGS_NONE,
+                                           -1,
+                                           NULL,
+                                           &err);
+               if (err) {
+                       /* We omit showing glib-provided error message here
+                        * as it's not really that useful for end-users.
+                        *
+                        * This message could be probably shown in verbose
+                        * mode (which we don't have yet).
+                        */
+                       fprintf(stderr, "Unable to initialize gadgetd backend\n");
+                       goto out_gadgetd;
+               }
+
+               backend_ctx.backend = GT_BACKEND_GADGETD;
+               backend_ctx.gadgetd_conn = conn;
+               return 0;
+
+       }
+
+out_gadgetd:
+       if (err && backend == GT_BACKEND_GADGETD)
+               return -1;
+
+       if (backend == GT_BACKEND_LIBUSBG || backend == GT_BACKEND_AUTO) {
+               usbg_state *s = NULL;
+               int r;
+
+               r = usbg_init("/sys/kernel/config", &s);
+               if (r != USBG_SUCCESS) {
+                       fprintf(stderr, "Unable to initialize libusbg backend: %s\n", usbg_strerror(r));
+                       goto out_libusbg;
+               }
+
+               backend_ctx.backend = GT_BACKEND_LIBUSBG;
+               backend_ctx.libusbg_state = s;
+               return 0;
+       }
+
+out_libusbg:
+       return -1;
+}
index 9ca9f95..2c29eb8 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "common.h"
 #include "parser.h"
+#include "backend.h"
 #include "executable_command.h"
 
 char *program_name;
@@ -53,11 +54,15 @@ int main(int argc, char **argv)
        char *buf = NULL;
 
        program_name = program_name_get(argv[0], &buf);
+       ret = gt_backend_init(program_name, 0);
+       if (ret < 0)
+               goto out;
        gt_parse_commands(argc, argv, &cmd);
 
        ret = executable_command_exec(&cmd);
        executable_command_clean(&cmd);
 
+out:
        free(buf);
        return ret;
 }