summaryrefslogtreecommitdiff
path: root/src/util/usb.c
blob: 2d9dafb2538b43253fd549dda92d038d5da44ea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * 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>

#include "util/usb.h"

#define DEVICED_INTERFACE_BLOCK "org.tizen.system.storage.BlockManager"

#define SIGNAL_DEVICE_BLOCKED "DeviceBlocked"
#define SIGNAL_DEVICE_CHANGED "DeviceChanged"

typedef void (*sig_cb_func)(GDBusConnection *connection,
			const gchar *sender_name, const gchar *object_path,
			const gchar *interface_name, const gchar *signal_name,
			GVariant *parameters, gpointer user_data);

static void _device_blocked_cb(GDBusConnection *, const gchar *, const gchar *,
			const gchar *, const gchar *, GVariant *, gpointer);
static void _device_changed_cb(GDBusConnection *, const gchar *, const gchar *,
			const gchar *, const gchar *, GVariant *, gpointer);

struct _signal_info {
	const char *interface;
	const char *sig_name;
	sig_cb_func sig_cb;
};

static struct _signal_info g_signal_info[E_SIGNAL_MAX] = {
	[E_DEVICE_BLOCKED] = {
		DEVICED_INTERFACE_BLOCK,
		SIGNAL_DEVICE_BLOCKED,
		_device_blocked_cb
	},
	[E_DEVICE_CHANGED] = {
		DEVICED_INTERFACE_BLOCK,
		SIGNAL_DEVICE_CHANGED,
		_device_changed_cb
	}
};

struct _cb_data {
	void (*func)(void *data, int state);
	void *data;
};

struct usb {
	GDBusConnection *conn;
	int sub_id[E_SIGNAL_MAX];
	struct _cb_data cb[E_SIGNAL_MAX];
};

static void _device_blocked_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 *uuid, *mount_point;
	struct usb *m;
	int sig;

	if (!parameters || !user_data) {
		_ERR("invalid argument");
		return;
	}

	m = user_data;

	g_variant_get(parameters, "(ss)", &uuid, &mount_point);

	sig = E_DEVICE_BLOCKED;

	if (m->cb[sig].func)
		m->cb[sig].func(m->cb[sig].data, USB_REMOVED);
}

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 *dev_node, *sys_path;
	const gchar *fs_usage, *fs_type, *fs_ver, *fs_uuid;
	const gchar *mount_point;
	gint32 block_type, read_only, state, flags, storage_id;
	gboolean primary;
	struct usb *m;
	int sig;

	if (!parameters || !user_data) {
		_ERR("invalid argument");
		return;
	}

	m = user_data;

	g_variant_get(parameters, "(issssssisibii)", &block_type, &dev_node,
				&sys_path, &fs_usage, &fs_type, &fs_ver,
				&fs_uuid, &read_only, &mount_point,
				&state, &primary, &flags, &storage_id);

	sig = E_DEVICE_CHANGED;

	if (m->cb[sig].func)
		m->cb[sig].func(m->cb[sig].data, state);
}

void usb_set_callback(struct usb *m, int sig,
			void (*func)(void *, int), void *data)
{
	if (!m) {
		_ERR("failed to get usb handle");
		return;
	}

	if (sig < 0 || sig >= E_SIGNAL_MAX) {
		_ERR("invalid argument");
		return;
	}

	m->cb[sig].func = func;
	m->cb[sig].data = data;
}

struct usb *usb_create(void)
{
	GDBusConnection *conn;
	GError *error;
	struct usb *m;
	struct _signal_info si;
	int i;

	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;
	}

	for (i = 0; i < E_SIGNAL_MAX; i++) {
		si = g_signal_info[i];

		m->sub_id[i] = g_dbus_connection_signal_subscribe(conn, NULL,
					si.interface, si.sig_name, NULL, NULL,
					G_DBUS_SIGNAL_FLAGS_NONE,
					si.sig_cb, m, NULL);
	}

	m->conn = conn;

	return m;
}

void usb_destroy(struct usb *m)
{
	int i;

	if (!m) {
		_ERR("failed to get usb handle");
		return;
	}

	for (i = 0; i < E_SIGNAL_MAX; i++)
		g_dbus_connection_signal_unsubscribe(m->conn, m->sub_id[i]);

	free(m);
}