tizen 2.3 release
[framework/system/deviced.git] / src / libdeviced / mmc.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <stdio.h>
21 #include <vconf.h>
22 #include <errno.h>
23
24 #include "log.h"
25 #include "dbus.h"
26 #include "common.h"
27 #include "dd-mmc.h"
28
29 #define METHOD_REQUEST_SECURE_MOUNT             "RequestSecureMount"
30 #define METHOD_REQUEST_SECURE_UNMOUNT   "RequestSecureUnmount"
31 #define METHOD_REQUEST_MOUNT            "RequestMount"
32 #define METHOD_REQUEST_UNMOUNT          "RequestUnmount"
33 #define METHOD_REQUEST_FORMAT           "RequestFormat"
34
35 #define ODE_MOUNT_STATE 1
36
37 #define FORMAT_TIMEOUT  (120*1000)
38
39 API int mmc_secure_mount(const char *mount_point)
40 {
41         if (mount_point == NULL) {
42                 return -EINVAL;
43         }
44
45         char *arr[1];
46         arr[0] = (char *)mount_point;
47         return dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH_MMC,
48                         DEVICED_INTERFACE_MMC, METHOD_REQUEST_SECURE_MOUNT, "s", arr);
49 }
50
51 API int mmc_secure_unmount(const char *mount_point)
52 {
53         if (mount_point == NULL) {
54                 return -EINVAL;
55         }
56
57         char *arr[1];
58         arr[0] = (char *)mount_point;
59         return dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH_MMC,
60                         DEVICED_INTERFACE_MMC, METHOD_REQUEST_SECURE_UNMOUNT, "s", arr);
61 }
62
63 static void mount_mmc_cb(void *data, DBusMessage *msg, DBusError *err)
64 {
65         struct mmc_contents *mmc_data = (struct mmc_contents*)data;
66         DBusError r_err;
67         int r, mmc_ret;
68
69         _D("mount_mmc_cb called");
70
71         if (!msg) {
72                 _E("no message [%s:%s]", err->name, err->message);
73                 mmc_ret = -EBADMSG;
74                 goto exit;
75         }
76
77         dbus_error_init(&r_err);
78         r = dbus_message_get_args(msg, &r_err, DBUS_TYPE_INT32, &mmc_ret, DBUS_TYPE_INVALID);
79         if (!r) {
80                 _E("no message [%s:%s]", r_err.name, r_err.message);
81                 dbus_error_free(&r_err);
82                 mmc_ret = -EBADMSG;
83                 goto exit;
84         }
85
86         _I("Mount State : %d", mmc_ret);
87
88 //      if (mmc_ret == ODE_MOUNT_STATE)
89 //              return;
90
91 exit:
92         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
93 }
94
95 API int deviced_request_mount_mmc(struct mmc_contents *mmc_data)
96 {
97         void (*mount_cb)(void*, DBusMessage*, DBusError*) = NULL;
98         void *data = NULL;
99
100         if (mmc_data != NULL && mmc_data->mmc_cb != NULL) {
101                 mount_cb = mount_mmc_cb;
102                 data = mmc_data;
103         }
104
105         return dbus_method_async_with_reply(DEVICED_BUS_NAME,
106                         DEVICED_PATH_MMC,
107                         DEVICED_INTERFACE_MMC,
108                         METHOD_REQUEST_MOUNT,
109                         NULL, NULL, mount_cb, -1, data);
110 }
111
112 static void unmount_mmc_cb(void *data, DBusMessage *msg, DBusError *err)
113 {
114         struct mmc_contents *mmc_data = (struct mmc_contents*)data;
115         DBusError r_err;
116         int r, mmc_ret;
117
118         _D("unmount_mmc_cb called");
119
120         if (!msg) {
121                 _E("no message [%s:%s]", err->name, err->message);
122                 mmc_ret = -EBADMSG;
123                 goto exit;
124         }
125
126         dbus_error_init(&r_err);
127         r = dbus_message_get_args(msg, &r_err, DBUS_TYPE_INT32, &mmc_ret, DBUS_TYPE_INVALID);
128         if (!r) {
129                 _E("no message [%s:%s]", r_err.name, r_err.message);
130                 dbus_error_free(&r_err);
131                 mmc_ret = -EBADMSG;
132                 goto exit;
133         }
134
135         _I("Unmount State : %d", mmc_ret);
136
137 exit:
138         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
139 }
140
141 API int deviced_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
142 {
143         char *arr[1];
144         char buf_opt[32];
145         void (*unmount_cb)(void*, DBusMessage*, DBusError*) = NULL;
146         void *data = NULL;
147
148         if (option < 0 || option > 1)
149                 return -EINVAL;
150
151         if (mmc_data != NULL && mmc_data->mmc_cb != NULL) {
152                 unmount_cb = unmount_mmc_cb;
153                 data = mmc_data;
154         }
155
156         snprintf(buf_opt, sizeof(buf_opt), "%d", option);
157         arr[0] = buf_opt;
158         return dbus_method_async_with_reply(DEVICED_BUS_NAME,
159                         DEVICED_PATH_MMC,
160                         DEVICED_INTERFACE_MMC,
161                         METHOD_REQUEST_UNMOUNT,
162                         "i", arr, unmount_cb, -1, data);
163 }
164
165 static void format_mmc_cb(void *data, DBusMessage *msg, DBusError *err)
166 {
167         struct mmc_contents *mmc_data = (struct mmc_contents*)data;
168         DBusError r_err;
169         int r, mmc_ret;
170
171         _D("format_mmc_cb called");
172
173         if (!msg) {
174                 _E("no message [%s:%s]", err->name, err->message);
175                 mmc_ret = -EBADMSG;
176                 goto exit;
177         }
178
179         dbus_error_init(&r_err);
180         r = dbus_message_get_args(msg, &r_err, DBUS_TYPE_INT32, &mmc_ret, DBUS_TYPE_INVALID);
181         if (!r) {
182                 _E("no message [%s:%s]", r_err.name, r_err.message);
183                 dbus_error_free(&r_err);
184                 mmc_ret = -EBADMSG;
185                 goto exit;
186         }
187
188         _I("Format State : %d", mmc_ret);
189
190 exit:
191         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
192 }
193
194 API int deviced_request_format_mmc(struct mmc_contents *mmc_data)
195 {
196         return deviced_format_mmc(mmc_data, 1);
197 }
198
199 API int deviced_format_mmc(struct mmc_contents *mmc_data, int option)
200 {
201         char *arr[1];
202         char buf_opt[32];
203         void (*format_cb)(void*, DBusMessage*, DBusError*) = NULL;
204         void *data = NULL;
205
206         if (option < 0 || option > 1)
207                 return -EINVAL;
208
209         if (mmc_data != NULL && mmc_data->mmc_cb != NULL) {
210                 format_cb = format_mmc_cb;
211                 data = mmc_data;
212         }
213
214         snprintf(buf_opt, sizeof(buf_opt), "%d", option);
215         arr[0] = buf_opt;
216         return dbus_method_async_with_reply(DEVICED_BUS_NAME,
217                         DEVICED_PATH_MMC,
218                         DEVICED_INTERFACE_MMC,
219                         METHOD_REQUEST_FORMAT,
220                         "i", arr, format_cb, FORMAT_TIMEOUT, data);
221 }