add usb util to get usb device changed signal 67/54267/1
authorJehun Lim <jehun.lim@samsung.com>
Mon, 14 Dec 2015 11:04:38 +0000 (20:04 +0900)
committerJehun Lim <jehun.lim@samsung.com>
Mon, 14 Dec 2015 11:04:38 +0000 (20:04 +0900)
Change-Id: If0f0128234af530eda7d02f36a337b4d09b49026
Signed-off-by: Jehun Lim <jehun.lim@samsung.com>
CMakeLists.txt
include/util/usb.h [new file with mode: 0644]
packaging/org.tizen.mediahub.spec
src/util/usb.c [new file with mode: 0644]

index 8d9e4da..b75a26d 100644 (file)
@@ -19,6 +19,7 @@ PROJECT("mediahub" C)
 INCLUDE(FindPkgConfig)
 pkg_check_modules(PKGS REQUIRED
                glib-2.0
+               gio-2.0
                elementary
                capi-appfw-application
                capi-media-player
@@ -71,6 +72,7 @@ src/layout/music.c
 src/util/controller.c
 src/util/listmgr.c
 src/util/timeout_handler.c
+src/util/usb.c
 src/util/util.c
 src/util/playermgr.c
 src/util/ctxpopup.c
diff --git a/include/util/usb.h b/include/util/usb.h
new file mode 100644 (file)
index 0000000..cc19546
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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.
+ */
+
+#ifndef __AIR_MEDIAHUB_USB_H__
+#define __AIR_MEDIAHUB_USB_H__
+
+struct usb;
+
+struct usb *usb_create(void);
+void usb_destroy(struct usb *m);
+
+int usb_set_callback(struct usb *m, void (*func)(void *, int),
+                       void *data);
+
+#endif /* __AIR_MEDIAHUB_USB_H__ */
index 978ad20..6058c18 100644 (file)
@@ -9,6 +9,7 @@ Source1:   %{name}.manifest
 
 BuildRequires: cmake
 BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(gio-2.0)
 BuildRequires: pkgconfig(elementary)
 BuildRequires: pkgconfig(capi-appfw-application)
 BuildRequires: pkgconfig(capi-media-player)
diff --git a/src/util/usb.c b/src/util/usb.c
new file mode 100644 (file)
index 0000000..001b72d
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <stdlib.h>
+#include <stdbool.h>
+#include <app_debug.h>
+#include <gio/gio.h>
+
+#define DEVICED_BUS_NAME "org.tizen.system.deviced"
+#define DEVICED_INTERFACE_BLOCK "org.tizen.system.deviced.Block"
+
+#define SIGNAL_DEVICE_CHANGED "DeviceChanged"
+
+struct _changed_cb {
+       void (*func)(void *data, int state);
+       void *data;
+};
+
+struct usb {
+       GDBusConnection *conn;
+       guint id;
+       struct _changed_cb cb;
+};
+
+static void _device_changed_cb(GDBusConnection *connection,
+                       const gchar *sender_name, const gchar *object_path,
+                       const gchar *interface_name, const gchar *signal_name,
+                       GVariant *parameters, gpointer user_data)
+{
+       const gchar *devNode, *sysPath, *fsUsage, *fsType, *fsVer, *fsUuid;
+       const gchar *mountPoint;
+       gint32 blockType, readOnly, state;
+       gboolean primary;
+       struct usb *m;
+
+       if (!parameters || !user_data) {
+               _ERR("invalid argument");
+               return;
+       }
+
+       m = user_data;
+
+       g_variant_get(parameters, "(issssssisib)", &blockType, &devNode,
+                               &sysPath, &fsUsage, &fsType, &fsVer, &fsUuid,
+                               &readOnly, &mountPoint, &state, &primary);
+
+       if (m->cb.func)
+               m->cb.func(m->cb.data, state);
+}
+
+void usb_set_callback(void *handle, void (*func)(void *, int), void *data)
+{
+       struct usb *m;
+
+       if (!handle) {
+               _ERR("failed to get usb handle");
+               return;
+       }
+
+       m = handle;
+
+       m->cb.func = func;
+       m->cb.data = data;
+}
+
+struct usb *usb_create(void)
+{
+       GDBusConnection *conn;
+       GError *error;
+       guint id;
+       struct usb *m;
+
+       m = calloc(1, sizeof(*m));
+       if (!m) {
+               _ERR("failed to alloc usb");
+               return NULL;
+       }
+
+       error = NULL;
+
+       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (!conn) {
+               _ERR("failed to get bus connection: %s",
+                                       error ? error->message : "");
+               g_error_free(error);
+               free(m);
+               return NULL;
+       }
+
+       id = g_dbus_connection_signal_subscribe(conn, NULL,
+                               DEVICED_INTERFACE_BLOCK, SIGNAL_DEVICE_CHANGED,
+                               NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
+                               _device_changed_cb, m, NULL);
+
+       m->conn = conn;
+       m->id = id;
+
+       return m;
+}
+
+void usb_destroy(struct usb *m)
+{
+       if (!m) {
+               _ERR("failed to get usb handle");
+               return;
+       }
+
+       g_dbus_connection_signal_unsubscribe(m->conn, m->id);
+
+       free(m);
+}