From a167d7f44c54bd229a306ffce4106616c9d19993 Mon Sep 17 00:00:00 2001 From: Jehun Lim Date: Mon, 14 Dec 2015 20:04:38 +0900 Subject: add usb util to get usb device changed signal Change-Id: If0f0128234af530eda7d02f36a337b4d09b49026 Signed-off-by: Jehun Lim --- CMakeLists.txt | 2 + include/util/usb.h | 28 +++++++++ packaging/org.tizen.mediahub.spec | 1 + src/util/usb.c | 124 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 include/util/usb.h create mode 100644 src/util/usb.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d9e4da..b75a26d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 index 0000000..cc19546 --- /dev/null +++ b/include/util/usb.h @@ -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__ */ diff --git a/packaging/org.tizen.mediahub.spec b/packaging/org.tizen.mediahub.spec index 978ad20..6058c18 100644 --- a/packaging/org.tizen.mediahub.spec +++ b/packaging/org.tizen.mediahub.spec @@ -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 index 0000000..001b72d --- /dev/null +++ b/src/util/usb.c @@ -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 +#include +#include +#include + +#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); +} -- cgit v1.2.3