Fix documentation for storage_get_internal_memory_size and storage_get_external_memor...
[platform/core/system/libstorage.git] / src / storage-external-dbus.c
index 8b66ca3..5d45189 100755 (executable)
 #include <assert.h>
 #include <limits.h>
 #include <sys/statvfs.h>
+#include <tzplatform_config.h>
 
 #include "log.h"
 #include "storage-external-dbus.h"
 
 #define CHECK_STR(a) (a ? a : "")
 
-#define STORAGE_EXT_GET_LIST       "GetDeviceList"
-#define STORAGE_EXT_GET_STATVFS    "GetStatvfs"
+#define STORAGE_EXT_GET_LIST               "GetDeviceList"
+#define STORAGE_EXT_GET_STATVFS            "GetStatvfs"
+#define STORAGE_EXT_GET_STORAGE_LEVEL      "GetStorageLevel"
 
-#define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged"
-#define STORAGE_EXT_DEVICE_ADDED   "DeviceAdded"
-#define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved"
-#define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked"
+#define STORAGE_EXT_DEVICE_CHANGED         "DeviceChanged"
+#define STORAGE_EXT_DEVICE_ADDED           "DeviceAdded"
+#define STORAGE_EXT_DEVICE_REMOVED         "DeviceRemoved"
+#define STORAGE_EXT_DEVICE_BLOCKED         "DeviceBlocked"
 
 #define DBUS_REPLY_TIMEOUT (-1)
 
@@ -49,6 +51,11 @@ struct storage_ext_callback {
        guint block_id;
 };
 
+typedef struct {
+        dbus_pending_cb func;
+        void *data;
+} pending_call_data;
+
 static dd_list *changed_list;
 
 static void storage_ext_release_internal(storage_ext_device *dev)
@@ -116,6 +123,79 @@ static GDBusConnection *get_dbus_connection(void)
        return conn;
 }
 
+static void _cb_pending(GDBusConnection *conn,
+                       GAsyncResult *res,
+                       gpointer user_data)
+{
+       GVariant *reply = NULL;
+       GError *err = NULL;
+       pending_call_data *data = (pending_call_data *)user_data;
+
+       reply = g_dbus_connection_call_finish(conn, res, &err);
+       if (!reply || err) {
+               if (!err)
+                       g_set_error(&err, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Error during g_dbus_connection_call");
+
+               if (data && data->func)
+                       data->func(NULL, data->data, err);
+               goto out;
+       }
+
+       if (data && data->func)
+               data->func(reply, data->data, err);
+out:
+       if (err)
+               g_error_free(err);
+       if (data)
+               free(data);
+}
+
+int dbus_method_async_with_reply_var(const char *dest, const char *path,
+               const char *iface, const char *method, GVariant *param,
+               dbus_pending_cb cb, int timeout, void *data)
+{
+       GDBusConnection *conn;
+       pending_call_data *pdata = NULL;
+       int ret = 0;
+
+       if (!dest || !path || !iface || !method)
+               return -EINVAL;
+
+       if (timeout < -1) {
+               _E("wrong timeout %d", timeout);
+               return -EINVAL;
+       }
+
+       conn = get_dbus_connection();
+       if (!conn) {
+               _E("fail to get dbus connection"); //LCOV_EXCL_LINE
+               return -1;
+       }
+
+       if (cb) {
+               pdata = (pending_call_data*)malloc(sizeof(pending_call_data));
+               if (!pdata) {
+                       ret = -ENOMEM;
+                       goto err;
+               }
+
+               pdata->func = cb;
+               pdata->data = data;
+       }
+
+       g_dbus_connection_call(conn, dest, path, iface, method,
+                       param, NULL, G_DBUS_CALL_FLAGS_NONE, timeout, NULL,
+                       (GAsyncReadyCallback)_cb_pending,
+                       pdata);
+
+       return ret;
+err:
+       if (param)
+               g_variant_unref(param);
+       return ret;
+}
+
 GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path,
                                const gchar *iface, const gchar *method, GVariant *param)
 {
@@ -286,6 +366,41 @@ int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf)
        return 0;
 }
 
+int storage_ext_get_storage_level(const char *path, char **level)
+{
+       GVariant *result;
+       char *tmp;
+       enum tzplatform_variable id;
+
+       if (!strcmp(path, tzplatform_getenv(TZ_SYS_USER)))
+               id = TZ_SYS_USER;
+       else if (!strcmp(path, tzplatform_getenv(TZ_SYS_TMP)))
+               id = TZ_SYS_TMP;
+       else if (!strcmp(path, tzplatform_getenv(TZ_SYS_OPT)))
+               id = TZ_SYS_OPT;
+       else {
+               _E("Invalid path");
+               return -EINVAL;
+       }
+
+       result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
+                       STORAGE_EXT_PATH_STORAGE,
+                       STORAGE_EXT_IFACE_STORAGE,
+                       STORAGE_EXT_GET_STORAGE_LEVEL,
+                       g_variant_new("(i)", id));
+       if (!result) {
+               _E("Failed to get %d level", id);
+               return -EIO;
+       }
+
+       g_variant_get(result, "(s)", &tmp);
+       *level = strdup(tmp);
+       if (*level == NULL)
+               return -ENOMEM;
+
+       return 0;
+}
+
 //LCOV_EXCL_START Not called Callback
 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
 {