4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
30 #include "storage-external-dbus.h"
32 #define CHECK_STR(a) (a ? a : "")
34 #define STORAGE_EXT_GET_LIST "GetDeviceList"
36 #define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged"
37 #define STORAGE_EXT_DEVICE_ADDED "DeviceAdded"
38 #define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved"
39 #define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked"
41 #define DBUS_REPLY_TIMEOUT (-1)
43 struct storage_ext_callback {
44 storage_ext_changed_cb func;
49 static dd_list *changed_list;
51 static void storage_ext_release_internal(storage_ext_device *dev)
59 free(dev->fs_version);
61 free(dev->mount_point);
64 void storage_ext_release_device(storage_ext_device **dev)
68 storage_ext_release_internal(*dev);
73 void storage_ext_release_list(dd_list **list)
75 storage_ext_device *dev;
81 DD_LIST_FOREACH(*list, elem, dev) {
82 storage_ext_release_internal(dev);
90 static GDBusConnection *get_dbus_connection(void)
93 static GDBusConnection *conn;
98 #if !GLIB_CHECK_VERSION(2, 35, 0)
102 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
104 //LCOV_EXCL_START System Error
106 _E("fail to get dbus connection : %s", err->message);
109 _E("fail to get dbus connection");
116 GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path,
117 const gchar *iface, const gchar *method, GVariant *param)
119 GDBusConnection *conn;
123 if (!dest || !path || !iface || !method)
126 conn = get_dbus_connection();
128 _E("fail to get dbus connection"); //LCOV_EXCL_LINE
132 ret = g_dbus_connection_call_sync(conn,
133 dest, path, iface, method,
134 param, NULL, G_DBUS_CALL_FLAGS_NONE,
137 //LCOV_EXCL_START System Error
139 _E("dbus method sync call failed(%s)", err->message);
142 _E("g_dbus_connection_call_sync() failed");
150 int storage_ext_get_list(dd_list **list)
154 storage_ext_device *elem, info;
160 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
161 STORAGE_EXT_PATH_MANAGER,
162 STORAGE_EXT_IFACE_MANAGER,
163 STORAGE_EXT_GET_LIST,
164 g_variant_new("(s)", "all"));
166 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
170 g_variant_get(result, "(a(issssssisibii))", &iter);
172 while (g_variant_iter_loop(iter, "(issssssisibii)",
173 &info.type, &info.devnode, &info.syspath,
174 &info.fs_usage, &info.fs_type,
175 &info.fs_version, &info.fs_uuid,
176 &info.readonly, &info.mount_point,
177 &info.state, &info.primary,
178 &info.flags, &info.storage_id)) {
180 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
182 _E("malloc() failed"); //LCOV_EXCL_LINE
187 elem->type = info.type;
188 elem->readonly = info.readonly;
189 elem->state = info.state;
190 elem->primary = info.primary;
191 elem->devnode = strdup(CHECK_STR(info.devnode));
192 elem->syspath = strdup(CHECK_STR(info.syspath));
193 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
194 elem->fs_type = strdup(CHECK_STR(info.fs_type));
195 elem->fs_version = strdup(CHECK_STR(info.fs_version));
196 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
197 elem->mount_point = strdup(CHECK_STR(info.mount_point));
198 elem->flags = info.flags;
199 elem->storage_id = info.storage_id;
201 DD_LIST_APPEND(*list, elem);
204 ret = g_list_length(*list);
208 storage_ext_release_list(list);
209 g_variant_iter_free(iter);
210 g_variant_unref(result);
214 //LCOV_EXCL_START Not called Callback
215 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
217 storage_ext_device *dev;
219 struct storage_ext_callback *callback;
225 dev = calloc(1, sizeof(storage_ext_device));
229 g_variant_get(params, "(issssssisibii)",
244 /* Callback is called when unmount is started(DeviceBlocked signal) */
245 if (state == STORAGE_EXT_CHANGED && dev->state == STORAGE_EXT_UNMOUNTED) {
246 storage_ext_release_device(&dev);
250 DD_LIST_FOREACH(changed_list, elem, callback) {
253 ret = callback->func(dev, state, callback->data);
255 _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret);
258 storage_ext_release_device(&dev);
262 //LCOV_EXCL_START Not called Callback
263 static void storage_ext_changed(GDBusConnection *conn,
271 size_t iface_len, signal_len;
272 enum storage_ext_state state;
274 if (!params || !sender || !path || !iface || !signal)
277 iface_len = strlen(iface) + 1;
278 signal_len = strlen(signal) + 1;
280 if (strncmp(iface, STORAGE_EXT_IFACE_MANAGER, iface_len))
283 if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
284 state = STORAGE_EXT_CHANGED;
286 else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
287 state = STORAGE_EXT_ADDED;
289 else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
290 state = STORAGE_EXT_REMOVED;
292 else if (!strncmp(signal, STORAGE_EXT_DEVICE_BLOCKED, signal_len))
293 state = STORAGE_EXT_BLOCKED;
298 storage_ext_device_changed(params, state, user_data);
302 int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
304 GDBusConnection *conn;
306 struct storage_ext_callback *callback;
312 DD_LIST_FOREACH(changed_list, elem, callback) {
313 if (callback->func != func)
315 if (callback->block_id == 0)
321 callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
323 //LCOV_EXCL_START System Error
324 _E("malloc() failed");
329 conn = get_dbus_connection();
331 //LCOV_EXCL_START System Error
333 _E("Failed to get dbus connection");
338 block_id = g_dbus_connection_signal_subscribe(conn,
340 STORAGE_EXT_IFACE_MANAGER,
344 G_DBUS_SIGNAL_FLAGS_NONE,
350 _E("Failed to subscrive bus signal");
354 callback->func = func;
355 callback->data = data;
356 callback->block_id = block_id;
358 DD_LIST_APPEND(changed_list, callback);
363 void storage_ext_unregister_device_change(storage_ext_changed_cb func)
365 GDBusConnection *conn;
366 struct storage_ext_callback *callback;
372 conn = get_dbus_connection();
374 //LCOV_EXCL_START System Error
375 _E("fail to get dbus connection");
380 DD_LIST_FOREACH(changed_list, elem, callback) {
381 if (callback->func != func)
383 if (callback->block_id > 0)
384 g_dbus_connection_signal_unsubscribe(conn, callback->block_id);
386 DD_LIST_REMOVE(changed_list, callback);
391 int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
395 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
396 STORAGE_EXT_PATH_MANAGER,
397 STORAGE_EXT_IFACE_MANAGER,
399 g_variant_new("(i)", storage_id));
401 _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
405 if (g_variant_check_format_string(result, "(issssssisibii)", true)) {
406 g_variant_get(result, "(issssssisibii)",
407 &info->type, &info->devnode, &info->syspath,
408 &info->fs_usage, &info->fs_type,
409 &info->fs_version, &info->fs_uuid,
410 &info->readonly, &info->mount_point,
411 &info->state, &info->primary,
412 &info->flags, &info->storage_id);
414 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
418 g_variant_unref(result);