Tizen 2.1 base
[framework/multimedia/media-server.git] / common / media-server-utils.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /**
23  * This file defines api utilities of contents manager engines.
24  *
25  * @file                media-server-utils.c
26  * @author      Yong Yeon Kim(yy9875.kim@samsung.com)
27  * @version     1.0
28  * @brief       This file implements main database operation.
29  */
30
31 #include <errno.h>
32 #include <pmapi.h>
33 #include <vconf.h>
34
35 #include "media-util.h"
36 #include "media-server-dbg.h"
37 #include "media-server-ipc.h"
38 #include "media-server-inotify.h"
39 #include "media-server-utils.h"
40 #include "media-server-drm.h"
41 #include "media-server-dbus.h"
42 #include "media-server-socket.h"
43
44 #ifdef FMS_PERF
45 #include <sys/time.h>
46 #define MILLION 1000000L
47 struct timeval g_mmc_start_time;
48 struct timeval g_mmc_end_time;
49 #endif
50
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
52
53 extern int mmc_state;
54
55 static int
56 _ms_set_power_mode(ms_db_status_type_t status)
57 {
58         int res = MS_MEDIA_ERR_NONE;
59         int err;
60
61         switch (status) {
62         case MS_DB_UPDATING:
63                 err = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
64                 if (err != 0)
65                         res = MS_MEDIA_ERR_INTERNAL;
66                 break;
67         case MS_DB_UPDATED:
68                 err = pm_unlock_state(LCD_OFF, STAY_CUR_STATE);
69                 if (err != 0)
70                         res = MS_MEDIA_ERR_INTERNAL;
71                 break;
72         default:
73                 MS_DBG_ERR("Unacceptable type : %d", status);
74                 break;
75         }
76
77         return res;
78 }
79
80 int
81 ms_set_db_status(ms_db_status_type_t status)
82 {
83         int res = MS_MEDIA_ERR_NONE;
84         int err = 0;
85
86         if (status == MS_DB_UPDATING) {
87                 if (ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
88                           res = MS_MEDIA_ERR_VCONF_SET_FAIL;
89         } else if (status == MS_DB_UPDATED) {
90                 if(ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS,  VCONFKEY_FILEMANAGER_DB_UPDATED))
91                           res = MS_MEDIA_ERR_VCONF_SET_FAIL;
92                 /*notify to other application about db updated by DBUS*/
93                 ms_dbus_send_noti(MS_DBUS_DB_UPDATED);
94         }
95
96         err = _ms_set_power_mode(status);
97         if (err != MS_MEDIA_ERR_NONE) {
98                 MS_DBG_ERR("_ms_set_power_mode fail");
99                 res = err;
100         }
101
102         return res;
103 }
104
105 #ifdef FMS_PERF
106 void
107 ms_check_start_time(struct timeval *start_time)
108 {
109         gettimeofday(start_time, NULL);
110 }
111
112 void
113 ms_check_end_time(struct timeval *end_time)
114 {
115         gettimeofday(end_time, NULL);
116 }
117
118 void
119 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
120 {
121         struct timeval time;
122         long difftime;
123
124         time.tv_sec = end_time->tv_sec - start_time->tv_sec;
125         time.tv_usec = end_time->tv_usec - start_time->tv_usec;
126         difftime = MILLION * time.tv_sec + time.tv_usec;
127         MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
128                difftime, difftime / (double)MILLION);
129 }
130 #endif
131
132 bool
133 ms_is_mmc_inserted(void)
134 {
135         int data = -1;
136         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
137         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
138                 return false;
139         } else {
140                 return true;
141         }
142 }
143
144 /*CAUTION : Before using this function, Have to allocate static memory of ret_path*/
145 /*And the array length does not over MS_FILE_PATH_LEN_MAX*/
146 /*for example : char path[MS_FILE_PATH_LEN_MAX] = {0};*/
147 int
148 ms_get_full_path_from_node(ms_dir_scan_info * const node, char *ret_path, int depth)
149 {
150         int i = 0;
151         int path_length = 0;
152         int length = 0;
153         ms_dir_scan_info *cur_node;
154         char **path_array;
155
156         if (depth < 0) {
157                 MS_DBG_ERR("depth < 0");
158                 return MS_MEDIA_ERR_INVALID_PATH;
159         }
160
161         MS_MALLOC(path_array, sizeof(char*) * (depth + 1));
162
163         cur_node = node;
164
165         while (1) {
166                 path_array[i] = cur_node->name;
167                 if (cur_node->parent == NULL)
168                         break;
169
170                 cur_node = cur_node->parent;
171                 i++;
172         }
173
174         for(i = depth ; i >= 0 ; i --) {
175                 length = strlen(path_array[i]);
176
177                 if (path_length + length > MS_FILE_PATH_LEN_MAX) {
178                         MS_DBG_ERR("This is invalid path, %s, %d", node->name, depth);
179                         MS_SAFE_FREE(path_array);
180                         return MS_MEDIA_ERR_INVALID_PATH;
181                 }
182
183                 strncpy(ret_path+path_length, path_array[i], length);
184                 path_length += length;
185
186                 ret_path[path_length] = '/';
187                 path_length ++;
188         }
189
190         ret_path[-- path_length] = '\0';
191
192         MS_SAFE_FREE(path_array);
193
194         return MS_MEDIA_ERR_NONE;
195 }
196
197 ms_storage_type_t
198 ms_get_storage_type_by_full(const char *path)
199 {
200         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
201                 return MS_STORAGE_INTERNAL;
202         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
203                 return MS_STORAGE_EXTERNAL;
204         } else
205                 return MS_MEDIA_ERR_INVALID_PATH;
206 }
207
208 int
209 ms_strappend(char *res, const int size, const char *pattern,
210              const char *str1, const char *str2)
211 {
212         int len = 0;
213         int real_size = size - 1;
214
215         if (!res ||!pattern || !str1 ||!str2 )
216                 return MS_MEDIA_ERR_INVALID_PARAMETER;
217
218         if (real_size < (strlen(str1) + strlen(str2)))
219                 return MS_MEDIA_ERR_INVALID_PARAMETER;
220
221         len = snprintf(res, real_size, pattern, str1, str2);
222         if (len < 0) {
223                 return MS_MEDIA_ERR_INVALID_PARAMETER;
224         }
225
226         res[len] = '\0';
227
228         return MS_MEDIA_ERR_NONE;
229 }
230
231 int
232 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
233 {
234         int len = 0;
235         int real_size = size;
236
237         if (!res || !pattern || !str1) {
238                 MS_DBG_ERR("parameta is invalid");
239                 return MS_MEDIA_ERR_INVALID_PARAMETER;
240         }
241
242         if (real_size < strlen(str1)) {
243                 MS_DBG_ERR("size is wrong");
244                 return MS_MEDIA_ERR_INVALID_PARAMETER;
245         }
246
247         len = snprintf(res, real_size, pattern, str1);
248         if (len < 0) {
249                 MS_DBG_ERR("snprintf failed");
250                 return MS_MEDIA_ERR_INVALID_PARAMETER;
251         }
252
253         res[len] = '\0';
254
255         return MS_MEDIA_ERR_NONE;
256 }
257
258 bool
259 ms_config_get_int(const char *key, int *value)
260 {
261         int err;
262
263         if (!key || !value) {
264                 MS_DBG_ERR("Arguments key or value is NULL");
265                 return false;
266         }
267
268         err = vconf_get_int(key, value);
269         if (err == 0)
270                 return true;
271         else if (err == -1)
272                 return false;
273         else
274                 MS_DBG_ERR("Unexpected error code: %d", err);
275
276         return false;
277 }
278
279 bool
280 ms_config_set_int(const char *key, int value)
281 {
282         int err;
283
284         if (!key) {
285                 MS_DBG_ERR("Arguments key is NULL");
286                 return false;
287         }
288
289         err = vconf_set_int(key, value);
290         if (err == 0)
291                 return true;
292         else if (err == -1)
293                 return false;
294         else
295                 MS_DBG_ERR("Unexpected error code: %d", err);
296
297         return false;
298 }
299
300 bool
301 ms_config_get_str(const char *key, char *value)
302 {
303         char *res;
304         if (!key || !value) {
305                 MS_DBG_ERR("Arguments key or value is NULL");
306                 return false;
307         }
308
309         res = vconf_get_str(key);
310         if (res) {
311                 strncpy(value, res, strlen(res) + 1);
312                 return true;
313         }
314
315         return false;
316 }
317
318 bool
319 ms_config_set_str(const char *key, const char *value)
320 {
321         int err;
322
323         if (!key || !value) {
324                 MS_DBG_ERR("Arguments key or value is NULL");
325                 return false;
326         }
327
328         err = vconf_set_str(key, value);
329         if (err == 0)
330                 return true;
331         else
332                 MS_DBG_ERR("fail to vconf_set_str %d", err);
333
334         return false;
335 }
336