2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <sys/types.h>
23 #include <tapi_common.h>
24 #include <ITapiModem.h>
25 #include <system_info.h>
29 #include "mtp_support.h"
31 #include <storage/storage.h>
33 #include <systemd/sd-login.h>
34 #include <sys/types.h>
36 #include <media_content_internal.h>
40 /* time to wait for user session creation, in ms */
41 #define WAIT_FOR_USER_TIMEOUT 10000
43 static phone_state_t g_ph_status = { 0 };
47 void _util_print_error()
49 /*In glibc-2.7, the longest error message string is 50 characters
50 ("Invalid or incomplete multibyte or wide character"). */
51 mtp_char buff[100] = {0};
53 strerror_r(errno, buff, sizeof(buff));
54 ERR("Error: [%d]:[%s]\n", errno, buff);
58 mtp_int32 _util_get_battery_level(void)
61 mtp_int32 battery_level = 100;
63 result = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
66 ERR("VCONFKEY_SYSMAN_BATTERY_CAPACITY Fail!");
71 mtp_bool _util_get_serial(mtp_char *serial, mtp_uint32 len)
73 TapiHandle *handle = NULL;
74 mtp_char *imei_no = NULL;
75 mtp_char *serial_no = NULL;
77 char hash_value[MD5_HASH_LEN] = { 0 };
79 if (serial == NULL || len <= MD5_HASH_LEN * 2) {
80 ERR("serial is null or length is less than (MD5_HASH_LEN * 2)");
84 serial_no = vconf_get_str(VCONFKEY_MTP_SERIAL_NUMBER_STR);
85 if (serial_no == NULL) {
86 ERR("vconf_get Fail for %s\n",
87 VCONFKEY_MTP_SERIAL_NUMBER_STR);
91 if (strlen(serial_no) > 0) {
92 g_strlcpy(serial, serial_no, len);
99 handle = tel_init(NULL);
101 ERR("tel_init Fail");
105 imei_no = tel_get_misc_me_imei_sync(handle);
107 ERR("tel_get_misc_me_imei_sync Fail");
114 gcry_md_hash_buffer(GCRY_MD_MD5, hash_value, imei_no, strlen(imei_no));
116 for (i = 0; i < MD5_HASH_LEN; i++)
117 g_snprintf(&serial[i*2], 3, "%02X", hash_value[i]);
119 if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1) {
120 ERR("vconf_set Fail for %s\n",
121 VCONFKEY_MTP_SERIAL_NUMBER_STR);
131 void _util_get_vendor_ext_desc(mtp_char *vendor_ext_desc, mtp_uint32 len)
133 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
134 mtp_char *version = NULL;
137 ret_if(vendor_ext_desc == NULL);
139 ret = system_info_get_platform_string(
140 "http://tizen.org/feature/platform.version", &version);
142 if (ret != SYSTEM_INFO_ERROR_NONE) {
143 ERR("system_info_get_value_string Fail : 0x%X\n", ret); // LCOV_EXCL_LINE
144 g_strlcpy(vendor_ext_desc, MTP_VENDOR_EXTENSIONDESC_CHAR, len); // LCOV_EXCL_LINE
145 return; // LCOV_EXCL_LINE
147 g_snprintf(vendor_ext_desc, len, "%stizen.org:%s; ",
148 MTP_VENDOR_EXTENSIONDESC_CHAR, version);
153 void _util_get_model_name(mtp_char *model_name, mtp_uint32 len)
155 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
156 mtp_char *model = NULL;
159 ret_if(model_name == NULL);
161 ret = system_info_get_platform_string(
162 "http://tizen.org/system/model_name", &model);
164 if (ret != SYSTEM_INFO_ERROR_NONE) {
165 ERR("system_info_get_value_string Fail : 0x%X\n", ret); // LCOV_EXCL_LINE
166 g_strlcpy(model_name, MTP_DEFAULT_MODEL_NAME, len); // LCOV_EXCL_LINE
167 return; // LCOV_EXCL_LINE
169 g_strlcpy(model_name, model, len);
174 void _util_get_device_version(mtp_char *device_version, mtp_uint32 len)
176 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
177 mtp_char *version = NULL;
178 mtp_char *build_info = NULL;
181 ret_if(device_version == NULL);
183 ret = system_info_get_platform_string(
184 "http://tizen.org/feature/platform.version", &version);
186 if (ret != SYSTEM_INFO_ERROR_NONE) {
187 ERR("system_info_get_value_string Fail : 0x%X\n", ret); // LCOV_EXCL_LINE
188 g_strlcpy(device_version, MTP_DEFAULT_DEVICE_VERSION, len); // LCOV_EXCL_LINE
189 return; // LCOV_EXCL_LINE
192 ret = system_info_get_platform_string(
193 "http://tizen.org/system/build.string", &build_info);
195 if (ret != SYSTEM_INFO_ERROR_NONE) {
196 ERR("system_info_get_value_string Fail : 0x%X\n", ret); // LCOV_EXCL_LINE
197 g_strlcpy(device_version, MTP_DEFAULT_DEVICE_VERSION, len); // LCOV_EXCL_LINE
198 g_free(version); // LCOV_EXCL_LINE
201 g_snprintf(device_version, len, "TIZEN %s (%s)", version, build_info);
207 /* LCOV_EXCL_START */
208 void _util_gen_alt_serial(mtp_char *serial, mtp_uint32 len)
211 mtp_char model_name[MTP_MODEL_NAME_LEN_MAX + 1] = { 0 };
214 ret_if(serial == NULL);
216 if (gettimeofday(&st, NULL) < 0) {
217 ERR("gettimeofday Fail");
221 _util_get_model_name(model_name, sizeof(model_name));
222 g_snprintf(serial, len, "%s-%010ld-%011ld", model_name,
223 st.tv_sec, st.tv_usec);
225 if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1)
226 ERR("vconf_set Fail %s\n", VCONFKEY_MTP_SERIAL_NUMBER_STR);
231 void _util_get_usb_status(phone_status_t *val)
236 ret = vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &state);
237 if (ret == -1 || state == VCONFKEY_SYSMAN_USB_DISCONNECTED) {
238 *val = MTP_PHONE_USB_DISCONNECTED;
242 *val = MTP_PHONE_USB_CONNECTED;
246 phone_status_t _util_get_local_usb_status(void)
248 return g_ph_status.usb_state;
251 void _util_set_local_usb_status(const phone_status_t val)
253 g_ph_status.usb_state = val;
257 void _util_get_mmc_status(phone_status_t *val)
262 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &state);
263 if (ret == -1 || state !=
264 VCONFKEY_SYSMAN_MMC_MOUNTED) {
265 *val = MTP_PHONE_MMC_NONE;
269 *val = MTP_PHONE_MMC_INSERTED;
274 phone_status_t _util_get_local_mmc_status(void)
276 return g_ph_status.mmc_state;
279 /* LCOV_EXCL_START */
280 void _util_set_local_mmc_status(const phone_status_t val)
282 g_ph_status.mmc_state = val;
286 void _util_get_usbmode_status(phone_status_t *val)
291 ret = vconf_get_int(VCONFKEY_USB_CUR_MODE,
294 *val = MTP_PHONE_USB_MODE_OTHER;
298 if (state == SET_USB_NONE)
299 *val = MTP_PHONE_USB_DISCONNECTED;
301 *val = MTP_PHONE_USB_CONNECTED;
305 phone_status_t _util_get_local_usbmode_status(void)
307 return g_ph_status.usb_mode_state;
310 void _util_set_local_usbmode_status(const phone_status_t val)
312 g_ph_status.usb_mode_state = val;
316 void _util_get_lock_status(phone_status_t *val)
320 vconf_get_int(VCONFKEY_IDLE_LOCK_STATE_READ_ONLY,
324 *val = MTP_PHONE_LOCK_ON;
326 *val = MTP_PHONE_LOCK_OFF;
331 phone_status_t _util_get_local_lock_status(void)
333 return g_ph_status.lock_state;
336 /* LCOV_EXCL_START */
337 void _util_set_local_lock_status(const phone_status_t val)
339 g_ph_status.lock_state = val;
343 static bool _util_device_external_supported_cb(int storage_id, storage_type_e type,
344 storage_state_e state, const char *path, void *user_data)
346 char *storage_path = (char *)user_data;
348 //DBG("storage id: %d, path: %s", storage_id, path);
350 if (type == STORAGE_TYPE_EXTERNAL && path != NULL) {
351 strncpy(storage_path, path, strlen(path)+1);
352 storage_path[strlen(path)] = 0;
353 //DBG("external storage path : %s", storage_path);
360 void _util_get_external_path(char *external_path)
362 int error = STORAGE_ERROR_NONE;
364 error = storage_foreach_device_supported(_util_device_external_supported_cb, external_path);
366 if (error != STORAGE_ERROR_NONE) {
367 /* LCOV_EXCL_START */
368 ERR("get external storage path Fail");
369 if (external_path != NULL) {
370 strncpy(external_path, MTP_EXTERNAL_PATH_CHAR, sizeof(MTP_EXTERNAL_PATH_CHAR));
371 external_path[sizeof(MTP_EXTERNAL_PATH_CHAR) - 1] = 0;
376 int _util_wait_for_user()
378 __attribute__((cleanup(sd_login_monitor_unrefp))) sd_login_monitor *monitor = NULL;
382 ret = sd_login_monitor_new("uid", &monitor);
384 char buf[256] = {0,};
385 strerror_r(-ret, buf, sizeof(buf));
386 ERR("Failed to allocate login monitor object: [%d]:[%s]", ret, buf);
390 fds.fd = sd_login_monitor_get_fd(monitor);
391 fds.events = sd_login_monitor_get_events(monitor);
393 ret = poll(&fds, 1, WAIT_FOR_USER_TIMEOUT);
395 ERR("Error polling: %m");
403 uid_t _util_get_active_user()
405 uid_t *active_user_list = NULL;
406 uid_t active_user = 0;
410 user_cnt = sd_get_active_uids(&active_user_list);
412 /* LCOV_EXCL_START */
413 ret = _util_wait_for_user();
417 user_cnt = sd_get_active_uids(&active_user_list);
422 /* LCOV_EXCL_START */
423 ERR("Active user not exists : %d", user_cnt);
425 if (active_user_list != NULL)
426 free(active_user_list);
432 if (active_user_list == NULL) {
433 ERR("active_user_list is NULL");
437 active_user = active_user_list[0];
439 DBG("Active UID : %d", active_user);
441 free(active_user_list);
443 if (active_user <= 0) {
444 ERR("UID is not proper value : %d", active_user);
451 void _util_get_internal_path(char *internal_path)
454 uid_t active_user = 0;
455 char *active_name = NULL;
457 active_user = _util_get_active_user();
458 pwd = getpwuid(active_user);
459 active_name = pwd->pw_name;
461 if (active_name == NULL) {
462 /* LCOV_EXCL_START */
463 ERR("active_name is NULL");
464 strncpy(internal_path, MTP_USER_DIRECTORY, sizeof(MTP_USER_DIRECTORY));
465 internal_path[sizeof(MTP_USER_DIRECTORY) - 1] = 0;
470 if (internal_path != NULL) {
471 strncpy(internal_path, MTP_INTERNAL_PATH_CHAR, sizeof(MTP_INTERNAL_PATH_CHAR));
472 strncat(internal_path, active_name, strlen(active_name) + 1);
473 strncat(internal_path, "/media", 7);
474 internal_path[strlen(internal_path)] = 0;
477 ERR("internal path is %s", internal_path);
480 /* LCOV_EXCL_START */
481 mtp_bool _util_media_content_connect()
484 uid_t active_user = 0;
486 active_user = _util_get_active_user();
488 ret = media_content_connect_with_uid(active_user);
489 if (ret != MEDIA_CONTENT_ERROR_NONE) {
490 ERR("media_content_connect() failed : %d", ret);
497 void _util_media_content_disconnect()
499 media_content_disconnect();