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.
27 #include <notification.h>
29 #include "media-util.h"
30 #include "media-server-dbg.h"
31 #include "media-server-utils.h"
32 #include "media-server-ipc.h"
33 #include "media-server-socket.h"
34 #include "media-server-inotify.h"
35 #include "media-server-db-svc.h"
36 #include "media-server-scanner.h"
37 #include "media-server-external-storage.h"
38 #include "media-server-drm.h"
40 #define MMC_INFO_SIZE 256
44 char default_path[][MS_FILE_NAME_LEN_MAX + 1] = {
45 {"/opt/storage/sdcard/Images"},
46 {"/opt/storage/sdcard/Videos"},
47 {"/opt/storage/sdcard/Sounds"},
48 {"/opt/storage/sdcard/Downloads"},
49 {"/opt/storage/sdcard/Camera"}
52 #define DIR_NUM ((int)(sizeof(default_path)/sizeof(default_path[0])))
55 ms_make_default_path_mmc(void)
61 for (i = 0; i < DIR_NUM; ++i) {
62 dp = opendir(default_path[i]);
64 ret = mkdir(default_path[i], 0777);
69 ms_inoti_add_watch(default_path[i]);
71 /*this fuction for emulator*/
72 /*at the first time, the directroies are made permission 755*/
73 chmod(default_path[i], 0777);
74 chown(default_path[i], 5000, 5000);
83 _ms_update_mmc_info(const char *cid)
88 MS_DBG_ERR("Parameters are invalid");
89 return MS_MEDIA_ERR_INVALID_PARAMETER;
92 res = ms_config_set_str(MS_MMC_INFO_KEY, cid);
94 MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
95 return MS_MEDIA_ERR_VCONF_SET_FAIL;
98 return MS_MEDIA_ERR_NONE;
102 _ms_check_mmc_info(const char *cid)
104 char pre_mmc_info[MMC_INFO_SIZE] = { 0 };
108 MS_DBG_ERR("Parameters are invalid");
112 res = ms_config_get_str(MS_MMC_INFO_KEY, pre_mmc_info);
114 MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
118 MS_DBG("Last MMC info = %s", pre_mmc_info);
119 MS_DBG("Current MMC info = %s", cid);
121 if (strcmp(pre_mmc_info, cid) == 0) {
129 _get_contents(const char *filename, char *buf)
133 fp = fopen(filename, "rt");
135 MS_DBG_ERR("fp is NULL. file name : %s", filename);
136 return MS_MEDIA_ERR_FILE_OPEN_FAIL;
138 if (fgets(buf, 255, fp) == NULL)
139 MS_DBG_ERR("fgets failed");
143 return MS_MEDIA_ERR_NONE;
148 _ms_get_mmc_info(char *cid)
154 bool getdata = false;
155 bool bHasColon = false;
156 char path[MS_FILE_PATH_LEN_MAX] = { 0 };
157 char mmcpath[MS_FILE_PATH_LEN_MAX] = { 0 };
161 struct dirent *res = NULL;
163 /* mmcblk0 and mmcblk1 is reserved for movinand */
164 for (j = 1; j < 3; j++) {
165 len = snprintf(mmcpath, MS_FILE_PATH_LEN_MAX, "/sys/class/mmc_host/mmc%d/", j);
167 MS_DBG_ERR("FAIL : snprintf");
168 return MS_MEDIA_ERR_INTERNAL;
174 dp = opendir(mmcpath);
176 MS_DBG_ERR("dp is NULL");
177 return MS_MEDIA_ERR_DIR_OPEN_FAIL;
180 while (!readdir_r(dp, &ent, &res)) {
186 if (ent.d_name[0] == '.')
189 if (ent.d_type == DT_DIR) {
190 /*ent->d_name is including ':' */
191 for (i = 0; i < strlen(ent.d_name); i++) {
192 if (ent.d_name[i] == ':') {
200 err = ms_strappend(path, sizeof(path), "%s%s/cid", mmcpath, ent.d_name);
202 MS_DBG_ERR("ms_strappend error : %d", err);
206 if (_get_contents(path, cid) != MS_MEDIA_ERR_NONE)
215 if (getdata == true) {
220 return MS_MEDIA_ERR_NONE;
224 ms_get_mmc_state(void)
226 char cid[MMC_INFO_SIZE] = { 0 };
227 ms_dir_scan_type_t ret = MS_SCAN_ALL;
230 _ms_get_mmc_info(cid);
232 /*check it's same mmc */
233 if (_ms_check_mmc_info(cid)) {
241 ms_update_mmc_info(void)
244 char cid[MMC_INFO_SIZE] = { 0 };
246 err = _ms_get_mmc_info(cid);
248 err = _ms_update_mmc_info(cid);
252 MS_DBG_ERR("malloc_trim is failed");
257 #define _GETSYSTEMSTR(ID) dgettext("sys_string", (ID))
259 void update_lang(void)
264 lang = vconf_get_str(VCONFKEY_LANGSET);
266 setenv("LANG", lang, 1);
267 setenv("LC_MESSAGES", lang, 1);
268 r = setlocale(LC_ALL, "");
270 r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
271 MS_DBG_ERR("*****appcore setlocale=%s\n", r);
278 ms_present_mmc_insert(void)
284 ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_PREPARING_SD_CARD"));
285 if(ret != NOTIFICATION_ERROR_NONE)
286 return MS_MEDIA_ERR_INTERNAL;
288 return MS_MEDIA_ERR_NONE;
292 ms_mmc_vconf_cb(void *data)
296 if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
297 MS_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
300 MS_DBG("VCONFKEY_SYSMAN_MMC_STATUS :%d", status);
304 /* If scanner is not working, media server executes media scanner and sends request. */
305 /* If scanner is working, it detects changing status of SD card. */
306 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED ||
307 mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
309 /*remove added watch descriptors */
311 ms_inoti_remove_watch_recursive(MEDIA_ROOT_PATH_SDCARD);
312 ms_inoti_delete_mmc_ignore_file();
314 if (!ms_get_scanner_status()) {
315 if (!ms_drm_extract_ext_memory())
316 MS_DBG_ERR("ms_drm_extract_ext_memory failed");
318 ms_send_storage_scan_request(MS_STORAGE_EXTERNAL, MS_SCAN_INVALID);
320 } else if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
322 ms_make_default_path_mmc();
324 ms_inoti_add_watch_all_directory(MS_STORAGE_EXTERNAL);
326 ms_present_mmc_insert();
328 if (!ms_get_scanner_status()) {
329 if (!ms_drm_insert_ext_memory())
330 MS_DBG_ERR("ms_drm_insert_ext_memory failed");
332 ms_send_storage_scan_request(MS_STORAGE_EXTERNAL, ms_get_mmc_state());