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"
40 #define DBUS_REPLY_TIMEOUT (-1)
42 struct storage_ext_callback {
43 storage_ext_changed_cb func;
48 static dd_list *changed_list;
50 static void storage_ext_release_internal(storage_ext_device *dev)
58 free(dev->fs_version);
60 free(dev->mount_point);
63 void storage_ext_release_device(storage_ext_device **dev)
67 storage_ext_release_internal(*dev);
72 void storage_ext_release_list(dd_list **list)
74 storage_ext_device *dev;
80 DD_LIST_FOREACH(*list, elem, dev) {
81 storage_ext_release_internal(dev);
89 static GDBusConnection *get_dbus_connection(void)
92 static GDBusConnection *conn;
97 #if !GLIB_CHECK_VERSION(2, 35, 0)
101 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
103 //LCOV_EXCL_START System Error
105 _E("fail to get dbus connection : %s", err->message);
108 _E("fail to get dbus connection");
115 static GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path,
116 const gchar *iface, const gchar *method, GVariant *param)
118 GDBusConnection *conn;
122 if (!dest || !path || !iface || !method || !param)
125 conn = get_dbus_connection();
127 _E("fail to get dbus connection"); //LCOV_EXCL_LINE
131 ret = g_dbus_connection_call_sync(conn,
132 dest, path, iface, method,
133 param, NULL, G_DBUS_CALL_FLAGS_NONE,
136 //LCOV_EXCL_START System Error
138 _E("dbus method sync call failed(%s)", err->message);
141 _E("g_dbus_connection_call_sync() failed");
149 int storage_ext_get_list(dd_list **list)
153 storage_ext_device *elem, info;
159 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
160 STORAGE_EXT_PATH_MANAGER,
161 STORAGE_EXT_IFACE_MANAGER,
162 STORAGE_EXT_GET_LIST,
163 g_variant_new("(s)", "all"));
165 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
169 g_variant_get(result, "(a(issssssisibii))", &iter);
171 while (g_variant_iter_loop(iter, "(issssssisibii)",
172 &info.type, &info.devnode, &info.syspath,
173 &info.fs_usage, &info.fs_type,
174 &info.fs_version, &info.fs_uuid,
175 &info.readonly, &info.mount_point,
176 &info.state, &info.primary,
177 &info.flags, &info.storage_id)) {
179 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
181 _E("malloc() failed"); //LCOV_EXCL_LINE
186 elem->type = info.type;
187 elem->readonly = info.readonly;
188 elem->state = info.state;
189 elem->primary = info.primary;
190 elem->devnode = strdup(CHECK_STR(info.devnode));
191 elem->syspath = strdup(CHECK_STR(info.syspath));
192 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
193 elem->fs_type = strdup(CHECK_STR(info.fs_type));
194 elem->fs_version = strdup(CHECK_STR(info.fs_version));
195 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
196 elem->mount_point = strdup(CHECK_STR(info.mount_point));
197 elem->flags = info.flags;
198 elem->storage_id = info.storage_id;
200 DD_LIST_APPEND(*list, elem);
203 ret = g_list_length(*list);
207 storage_ext_release_list(list);
208 g_variant_iter_free(iter);
209 g_variant_unref(result);
213 //LCOV_EXCL_START Not called Callback
214 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
216 storage_ext_device *dev;
218 struct storage_ext_callback *callback;
224 dev = calloc(1, sizeof(storage_ext_device));
228 g_variant_get(params, "(issssssisibii)",
243 DD_LIST_FOREACH(changed_list, elem, callback) {
246 ret = callback->func(dev, state, callback->data);
248 _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret);
251 storage_ext_release_device(&dev);
255 //LCOV_EXCL_START Not called Callback
256 static void storage_ext_changed(GDBusConnection *conn,
264 size_t iface_len, signal_len;
265 enum storage_ext_state state;
267 if (!params || !sender || !path || !iface || !signal)
270 iface_len = strlen(iface) + 1;
271 signal_len = strlen(signal) + 1;
273 if (strncmp(iface, STORAGE_EXT_IFACE, iface_len))
276 if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
277 state = STORAGE_EXT_CHANGED;
279 else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
280 state = STORAGE_EXT_ADDED;
282 else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
283 state = STORAGE_EXT_REMOVED;
288 storage_ext_device_changed(params, state, user_data);
292 int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
294 GDBusConnection *conn;
296 struct storage_ext_callback *callback;
302 DD_LIST_FOREACH(changed_list, elem, callback) {
303 if (callback->func != func)
305 if (callback->block_id == 0)
311 callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
313 //LCOV_EXCL_START System Error
314 _E("malloc() failed");
319 conn = get_dbus_connection();
321 //LCOV_EXCL_START System Error
323 _E("Failed to get dbus connection");
328 block_id = g_dbus_connection_signal_subscribe(conn,
329 STORAGE_EXT_BUS_NAME,
334 G_DBUS_SIGNAL_FLAGS_NONE,
340 _E("Failed to subscrive bus signal");
344 callback->func = func;
345 callback->data = data;
346 callback->block_id = block_id;
348 DD_LIST_APPEND(changed_list, callback);
353 void storage_ext_unregister_device_change(storage_ext_changed_cb func)
355 GDBusConnection *conn;
356 struct storage_ext_callback *callback;
362 conn = get_dbus_connection();
364 //LCOV_EXCL_START System Error
365 _E("fail to get dbus connection");
370 DD_LIST_FOREACH(changed_list, elem, callback) {
371 if (callback->func != func)
373 if (callback->block_id > 0)
374 g_dbus_connection_signal_unsubscribe(conn, callback->block_id);
376 DD_LIST_REMOVE(changed_list, callback);
381 int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
385 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
386 STORAGE_EXT_PATH_MANAGER,
387 STORAGE_EXT_IFACE_MANAGER,
389 g_variant_new("(i)", storage_id));
391 _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
395 g_variant_get(result, "(issssssisibii)",
396 &info->type, &info->devnode, &info->syspath,
397 &info->fs_usage, &info->fs_type,
398 &info->fs_version, &info->fs_uuid,
399 &info->readonly, &info->mount_point,
400 &info->state, &info->primary,
401 &info->flags, &info->storage_id);
403 g_variant_unref(result);