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>
34 static phone_state_t g_ph_status = { 0 };
37 void _util_print_error()
39 /*In glibc-2.7, the longest error message string is 50 characters
40 ("Invalid or incomplete multibyte or wide character"). */
41 mtp_char buff[100] = {0};
43 strerror_r(errno, buff, sizeof(buff));
44 ERR("Error: [%d]:[%s]\n", errno, buff);
47 mtp_int32 _util_get_battery_level(void)
50 mtp_int32 battery_level = 100;
52 result = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
55 ERR("VCONFKEY_SYSMAN_BATTERY_CAPACITY Fail!");
60 mtp_bool _util_get_serial(mtp_char *serial, mtp_uint32 len)
62 TapiHandle *handle = NULL;
63 mtp_char *imei_no = NULL;
64 mtp_char *serial_no = NULL;
66 char hash_value[MD5_HASH_LEN] = { 0 };
68 if (serial == NULL || len <= MD5_HASH_LEN * 2) {
69 ERR("serial is null or length is less than (MD5_HASH_LEN * 2)");
73 serial_no = vconf_get_str(VCONFKEY_MTP_SERIAL_NUMBER_STR);
74 if (serial_no == NULL) {
75 ERR("vconf_get Fail for %s\n",
76 VCONFKEY_MTP_SERIAL_NUMBER_STR);
80 if (strlen(serial_no) > 0) {
81 g_strlcpy(serial, serial_no, len);
87 handle = tel_init(NULL);
93 imei_no = tel_get_misc_me_imei_sync(handle);
95 ERR("tel_get_misc_me_imei_sync Fail");
102 gcry_md_hash_buffer(GCRY_MD_MD5, hash_value, imei_no, strlen(imei_no));
104 for (i = 0; i < MD5_HASH_LEN; i++)
105 g_snprintf(&serial[i*2], 3, "%02X", hash_value[i]);
107 if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1) {
108 ERR("vconf_set Fail for %s\n",
109 VCONFKEY_MTP_SERIAL_NUMBER_STR);
118 void _util_get_vendor_ext_desc(mtp_char *vendor_ext_desc, mtp_uint32 len)
120 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
121 mtp_char *version = NULL;
124 ret_if(vendor_ext_desc == NULL);
126 ret = system_info_get_platform_string(
127 "http://tizen.org/feature/platform.version", &version);
129 if (ret != SYSTEM_INFO_ERROR_NONE) {
130 ERR("system_info_get_value_string Fail : 0x%X\n", ret);
131 g_strlcpy(vendor_ext_desc, MTP_VENDOR_EXTENSIONDESC_CHAR, len);
134 g_snprintf(vendor_ext_desc, len, "%stizen.org:%s; ",
135 MTP_VENDOR_EXTENSIONDESC_CHAR, version);
140 void _util_get_model_name(mtp_char *model_name, mtp_uint32 len)
142 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
143 mtp_char *model = NULL;
146 ret_if(model_name == NULL);
148 ret = system_info_get_platform_string(
149 "http://tizen.org/system/model_name", &model);
151 if (ret != SYSTEM_INFO_ERROR_NONE) {
152 ERR("system_info_get_value_string Fail : 0x%X\n", ret);
153 g_strlcpy(model_name, MTP_DEFAULT_MODEL_NAME, len);
156 g_strlcpy(model_name, model, len);
161 void _util_get_device_version(mtp_char *device_version, mtp_uint32 len)
163 mtp_int32 ret = SYSTEM_INFO_ERROR_NONE;
164 mtp_char *version = NULL;
165 mtp_char *build_info = NULL;
168 ret_if(device_version == NULL);
170 ret = system_info_get_platform_string(
171 "http://tizen.org/feature/platform.version", &version);
173 if (ret != SYSTEM_INFO_ERROR_NONE) {
174 ERR("system_info_get_value_string Fail : 0x%X\n", ret);
175 g_strlcpy(device_version, MTP_DEFAULT_DEVICE_VERSION, len);
179 ret = system_info_get_platform_string(
180 "http://tizen.org/system/build.string", &build_info);
182 if (ret != SYSTEM_INFO_ERROR_NONE) {
183 ERR("system_info_get_value_string Fail : 0x%X\n", ret);
184 g_strlcpy(device_version, MTP_DEFAULT_DEVICE_VERSION, len);
188 g_snprintf(device_version, len, "TIZEN %s (%s)", version, build_info);
194 void _util_gen_alt_serial(mtp_char *serial, mtp_uint32 len)
197 mtp_char model_name[MTP_MODEL_NAME_LEN_MAX + 1] = { 0 };
200 ret_if(serial == NULL);
202 if (gettimeofday(&st, NULL) < 0) {
203 ERR("gettimeofday Fail");
207 _util_get_model_name(model_name, sizeof(model_name));
208 g_snprintf(serial, len, "%s-%010ld-%011ld", model_name,
209 st.tv_sec, st.tv_usec);
211 if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1)
212 ERR("vconf_set Fail %s\n", VCONFKEY_MTP_SERIAL_NUMBER_STR);
217 void _util_get_usb_status(phone_status_t *val)
222 ret = vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &state);
223 if (ret == -1 || state == VCONFKEY_SYSMAN_USB_DISCONNECTED) {
224 *val = MTP_PHONE_USB_DISCONNECTED;
228 *val = MTP_PHONE_USB_CONNECTED;
232 phone_status_t _util_get_local_usb_status(void)
234 return g_ph_status.usb_state;
237 void _util_set_local_usb_status(const phone_status_t val)
239 g_ph_status.usb_state = val;
243 void _util_get_mmc_status(phone_status_t *val)
248 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &state);
249 if (ret == -1 || state !=
250 VCONFKEY_SYSMAN_MMC_MOUNTED) {
251 *val = MTP_PHONE_MMC_NONE;
255 *val = MTP_PHONE_MMC_INSERTED;
259 phone_status_t _util_get_local_mmc_status(void)
261 return g_ph_status.mmc_state;
264 void _util_set_local_mmc_status(const phone_status_t val)
266 g_ph_status.mmc_state = val;
270 void _util_get_usbmode_status(phone_status_t *val)
275 ret = vconf_get_int(VCONFKEY_USB_CUR_MODE,
278 *val = MTP_PHONE_USB_MODE_OTHER;
282 if (state == SET_USB_NONE)
283 *val = MTP_PHONE_USB_DISCONNECTED;
285 *val = MTP_PHONE_USB_CONNECTED;
289 phone_status_t _util_get_local_usbmode_status(void)
291 return g_ph_status.usb_mode_state;
294 void _util_set_local_usbmode_status(const phone_status_t val)
296 g_ph_status.usb_mode_state = val;
300 void _util_get_lock_status(phone_status_t *val)
308 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE_READ_ONLY,
311 ret = stat("/opt/usr/home", &st);
314 *val = MTP_PHONE_LOCK_ON;
316 *val = MTP_PHONE_LOCK_OFF;
320 phone_status_t _util_get_local_lock_status(void)
322 return g_ph_status.lock_state;
325 void _util_set_local_lock_status(const phone_status_t val)
327 g_ph_status.lock_state = val;
331 static bool _util_device_external_supported_cb(int storage_id, storage_type_e type,
332 storage_state_e state, const char *path, void *user_data)
334 char *storage_path = (char *)user_data;
336 //DBG("storage id: %d, path: %s", storage_id, path);
338 if (type == STORAGE_TYPE_EXTERNAL && path != NULL) {
339 strncpy(storage_path, path, strlen(path));
340 //DBG("external storage path : %s", storage_path);
346 void _util_get_external_path(char *external_path)
348 int error = STORAGE_ERROR_NONE;
349 error = storage_foreach_device_supported(_util_device_external_supported_cb, external_path);
351 if (error != STORAGE_ERROR_NONE) {
352 ERR("get external storage path Fail");
353 strncpy(external_path, MTP_EXTERNAL_PATH_CHAR, strlen(MTP_EXTERNAL_PATH_CHAR));
357 static bool _util_device_internal_supported_cb(int storage_id, storage_type_e type,
358 storage_state_e state, const char *path, void *user_data)
360 char *storage_path = (char *)user_data;
362 //DBG("storage id: %d, path: %s", storage_id, path);
364 if (type == STORAGE_TYPE_INTERNAL && path != NULL) {
365 strncpy(storage_path, path, strlen(path));
366 //DBG("internal storage path : %s", storage_path);
368 if (storage_get_root_directory(storage_id, &storage_path) != STORAGE_ERROR_NONE) {
369 ERR("get internal storage path Fail");
372 //DBG("get internal storage path : %s", storage_path);
379 void _util_get_internal_path(char *internal_path)
381 int error = STORAGE_ERROR_NONE;
382 error = storage_foreach_device_supported(_util_device_internal_supported_cb, internal_path);
384 if (error != STORAGE_ERROR_NONE) {
385 ERR("get internal storage path Fail");
386 strncpy(internal_path, MTP_STORE_PATH_CHAR, strlen(MTP_STORE_PATH_CHAR));