merge with master
[platform/core/multimedia/media-server.git] / src / common / media-common-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 #include <aul/aul.h>
35
36 #include "media-util.h"
37 #include "media-server-ipc.h"
38 #include "media-common-dbg.h"
39 #include "media-common-drm.h"
40 #include "media-common-utils.h"
41
42 #ifdef FMS_PERF
43 #include <sys/time.h>
44 #define MILLION 1000000L
45 struct timeval g_mmc_start_time;
46 struct timeval g_mmc_end_time;
47 #endif
48
49 #define MS_DRM_CONTENT_TYPE_LENGTH 100
50
51 #ifdef FMS_PERF
52 void
53 ms_check_start_time(struct timeval *start_time)
54 {
55         gettimeofday(start_time, NULL);
56 }
57
58 void
59 ms_check_end_time(struct timeval *end_time)
60 {
61         gettimeofday(end_time, NULL);
62 }
63
64 void
65 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
66 {
67         struct timeval time;
68         long difftime;
69
70         time.tv_sec = end_time->tv_sec - start_time->tv_sec;
71         time.tv_usec = end_time->tv_usec - start_time->tv_usec;
72         difftime = MILLION * time.tv_sec + time.tv_usec;
73         MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
74                difftime, difftime / (double)MILLION);
75 }
76 #endif
77
78 bool
79 ms_is_mmc_inserted(void)
80 {
81         int data = -1;
82         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
83         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
84                 return false;
85         } else {
86                 return true;
87         }
88 }
89
90 ms_storage_type_t
91 ms_get_storage_type_by_full(const char *path)
92 {
93         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
94                 return MS_STORAGE_INTERNAL;
95         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
96                 return MS_STORAGE_EXTERNAL;
97         } else
98                 return MS_MEDIA_ERR_INVALID_PATH;
99 }
100
101 int
102 ms_get_mime(const char *path, char *mimetype)
103 {
104         int ret = MS_MEDIA_ERR_NONE;
105
106         if (path == NULL)
107                 return MS_MEDIA_ERR_INVALID_PARAMETER;
108
109         /*get content type and mime type from file. */
110         /*in case of drm file. */
111         if (ms_is_drm_file(path)) {
112                 if (ms_get_mime_in_drm_info(path, mimetype) != MS_MEDIA_ERR_NONE) {
113                         MS_DBG_ERR("Fail to get mime from drm API");
114                         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
115                                 MS_DBG_ERR("aul_get_mime_from_file fail");
116                                 ret = MS_MEDIA_ERR_MIME_GET_FAIL;
117                         } else {
118                                 MS_DBG_ERR("aul_get_mime_from_file success");
119                                 ret = MS_MEDIA_ERR_NONE;
120                         }
121                 }
122         } else {
123                 /*in case of normal files */
124                 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
125                         MS_DBG_ERR("aul_get_mime_from_file fail");
126                         ret = MS_MEDIA_ERR_MIME_GET_FAIL;
127                 }
128         }
129
130         return ret;
131 }
132
133 int
134 ms_strappend(char *res, const int size, const char *pattern,
135              const char *str1, const char *str2)
136 {
137         int len = 0;
138         int real_size = size - 1;
139
140         if (!res ||!pattern || !str1 ||!str2 )
141                 return MS_MEDIA_ERR_INVALID_PARAMETER;
142
143         if (real_size < (strlen(str1) + strlen(str2)))
144                 return MS_MEDIA_ERR_INVALID_PARAMETER;
145
146         len = snprintf(res, real_size, pattern, str1, str2);
147         if (len < 0) {
148                 return MS_MEDIA_ERR_INVALID_PARAMETER;
149         }
150
151         res[len] = '\0';
152
153         return MS_MEDIA_ERR_NONE;
154 }
155
156 int
157 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
158 {
159         int len = 0;
160         int real_size = size;
161
162         if (!res || !pattern || !str1) {
163                 MS_DBG_ERR("parameta is invalid");
164                 return MS_MEDIA_ERR_INVALID_PARAMETER;
165         }
166
167         if (real_size < strlen(str1)) {
168                 MS_DBG_ERR("size is wrong");
169                 return MS_MEDIA_ERR_INVALID_PARAMETER;
170         }
171
172         len = snprintf(res, real_size, pattern, str1);
173         if (len < 0) {
174                 MS_DBG_ERR("snprintf failed");
175                 return MS_MEDIA_ERR_INVALID_PARAMETER;
176         }
177
178         res[len] = '\0';
179
180         return MS_MEDIA_ERR_NONE;
181 }
182
183 bool
184 ms_config_get_int(const char *key, int *value)
185 {
186         int err;
187
188         if (!key || !value) {
189                 MS_DBG_ERR("Arguments key or value is NULL");
190                 return false;
191         }
192
193         err = vconf_get_int(key, value);
194         if (err == 0)
195                 return true;
196         else if (err == -1)
197                 return false;
198         else
199                 MS_DBG_ERR("Unexpected error code: %d", err);
200
201         return false;
202 }
203
204 bool
205 ms_config_set_int(const char *key, int value)
206 {
207         int err;
208
209         if (!key) {
210                 MS_DBG_ERR("Arguments key is NULL");
211                 return false;
212         }
213
214         err = vconf_set_int(key, value);
215         if (err == 0)
216                 return true;
217         else if (err == -1)
218                 return false;
219         else
220                 MS_DBG_ERR("Unexpected error code: %d", err);
221
222         return false;
223 }
224
225 bool
226 ms_config_get_str(const char *key, char *value)
227 {
228         char *res;
229         if (!key || !value) {
230                 MS_DBG_ERR("Arguments key or value is NULL");
231                 return false;
232         }
233
234         res = vconf_get_str(key);
235         if (res) {
236                 strncpy(value, res, strlen(res) + 1);
237                 return true;
238         }
239
240         return false;
241 }
242
243 bool
244 ms_config_set_str(const char *key, const char *value)
245 {
246         int err;
247
248         if (!key || !value) {
249                 MS_DBG_ERR("Arguments key or value is NULL");
250                 return false;
251         }
252
253         err = vconf_set_str(key, value);
254         if (err == 0)
255                 return true;
256         else
257                 MS_DBG_ERR("fail to vconf_set_str %d", err);
258
259         return false;
260 }
261