4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * This file defines api utilities of contents manager engines.
25 * @file media-server-utils.c
26 * @author Yong Yeon Kim(yy9875.kim@samsung.com)
28 * @brief This file implements main database operation.
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"
46 #define MILLION 1000000L
47 struct timeval g_mmc_start_time;
48 struct timeval g_mmc_end_time;
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
56 _ms_set_power_mode(ms_db_status_type_t status)
58 int res = MS_MEDIA_ERR_NONE;
63 err = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
65 res = MS_MEDIA_ERR_INTERNAL;
68 err = pm_unlock_state(LCD_OFF, STAY_CUR_STATE);
70 res = MS_MEDIA_ERR_INTERNAL;
73 MS_DBG_ERR("Unacceptable type : %d", status);
81 ms_set_db_status(ms_db_status_type_t status)
83 int res = MS_MEDIA_ERR_NONE;
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);
96 err = _ms_set_power_mode(status);
97 if (err != MS_MEDIA_ERR_NONE) {
98 MS_DBG_ERR("_ms_set_power_mode fail");
107 ms_check_start_time(struct timeval *start_time)
109 gettimeofday(start_time, NULL);
113 ms_check_end_time(struct timeval *end_time)
115 gettimeofday(end_time, NULL);
119 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
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);
133 ms_is_mmc_inserted(void)
136 ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
137 if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
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};*/
148 ms_get_full_path_from_node(ms_dir_scan_info * const node, char *ret_path, int depth)
153 ms_dir_scan_info *cur_node;
157 MS_DBG_ERR("depth < 0");
158 return MS_MEDIA_ERR_INVALID_PATH;
161 MS_MALLOC(path_array, sizeof(char*) * (depth + 1));
166 path_array[i] = cur_node->name;
167 if (cur_node->parent == NULL)
170 cur_node = cur_node->parent;
174 for(i = depth ; i >= 0 ; i --) {
175 length = strlen(path_array[i]);
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;
183 strncpy(ret_path+path_length, path_array[i], length);
184 path_length += length;
186 ret_path[path_length] = '/';
190 ret_path[-- path_length] = '\0';
192 MS_SAFE_FREE(path_array);
194 return MS_MEDIA_ERR_NONE;
198 ms_get_storage_type_by_full(const char *path)
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;
205 return MS_MEDIA_ERR_INVALID_PATH;
209 ms_strappend(char *res, const int size, const char *pattern,
210 const char *str1, const char *str2)
213 int real_size = size - 1;
215 if (!res ||!pattern || !str1 ||!str2 )
216 return MS_MEDIA_ERR_INVALID_PARAMETER;
218 if (real_size < (strlen(str1) + strlen(str2)))
219 return MS_MEDIA_ERR_INVALID_PARAMETER;
221 len = snprintf(res, real_size, pattern, str1, str2);
223 return MS_MEDIA_ERR_INVALID_PARAMETER;
228 return MS_MEDIA_ERR_NONE;
232 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
235 int real_size = size;
237 if (!res || !pattern || !str1) {
238 MS_DBG_ERR("parameta is invalid");
239 return MS_MEDIA_ERR_INVALID_PARAMETER;
242 if (real_size < strlen(str1)) {
243 MS_DBG_ERR("size is wrong");
244 return MS_MEDIA_ERR_INVALID_PARAMETER;
247 len = snprintf(res, real_size, pattern, str1);
249 MS_DBG_ERR("snprintf failed");
250 return MS_MEDIA_ERR_INVALID_PARAMETER;
255 return MS_MEDIA_ERR_NONE;
259 ms_config_get_int(const char *key, int *value)
263 if (!key || !value) {
264 MS_DBG_ERR("Arguments key or value is NULL");
268 err = vconf_get_int(key, value);
274 MS_DBG_ERR("Unexpected error code: %d", err);
280 ms_config_set_int(const char *key, int value)
285 MS_DBG_ERR("Arguments key is NULL");
289 err = vconf_set_int(key, value);
295 MS_DBG_ERR("Unexpected error code: %d", err);
301 ms_config_get_str(const char *key, char *value)
304 if (!key || !value) {
305 MS_DBG_ERR("Arguments key or value is NULL");
309 res = vconf_get_str(key);
311 strncpy(value, res, strlen(res) + 1);
319 ms_config_set_str(const char *key, const char *value)
323 if (!key || !value) {
324 MS_DBG_ERR("Arguments key or value is NULL");
328 err = vconf_set_str(key, value);
332 MS_DBG_ERR("fail to vconf_set_str %d", err);