From d18f62ff3655deabccc1361475443911f40cdc48 Mon Sep 17 00:00:00 2001 From: Hyihong Chae Date: Fri, 13 Jan 2017 10:50:22 +0900 Subject: [PATCH 01/16] Sync code with Tizen 3.0 branch Change-Id: I623bf81942d5b0d9d0e154741430d2a418918280 Signed-off-by: HyiHong Chae --- include/entity/mtp_property.h | 2 +- include/mtp_cmd_handler.h | 2 +- include/mtp_config.h | 4 +- include/mtp_datatype.h | 2 - include/util/mtp_fs.h | 14 +-- include/util/mtp_thread.h | 10 +- packaging/mtp-responder.spec | 14 +-- src/entity/mtp_device.c | 2 +- src/entity/mtp_object.c | 6 +- src/entity/mtp_property.c | 211 +++++++++++++++------------------------ src/entity/mtp_store.c | 71 ++++++------- src/mtp_cmd_handler.c | 221 ++++++++++++++++++++--------------------- src/mtp_cmd_handler_util.c | 95 ++++++++---------- src/mtp_event_handler.c | 13 ++- src/mtp_init.c | 14 ++- src/mtp_inoti_handler.c | 40 +++----- src/ptp_container.c | 11 +- src/transport/mtp_transport.c | 14 ++- src/transport/mtp_usb_driver.c | 21 ++-- src/util/mtp_fs.c | 64 ++++++------ src/util/mtp_list.c | 6 +- src/util/mtp_media_info.c | 10 +- src/util/mtp_msgq.c | 5 +- src/util/mtp_support.c | 39 +++----- src/util/mtp_thread.c | 5 +- src/util/mtp_util.c | 20 ++-- 26 files changed, 394 insertions(+), 522 deletions(-) diff --git a/include/entity/mtp_property.h b/include/entity/mtp_property.h index 4bb6591..8ad9818 100755 --- a/include/entity/mtp_property.h +++ b/include/entity/mtp_property.h @@ -84,7 +84,7 @@ enum { _prop_destroy_obj_propval(prop_val);\ ERR("_util_add_node() Fail");\ return (FALSE);\ - }\ + } \ } while (0); #define propvalue_alloc_and_check(prop_val)\ diff --git a/include/mtp_cmd_handler.h b/include/mtp_cmd_handler.h index 1fc6992..90559cf 100755 --- a/include/mtp_cmd_handler.h +++ b/include/mtp_cmd_handler.h @@ -77,7 +77,7 @@ typedef struct { mtp_uint32 cmd_size; mtp_uint32 data_size; mtp_uint32 data_count; - mtp_uint32 fhandle; /* for temporary mtp file */ + FILE* fhandle; /* for temporary mtp file */ mtp_char *filepath; mtp_uint32 file_size; mtp_uint32 size_remaining; diff --git a/include/mtp_config.h b/include/mtp_config.h index b4249e1..d7052a9 100755 --- a/include/mtp_config.h +++ b/include/mtp_config.h @@ -28,7 +28,7 @@ /*MtpObject.c, for unknown metadata */ #define MTP_USE_FILL_EMPTYMETADATA_WITH_UNKNOWN -/*define MTP_USE_VARIABLE_PTP_STRING_MALLOC*/ /*allocPtpString in ptpstring.c*/ +/*define MTP_USE_VARIABLE_PTP_STRING_MALLOC*/ /*allocPtpString in ptpstring.c*/ #define MTP_USE_RUNTIME_GETOBJECTPROPVALUE /*use runtime get object property list*/ /*keywords has many space. not support*/ @@ -143,7 +143,7 @@ #define MTP_MAX_PACKET_SIZE_SEND_HS 512 /* High speed */ #define MTP_MAX_PACKET_SIZE_SEND_FS 64 /* Full speed */ -#define MTP_FILESIZE_4GB 4294967296L +#define MTP_FILESIZE_4GB 4294967296L /* approximately 3 times of media files. consider album*/ #define MTP_MAX_REFDB_ROWCNT 4500 diff --git a/include/mtp_datatype.h b/include/mtp_datatype.h index 07f51f4..4fa9782 100755 --- a/include/mtp_datatype.h +++ b/include/mtp_datatype.h @@ -25,8 +25,6 @@ #endif #ifdef _MTP_USE_OWNTYPES -#define INVALID_FILE (0) - typedef unsigned char mtp_byte; typedef unsigned char mtp_bool; typedef unsigned char mtp_uchar; diff --git a/include/util/mtp_fs.h b/include/util/mtp_fs.h index 3a88e55..6ce634d 100755 --- a/include/util/mtp_fs.h +++ b/include/util/mtp_fs.h @@ -67,13 +67,13 @@ typedef struct { mtp_uint64 reserved_size; } fs_info_t; -mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, +FILE* _util_file_open(const mtp_char *filename, file_mode_t mode, mtp_int32 *error); -void _util_file_read(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size, +void _util_file_read(FILE* fhandle, void *bufptr, mtp_uint32 size, mtp_uint32 *read_count); -mtp_uint32 _util_file_write(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size); -mtp_int32 _util_file_close(mtp_uint32 fhandle); -mtp_bool _util_file_seek(mtp_uint32 fhandle, off_t offset, mtp_int32 whence); +mtp_uint32 _util_file_write(FILE* fhandle, void *bufptr, mtp_uint32 size); +mtp_int32 _util_file_close(FILE* fhandle); +mtp_bool _util_file_seek(FILE* fhandle, off_t offset, mtp_int32 whence); mtp_bool _util_file_copy(const mtp_char *origpath, const mtp_char *newpath, mtp_int32 *error); mtp_bool _util_copy_dir_children_recursive(const mtp_char *origpath, @@ -89,8 +89,8 @@ mtp_bool _util_ifind_next(char *dir_name, DIR *dirp, dir_entry_t *dir_info); mtp_bool _util_ifind_first(char *dir_name, DIR **dirp, dir_entry_t *dir_info); mtp_bool _util_is_file_opened(const mtp_char *fullpath); mtp_bool _util_get_filesystem_info(mtp_char *storepath, fs_info_t *fs_info); -void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines); -void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, mtp_uint32 fhandle, +void _util_count_num_lines(FILE* fhandle, mtp_uint32 *num_lines); +void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, FILE* fhandle, mtp_uint32 size); void FLOGD(const char *fmt, ...); diff --git a/include/util/mtp_thread.h b/include/util/mtp_thread.h index 41c4cfe..f742158 100755 --- a/include/util/mtp_thread.h +++ b/include/util/mtp_thread.h @@ -28,15 +28,15 @@ typedef void *(*thread_func_t) (void *pArg); int lock_ret = 0;\ DBG("Thread [%d] trying to lock the Mutex \n", syscall(__NR_gettid));\ lock_ret = pthread_mutex_lock(mut);\ - if(lock_ret != 0) {\ - if(lock_ret == EDEADLK) {\ + if (lock_ret != 0) {\ + if (lock_ret == EDEADLK) {\ DBG("Mutex is already locked by the same thread");\ } else {\ ERR("Error locking mutex. Error = %d \n", lock_ret);\ - }\ + } \ } else {\ DBG("Mutex locked by thread [%d] \n", syscall(__NR_gettid));\ - }\ + } \ } while (0);\ @@ -48,7 +48,7 @@ typedef void *(*thread_func_t) (void *pArg); ERR("Error unlocking mutex. Error = %d \n", unlock_ret);\ } else {\ DBG("Mutex unlocked by thread [%d] \n", syscall(__NR_gettid));\ - }\ + } \ } while (0);\ mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname, diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index 93ef8c5..d292281 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -5,7 +5,7 @@ ExcludeArch: %arm aarch64 Name: mtp-responder Summary: Media Transfer Protocol daemon (responder) -Version: 0.0.18 +Version: 0.0.19 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 @@ -43,24 +43,24 @@ make %{?jobs:-j%jobs} %install %make_install -mkdir -p %{buildroot}%{_libdir}/udev/rules.d -cp packaging/99-mtp-responder.rules %{buildroot}%{_libdir}/udev/rules.d/99-mtp-responder.rules +mkdir -p %{buildroot}/%{_prefix}/lib/udev/rules.d +cp packaging/99-mtp-responder.rules %{buildroot}/%{_prefix}/lib/udev/rules.d/99-mtp-responder.rules mkdir -p %{buildroot}%{upgrade_script_path} cp -f scripts/500.%{name}-upgrade.sh %{buildroot}%{upgrade_script_path} -install -D -m 0644 mtp-responder.service %{buildroot}%{_libdir}/systemd/system/mtp-responder.service +install -D -m 0644 mtp-responder.service %{buildroot}/%{_unitdir}/mtp-responder.service %post mkdir -p %{_sysconfdir}/systemd/default-extra-dependencies/ignore-units.d/ -ln -sf %{_libdir}/systemd/system/mtp-responder.service %{_sysconfdir}/systemd/default-extra-dependencies/ignore-units.d/ +ln -sf %{_unitdir}/mtp-responder.service %{_sysconfdir}/systemd/default-extra-dependencies/ignore-units.d/ %files %manifest mtp-responder.manifest %defattr(-,root,root,-) %{_bindir}/mtp-responder -%{_libdir}/systemd/system/mtp-responder.service -%{_libdir}/udev/rules.d/99-mtp-responder.rules +%{_unitdir}/mtp-responder.service +%{_prefix}/lib/udev/rules.d/99-mtp-responder.rules /opt/var/lib/misc/mtp-responder.conf %{upgrade_script_path}/500.%{name}-upgrade.sh #%license LICENSE.APLv2 diff --git a/src/entity/mtp_device.c b/src/entity/mtp_device.c index 8bf6485..da88199 100755 --- a/src/entity/mtp_device.c +++ b/src/entity/mtp_device.c @@ -742,7 +742,7 @@ mtp_bool _device_install_storage(mtp_int32 type) break; mounted = _device_is_store_mounted(MTP_STORAGE_EXTERNAL); if (mounted == FALSE) { - if (__add_store_to_device(MTP_STORAGE_EXTERNAL)== FALSE) { + if (__add_store_to_device(MTP_STORAGE_EXTERNAL) == FALSE) { ERR("__add_store_to_device() Fail"); return FALSE; } diff --git a/src/entity/mtp_object.c b/src/entity/mtp_object.c index 1cc13bf..358af3c 100755 --- a/src/entity/mtp_object.c +++ b/src/entity/mtp_object.c @@ -501,9 +501,8 @@ mtp_bool _entity_check_child_obj_path(mtp_obj_t *obj, mtp_uint32 *ptr32 = child_arr.array_entry; child_obj = _entity_get_object_from_store(src_store, ptr32[idx]); - if (NULL == child_obj) { + if (NULL == child_obj) continue; - } if (_util_is_file_opened(child_obj->file_path) == TRUE) { ERR_SECURE("File [%s] is already opened\n", @@ -524,9 +523,8 @@ mtp_bool _entity_check_child_obj_path(mtp_obj_t *obj, } ptr = strstr(child_obj->file_path, src_path); - if (NULL == ptr) { + if (NULL == ptr) continue; - } _util_utf8_to_utf16(dest_chld_wpath, sizeof(dest_chld_wpath) / WCHAR_SIZ, src_path); diff --git a/src/entity/mtp_property.c b/src/entity/mtp_property.c index 37151d2..9f33c6c 100755 --- a/src/entity/mtp_property.c +++ b/src/entity/mtp_property.c @@ -148,8 +148,8 @@ static mtp_bool __create_prop_string(mtp_obj_t *obj, mtp_uint16 propcode, static mtp_bool __create_prop_timestring(mtp_obj_t *obj, mtp_uint32 propcode, ptp_time_string_t *value) { - obj_prop_desc_t *prop = NULL; - obj_prop_val_t *prop_val= NULL; + obj_prop_desc_t *prop = NULL; + obj_prop_val_t *prop_val = NULL; mtp_uint32 fmt_code = obj->obj_info->obj_fmt; retv_if(obj == NULL, FALSE); @@ -195,7 +195,7 @@ static mtp_bool __update_prop_values_audio(mtp_obj_t *obj) { mtp_bool success = TRUE; mtp_int32 converted_rating = 0; - comp_audio_meta_t audio_data = {{0}, {0}}; + comp_audio_meta_t audio_data = {{0}, {0} }; retv_if(obj == NULL, FALSE); retv_if(obj->obj_info == NULL, FALSE); @@ -727,9 +727,8 @@ ptp_array_t *_prop_alloc_ptparray(data_type_t type) { ptp_array_t *parray; parray = (ptp_array_t *)g_malloc(sizeof(ptp_array_t)); - if (parray != NULL) { + if (parray != NULL) _prop_init_ptparray(parray, type); - } return (parray); } @@ -799,18 +798,16 @@ mtp_int32 _prop_find_ele_ptparray(ptp_array_t *parray, mtp_uint32 element) case UINT8_TYPE: ptr8 = parray->array_entry; for (ii = 0; ii < parray->num_ele; ii++) { - if (ptr8[ii] == (mtp_uchar) element) { + if (ptr8[ii] == (mtp_uchar) element) return ii; - } } break; case UINT16_TYPE: ptr16 = parray->array_entry; for (ii = 0; ii < parray->num_ele; ii++) { - if (ptr16[ii] == (mtp_uint16) element) { + if (ptr16[ii] == (mtp_uint16) element) return ii; - } } break; @@ -818,9 +815,8 @@ mtp_int32 _prop_find_ele_ptparray(ptp_array_t *parray, mtp_uint32 element) case UINT32_TYPE: ptr32 = parray->array_entry; for (ii = 0; ii < parray->num_ele; ii++) { - if (ptr32[ii] == (mtp_uint32)element) { + if (ptr32[ii] == (mtp_uint32)element) return ii; - } } break; @@ -1087,9 +1083,9 @@ void _prop_deinit_ptparray(ptp_array_t *parray) { parray->num_ele = 0; parray->arr_size = 0; - if (parray->array_entry) { + if (parray->array_entry) g_free(parray->array_entry); - } + parray->array_entry = NULL; return; } @@ -1099,9 +1095,9 @@ void _prop_destroy_ptparray(ptp_array_t *parray) if (parray == NULL) return; - if (parray->array_entry != NULL) { + if (parray->array_entry != NULL) g_free(parray->array_entry); - } + parray->arr_size = 0; parray->num_ele = 0; g_free(parray); @@ -1141,9 +1137,8 @@ static ptp_string_t *__alloc_ptpstring(void) ptp_string_t *pstring = NULL; pstring = (ptp_string_t *)g_malloc(sizeof(ptp_string_t)); - if (pstring != NULL) { + if (pstring != NULL) _prop_init_ptpstring(pstring); - } return (pstring); } @@ -1158,9 +1153,8 @@ static ptp_string_t *__alloc_ptpstring(mtp_uint32 size) alloc_size = ((size_tmp >> 5) + 1) << 5; /* multiple of 32 */ pstring = (ptp_string_t *)g_malloc(alloc_size); /* for margin */ - if (pstring != NULL) { + if (pstring != NULL) _prop_init_ptpstring(pstring); - } return (pstring); } @@ -1200,17 +1194,15 @@ void _prop_copy_char_to_ptpstring(ptp_string_t *pstring, void *str, pstring->num_chars = 0; return; } - for (i = 0; i < MAX_PTP_STRING_CHARS && pchar[i]; i++) { + for (i = 0; i < MAX_PTP_STRING_CHARS && pchar[i]; i++) pstring->str[i] = (mtp_wchar)pchar[i]; - } } else if (cmode == WCHAR_TYPE) { if (pwchar[0] == 0) { pstring->num_chars = 0; return; } - for (i = 0; i < MAX_PTP_STRING_CHARS && pwchar[i]; i++) { + for (i = 0; i < MAX_PTP_STRING_CHARS && pwchar[i]; i++) pstring->str[i] = pwchar[i]; - } } else { ERR("Unknown character mode : %d\n", cmode); pstring->num_chars = 0; @@ -1261,9 +1253,9 @@ void _prop_copy_ptpstring(ptp_string_t *dst, ptp_string_t *src) mtp_uint16 ii; dst->num_chars = src->num_chars; - for (ii = 0; ii < src->num_chars; ii++) { + for (ii = 0; ii < src->num_chars; ii++) dst->str[ii] = src->str[ii]; - } + return; } @@ -1272,9 +1264,9 @@ void _prop_copy_ptptimestring(ptp_time_string_t *dst, ptp_time_string_t *src) mtp_uint16 ii; dst->num_chars = src->num_chars; - for (ii = 0; ii < src->num_chars; ii++) { + for (ii = 0; ii < src->num_chars; ii++) dst->str[ii] = src->str[ii]; - } + return; } @@ -1338,9 +1330,8 @@ mtp_uint32 _prop_pack_ptpstring(ptp_string_t *pstring, mtp_uchar *buf, buf[0] = pstring->num_chars; bytes_written = _prop_size_ptpstring(pstring); - for (ii = 0; ii < (bytes_written - 1); ii++) { + for (ii = 0; ii < (bytes_written - 1); ii++) buf[ii + 1] = pchar[ii]; - } } return bytes_written; } @@ -1386,9 +1377,8 @@ mtp_uint32 _prop_parse_rawstring(ptp_string_t *pstring, mtp_uchar *buf, { mtp_uint16 ii; - if (buf == NULL) { + if (buf == NULL) return 0; - } if (buf[0] == 0) { pstring->num_chars = 0; @@ -1396,9 +1386,8 @@ mtp_uint32 _prop_parse_rawstring(ptp_string_t *pstring, mtp_uchar *buf, } else { pstring->num_chars = buf[0]; ii = (mtp_uint16) ((size - 1) / sizeof(mtp_wchar)); - if (pstring->num_chars > ii) { + if (pstring->num_chars > ii) pstring->num_chars = (mtp_uchar)ii; - } for (ii = 1; ii <= pstring->num_chars; ii++) { #ifdef __BIG_ENDIAN__ @@ -1416,9 +1405,9 @@ mtp_uint32 _prop_parse_rawstring(ptp_string_t *pstring, mtp_uchar *buf, void _prop_destroy_ptpstring(ptp_string_t *pstring) { - if (pstring != NULL) { + if (pstring != NULL) g_free(pstring); - } + return; } @@ -1442,9 +1431,8 @@ mtp_bool _prop_is_valid_integer(prop_info_t *prop_info, mtp_uint64 value) mtp_uint32 ii; for (ii = 0; ii < prop_info->supp_value_list.nnodes; ii++, node = node->link) { - if (value == (mtp_uint32) node->value) { + if (value == (mtp_uint32) node->value) return TRUE; - } } /* if it hits here, must be an invalid value */ @@ -1460,12 +1448,10 @@ mtp_bool _prop_is_valid_integer(prop_info_t *prop_info, mtp_uint64 value) mtp_bool _prop_is_valid_string(prop_info_t *prop_info, ptp_string_t *pstring) { - if ((prop_info->data_type != PTP_DATATYPE_STRING) || (pstring == NULL)) { + if ((prop_info->data_type != PTP_DATATYPE_STRING) || (pstring == NULL)) return FALSE; - } - if (prop_info->form_flag == ENUM_FORM) - { + if (prop_info->form_flag == ENUM_FORM) { slist_node_t *node = NULL; mtp_uint32 ii; ptp_string_t *ele_str = NULL; @@ -1515,8 +1501,7 @@ mtp_bool _prop_set_default_string(prop_info_t *prop_info, mtp_wchar *val) _prop_copy_char_to_ptpstring(prop_info->default_val.str, val, WCHAR_TYPE); return TRUE; - } - else { + } else { return FALSE; } } @@ -1553,8 +1538,7 @@ mtp_bool _prop_set_default_array(prop_info_t *prop_info, mtp_uchar *parray, return FALSE; /* Copies the data into the PTP array */ - if ((prop_info->default_val.array != NULL) && (num_ele != 0)) - { + if ((prop_info->default_val.array != NULL) && (num_ele != 0)) { mtp_uchar *ptr8 = NULL; mtp_uint16 *ptr16 = NULL; mtp_uint32 *ptr32 = NULL; @@ -1563,8 +1547,7 @@ mtp_bool _prop_set_default_array(prop_info_t *prop_info, mtp_uchar *parray, _prop_grow_ptparray(prop_info->default_val.array, num_ele); if ((prop_info->data_type == PTP_DATATYPE_AUINT8) || - (prop_info->data_type == PTP_DATATYPE_AINT8)) - { + (prop_info->data_type == PTP_DATATYPE_AINT8)) { ptr8 = (mtp_uchar *) parray; for (ii = 0; ii < num_ele; ii++) _prop_append_ele_ptparray(prop_info->default_val.array, @@ -1599,9 +1582,8 @@ mtp_bool _prop_set_current_integer(device_prop_desc_t *prop, mtp_uint32 val) ptr = (mtp_uchar *) &val; - for (ii = 0; ii < sizeof(mtp_uint32); ii++) { + for (ii = 0; ii < sizeof(mtp_uint32); ii++) prop->current_val.integer[ii] = ptr[ii]; - } return TRUE; } else { @@ -1612,8 +1594,7 @@ mtp_bool _prop_set_current_integer(device_prop_desc_t *prop, mtp_uint32 val) mtp_bool _prop_set_current_string(device_prop_desc_t *prop, ptp_string_t *str) { - if (_prop_is_valid_string(&(prop->propinfo), str)) - { + if (_prop_is_valid_string(&(prop->propinfo), str)) { _prop_destroy_ptpstring(prop->current_val.str); #ifndef MTP_USE_VARIABLE_PTP_STRING_MALLOC prop->current_val.str = __alloc_ptpstring(); @@ -1623,8 +1604,7 @@ mtp_bool _prop_set_current_string(device_prop_desc_t *prop, ptp_string_t *str) if (prop->current_val.str != NULL) { _prop_copy_ptpstring(prop->current_val.str, str); return TRUE; - } - else { + } else { _prop_destroy_ptpstring(prop->current_val.str); return FALSE; } @@ -1739,21 +1719,19 @@ mtp_bool _prop_set_current_device_prop(device_prop_desc_t *prop, mtp_uchar *val, PTP_DATATYPE_ARRAY) { mtp_uint32 *ptr = (mtp_uint32 *) val; - if (size < sizeof(mtp_uint32)) { + if (size < sizeof(mtp_uint32)) return FALSE; - } - if (size < sizeof(mtp_uint32) + ptr[0] * prop->propinfo.dts_size) { + + if (size < sizeof(mtp_uint32) + ptr[0] * prop->propinfo.dts_size) return FALSE; - } + return _prop_set_current_array(prop, val); - } - else if ((prop->propinfo.data_type & PTP_DATATYPE_VALUEMASK) == + } else if ((prop->propinfo.data_type & PTP_DATATYPE_VALUEMASK) == PTP_DATATYPE_VALUE) { - if (prop->propinfo.dts_size > size) { + if (prop->propinfo.dts_size > size) return FALSE; - } if ((prop->propinfo.data_type == PTP_DATATYPE_INT64) || (prop->propinfo.data_type == PTP_DATATYPE_UINT64) || @@ -1806,7 +1784,7 @@ mtp_bool _prop_set_current_string_val(obj_prop_val_t *pval, ptp_string_t *str) pval->current_val.str = __alloc_ptpstring(str->num_chars); #endif /* MTP_USE_VARIABLE_PTP_STRING_MALLOC */ if (pval->current_val.str != NULL) { - _prop_copy_ptpstring (pval->current_val.str, str); + _prop_copy_ptpstring(pval->current_val.str, str); return TRUE; } else return FALSE; @@ -2271,9 +2249,8 @@ mtp_uint32 _prop_pack_device_prop_desc(device_prop_desc_t *prop, slist_node_t *node = NULL; mtp_uint32 ii; - if (!buf || size < _prop_size_device_prop_desc(prop)) { + if (!buf || size < _prop_size_device_prop_desc(prop)) return 0; - } /* Pack propcode, data_type, & get_set */ bytes_to_write = sizeof(mtp_uint16); @@ -2441,9 +2418,8 @@ mtp_uint32 _prop_pack_curval_device_prop_desc(device_prop_desc_t *prop, bytes_to_write = __size_curval_device_prop(prop); - if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) { + if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) return 0; - } if (prop->propinfo.data_type == PTP_DATATYPE_STRING) { if (bytes_to_write != _prop_pack_ptpstring(prop->current_val.str, @@ -2489,7 +2465,7 @@ void _prop_reset_device_prop_desc(device_prop_desc_t *prop) if (NULL == prop->current_val.str) return; - _prop_copy_ptpstring (prop->current_val.str, + _prop_copy_ptpstring(prop->current_val.str, prop->propinfo.default_val.str); } else if ((prop->propinfo.data_type & PTP_DATATYPE_ARRAYMASK) == @@ -2519,9 +2495,8 @@ obj_prop_val_t * _prop_alloc_obj_propval(obj_prop_desc_t *prop) obj_prop_val_t *pval = NULL; pval = (obj_prop_val_t *)g_malloc(sizeof(obj_prop_val_t)); - if (pval != NULL) { + if (pval != NULL) __init_obj_propval(pval, prop); - } return pval; } @@ -2532,9 +2507,8 @@ static void __init_obj_propval(obj_prop_val_t *pval, obj_prop_desc_t *prop) pval->prop = prop; - for (ii = 0; ii < 16; ii++) { + for (ii = 0; ii < 16; ii++) pval->current_val.integer[ii] = 0; - } if (prop->propinfo.data_type == PTP_DATATYPE_STRING) { @@ -2545,7 +2519,7 @@ static void __init_obj_propval(obj_prop_val_t *pval, obj_prop_desc_t *prop) #endif /* MTP_USE_VARIABLE_PTP_STRING_MALLOC */ if (NULL == pval->current_val.str) return; - _prop_copy_ptpstring (pval->current_val.str, + _prop_copy_ptpstring(pval->current_val.str, prop->propinfo.default_val.str); } else if ((prop->propinfo.data_type & PTP_DATATYPE_VALUEMASK) == PTP_DATATYPE_VALUE) { @@ -2607,9 +2581,8 @@ obj_prop_val_t *_prop_get_prop_val(mtp_obj_t *obj, mtp_uint32 propcode) prop_val = (obj_prop_val_t *)node->value; if (prop_val) { - if (prop_val->prop->propinfo.prop_code == propcode) { + if (prop_val->prop->propinfo.prop_code == propcode) return prop_val; - } } } @@ -2640,9 +2613,8 @@ mtp_uint32 _prop_pack_obj_propval(obj_prop_val_t *pval, mtp_uchar *buf, { mtp_uint32 bytes_to_write = _prop_size_obj_propval(pval); - if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) { + if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) return 0; - } if (pval->prop->propinfo.data_type == PTP_DATATYPE_STRING) { @@ -2682,11 +2654,10 @@ mtp_uint32 _prop_size_obj_propval(obj_prop_val_t *pval) return size; if (pval->prop->propinfo.data_type == PTP_DATATYPE_STRING) { - if (pval->current_val.str == NULL) { + if (pval->current_val.str == NULL) size = 0; - } else { + else size = _prop_size_ptpstring(pval->current_val.str); - } } else if ((pval->prop->propinfo.data_type & PTP_DATATYPE_ARRAYMASK) == PTP_DATATYPE_ARRAY) { @@ -2702,9 +2673,8 @@ mtp_uint32 _prop_size_obj_propval(obj_prop_val_t *pval) void _prop_destroy_obj_propval(obj_prop_val_t *pval) { - if (pval == NULL) { + if (pval == NULL) return; - } if (pval->prop == NULL) { g_free(pval); @@ -2745,11 +2715,10 @@ static void __init_obj_prop_desc(obj_prop_desc_t *prop, mtp_uint16 propcode, prop->propinfo.form_flag = form_flag; - if (prop->propinfo.form_flag == BYTE_ARRAY_FORM) { + if (prop->propinfo.form_flag == BYTE_ARRAY_FORM) prop->propinfo.data_type = PTP_DATATYPE_AUINT8; - } else if (prop->propinfo.form_flag == LONG_STRING_FORM) { + else if (prop->propinfo.form_flag == LONG_STRING_FORM) prop->propinfo.data_type = PTP_DATATYPE_AUINT16; - } prop->group_code = group_code; @@ -2859,9 +2828,9 @@ mtp_uint32 _prop_size_obj_prop_desc(obj_prop_desc_t *prop) break; case RANGE_FORM: - if (prop->propinfo.data_type != PTP_DATATYPE_STRING) { + if (prop->propinfo.data_type != PTP_DATATYPE_STRING) size += 3 * prop->propinfo.dts_size;/* Min,Max,Step */ - } + break; case ENUM_FORM: @@ -2913,9 +2882,8 @@ mtp_uint32 _prop_pack_obj_prop_desc(obj_prop_desc_t *prop, mtp_uchar *buf, slist_node_t *node = NULL; mtp_uint16 ii; - if (!buf || size < _prop_size_obj_prop_desc(prop)) { + if (!buf || size < _prop_size_obj_prop_desc(prop)) return 0; - } /* Pack propcode, data_type, & get_set */ bytes_to_write = sizeof(mtp_uint16); @@ -3095,9 +3063,8 @@ mtp_uint32 _prop_pack_default_val_obj_prop_desc(obj_prop_desc_t *prop, bytes_to_write = __get_size_default_val_obj_prop_desc(prop); - if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) { + if ((!bytes_to_write) || (buf == NULL) || (size < bytes_to_write)) return 0; - } if (prop->propinfo.data_type == PTP_DATATYPE_STRING) { if (bytes_to_write != @@ -3133,32 +3100,28 @@ obj_prop_desc_t *_prop_get_obj_prop_desc(mtp_uint32 format_code, int num_default_obj_props = 0; /*Default*/ - if (_get_oma_drm_status() == TRUE) { + if (_get_oma_drm_status() == TRUE) num_default_obj_props = NUM_OBJECT_PROP_DESC_DEFAULT; - } else { + else num_default_obj_props = NUM_OBJECT_PROP_DESC_DEFAULT - 1; - } for (i = 0; i < num_default_obj_props; i++) { - if (props_list_default[i].propinfo.prop_code == propcode) { + if (props_list_default[i].propinfo.prop_code == propcode) return &(props_list_default[i]); - } } switch (format_code) { case PTP_FMT_MP3: case PTP_FMT_WAVE: for (i = 0; i < NUM_OBJECT_PROP_DESC_MP3; i++) { - if (props_list_mp3[i].propinfo.prop_code == propcode) { + if (props_list_mp3[i].propinfo.prop_code == propcode) return &(props_list_mp3[i]); - } } break; case MTP_FMT_WMA: for (i = 0; i < NUM_OBJECT_PROP_DESC_WMA; i++) { - if (props_list_wma[i].propinfo.prop_code == propcode) { + if (props_list_wma[i].propinfo.prop_code == propcode) return &(props_list_wma[i]); - } } break; case MTP_FMT_WMV: @@ -3168,9 +3131,8 @@ obj_prop_desc_t *_prop_get_obj_prop_desc(mtp_uint32 format_code, case PTP_FMT_MPEG: case MTP_FMT_3GP: for (i = 0; i < NUM_OBJECT_PROP_DESC_WMV; i++) { - if (props_list_wmv[i].propinfo.prop_code == propcode) { + if (props_list_wmv[i].propinfo.prop_code == propcode) return &(props_list_wmv[i]); - } } break; case MTP_FMT_ABSTRACT_AUDIO_ALBUM: @@ -3179,9 +3141,8 @@ obj_prop_desc_t *_prop_get_obj_prop_desc(mtp_uint32 format_code, case PTP_FMT_IMG_BMP: case PTP_FMT_IMG_PNG: for (i = 0; i < NUM_OBJECT_PROP_DESC_ALBUM; i++) { - if (props_list_album[i].propinfo.prop_code == propcode) { + if (props_list_album[i].propinfo.prop_code == propcode) return &(props_list_album[i]); - } } break; @@ -3266,9 +3227,9 @@ mtp_uint32 _prop_size_obj_proplist(obj_proplist_t *prop_list) node = prop_list->prop_quad_list.start; for (ii = 0; ii < prop_list->prop_quad_list.nnodes; ii++) { quad = (prop_quad_t *) node->value; - if (quad) { + if (quad) size += quad->val_size; - } + node = node->link; } return size; @@ -3292,9 +3253,8 @@ mtp_uint32 _prop_get_obj_proplist(mtp_obj_t *obj, mtp_uint32 propcode, ii++, node = node->link) { propval = (obj_prop_val_t *)node->value; - if (NULL == propval) { + if (NULL == propval) continue; - } if (FALSE == __check_object_propcode(propval->prop, propcode, group_code)) { @@ -3536,9 +3496,8 @@ mtp_uint32 _prop_pack_obj_proplist(obj_proplist_t *prop_list, mtp_uchar *buf, mtp_uint32 ii; slist_node_t *node = NULL; - if (!buf || size < _prop_size_obj_proplist(prop_list)) { + if (!buf || size < _prop_size_obj_proplist(prop_list)) return 0; - } *(mtp_uint32 *) buf = prop_list->prop_quad_list.nnodes; #ifdef __BIG_ENDIAN__ @@ -3585,7 +3544,7 @@ mtp_uint32 _prop_pack_obj_proplist(obj_proplist_t *prop_list, mtp_uchar *buf, str = (ptp_string_t *) quad->pval; if (str) { temp += _prop_pack_ptpstring(str, temp, - _prop_size_ptpstring (str)); + _prop_size_ptpstring(str)); } else { /* Put in an empty string: NumOfChars = 0; */ *temp++ = 0; @@ -3683,11 +3642,10 @@ mtp_uint32 _prop_get_supp_obj_props(mtp_uint32 format_code, mtp_uint32 num_default_obj_props = 0; /*Default*/ - if (_get_oma_drm_status() == TRUE) { + if (_get_oma_drm_status() == TRUE) num_default_obj_props = NUM_OBJECT_PROP_DESC_DEFAULT; - } else { + else num_default_obj_props = NUM_OBJECT_PROP_DESC_DEFAULT - 1; - } for (i = 0; i < num_default_obj_props; i++) { _prop_append_ele_ptparray(supp_props, @@ -4799,31 +4757,26 @@ void _prop_destroy_supp_obj_props(void) mtp_uint32 i = 0; int num_default_obj_prps = 0; - for (i = 0; i < NUM_OBJECT_PROP_DESC_MP3; i++) { + for (i = 0; i < NUM_OBJECT_PROP_DESC_MP3; i++) __destroy_obj_prop_desc(&(props_list_mp3[i])); - } - for (i = 0; i < NUM_OBJECT_PROP_DESC_WMA; i++) { + for (i = 0; i < NUM_OBJECT_PROP_DESC_WMA; i++) __destroy_obj_prop_desc(&(props_list_wma[i])); - } - for (i = 0; i < NUM_OBJECT_PROP_DESC_WMV; i++) { + for (i = 0; i < NUM_OBJECT_PROP_DESC_WMV; i++) __destroy_obj_prop_desc(&(props_list_wmv[i])); - } - for (i = 0; i < NUM_OBJECT_PROP_DESC_ALBUM; i++) { + for (i = 0; i < NUM_OBJECT_PROP_DESC_ALBUM; i++) __destroy_obj_prop_desc(&(props_list_album[i])); - } - if (_get_oma_drm_status() == TRUE) { + if (_get_oma_drm_status() == TRUE) num_default_obj_prps = NUM_OBJECT_PROP_DESC_DEFAULT; - } else { + else num_default_obj_prps = NUM_OBJECT_PROP_DESC_DEFAULT - 1; - } - for (i = 0; i < num_default_obj_prps; i++) { + for (i = 0; i < num_default_obj_prps; i++) __destroy_obj_prop_desc(&(props_list_default[i])); - } + return; } @@ -4855,9 +4808,8 @@ mtp_uint32 _prop_get_size_interdep_prop(interdep_prop_config_t *prop_config) node = prop_config->propdesc_list.start; for (ii = 0; ii < prop_config->propdesc_list.nnodes; ii++) { prop = node->value; - if (prop) { + if (prop) size += _prop_size_obj_prop_desc(prop); - } } return size; } @@ -4871,9 +4823,8 @@ mtp_uint32 _prop_pack_interdep_prop(interdep_prop_config_t *prop_config, mtp_uint32 ele_size = 0; mtp_int32 ii; - if (!buf || size < _prop_get_size_interdep_prop(prop_config)) { + if (!buf || size < _prop_get_size_interdep_prop(prop_config)) return 0; - } *(mtp_uint32 *) buf = prop_config->propdesc_list.nnodes; #ifdef __BIG_ENDIAN__ diff --git a/src/entity/mtp_store.c b/src/entity/mtp_store.c index 7b9cba8..7297d8b 100755 --- a/src/entity/mtp_store.c +++ b/src/entity/mtp_store.c @@ -92,18 +92,18 @@ mtp_bool _entity_get_store_path_by_id(mtp_uint32 store_id, mtp_char *path) { char sto_path[MTP_MAX_PATHNAME_SIZE + 1] = { 0 }; - switch(store_id) { - case MTP_INTERNAL_STORE_ID : + switch (store_id) { + case MTP_INTERNAL_STORE_ID: _util_get_internal_path(sto_path); g_strlcpy(path, sto_path, MTP_MAX_PATHNAME_SIZE + 1); break; - case MTP_EXTERNAL_STORE_ID : + case MTP_EXTERNAL_STORE_ID: _util_get_external_path(sto_path); g_strlcpy(path, sto_path, MTP_MAX_PATHNAME_SIZE + 1); break; - default : + default: ERR("No valid match for the store id [0x%x]\n", store_id); return FALSE; @@ -316,9 +316,8 @@ mtp_bool _entity_add_object_to_store(mtp_store_t *store, mtp_obj_t *obj) /* references */ if (PTP_OBJECTHANDLE_ROOT != obj->obj_info->h_parent) { par_obj = _entity_get_object_from_store(store, obj->obj_info->h_parent); - if (NULL != par_obj) { + if (NULL != par_obj) _entity_add_reference_child_array(par_obj, obj->obj_handle); - } } return TRUE; @@ -337,7 +336,7 @@ mtp_obj_t *_entity_get_object_from_store(mtp_store_t *store, mtp_uint32 handle) return NULL; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj && obj->obj_handle == handle) { @@ -366,12 +365,11 @@ mtp_obj_t *_entity_get_last_object_from_store(mtp_store_t *store, return NULL; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { temp_obj = (mtp_obj_t *)_util_get_list_next(iter); - if (temp_obj && temp_obj->obj_handle == handle) { + if (temp_obj && temp_obj->obj_handle == handle) obj = temp_obj; - } } _util_deinit_list_iterator(iter); @@ -392,7 +390,7 @@ mtp_obj_t *_entity_get_object_from_store_by_path(mtp_store_t *store, return NULL; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL) { ERR("Object is NULL"); @@ -434,7 +432,7 @@ mtp_uint32 _entity_get_objects_from_store(mtp_store_t *store, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL) { @@ -444,7 +442,7 @@ mtp_uint32 _entity_get_objects_from_store(mtp_store_t *store, if ((fmt == obj->obj_info->obj_fmt) || (fmt == PTP_FORMATCODE_ALL) || (fmt == PTP_FORMATCODE_NOTUSED)) { - _prop_append_ele_ptparray(obj_arr, (mtp_uint32)obj); + _prop_append_ele_ptparray(obj_arr, obj->obj_handle); } } _util_deinit_list_iterator(iter); @@ -455,8 +453,6 @@ mtp_uint32 _entity_get_objects_from_store_till_depth(mtp_store_t *store, mtp_uint32 obj_handle, mtp_uint32 fmt_code, mtp_uint32 depth, ptp_array_t *obj_arr) { - mtp_obj_t *obj = NULL; - retv_if(store == NULL, 0); retv_if(obj_arr == NULL, 0); @@ -467,12 +463,9 @@ mtp_uint32 _entity_get_objects_from_store_till_depth(mtp_store_t *store, return obj_arr->num_ele; } - if (PTP_OBJECTHANDLE_ROOT != obj_handle) { - obj = _entity_get_object_from_store(store, obj_handle); - if (obj) { - _prop_append_ele_ptparray(obj_arr, (mtp_uint32)obj); - } - } + if (PTP_OBJECTHANDLE_ROOT != obj_handle) + _prop_append_ele_ptparray(obj_arr, obj_handle); + if (depth > 0) { ptp_array_t *child_arr = NULL; mtp_uint32 *ptr = NULL; @@ -515,7 +508,7 @@ mtp_uint32 _entity_get_objects_from_store_by_format(mtp_store_t *store, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL || obj->obj_info == NULL) @@ -544,9 +537,8 @@ mtp_uint32 _entity_get_num_object_with_same_format(mtp_store_t *store, retv_if(store == NULL, 0); - if (PTP_FORMATCODE_NOTUSED == format) { + if (PTP_FORMATCODE_NOTUSED == format) return store->obj_list.nnodes; - } iter = (slist_iterator *)_util_init_list_iterator(&(store->obj_list)); if (iter == NULL) { @@ -554,7 +546,7 @@ mtp_uint32 _entity_get_num_object_with_same_format(mtp_store_t *store, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL || obj->obj_info == NULL) @@ -586,7 +578,7 @@ mtp_uint32 _entity_get_num_children(mtp_store_t *store, mtp_uint32 h_parent, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL || obj->obj_info == NULL) @@ -629,15 +621,14 @@ mtp_uint32 _entity_get_child_handles(mtp_store_t *store, mtp_uint32 h_parent, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL || obj->obj_info == NULL) continue; - if (obj->obj_info->h_parent == h_parent) { + if (obj->obj_info->h_parent == h_parent) _prop_append_ele_ptparray(child_arr, obj->obj_handle); - } } _util_deinit_list_iterator(iter); @@ -659,7 +650,7 @@ mtp_uint32 _entity_get_child_handles_with_same_format(mtp_store_t *store, return 0; } - while(UTIL_CHECK_LIST_NEXT(iter) == TRUE) { + while (UTIL_CHECK_LIST_NEXT(iter) == TRUE) { obj = (mtp_obj_t *)_util_get_list_next(iter); if (obj == NULL || obj->obj_info == NULL) @@ -752,9 +743,9 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, child_obj = _entity_get_object_from_store(store, ptr32[i]); - if (NULL == child_obj) { + if (NULL == child_obj) continue; - } + if (_entity_remove_object_mtp_store(store, child_obj, format, response, atleast_one, read_only)) { @@ -803,7 +794,7 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, DBG_SECURE("Folder[%s] contains only read-only files\n", obj->file_path); all_del = FALSE; - } else if (num_of_deleted_file < num_of_file){ + } else if (num_of_deleted_file < num_of_file) { DBG("num of files[%d] is present in folder[%s]\ and number of deleted files[%d]\n", num_of_file, obj->file_path, @@ -872,7 +863,7 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, obj->obj_handle); } } - } else if(*atleast_one) { + } else if (*atleast_one) { *response = PTP_RESPONSE_PARTIAL_DELETION; return FALSE; } else { @@ -977,9 +968,9 @@ mtp_uint16 _entity_delete_obj_mtp_store(mtp_store_t *store, } } - if (all_del) { + if (all_del) response = PTP_RESPONSE_OK; - } else if (atleas_one) + else if (atleas_one) response = PTP_RESPONSE_PARTIAL_DELETION; return response; @@ -1148,9 +1139,9 @@ void _entity_store_recursive_enum_folder_objects(mtp_store_t *store, do { if (TRUE == _transport_get_usb_discon_state()) { DBG("USB is disconnected"); - if (closedir(h_dir) < 0) { + if (closedir(h_dir) < 0) ERR("Close directory Fail"); - } + return; } @@ -1183,9 +1174,9 @@ NEXT: &entry); } while (status); - if (closedir(h_dir) < 0) { + if (closedir(h_dir) < 0) ERR("close directory fail"); - } + #ifdef MTP_SUPPORT_OBJECTADDDELETE_EVENT _inoti_add_watch_for_fs_events(folder_name); #endif /*MTP_SUPPORT_OBJECTADDDELETE_EVENT*/ diff --git a/src/mtp_cmd_handler.c b/src/mtp_cmd_handler.c index 282ac32..acaa3e4 100755 --- a/src/mtp_cmd_handler.c +++ b/src/mtp_cmd_handler.c @@ -114,9 +114,8 @@ static void __finish_receiving_file_packets(mtp_char *data, mtp_int32 data_len); */ void _cmd_hdlr_reset_cmd(mtp_handler_t *hdlr) { - if (hdlr->data4_send_obj.obj != NULL) { + if (hdlr->data4_send_obj.obj != NULL) _entity_dealloc_mtp_obj(hdlr->data4_send_obj.obj); - } memset(hdlr, 0x00, sizeof(mtp_handler_t)); @@ -460,8 +459,7 @@ static void __get_storage_ids(mtp_handler_t *hdlr) _prop_init_ptparray(&ids, UINT32_TYPE); - if (_hutil_get_storage_ids(&ids) == MTP_ERROR_NONE) - { + if (_hutil_get_storage_ids(&ids) == MTP_ERROR_NONE) { _hdlr_init_data_container(&blk, hdlr->usb_cmd.code, hdlr->usb_cmd.tid); num_bytes = _prop_get_size_ptparray(&ids); @@ -563,11 +561,10 @@ static void __get_num_objects(mtp_handler_t *hdlr) resp = PTP_RESPONSE_GEN_ERROR; } - if (resp == PTP_RESPONSE_OK) { + if (resp == PTP_RESPONSE_OK) _cmd_hdlr_send_response(hdlr, resp, 1, (mtp_uint32 *)&num_obj); - } else { + else _cmd_hdlr_send_response_code(hdlr, resp); - } } static void __get_object_handles(mtp_handler_t *hdlr) @@ -701,7 +698,7 @@ static void __get_object(mtp_handler_t *hdlr) mtp_uint16 resp = PTP_RESPONSE_OK; mtp_uint32 packet_len; mtp_uint32 read_len = 0; - mtp_uint32 h_file = INVALID_FILE; + FILE* h_file = NULL; mtp_int32 error = 0; if (_hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 1) || @@ -745,7 +742,7 @@ static void __get_object(mtp_handler_t *hdlr) _device_set_phase(DEVICE_PHASE_DATAIN); h_file = _util_file_open(path, MTP_FILE_READ, &error); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { ERR("_util_file_open() Fail"); _device_set_phase(DEVICE_PHASE_NOTREADY); if (EACCES == error) { @@ -801,9 +798,8 @@ static void __get_object(mtp_handler_t *hdlr) sent += read_len; } - if (total_len % ((mtp_uint64)_transport_get_usb_packet_len()) == 0) { + if (total_len % ((mtp_uint64)_transport_get_usb_packet_len()) == 0) _transport_send_zlp(); - } Done: _util_file_close(h_file); @@ -825,16 +821,16 @@ static void __send_object_info(mtp_handler_t *hdlr) obj_data_t obdata = { 0 }; store_id = _hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 0); - if (store_id == 0) { + if (store_id == 0) store_id = _device_get_default_store_id(); - } + h_parent = _hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 1); if (_hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 2)) { resp = PTP_RESPONSE_PARAM_NOTSUPPORTED; - if (_device_get_phase() != DEVICE_PHASE_NOTREADY) { + if (_device_get_phase() != DEVICE_PHASE_NOTREADY) _cmd_hdlr_send_response_code(hdlr, resp); - } + return; } @@ -1183,7 +1179,7 @@ static void __get_device_prop_desc(mtp_handler_t *hdlr) } case MTP_PROPERTYCODE_DEVICEICON: { - mtp_uint32 h_file; + FILE* h_file; mtp_uint32 bytes_read = 0; mtp_uint32 file_size = 0; struct stat buf; @@ -1197,7 +1193,7 @@ static void __get_device_prop_desc(mtp_handler_t *hdlr) } h_file = _util_file_open(MTP_DEVICE_ICON, MTP_FILE_READ, &err); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { ERR("file handle is not valid"); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); @@ -1291,7 +1287,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) prop_id = _hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 0); _hdlr_init_data_container(&blk, hdlr->usb_cmd.code, hdlr->usb_cmd.tid); - switch(prop_id) { + switch (prop_id) { #ifdef MTP_SUPPORT_DEVICEPROP_BATTERYLEVEL case PTP_PROPERTYCODE_BATTERYLEVEL: { mtp_int32 batt = 0; @@ -1380,7 +1376,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) case MTP_PROPERTYCODE_DEVICEICON: { - mtp_uint32 h_file; + FILE *h_file; mtp_uint32 read_bytes = 0; mtp_uint32 file_size = 0; struct stat buf; @@ -1390,13 +1386,13 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) mtp_uint32 ii; h_file = _util_file_open(MTP_DEVICE_ICON, MTP_FILE_READ, &err); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { ERR("file handle is not valid"); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); return; } - if (fstat(fileno((FILE *)h_file), &buf) != 0) { + if (fstat(fileno(h_file), &buf) != 0) { _util_file_close(h_file); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); @@ -1425,9 +1421,8 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) _prop_init_ptparray(&val_arr, UINT8_TYPE); _prop_grow_ptparray(&val_arr, read_bytes); - for (ii = 0; ii < read_bytes; ii++) { + for (ii = 0; ii < read_bytes; ii++) _prop_append_ele_ptparray(&val_arr, data[ii]); - } no_bytes = _prop_get_size_ptparray(&val_arr); ptr = _hdlr_alloc_buf_data_container(&blk, no_bytes, no_bytes); @@ -1654,7 +1649,7 @@ static void __get_object_references(mtp_handler_t *hdlr) case MTP_ERROR_NONE: resp = PTP_RESPONSE_OK; break; - default : + default: resp = PTP_RESPONSE_GEN_ERROR; } @@ -1729,9 +1724,8 @@ static void __set_object_references(mtp_handler_t *hdlr) } ptr = _hdlr_get_payload_data(&blk); - if (ptr == NULL) { + if (ptr == NULL) return; - } memcpy(&num_ref, ptr, sizeof(mtp_uint32)); #ifdef __BIG_ENDIAN__ @@ -2022,7 +2016,6 @@ static void __get_object_prop_list(mtp_handler_t *hdlr) slist_node_t *next_node = NULL; mtp_uint32 ii = 0; mtp_uint32 jj = 0; - mtp_obj_t **ptr_array = NULL; mtp_obj_t *obj = NULL; #endif /*MTP_USE_RUNTIME_GETOBJECTPROPVALUE*/ @@ -2085,13 +2078,19 @@ static void __get_object_prop_list(mtp_handler_t *hdlr) #ifdef MTP_USE_RUNTIME_GETOBJECTPROPVALUE if (resp == PTP_RESPONSE_OK && obj_arr.array_entry) { - ptr_array = obj_arr.array_entry; + mtp_uint32 *obj_handles = obj_arr.array_entry; for (ii = 0; ii < obj_arr.num_ele; ii++) { - obj = ptr_array[ii]; - if (NULL == obj || obj->propval_list.nnodes == 0) { + mtp_store_t *store = NULL; + + store = _device_get_store_containing_obj(obj_handles[ii]); + if (store == NULL) continue; - } + + obj = _entity_get_object_from_store(store, obj_handles[ii]); + if (NULL == obj || obj->propval_list.nnodes == 0) + continue; + /*Remove all the old property value, and ready to set up new */ for (jj = 0, next_node = obj->propval_list.start; jj < obj->propval_list.nnodes; jj++) { @@ -2273,7 +2272,7 @@ static void __report_acquired_content(mtp_handler_t *hdlr) mtp_uint32 num_bytes = 0; mtp_uint32 num_lines = 0; mtp_uint32 rem_modified = 0; - mtp_uint32 h_file; + FILE* h_file; time_t cur_time; time_t l_time; mtp_int32 diff_time; @@ -2295,9 +2294,9 @@ static void __report_acquired_content(mtp_handler_t *hdlr) if (tid == 0) { if (access(MTP_FILES_MODIFIED_FILES, F_OK) == 0) - if (remove(MTP_FILES_MODIFIED_FILES) < 0) { + if (remove(MTP_FILES_MODIFIED_FILES) < 0) ERR("remove(%s) Fail", MTP_FILES_MODIFIED_FILES); - } + resp = PTP_RESPONSE_OK; _prop_grow_ptparray(&guid_arr, 1); _prop_append_ele_ptparray(&guid_arr, 0); @@ -2328,7 +2327,7 @@ static void __report_acquired_content(mtp_handler_t *hdlr) } h_file = _util_file_open(MTP_FILES_MODIFIED_FILES, MTP_FILE_READ, &err); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { resp = PTP_RESPONSE_GEN_ERROR; _prop_init_ptparray(&guid_arr, UINT32_TYPE); _prop_append_ele_ptparray(&guid_arr, 0); @@ -2344,7 +2343,7 @@ static void __report_acquired_content(mtp_handler_t *hdlr) max_size : num_lines - start_idx; rem_modified = (num_lines - start_idx > max_size) ? - (num_lines - start_idx- max_size) : 0; + (num_lines - start_idx - max_size) : 0; g_has_round_trip = FALSE; _prop_init_ptparray(&guid_arr, UINT32_TYPE); @@ -2354,9 +2353,9 @@ static void __report_acquired_content(mtp_handler_t *hdlr) _util_file_close(h_file); if (rem_modified == 0) { - if (remove(MTP_FILES_MODIFIED_FILES) < 0) { + if (remove(MTP_FILES_MODIFIED_FILES) < 0) ERR("remove(%s) Fail", MTP_FILES_MODIFIED_FILES); - } + g_mgr->meta_info.mod = 0; } @@ -2370,11 +2369,10 @@ DONE: _device_set_phase(DEVICE_PHASE_DATAIN); } - if (_hdlr_send_data_container(&blk)) { + if (_hdlr_send_data_container(&blk)) resp = PTP_RESPONSE_OK; - } else { + else resp = PTP_RESPONSE_GEN_ERROR; - } _prop_deinit_ptparray(&guid_arr); g_free(blk.data); @@ -2397,9 +2395,9 @@ static void __send_playback_skip(mtp_handler_t *hdlr) mtp_uint16 resp = PTP_RESPONSE_INVALIDPARAM; skip = (mtp_int32) _hdlr_get_param_cmd_container(&(hdlr->usb_cmd), 0); - if (MTP_ERROR_NONE == _hutil_get_playback_skip(skip)) { + if (MTP_ERROR_NONE == _hutil_get_playback_skip(skip)) resp = PTP_RESPONSE_OK; - } + _cmd_hdlr_send_response_code(hdlr, resp); return; } @@ -2583,11 +2581,10 @@ static void __copy_object(mtp_handler_t *hdlr) } _transport_set_mtp_operation_state(MTP_STATE_ONSERVICE); - if (resp == PTP_RESPONSE_OK) { + if (resp == PTP_RESPONSE_OK) _cmd_hdlr_send_response(hdlr, resp, 1, &new_handle); - } else { + else _cmd_hdlr_send_response_code(hdlr, resp); - } return; } @@ -2630,11 +2627,10 @@ static void __reset_device_prop_value(mtp_handler_t *hdlr) prop->current_val.str->str); _device_set_sync_partner(temp); - if (!g_strcmp0(temp, MTP_DEV_PROPERTY_NULL_SYNCPARTNER)) { + if (!g_strcmp0(temp, MTP_DEV_PROPERTY_NULL_SYNCPARTNER)) vconf_set_str(VCONFKEY_MTP_SYNC_PARTNER_STR, ""); - } else { + else vconf_set_str(VCONFKEY_MTP_SYNC_PARTNER_STR, temp); - } } _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_OK); @@ -2745,11 +2741,10 @@ static void __send_object_prop_list(mtp_handler_t *hdlr) idx = 0; if (store_id) { - if (!h_parent) { + if (!h_parent) h_parent = _device_get_default_parent_handle(); - } else if (h_parent == 0xFFFFFFFF) { + else if (h_parent == 0xFFFFFFFF) h_parent = PTP_OBJECTHANDLE_ROOT; - } } else { store_id = _device_get_default_store_id(); if (!store_id) @@ -2874,9 +2869,8 @@ static void __send_object_prop_list(mtp_handler_t *hdlr) } if (PTP_RESPONSE_OK != resp) { - if (hdlr->data4_send_obj.obj) { + if (hdlr->data4_send_obj.obj) _entity_dealloc_mtp_obj(hdlr->data4_send_obj.obj); - } hdlr->data4_send_obj.obj = NULL; hdlr->data4_send_obj.is_valid = FALSE; @@ -2958,128 +2952,128 @@ mtp_bool _cmd_hdlr_send_response_code(mtp_handler_t *hdlr, mtp_uint16 resp) #ifdef MTP_SUPPORT_PRINT_COMMAND static void __print_command(mtp_uint16 code) { - switch(code) { - case PTP_OPCODE_GETDEVICEINFO : + switch (code) { + case PTP_OPCODE_GETDEVICEINFO: DBG("COMMAND ======== GET DEVICE INFO==========="); break; - case PTP_OPCODE_OPENSESSION : + case PTP_OPCODE_OPENSESSION: DBG("COMMAND ======== OPEN SESSION ==========="); break; - case PTP_OPCODE_CLOSESESSION : + case PTP_OPCODE_CLOSESESSION: DBG("COMMAND ======== CLOSE SESSION ==========="); break; - case PTP_OPCODE_GETSTORAGEIDS : + case PTP_OPCODE_GETSTORAGEIDS: DBG("COMMAND ======== GET STORAGE IDS ==========="); break; - case PTP_OPCODE_GETSTORAGEINFO : + case PTP_OPCODE_GETSTORAGEINFO: DBG("COMMAND ======== GET STORAGE INFO ==========="); break; - case PTP_OPCODE_GETNUMOBJECTS : + case PTP_OPCODE_GETNUMOBJECTS: DBG("COMMAND ======== GET NUM OBJECTS ==========="); break; - case PTP_OPCODE_GETOBJECTHANDLES : + case PTP_OPCODE_GETOBJECTHANDLES: DBG("COMMAND ======== GET OBJECT HANDLES ==========="); break; - case PTP_OPCODE_GETOBJECTINFO : + case PTP_OPCODE_GETOBJECTINFO: DBG("COMMAND ======== GET OBJECT INFO ==========="); break; - case PTP_OPCODE_GETOBJECT : + case PTP_OPCODE_GETOBJECT: DBG("COMMAND ======== GET OBJECT ==========="); break; - case PTP_OPCODE_DELETEOBJECT : + case PTP_OPCODE_DELETEOBJECT: DBG("COMMAND ======== DELETE OBJECT ==========="); break; - case PTP_OPCODE_SENDOBJECTINFO : + case PTP_OPCODE_SENDOBJECTINFO: DBG("COMMAND ======== SEND OBJECT INFO ==========="); break; - case PTP_OPCODE_SENDOBJECT : + case PTP_OPCODE_SENDOBJECT: DBG("COMMAND ======== SEND OBJECT ==========="); break; - case PTP_OPCODE_INITIATECAPTURE : + case PTP_OPCODE_INITIATECAPTURE: DBG("COMMAND ======== INITIATE CAPTURE ==========="); break; - case PTP_OPCODE_FORMATSTORE : + case PTP_OPCODE_FORMATSTORE: DBG("COMMAND ======== FORMAT STORE ==========="); break; - case PTP_OPCODE_RESETDEVICE : + case PTP_OPCODE_RESETDEVICE: DBG("COMMAND ======== RESET DEVICE ==========="); break; - case PTP_OPCODE_SELFTEST : + case PTP_OPCODE_SELFTEST: DBG("COMMAND ======== SELF TEST ==========="); break; - case PTP_OPCODE_SETOBJECTPROTECTION : + case PTP_OPCODE_SETOBJECTPROTECTION: DBG("COMMAND ======== SET OBJECT PROTECTION ==========="); break; - case PTP_OPCODE_POWERDOWN : + case PTP_OPCODE_POWERDOWN: DBG("COMMAND ======== POWER DOWN ==========="); break; - case PTP_OPCODE_GETDEVICEPROPDESC : + case PTP_OPCODE_GETDEVICEPROPDESC: DBG("COMMAND ======== GET DEVICE PROP DESC ==========="); break; - case PTP_OPCODE_GETDEVICEPROPVALUE : + case PTP_OPCODE_GETDEVICEPROPVALUE: DBG("COMMAND ======== GET DEVICE PROP VALUE ==========="); break; - case PTP_OPCODE_SETDEVICEPROPVALUE : + case PTP_OPCODE_SETDEVICEPROPVALUE: DBG("COMMAND ======== SET DEVICE PROP VALUE ==========="); break; - case PTP_OPCODE_RESETDEVICEPROPVALUE : + case PTP_OPCODE_RESETDEVICEPROPVALUE: DBG("COMMAND ======== RESET DEVICE PROP VALUE ==========="); break; - case PTP_OPCODE_TERMINATECAPTURE : + case PTP_OPCODE_TERMINATECAPTURE: DBG("COMMAND ======== TERMINATE CAPTURE ==========="); break; - case PTP_OPCODE_MOVEOBJECT : + case PTP_OPCODE_MOVEOBJECT: DBG("COMMAND ======== MOVE OBJECT ==========="); break; - case PTP_OPCODE_COPYOBJECT : + case PTP_OPCODE_COPYOBJECT: DBG("COMMAND ======== COPY OBJECT ==========="); break; - case PTP_OPCODE_GETPARTIALOBJECT : + case PTP_OPCODE_GETPARTIALOBJECT: DBG("COMMAND ======== GET PARTIAL OBJECT ==========="); break; - case PTP_OPCODE_INITIATEOPENCAPTURE : + case PTP_OPCODE_INITIATEOPENCAPTURE: DBG("COMMAND ======== INITIATE OPEN CAPTURE ==========="); break; - case MTP_OPCODE_WMP_UNDEFINED : + case MTP_OPCODE_WMP_UNDEFINED: DBG("COMMAND ======== WMP UNDEFINED =========="); break; - case MTP_OPCODE_WMP_REPORTACQUIREDCONTENT : + case MTP_OPCODE_WMP_REPORTACQUIREDCONTENT: DBG("COMMAND ======= REPORT ACQUIRED CONTENT ========="); break; - case MTP_OPCODE_GETOBJECTPROPSUPPORTED : + case MTP_OPCODE_GETOBJECTPROPSUPPORTED: DBG("COMMAND ======= GET OBJECT PROP SUPPORTED ========"); break; - case MTP_OPCODE_GETOBJECTPROPDESC : + case MTP_OPCODE_GETOBJECTPROPDESC: DBG("COMMAND ======== GET OBJECT PROP DESC =========="); break; - case MTP_OPCODE_GETOBJECTPROPVALUE : + case MTP_OPCODE_GETOBJECTPROPVALUE: DBG("COMMAND ======== GET OBJECT PROP VALUE =========="); break; - case MTP_OPCODE_SETOBJECTPROPVALUE : + case MTP_OPCODE_SETOBJECTPROPVALUE: DBG("COMMAND ======== SET OBJECT PROP VALUE =========="); break; - case MTP_OPCODE_GETOBJECTPROPLIST : + case MTP_OPCODE_GETOBJECTPROPLIST: DBG("COMMAND ======== GET OBJECT PROP LIST =========="); break; - case MTP_OPCODE_SETOBJECTPROPLIST : + case MTP_OPCODE_SETOBJECTPROPLIST: DBG("COMMAND ======== SET OBJECT PROP LIST =========="); break; - case MTP_OPCODE_GETINTERDEPPROPDESC : + case MTP_OPCODE_GETINTERDEPPROPDESC: DBG("COMMAND ======== GET INTERDEP PROP DESC =========="); break; - case MTP_OPCODE_SENDOBJECTPROPLIST : + case MTP_OPCODE_SENDOBJECTPROPLIST: DBG("COMMAND ======== SEND OBJECT PROP LIST =========="); break; - case MTP_OPCODE_GETOBJECTREFERENCES : + case MTP_OPCODE_GETOBJECTREFERENCES: DBG("COMMAND ======== GET OBJECT REFERENCES =========="); break; - case MTP_OPCODE_SETOBJECTREFERENCES : + case MTP_OPCODE_SETOBJECTREFERENCES: DBG("COMMAND ======== SET OBJECT REFERENCES =========="); break; - case MTP_OPCODE_PLAYBACK_SKIP : + case MTP_OPCODE_PLAYBACK_SKIP: DBG("COMMAND ======== PLAYBACK SKIP =========="); break; - default : + default: DBG("======== UNKNOWN COMMAND =========="); break; } @@ -3190,14 +3184,13 @@ void _receive_mq_data_cb(mtp_char *buffer, mtp_int32 buf_len) _transport_set_control_event(0); _transport_set_mtp_operation_state(MTP_STATE_ONSERVICE); - if (g_mgr->ftemp_st.fhandle != INVALID_FILE) { + if (g_mgr->ftemp_st.fhandle != NULL) { DBG("In Cancel Transaction fclose "); _util_file_close(g_mgr->ftemp_st.fhandle); - g_mgr->ftemp_st.fhandle = INVALID_FILE; + g_mgr->ftemp_st.fhandle = NULL; DBG("In Cancel Transaction, remove "); - if (remove(g_mgr->ftemp_st.filepath) < 0) { + if (remove(g_mgr->ftemp_st.filepath) < 0) ERR_SECURE("remove(%s) Fail", g_mgr->ftemp_st.filepath); - } } else { DBG("g_mgr->ftemp_st.fhandle is not valid, return"); } @@ -3259,16 +3252,15 @@ static mtp_bool __receive_temp_file_first_packet(mtp_char *data, mtp_int32 data_len) { mtp_char *filepath = g_mgr->ftemp_st.filepath; - mtp_uint32 *fhandle = &g_mgr->ftemp_st.fhandle; mtp_int32 error = 0; mtp_uint32 *data_sz = &g_mgr->ftemp_st.data_size; mtp_char *buffer = g_mgr->ftemp_st.temp_buff; _transport_set_mtp_operation_state(MTP_STATE_DATA_TRANSFER_DL); if (access(filepath, F_OK) == 0) { - if (*fhandle != INVALID_FILE) { - _util_file_close(*fhandle); - *fhandle = INVALID_FILE; /* initialize */ + if (g_mgr->ftemp_st.fhandle != NULL) { + _util_file_close(g_mgr->ftemp_st.fhandle); + g_mgr->ftemp_st.fhandle = NULL; /* initialize */ } if (remove(filepath) < 0) { @@ -3278,8 +3270,8 @@ static mtp_bool __receive_temp_file_first_packet(mtp_char *data, } } - *fhandle = _util_file_open(filepath, MTP_FILE_WRITE, &error); - if (*fhandle == INVALID_FILE) { + g_mgr->ftemp_st.fhandle = _util_file_open(filepath, MTP_FILE_WRITE, &error); + if (g_mgr->ftemp_st.fhandle == NULL) { ERR("First file handle is invalid!!"); __finish_receiving_file_packets(data, data_len); return FALSE; @@ -3293,14 +3285,14 @@ static mtp_bool __receive_temp_file_first_packet(mtp_char *data, /* check whether last data packet */ if (*data_sz == g_mgr->ftemp_st.file_size) { - if (_util_file_write(*fhandle, &data[sizeof(header_container_t)], + if (_util_file_write(g_mgr->ftemp_st.fhandle, &data[sizeof(header_container_t)], data_len - sizeof(header_container_t)) != data_len - sizeof(header_container_t)) { ERR("fwrite error!"); } *data_sz = 0; - _util_file_close(*fhandle); - *fhandle = INVALID_FILE; /* initialize */ + _util_file_close(g_mgr->ftemp_st.fhandle); + g_mgr->ftemp_st.fhandle = NULL; /* initialize */ __finish_receiving_file_packets(data, data_len); } else { g_mgr->ftemp_st.data_count++; @@ -3317,14 +3309,13 @@ static mtp_bool __receive_temp_file_next_packets(mtp_char *data, mtp_uint32 rx_size = _get_rx_pkt_size(); mtp_uint32 *data_sz = &g_mgr->ftemp_st.data_size; mtp_char *buffer = g_mgr->ftemp_st.temp_buff; - mtp_uint32 *fhandle = &g_mgr->ftemp_st.fhandle; g_mgr->ftemp_st.data_count++; g_mgr->ftemp_st.size_remaining += data_len; if ((*data_sz + (mtp_uint32)data_len) > g_conf.write_file_size) { /* copy oversized packet to temp file */ - if (_util_file_write(*fhandle, buffer, *data_sz) != *data_sz) + if (_util_file_write(g_mgr->ftemp_st.fhandle, buffer, *data_sz) != *data_sz) ERR("fwrite error writeSize=[%u]\n", *data_sz); *data_sz = 0; @@ -3337,12 +3328,12 @@ static mtp_bool __receive_temp_file_next_packets(mtp_char *data, if (data_len < rx_size || g_mgr->ftemp_st.size_remaining == g_mgr->ftemp_st.file_size) { - if (_util_file_write(*fhandle, buffer, *data_sz) != *data_sz) { + if (_util_file_write(g_mgr->ftemp_st.fhandle, buffer, *data_sz) != *data_sz) ERR("fwrite error write size=[%u]\n", *data_sz); - } + *data_sz = 0; - _util_file_close(*fhandle); - *fhandle = INVALID_FILE; /* initialize */ + _util_file_close(g_mgr->ftemp_st.fhandle); + g_mgr->ftemp_st.fhandle = NULL; __finish_receiving_file_packets(data, data_len); } return TRUE; diff --git a/src/mtp_cmd_handler_util.c b/src/mtp_cmd_handler_util.c index c802fcf..2e265af 100755 --- a/src/mtp_cmd_handler_util.c +++ b/src/mtp_cmd_handler_util.c @@ -80,9 +80,8 @@ mtp_err_t _hutil_get_storage_ids(ptp_array_t *store_ids) num_elem = _device_get_store_ids(store_ids); num_stores = _device_get_num_stores(); - if (num_elem == num_stores) { + if (num_elem == num_stores) return MTP_ERROR_NONE; - } ERR("get storage id Fail. num_elem[%d], num_stores[%d]\n", num_elem, num_stores); @@ -153,9 +152,8 @@ mtp_err_t _hutil_reset_device_entry(mtp_uint32 prop_id) ERR("property reference is NULL"); return MTP_ERROR_GENERAL; } - for (ii = 0; ii < NUM_DEVICE_PROPERTIES; ii++) { + for (ii = 0; ii < NUM_DEVICE_PROPERTIES; ii++) _prop_reset_device_prop_desc(&prop[ii]); - } } else { prop = _device_get_device_property(prop_id); if (prop == NULL) @@ -444,10 +442,10 @@ mtp_err_t _hutil_add_object_entry(obj_info_t *obj_info, mtp_char *file_name, obj_info->association_type != PTP_ASSOCIATIONTYPE_FOLDER) || is_made_by_mtp) { - mtp_uint32 h_abs_file = INVALID_FILE; + FILE* h_abs_file = NULL; h_abs_file = _util_file_open(new_f_path, MTP_FILE_WRITE, &error); - if (h_abs_file == INVALID_FILE) { + if (h_abs_file == NULL) { ERR("create file fail!!"); _entity_dealloc_mtp_obj(obj); return MTP_ERROR_GENERAL; @@ -532,7 +530,7 @@ mtp_err_t _hutil_remove_object_entry(mtp_uint32 obj_handle, mtp_uint32 format) resp = MTP_ERROR_ACCESS_DENIED; break; case PTP_RESPONSE_INVALID_OBJ_HANDLE: - resp =MTP_ERROR_INVALID_OBJECTHANDLE; + resp = MTP_ERROR_INVALID_OBJECTHANDLE; break; default: break; @@ -553,9 +551,8 @@ mtp_err_t _hutil_get_object_entry(mtp_uint32 obj_handle, mtp_obj_t **obj_ptr) mtp_obj_t *obj = NULL; obj = _device_get_object_with_handle(obj_handle); - if (NULL == obj || NULL == obj->obj_info) { + if (NULL == obj || NULL == obj->obj_info) return MTP_ERROR_GENERAL; - } *obj_ptr = obj; return MTP_ERROR_NONE; @@ -788,9 +785,8 @@ mtp_err_t _hutil_copy_object_entries(mtp_uint32 dst_store_id, mtp_uint32 *ptr32 = child_arr.array_entry; child_obj = _entity_get_object_from_store(src, ptr32[ii]); - if (child_obj == NULL) { + if (child_obj == NULL) continue; - } ret = _hutil_copy_object_entries(dst_store_id, src_store_id, new_obj->obj_handle, child_obj->obj_handle, @@ -1097,7 +1093,7 @@ mtp_err_t _hutil_duplicate_object_entry(mtp_uint32 dst_store_id, return MTP_ERROR_STORE_FULL; } - if((ret = _hutil_copy_object_entries(dst_store_id, src->store_id, + if ((ret = _hutil_copy_object_entries(dst_store_id, src->store_id, h_parent, obj_handle, new_handle, FALSE)) != MTP_ERROR_NONE) { return ret; } @@ -1111,11 +1107,10 @@ mtp_err_t _hutil_duplicate_object_entry(mtp_uint32 dst_store_id, return MTP_ERROR_GENERAL; } - if (new_obj->obj_info->obj_fmt == PTP_FMT_ASSOCIATION) { + if (new_obj->obj_info->obj_fmt == PTP_FMT_ASSOCIATION) _util_scan_folder_contents_in_db(new_obj->file_path); - } else { + else _util_add_file_to_db(new_obj->file_path); - } return MTP_ERROR_NONE; } @@ -1124,7 +1119,7 @@ mtp_err_t _hutil_read_file_data_from_offset(mtp_uint32 obj_handle, off_t offset, void *data, mtp_uint32 *data_sz) { mtp_obj_t *obj = NULL; - mtp_uint32 h_file = INVALID_FILE; + FILE* h_file = NULL; mtp_int32 error = 0; mtp_char fname[MTP_MAX_PATHNAME_SIZE + 1]; off_t result = 0; @@ -1145,7 +1140,7 @@ mtp_err_t _hutil_read_file_data_from_offset(mtp_uint32 obj_handle, off_t offset, g_strlcpy(fname, obj->file_path, MTP_MAX_PATHNAME_SIZE + 1); h_file = _util_file_open(fname, MTP_FILE_READ, &error); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { ERR("file open Fail[%s]\n", fname); return MTP_ERROR_GENERAL; } @@ -1290,15 +1285,13 @@ mtp_err_t _hutil_set_protection(mtp_uint32 obj_handle, mtp_uint16 prot_status) return MTP_ERROR_GENERAL; } - if (MTP_FILE_ATTR_MODE_NONE == attrs.attribute) { + if (MTP_FILE_ATTR_MODE_NONE == attrs.attribute) return MTP_ERROR_GENERAL; - } - if (prot_status == PTP_PROTECTIONSTATUS_READONLY) { + if (prot_status == PTP_PROTECTIONSTATUS_READONLY) attrs.attribute |= MTP_FILE_ATTR_MODE_READ_ONLY; - } else { + else attrs.attribute &= ~MTP_FILE_ATTR_MODE_READ_ONLY; - } if (FALSE == _util_set_file_attrs(fname, attrs.attribute)) { ERR("Failed to set file[%s] attrs\n", fname); @@ -1452,11 +1445,10 @@ mtp_err_t _hutil_construct_object_entry(mtp_uint32 store_id, mtp_char file_name[MTP_MAX_FILENAME_SIZE + 1] = { 0 }; if (store_id) { - if (!h_parent) { + if (!h_parent) h_parent = _device_get_default_parent_handle(); - } else if (h_parent == 0xFFFFFFFF) { + else if (h_parent == 0xFFFFFFFF) h_parent = PTP_OBJECTHANDLE_ROOT; - } } else { store_id = _device_get_default_store_id(); @@ -1484,9 +1476,8 @@ mtp_err_t _hutil_construct_object_entry(mtp_uint32 store_id, } /* Delete and invalidate the old obj_info for send object */ - if (objdata->obj != NULL) { + if (objdata->obj != NULL) _entity_dealloc_mtp_obj(objdata->obj); - } } store = _device_get_store(store_id); @@ -1571,7 +1562,7 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, mtp_char alb_extn[MTP_MAX_EXTENSION_LENGTH + 1] = { 0 }; mtp_char *alb_buf = NULL; mtp_uint32 alb_sz = 0; - mtp_uint32 h_temp = INVALID_FILE; + FILE* h_temp = NULL; mtp_int32 error = 0; #endif /*MTP_SUPPORT_ALBUM_ART*/ mtp_char file_name[MTP_MAX_FILENAME_SIZE + 1] = { 0 }; @@ -1611,9 +1602,8 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, } obj_info->obj_fmt = format; - if (obj_info->obj_fmt == PTP_FMT_ASSOCIATION) { + if (obj_info->obj_fmt == PTP_FMT_ASSOCIATION) obj_info->association_type = PTP_ASSOCIATIONTYPE_FOLDER; - } obj_info->file_size = obj_sz; @@ -1706,9 +1696,9 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, /* Acquire object information related data. */ prop_val = _prop_alloc_obj_propval(prop_desc); - if (prop_val == NULL) { + if (prop_val == NULL) continue; - } + _prop_set_current_array_val(prop_val, temp, bytes_left); switch (prop_code) { case MTP_OBJ_PROPERTYCODE_WIDTH: @@ -1887,7 +1877,7 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, /* file write */ h_temp = _util_file_open(full_path, MTP_FILE_WRITE, &error); - if (h_temp != INVALID_FILE) { + if (h_temp != NULL) { _util_file_write(h_temp, alb_buf, sizeof(mtp_uchar) *alb_sz); _util_file_close(h_temp); @@ -1920,7 +1910,7 @@ mtp_err_t _hutil_get_object_prop_value(mtp_uint32 obj_handle, tobj = _device_get_object_with_handle(obj_handle); if (NULL == tobj) { - ERR("requested handle does not exist[0x%x]\n",obj_handle); + ERR("requested handle does not exist[0x%x]\n", obj_handle); return MTP_ERROR_INVALID_OBJECTHANDLE; } @@ -2073,9 +2063,8 @@ mtp_err_t _hutil_update_object_property(mtp_uint32 obj_handle, ERR("Propert [0x%x] is GETONLY\n", prop_code); } - if (prop_sz != NULL) { + if (prop_sz != NULL) *prop_sz = p_size; - } return MTP_ERROR_NONE; } @@ -2150,7 +2139,7 @@ mtp_err_t _hutil_get_object_prop_list(mtp_uint32 obj_handle, mtp_uint32 format, } _util_init_list(&(prop_list->prop_quad_list)); - _prop_init_ptparray(obj_arr, PTR_TYPE); + _prop_init_ptparray(obj_arr, UINT32_TYPE); if (store != NULL) { _entity_get_objects_from_store_till_depth(store, obj_handle, @@ -2164,11 +2153,10 @@ mtp_err_t _hutil_get_object_prop_list(mtp_uint32 obj_handle, mtp_uint32 format, } if (obj_arr->num_ele != 0) { - mtp_obj_t **ptr_obj; - ptr_obj = obj_arr->array_entry; + mtp_uint32 *obj_handles = obj_arr->array_entry; for (i = 0; i < obj_arr->num_ele; i++) { - obj = ptr_obj[i]; + obj = _entity_get_object_from_store(store, obj_handles[i]); if (!obj) continue; @@ -2201,9 +2189,8 @@ mtp_err_t _hutil_remove_object_reference(mtp_uint32 obj_handle, return MTP_ERROR_NONE; } - if (_entity_remove_reference_child_array(obj, ref_handle) == FALSE) { + if (_entity_remove_reference_child_array(obj, ref_handle) == FALSE) ERR("_entity_remove_reference_child_array Fail"); - } return MTP_ERROR_NONE; } @@ -2258,9 +2245,8 @@ mtp_err_t _hutil_get_object_references(mtp_uint32 obj_handle, for (idx = 0; idx < ref_arr.num_ele; idx++) { ref_obj = _device_get_object_with_handle(ref_ptr[idx]); - if (ref_obj == NULL) { + if (ref_obj == NULL) _entity_remove_reference_child_array(obj, ref_ptr[idx]); - } } *num_ele = obj->child_array.num_ele; @@ -2319,9 +2305,8 @@ mtp_err_t _hutil_get_playback_skip(mtp_int32 skip_param) return MTP_ERROR_GENERAL; } - if (obj_handle == 0x0) { + if (obj_handle == 0x0) return MTP_ERROR_NONE; - } obj = _device_get_object_with_handle(obj_handle); if (obj == NULL || obj->obj_info == NULL) { @@ -2343,13 +2328,12 @@ mtp_err_t _hutil_get_playback_skip(mtp_int32 skip_param) ref_arr = _entity_get_reference_child_array(par_obj); idx = _prop_find_ele_ptparray(ref_arr, obj_handle); if (idx != ELEMENT_NOT_FOUND) { - if (((long long)idx + (long long)skip_param) > UINT_MAX) { + if (((long long)idx + (long long)skip_param) > UINT_MAX) new_idx = ref_arr->num_ele - 1; - } else if ((idx + skip_param) >= ref_arr->num_ele) { + else if ((idx + skip_param) >= ref_arr->num_ele) new_idx = 0; - } else { + else new_idx = idx + skip_param; - } if (_prop_get_ele_ptparray(ref_arr, new_idx, (void *)(&new_hobj)) == TRUE) { @@ -2373,13 +2357,13 @@ mtp_err_t _hutil_get_playback_skip(mtp_int32 skip_param) } memcpy(&idx, dev_prop->current_val.integer, sizeof(mtp_uint32)); ref_arr = _entity_get_reference_child_array(obj); - if (((long long)idx + (long long)skip_param) > UINT_MAX) { + if (((long long)idx + (long long)skip_param) > UINT_MAX) new_idx = ref_arr->num_ele - 1; - } else if ((idx + skip_param) >= ref_arr->num_ele) { + else if ((idx + skip_param) >= ref_arr->num_ele) new_idx = 0; - } else { + else new_idx = idx + skip_param; - } + _prop_set_current_integer(dev_prop, new_idx); return MTP_ERROR_NONE; } @@ -2399,9 +2383,8 @@ mtp_err_t _hutil_format_storage(mtp_uint32 store_id, mtp_uint32 fs_format) } error = _entity_format_store(store, fs_format); - if (error == PTP_RESPONSE_OK) { + if (error == PTP_RESPONSE_OK) return MTP_ERROR_NONE; - } ERR("Format store Fail"); return error; diff --git a/src/mtp_event_handler.c b/src/mtp_event_handler.c index ccab66a..fa549c8 100755 --- a/src/mtp_event_handler.c +++ b/src/mtp_event_handler.c @@ -168,10 +168,10 @@ mtp_bool _eh_handle_usb_events(mtp_uint32 type) DBG("USB disconnected but temp file is remaind.\ It will be deleted."); - if (g_mgr->ftemp_st.fhandle != INVALID_FILE) { + if (g_mgr->ftemp_st.fhandle != NULL) { DBG("handle is found. At first close file"); _util_file_close(g_mgr->ftemp_st.fhandle); - g_mgr->ftemp_st.fhandle = INVALID_FILE; + g_mgr->ftemp_st.fhandle = NULL; } if (remove(g_mgr->ftemp_st.filepath) < 0) { ERR_SECURE("remove(%s) Fail", g_mgr->ftemp_st.filepath); @@ -261,7 +261,7 @@ static void *__thread_event_handler(void *arg) while (flag) { mtp_int32 status = 0; status = read(g_pipefd[0], &evt, sizeof(mtp_event_t)); - if(( status== -1) && errno == EINTR) { + if ((status == -1) && errno == EINTR) { ERR("read() Fail"); continue; } @@ -380,9 +380,8 @@ void _handle_lock_status_notification(keynode_t *key, void *data) previous_val = _util_get_local_lock_status(); _util_get_lock_status(¤t_val); - if (previous_val == current_val) { + if (previous_val == current_val) return; - } _util_set_local_lock_status(current_val); @@ -450,7 +449,7 @@ void _eh_send_event_req_to_eh_thread(event_code_t action, mtp_ulong param1, DBG("action[%d], param1[%ld], param2[%ld]\n", action, param1, param2); status = write(g_pipefd[1], &event, sizeof(mtp_event_t)); - if(status== -1 || errno == EINTR) { + if (status == -1 || errno == EINTR) { ERR("Event write over pipe Fail, status = [%d], pipefd = [%d], errno [%d]\n", status, g_pipefd[1], errno); } @@ -470,7 +469,7 @@ static mtp_bool __send_start_event_to_eh_thread(void) DBG("Action : START MTP OPERATION"); status = write(g_pipefd[1], &event, sizeof(mtp_event_t)); - if(status== -1 || errno == EINTR) { + if (status == -1 || errno == EINTR) { ERR("Event write over pipe Fail, status= [%d],pipefd = [%d], errno [%d]\n", status, g_pipefd[1], errno); return FALSE; diff --git a/src/mtp_init.c b/src/mtp_init.c index 981bfe4..1d79d17 100755 --- a/src/mtp_init.c +++ b/src/mtp_init.c @@ -69,9 +69,9 @@ static void __mtp_exit(void) DBG("## Terminate all threads"); if (g_eh_thrd) { _eh_send_event_req_to_eh_thread(EVENT_USB_REMOVED, 0, 0, NULL); - if (_util_thread_join(g_eh_thrd, NULL) == FALSE) { + if (_util_thread_join(g_eh_thrd, NULL) == FALSE) ERR("_util_thread_join() Fail"); - } + g_eh_thrd = 0; } @@ -87,7 +87,7 @@ static void __mtp_exit(void) return; } -static gboolean __check_internal_storage (gpointer user_data) +static gboolean __check_internal_storage(gpointer user_data) { _handle_lock_status_notification(NULL, NULL); @@ -322,9 +322,8 @@ static void __read_mtp_conf(void) *token = '\0'; token = strtok_r(buf, "=", &saveptr); - if (token == NULL) { + if (token == NULL) continue; - } if (strcasecmp(token, "mmap_threshold") == 0) { token = strtok_r(NULL, "=", &saveptr); @@ -421,11 +420,10 @@ void _features_supported_info(void) DBG("***********************************************************"); DBG("### Extension ###"); - if (_get_oma_drm_status() == TRUE) { + if (_get_oma_drm_status() == TRUE) DBG("### 2. OMADRM : [ON]"); - } else { + else DBG("### 2. OMADRM : [OFF]"); - } DBG("***********************************************************"); DBG("### Feature ###"); diff --git a/src/mtp_inoti_handler.c b/src/mtp_inoti_handler.c index 47faeaa..66980f3 100755 --- a/src/mtp_inoti_handler.c +++ b/src/mtp_inoti_handler.c @@ -100,11 +100,10 @@ void *_thread_inoti(void *arg) event = (struct inotify_event *)(&buffer[i]); __process_inoti_event(event); temp_idx = i + event->len + INOTI_EVENT_SIZE; - if (temp_idx > length) { + if (temp_idx > length) break; - } else { + else i = temp_idx; - } } } @@ -124,11 +123,10 @@ void _inoti_add_watch_for_fs_events(mtp_char *path) /* find empty cell */ for (i = 0; i < INOTI_FOLDER_COUNT_MAX; i++) { /* If not empty */ - if (g_inoti_watches[i].wd != 0) { + if (g_inoti_watches[i].wd != 0) continue; - } else { + else break; - } } if (i == INOTI_FOLDER_COUNT_MAX) { @@ -319,9 +317,8 @@ void _inoti_deinit_filesystem_events() return; } - if (_util_thread_join(g_inoti_thrd, 0) == FALSE) { + if (_util_thread_join(g_inoti_thrd, 0) == FALSE) ERR("_util_thread_join() Fail"); - } return; } @@ -331,13 +328,11 @@ static void __remove_inoti_watch(mtp_char *path) mtp_int32 i = 0; for (i = 0; i < g_cnt_watch_folder; i++) { - if (g_inoti_watches[i].forlder_name == NULL) { + if (g_inoti_watches[i].forlder_name == NULL) continue; - } - if (g_strcmp0(g_inoti_watches[i].forlder_name, path) != 0) { + if (g_strcmp0(g_inoti_watches[i].forlder_name, path) != 0) continue; - } g_free(g_inoti_watches[i].forlder_name); g_inoti_watches[i].forlder_name = NULL; @@ -399,17 +394,15 @@ static open_files_info_t *__find_file_in_inoti_open_files_list(mtp_int32 wd, static void __remove_file_from_inoti_open_files_list(open_files_info_t *node) { - if (NULL != node->previous) { + if (NULL != node->previous) node->previous->next = node->next; - } - if (NULL != node->next) { + if (NULL != node->next) node->next->previous = node->previous; - } - if (node == g_open_files_list) { + if (node == g_open_files_list) g_open_files_list = node->previous; - } + g_free(node->name); g_free(node); @@ -568,8 +561,7 @@ static void __process_object_added_event(mtp_char *fullpath, _util_get_internal_path(inter_path); if (!g_strcmp0(parent_path, inter_path) || - !g_strcmp0(parent_path, ext_path)) - { + !g_strcmp0(parent_path, ext_path)) { DBG("parent is the root folder"); h_parent = 0; } else { @@ -678,9 +670,8 @@ static void __process_object_deleted_event(mtp_char *fullpath, } } - if (TRUE == isdir) { + if (TRUE == isdir) __delete_children_from_store_inoti(store, obj); - } node = _util_delete_node(&(store->obj_list), obj); g_free(node); @@ -698,13 +689,12 @@ static void __destroy_inoti_open_files_list() ret_if(g_open_files_list == NULL); - while(g_open_files_list) { + while (g_open_files_list) { current = g_open_files_list; g_open_files_list = g_open_files_list->previous; - if (g_open_files_list) { + if (g_open_files_list) g_open_files_list->next = NULL; - } g_free(current->name); current->wd = 0; diff --git a/src/ptp_container.c b/src/ptp_container.c index d9a8aa8..5143569 100755 --- a/src/ptp_container.c +++ b/src/ptp_container.c @@ -37,9 +37,9 @@ void _hdlr_init_cmd_container(cmd_container_t *cntr) mtp_uint32 _hdlr_get_param_cmd_container(cmd_container_t *cntr, mtp_uint32 index) { - if (index < cntr->no_param) { + if (index < cntr->no_param) return cntr->params[index]; - } + return 0; } @@ -55,9 +55,9 @@ void _hdlr_copy_cmd_container_unknown_params(cmd_container_t *src, dst->no_param = (src->len - sizeof(header_container_t)) / sizeof(mtp_uint32); - for (ii = 0; ii < dst->no_param; ii++) { + for (ii = 0; ii < dst->no_param; ii++) dst->params[ii] = src->params[ii]; - } + return; } @@ -71,9 +71,8 @@ void _hdlr_copy_cmd_container(cmd_container_t *src, cmd_container_t *dst) dst->len = src->len; dst->no_param = src->no_param; - for (ii = 0; ii < dst->no_param; ii++) { + for (ii = 0; ii < dst->no_param; ii++) dst->params[ii] = src->params[ii]; - } return; } diff --git a/src/transport/mtp_transport.c b/src/transport/mtp_transport.c index 2e382c3..3eee08b 100755 --- a/src/transport/mtp_transport.c +++ b/src/transport/mtp_transport.c @@ -66,14 +66,14 @@ void _transport_save_cmd_buffer(mtp_char *buffer, mtp_uint32 size) mtp_err_t _transport_rcv_temp_file_data(mtp_byte *buffer, mtp_uint32 size, mtp_uint32 *count) { - mtp_uint32 h_file = INVALID_FILE; + FILE* h_file = NULL; mtp_int32 error = 0; mtp_uint32 data_sz; h_file = _util_file_open(g_mgr->ftemp_st.filepath, MTP_FILE_READ, &error); - if (h_file == INVALID_FILE) { + if (h_file == NULL) { DBG_SECURE("_util_file_open(%s) Fail", g_mgr->ftemp_st.filepath); return MTP_ERROR_NONE; } @@ -133,7 +133,7 @@ mtp_err_t _transport_rcv_temp_file_info(mtp_byte *buf, char *filepath, g_strlcpy(g_mgr->ftemp_st.filepath, MTP_TEMP_FILE_DEFAULT, MTP_MAX_PATHNAME_SIZE + 1); - g_mgr->ftemp_st.fhandle = INVALID_FILE; + g_mgr->ftemp_st.fhandle = NULL; g_mgr->ftemp_st.file_size = 0; return MTP_ERROR_NONE; @@ -421,14 +421,12 @@ void _transport_usb_finalize(void) } res = _util_thread_join(g_data_rcv, &th_result); - if (res == FALSE) { + if (res == FALSE) ERR("_util_thread_join(data_rcv) Fail"); - } } - if (_transport_mq_deinit(&g_usb_to_mtp_mqid, &mtp_to_usb_mqid) == FALSE) { + if (_transport_mq_deinit(&g_usb_to_mtp_mqid, &mtp_to_usb_mqid) == FALSE) ERR("_transport_mq_deinit() Fail"); - } _transport_deinit_usb_device(); @@ -442,7 +440,7 @@ static void *__transport_thread_data_rcv(void *func) mtp_uint32 pkt_len = 0; mtp_int32 flag = 1; mtp_int32 len = 0; - _cmd_handler_cb _cmd_handler_func = (_cmd_handler_cb )func; + _cmd_handler_cb _cmd_handler_func = (_cmd_handler_cb)func; while (flag) { if (_util_msgq_receive(g_usb_to_mtp_mqid, (void *)&pkt, diff --git a/src/transport/mtp_usb_driver.c b/src/transport/mtp_usb_driver.c index 91a9b73..e392bb9 100755 --- a/src/transport/mtp_usb_driver.c +++ b/src/transport/mtp_usb_driver.c @@ -337,9 +337,8 @@ static void __handle_control_request(mtp_int32 request) DBG("USB_PTPREQUEST_RESET"); _reset_mtp_device(); - if (kernel_reset == FALSE) { + if (kernel_reset == FALSE) kernel_reset = TRUE; - } status = ioctl(g_usb_fd, MTP_SEND_RESET_ACK, NULL); if (status < 0) { @@ -385,11 +384,10 @@ static void __handle_control_request(mtp_int32 request) statusreq_data.code = PTP_RESPONSE_GEN_ERROR; } - if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) { + if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) sent_busy = TRUE; - } else { + else sent_busy = FALSE; - } status = ioctl(g_usb_fd, MTP_SET_SETUP_DATA, &statusreq_data); if (status < 0) { @@ -431,20 +429,18 @@ mtp_bool _transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) if (*rx_mqid) { res = _util_msgq_deinit(rx_mqid); - if (res == FALSE) { + if (res == FALSE) ERR("rx_mqid deinit Fail [%d]\n", errno); - } else { + else *rx_mqid = 0; - } } if (*tx_mqid) { res = _util_msgq_deinit(tx_mqid); - if (res == FALSE) { + if (res == FALSE) ERR("tx_mqid deinit fail [%d]\n", errno); - } else { + else *tx_mqid = 0; - } } return res; @@ -464,9 +460,8 @@ mtp_uint32 _transport_get_usb_packet_len(void) } } - if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) { + if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) return MTP_MAX_PACKET_SIZE_SEND_FS; - } return MTP_MAX_PACKET_SIZE_SEND_HS; } diff --git a/src/util/mtp_fs.c b/src/util/mtp_fs.c index ba9e3b2..b206f9b 100755 --- a/src/util/mtp_fs.c +++ b/src/util/mtp_fs.c @@ -46,7 +46,7 @@ * @return This function returns the file handle on success, or INVALID_FILE on failure */ -mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, +FILE* _util_file_open(const mtp_char *filename, file_mode_t mode, mtp_int32 *error) { #ifdef __USE_STDIO__ @@ -77,7 +77,7 @@ mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, default: ERR("Invalid mode : %d\n", mode); *error = EINVAL; - return INVALID_FILE; + return NULL; } fhandle = fopen(filename, fmode); @@ -85,12 +85,12 @@ mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, ERR("File open Fail:mode[0x%x], errno [%d]\n", mode, errno); ERR_SECURE("filename[%s]\n", filename); *error = errno; - return INVALID_FILE; + return NULL; } fcntl(fileno(fhandle), F_SETFL, O_NOATIME); - return (mtp_uint32)fhandle; + return fhandle; #else /* __USE_STDIO__ */ @@ -125,7 +125,7 @@ mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, default: ERR("Invalid mode : %d\n", mode); *error = EINVAL; - return INVALID_FILE; + return NULL; } if (perm) @@ -137,10 +137,10 @@ mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, ERR("File open Fail:mode[0x%x], errno [%d]\n", mode, errno); ERR_SECURE("filename[%s]\n", filename); *error = errno; - return INVALID_FILE; + return NULL; } - return (mtp_uint32)fhandle; + return fhandle; #endif /* __USE_STDIO__ */ } @@ -156,13 +156,13 @@ mtp_uint32 _util_file_open(const mtp_char *filename, file_mode_t mode, * @param[out] preadcount Will store the actual num bytes read. * @return None */ -void _util_file_read(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size, +void _util_file_read(FILE* fhandle, void *bufptr, mtp_uint32 size, mtp_uint32 *read_count) { mtp_uint32 bytes_read = 0; #ifdef __USE_STDIO__ - bytes_read = fread_unlocked(bufptr, sizeof(mtp_char), size, (FILE *)fhandle); + bytes_read = fread_unlocked(bufptr, sizeof(mtp_char), size, fhandle); #else /* __USE_STDIO__ */ bytes_read = read(fhandle, bufptr, size); #endif /* __USE_STDIO__ */ @@ -180,12 +180,12 @@ void _util_file_read(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size, * @return This function returns num bytes written. */ -mtp_uint32 _util_file_write(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size) +mtp_uint32 _util_file_write(FILE* fhandle, void *bufptr, mtp_uint32 size) { mtp_uint32 bytes_written = 0; #ifdef __USE_STDIO__ - bytes_written = fwrite_unlocked(bufptr, sizeof(mtp_char), size, (FILE *)fhandle); + bytes_written = fwrite_unlocked(bufptr, sizeof(mtp_char), size, fhandle); #else /* __USE_STDIO__ */ mtp_int32 ret = 0; @@ -206,10 +206,10 @@ mtp_uint32 _util_file_write(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size) * @param[in] handle Specifies the handle of file to close. * @return 0 in case of success or EOF on failure. */ -mtp_int32 _util_file_close(mtp_uint32 fhandle) +mtp_int32 _util_file_close(FILE* fhandle) { #ifdef __USE_STDIO__ - return fclose((FILE *)fhandle); + return fclose(fhandle); #else /* __USE_STDIO__ */ return close(fhandle); #endif /* __USE_STDIO__ */ @@ -223,12 +223,12 @@ mtp_int32 _util_file_close(mtp_uint32 fhandle) * @param[in] whence Specifies the setting value * @return Returns TRUE in case of success or FALSE on Failure. */ -mtp_bool _util_file_seek(mtp_uint32 handle, off_t offset, mtp_int32 whence) +mtp_bool _util_file_seek(FILE* handle, off_t offset, mtp_int32 whence) { mtp_int64 ret_val = 0; #ifdef __USE_STDIO__ - ret_val = fseek((FILE *)handle, offset, whence); + ret_val = fseek(handle, offset, whence); #else /* __USE_STDIO__ */ ret_val = lseek(handle, offset, whence); if (ret_val > 0) @@ -521,7 +521,7 @@ mtp_int32 _util_remove_dir_children_recursive(const mtp_char *dirname, } if (MTP_ERROR_OBJECT_WRITE_PROTECTED == ret) { DBG("Folder[%s] contains read-only files,hence\ - folder is not deleted\n",pathname); + folder is not deleted\n", pathname); /* Read the next entry */ goto DONE; } @@ -543,7 +543,7 @@ mtp_int32 _util_remove_dir_children_recursive(const mtp_char *dirname, (S_IWGRP & entryinfo.st_mode) || (S_IWOTH & entryinfo.st_mode))) { ret = MTP_ERROR_OBJECT_WRITE_PROTECTED; - DBG("File [%s] is readOnly:Deletion Fail\n",pathname); + DBG("File [%s] is readOnly:Deletion Fail\n", pathname); goto DONE; } } @@ -722,9 +722,8 @@ mtp_bool _util_ifind_next(mtp_char *dir_name, DIR *dirp, dir_entry_t *dir_info) switch (stat_buf.st_mode & S_IFMT) { case S_IFREG: dir_info->type = MTP_FILE_TYPE; - if (!(stat_buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))) { + if (!(stat_buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))) dir_info->attrs.attribute |= MTP_FILE_ATTR_MODE_READ_ONLY; - } break; case S_IFDIR: @@ -779,9 +778,9 @@ mtp_bool _util_get_filesystem_info(mtp_char *storepath, fs_info_t *fs_info) return TRUE; } -void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines) +void _util_count_num_lines(FILE* fhandle, mtp_uint32 *num_lines) { - if (fhandle == INVALID_FILE) + if (fhandle == NULL) return; mtp_uint32 line_count = 0; @@ -797,8 +796,8 @@ void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines) } #ifdef __USE_STDIO__ - while((read_bytes = getline(&buffer, - &line_max_length, (FILE *)fhandle)) != -1) { + while ((read_bytes = getline(&buffer, + (size_t *)&line_max_length, fhandle)) != -1) { if (read_bytes > MTP_MAX_PATHNAME_SIZE + 1) continue; line_count++; @@ -809,7 +808,7 @@ void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines) mtp_uint32 prev_pos = -1; mtp_uint32 new_pos; mtp_uint32 filename_len = 0; - while((read_bytes = read(fhandle, buffer, LINUX_MAX_PATHNAME_LENGTH)) > 0) { + while ((read_bytes = read(fhandle, buffer, LINUX_MAX_PATHNAME_LENGTH)) > 0) { for (ii = 0; ii < read_bytes; ii++) { if (buffer[ii] != '\n') continue; @@ -820,9 +819,9 @@ void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines) continue; line_count++; } - if (buffer[read_bytes - 1] != '\n') { + if (buffer[read_bytes - 1] != '\n') _util_file_seek(fhandle, prev_pos + 1 - read_bytes, SEEK_CUR); - } + prev_pos = -1; } #endif /* __USE_STDIO__ */ @@ -833,7 +832,7 @@ void _util_count_num_lines(mtp_uint32 fhandle, mtp_uint32 *num_lines) } void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, - mtp_uint32 fhandle, mtp_uint32 size) + FILE* fhandle, mtp_uint32 size) { ptp_array_t *pguidarray = NULL; mtp_uint32 *guidptr = NULL; @@ -855,7 +854,7 @@ void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, } #ifdef __USE_STDIO__ - while ((len = getline(&buffer, &line_max_length, (FILE *)fhandle)) != -1 && + while ((len = getline(&buffer, (size_t *)&line_max_length, fhandle)) != -1 && (num_lines - start_index) <= size) { if (len > MTP_MAX_PATHNAME_SIZE + 1) continue; @@ -877,7 +876,7 @@ void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, mtp_char file_name[MTP_MAX_PATHNAME_SIZE + 1]; mtp_int32 read_bytes; - while ((read_bytes = read(fHandle, buffer, + while ((read_bytes = read(fhandle, buffer, LINUX_MAX_PATHNAME_LENGTH)) > 0 && (num_lines - start_index) <= size) { @@ -902,9 +901,9 @@ void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, pguidarray->num_elements += sizeof(mtp_uint32); } - if (buffer[read_bytes - 1] != '\n') { + if (buffer[read_bytes - 1] != '\n') _util_file_seek(fhandle, prev_pos + 1 - read_bytes, SEEK_CUR); - } + prev_pos = -1; } #endif /* __USE_STDIO__ */ @@ -935,9 +934,8 @@ void FLOGD(const char *fmt, ...) fp = fopen(MTP_LOG_FILE, "a+"); } - if (fp == NULL) { + if (fp == NULL) return; - } written_bytes += fprintf(fp, "%s ", __FILE__); va_start(ap, fmt); diff --git a/src/util/mtp_list.c b/src/util/mtp_list.c index 1ba7050..4bf2e15 100755 --- a/src/util/mtp_list.c +++ b/src/util/mtp_list.c @@ -67,9 +67,8 @@ slist_node_t* _util_delete_node(slist_t *l_ptr, void *data) slist_node_t *nptr = l_ptr->start; slist_node_t *temp = NULL; - if (nptr->value == data) { + if (nptr->value == data) return __util_del_first_node(l_ptr); - } while (nptr->link) { if (nptr->link->value == data) @@ -99,9 +98,8 @@ static slist_node_t *__util_del_first_node(slist_t *l_ptr) temp = l_ptr->start; l_ptr->nnodes -= 1; l_ptr->start = temp->link; - if (temp == l_ptr->end) { + if (temp == l_ptr->end) l_ptr->end = NULL; - } return temp; } diff --git a/src/util/mtp_media_info.c b/src/util/mtp_media_info.c index 0cdda55..6e7b34f 100755 --- a/src/util/mtp_media_info.c +++ b/src/util/mtp_media_info.c @@ -37,9 +37,8 @@ static bool __fill_media_id_cb(media_info_h media, void *user_data) static void __scan_folder_cb(media_content_error_e err, void *user_data) { - if (err != MEDIA_CONTENT_ERROR_NONE) { + if (err != MEDIA_CONTENT_ERROR_NONE) ERR("Scan folder callback returns error = [%d]\n", err); - } return; } @@ -115,7 +114,7 @@ mtp_bool _util_get_audio_metadata(const mtp_char *filepath, goto ERROR_EXIT; } - ret =audio_meta_get_composer(audio, &(audio_data->commonmeta.author)); + ret = audio_meta_get_composer(audio, &(audio_data->commonmeta.author)); if (ret != MEDIA_CONTENT_ERROR_NONE) { ERR("METADATA_AUTHOR Fail"); goto ERROR_EXIT; @@ -366,7 +365,7 @@ static media_info_h __util_find_media_info(mtp_char *condition) ERR("media_info_foreach_media_from_db() Fail"); break; } - }while (0); + } while (0); media_filter_destroy(filter); @@ -830,9 +829,8 @@ void _util_scan_folder_contents_in_db(const mtp_char *filepath) ret_if(filepath == NULL); ret = media_content_scan_folder(filepath, true, __scan_folder_cb, NULL); - if (ret != MEDIA_CONTENT_ERROR_NONE) { + if (ret != MEDIA_CONTENT_ERROR_NONE) ERR("media_content_scan_folder Fail : %d\n", ret); - } return; } diff --git a/src/util/mtp_msgq.c b/src/util/mtp_msgq.c index ad98fa5..aa4b5c6 100755 --- a/src/util/mtp_msgq.c +++ b/src/util/mtp_msgq.c @@ -49,11 +49,10 @@ mtp_bool _util_msgq_receive(msgq_id_t mq_id, void *buf, mtp_uint32 size, { int ret = 0; - if (flags == 1) { + if (flags == 1) ret = msgrcv(mq_id, buf, size, 0, IPC_NOWAIT); - } else { + else ret = msgrcv(mq_id, buf, size, 0, 0); - } if (ret == -1) { ERR("msgrcv() Fail"); diff --git a/src/util/mtp_support.c b/src/util/mtp_support.c index 144c2cd..83108d6 100755 --- a/src/util/mtp_support.c +++ b/src/util/mtp_support.c @@ -64,9 +64,8 @@ void _util_conv_byte_order_gen_str(void *str, mtp_int32 size, mtp_int32 elem_sz) ret_if(str == NULL); retm_if(elem_sz <= 1, "elem_sz(%d) is invalid", elem_sz); - for (idx = 0; idx < f_size; idx += elem_sz) { + for (idx = 0; idx < f_size; idx += elem_sz) _util_conv_byte_order(&(l_str[idx]), elem_sz); - } return; } @@ -145,13 +144,12 @@ void _util_wchar_cpy(mtp_wchar *dest, const mtp_wchar *src) ret_if(src == NULL); ret_if(dest == NULL); - if (!((int)dest & 0x1) && !((int)src & 0x1)){ + if (!((int)dest & 0x1) && !((int)src & 0x1)) { /* 2-byte aligned */ mtp_wchar *temp = dest; - while ((*temp++ = *src++) != '\0') { + while ((*temp++ = *src++) != '\0') ; /* DO NOTHING */ - } } else { /* not-aligned, byte to byte approach - slow */ mtp_char *pc1 = (mtp_char *)dest; @@ -187,14 +185,12 @@ void _util_wchar_ncpy(mtp_wchar *dest, const mtp_wchar *src, unsigned long n) if (!((int)dest & 0x1) && !((int)src & 0x1)) { /* 2-byte aligned */ temp = dest; - while (n && (*temp++ = *src++)) { + while (n && (*temp++ = *src++)) n--; - } if (n) { - while (--n) { + while (--n) *temp++ = 0; - } } } else { /* not-aligned, byte to byte approach - slow */ @@ -228,9 +224,8 @@ size_t _util_wchar_len(const mtp_wchar *s) if (!((int)s & 0x1)) { /* 2-byte aligned */ mtp_wchar *temp = (mtp_wchar *)s; - while (*temp++) { + while (*temp++) /* DO NOTHING */ ; - } DBG("Length : %d\n", temp - s - 1); return ((size_t)(temp - s - 1)); @@ -238,9 +233,8 @@ size_t _util_wchar_len(const mtp_wchar *s) unsigned char *temp = (unsigned char *)s; - while (*temp || *(temp + 1)) { + while (*temp || *(temp + 1)) temp += 2; - } DBG("Length : %d\n", (temp - (unsigned char *)s) / 2); return ((size_t) (temp - (unsigned char *)s) / 2); @@ -258,9 +252,9 @@ static mtp_char* __util_conv_int_to_hex_str(mtp_int32 int_val, mtp_char *str) *nstr++ = '0'; *nstr++ = 'x'; - for (val = int_val; val; val <<= 4) { + for (val = int_val; val; val <<= 4) *nstr++ = hex[(val >> (sizeof(int) * 8 - 4)) & 0xF]; - } + *nstr = '\0'; return str; } @@ -411,7 +405,7 @@ mtp_bool _util_get_file_name(const mtp_char *fullpath, mtp_char *f_name) i = strlen(fullpath); - for (j=0; i >= 0; i--) { + for (j = 0; i >= 0; i--) { if (fullpath[i] == '/') { g_strlcpy(f_name, &fullpath[i + 1], j); return TRUE; @@ -491,8 +485,7 @@ mtp_bool _util_is_path_len_valid(const mtp_char *path) root_path_len = internal_store_len; } else if (!strncmp(path, ext_path, external_store_len)) { root_path_len = external_store_len; - } - else { + } else { ERR("Unknown store's path : %s\n", path); return FALSE; } @@ -576,9 +569,8 @@ void _util_conv_wstr_to_guid(mtp_wchar *wstr, mtp_uint64 *guid) ret_if(wstr == NULL); ret_if(guid == NULL); - while (wstr[count] != 0) { + while (wstr[count] != 0) count++; - } memset(guid, 0, sizeof(temp)); skip_idx = sizeof(temp) / sizeof(mtp_wchar); @@ -587,9 +579,9 @@ void _util_conv_wstr_to_guid(mtp_wchar *wstr, mtp_uint64 *guid) memset(temp, 0, sizeof(temp)); cpy_sz = (count - cur_idx) * sizeof(mtp_wchar); - if (cpy_sz > sizeof(temp)) { + if (cpy_sz > sizeof(temp)) cpy_sz = sizeof(temp); - } + memcpy(temp, &(wstr[cur_idx]), cpy_sz); guid[0] += temp[0]; guid[1] += temp[1]; @@ -637,9 +629,8 @@ mtp_bool _util_get_unique_dir_path(const mtp_char *exist_path, while (val < max_value) { /* Including NUL and '_' */ g_snprintf(&buf[len], num_bytes + 2, "_%u", val++); - if (access(buf, F_OK) < 0) { + if (access(buf, F_OK) < 0) goto SUCCESS; - } } g_free(buf); diff --git a/src/util/mtp_thread.c b/src/util/mtp_thread.c index e1f48e3..e96d111 100755 --- a/src/util/mtp_thread.c +++ b/src/util/mtp_thread.c @@ -46,15 +46,14 @@ mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname, error = pthread_create(tid, &attr, thread_func, arg); if (error != 0) { - ERR( "[%s] Thread creation Fail errno [%d]\n", tname, errno); + ERR("[%s] Thread creation Fail errno [%d]\n", tname, errno); pthread_attr_destroy(&attr); return FALSE; } error = pthread_attr_destroy(&attr); - if (error != 0) { + if (error != 0) ERR("pthread_attr_destroy Fail [%d] errno [%d]\n", error, errno); - } return TRUE; } diff --git a/src/util/mtp_util.c b/src/util/mtp_util.c index b9c8e18..a039945 100755 --- a/src/util/mtp_util.c +++ b/src/util/mtp_util.c @@ -51,9 +51,9 @@ mtp_int32 _util_get_battery_level(void) result = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_level); - if (result != 0) { + if (result != 0) ERR("VCONFKEY_SYSMAN_BATTERY_CAPACITY Fail!"); - } + return battery_level; } @@ -101,9 +101,8 @@ mtp_bool _util_get_serial(mtp_char *serial, mtp_uint32 len) gcry_md_hash_buffer(GCRY_MD_MD5, hash_value, imei_no, strlen(imei_no)); - for (i = 0; i < MD5_HASH_LEN; i++) { + for (i = 0; i < MD5_HASH_LEN; i++) g_snprintf(&serial[i*2], 3, "%02X", hash_value[i]); - } if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1) { ERR("vconf_set Fail for %s\n", @@ -209,9 +208,8 @@ void _util_gen_alt_serial(mtp_char *serial, mtp_uint32 len) g_snprintf(serial, len, "%s-%010ld-%011ld", model_name, st.tv_sec, st.tv_usec); - if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1) { + if (vconf_set_str(VCONFKEY_MTP_SERIAL_NUMBER_STR, serial) == -1) ERR("vconf_set Fail %s\n", VCONFKEY_MTP_SERIAL_NUMBER_STR); - } return; } @@ -335,11 +333,11 @@ static bool _util_device_external_supported_cb(int storage_id, storage_type_e ty { char *storage_path = (char *)user_data; - DBG("storage id: %d, path: %s", storage_id, path); + //DBG("storage id: %d, path: %s", storage_id, path); if (type == STORAGE_TYPE_EXTERNAL && path != NULL) { strncpy(storage_path, path, strlen(path)); - DBG("external storage path : %s", storage_path); + //DBG("external storage path : %s", storage_path); } return TRUE; @@ -361,17 +359,17 @@ static bool _util_device_internal_supported_cb(int storage_id, storage_type_e ty { char *storage_path = (char *)user_data; - DBG("storage id: %d, path: %s", storage_id, path); + //DBG("storage id: %d, path: %s", storage_id, path); if (type == STORAGE_TYPE_INTERNAL && path != NULL) { strncpy(storage_path, path, strlen(path)); - DBG("internal storage path : %s", storage_path); + //DBG("internal storage path : %s", storage_path); if (storage_get_root_directory(storage_id, &storage_path) != STORAGE_ERROR_NONE) { ERR("get internal storage path Fail"); return FALSE; } else { - DBG("get internal storage path : %s", storage_path); + //DBG("get internal storage path : %s", storage_path); } } -- 2.7.4 From 93185a26f3c47822348bcec9fc6ea3d93e177e72 Mon Sep 17 00:00:00 2001 From: VBS Date: Wed, 29 Mar 2017 18:47:31 +0900 Subject: [PATCH 02/16] Sync with 3.0 : Ver. 0.0.19 to 0.0.22 Signed-off-by: Jihoon Jung Change-Id: I2b6a00e7a2ef25f6737adbef94d0b49d51409e6d --- packaging/mtp-responder.spec | 4 ++-- src/mtp_cmd_handler.c | 18 ++++++++++++++++-- src/mtp_cmd_handler_util.c | 6 ++++++ src/util/mtp_fs.c | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index d292281..bf0299d 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -5,7 +5,7 @@ ExcludeArch: %arm aarch64 Name: mtp-responder Summary: Media Transfer Protocol daemon (responder) -Version: 0.0.19 +Version: 0.0.22 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 @@ -57,10 +57,10 @@ ln -sf %{_unitdir}/mtp-responder.service %{_sysconfdir}/systemd/default-extra-de %files %manifest mtp-responder.manifest +%license LICENSE.APLv2 %defattr(-,root,root,-) %{_bindir}/mtp-responder %{_unitdir}/mtp-responder.service %{_prefix}/lib/udev/rules.d/99-mtp-responder.rules /opt/var/lib/misc/mtp-responder.conf %{upgrade_script_path}/500.%{name}-upgrade.sh -#%license LICENSE.APLv2 diff --git a/src/mtp_cmd_handler.c b/src/mtp_cmd_handler.c index acaa3e4..4e20d44 100755 --- a/src/mtp_cmd_handler.c +++ b/src/mtp_cmd_handler.c @@ -737,6 +737,7 @@ static void __get_object(mtp_handler_t *hdlr) if (NULL == ptr) { ERR("_hdlr_alloc_buf_data_container() Fail"); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -748,9 +749,11 @@ static void __get_object(mtp_handler_t *hdlr) if (EACCES == error) { _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_ACCESSDENIED); + g_free(blk.data); return; } _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -1252,6 +1255,7 @@ static void __get_device_prop_desc(mtp_handler_t *hdlr) if (ptr == NULL) { resp = PTP_RESPONSE_GEN_ERROR; _cmd_hdlr_send_response_code(hdlr, resp); + g_free(blk.data); return; } @@ -1299,6 +1303,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) if (ptr == NULL) { _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } memcpy(ptr, &batt, no_bytes); @@ -1329,6 +1334,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) if (ptr == NULL) { _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -1361,6 +1367,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) if (ptr == NULL) { _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -1429,6 +1436,7 @@ static void __get_device_prop_value(mtp_handler_t *hdlr) if (ptr == NULL) { _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); g_free(data); _util_file_close(h_file); _prop_deinit_ptparray(&val_arr); @@ -1487,6 +1495,7 @@ static void __set_device_prop_value(mtp_handler_t *hdlr) if (FALSE == _hdlr_rcv_data_container(&blk, max_bytes)) { ERR("_hdlr_rcv_data_container() Fail"); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -1724,8 +1733,10 @@ static void __set_object_references(mtp_handler_t *hdlr) } ptr = _hdlr_get_payload_data(&blk); - if (ptr == NULL) + if (ptr == NULL) { + g_free(blk.data); return; + } memcpy(&num_ref, ptr, sizeof(mtp_uint32)); #ifdef __BIG_ENDIAN__ @@ -2682,6 +2693,7 @@ static void __get_interdep_prop_desc(mtp_handler_t *hdlr) num_bytes, fmt)) { ERR("_hutil_get_interdep_prop_config_list_data() Fail"); _cmd_hdlr_send_response_code(hdlr, PTP_RESPONSE_GEN_ERROR); + g_free(blk.data); return; } @@ -2791,8 +2803,10 @@ static void __send_object_prop_list(mtp_handler_t *hdlr) } data = _hdlr_get_payload_data(&blk); - if (data == NULL) + if (data == NULL) { + g_free(blk.data); return; + } ret = _hutil_construct_object_entry_prop_list(store_id, h_parent, fmt, f_size, ((hdlr->data4_send_obj.is_valid == TRUE) ? diff --git a/src/mtp_cmd_handler_util.c b/src/mtp_cmd_handler_util.c index 2e265af..8f821dd 100755 --- a/src/mtp_cmd_handler_util.c +++ b/src/mtp_cmd_handler_util.c @@ -1893,12 +1893,18 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, g_free(alb_buf); #endif /* MTP_SUPPORT_ALBUM_ART */ *obj_ptr = obj; + if (obj_info != NULL) + _entity_dealloc_obj_info(obj_info); + return MTP_ERROR_NONE; ERROR_EXIT: #ifdef MTP_SUPPORT_ALBUM_ART g_free(alb_buf); #endif /* MTP_SUPPORT_ALBUM_ART */ + if (obj_info != NULL) + _entity_dealloc_obj_info(obj_info); + return resp; } diff --git a/src/util/mtp_fs.c b/src/util/mtp_fs.c index b206f9b..4085c0e 100755 --- a/src/util/mtp_fs.c +++ b/src/util/mtp_fs.c @@ -923,7 +923,7 @@ void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index, */ void FLOGD(const char *fmt, ...) { - static int written_bytes = 0; + static mtp_int64 written_bytes = 0; FILE *fp = NULL; va_list ap; -- 2.7.4 From 6c7181ea696544d60126550ba38db3e7315ed58c Mon Sep 17 00:00:00 2001 From: Hyihong Chae Date: Tue, 11 Apr 2017 16:04:44 +0900 Subject: [PATCH 03/16] [Tizen 4.0 TV] fix svace issue. Change-Id: Iacc22afdb51a1f11839430f3d65b2e23bf16014b Signed-off-by: HyiHong Chae --- packaging/mtp-responder.spec | 2 +- src/entity/mtp_object.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index bf0299d..d333fb0 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -5,7 +5,7 @@ ExcludeArch: %arm aarch64 Name: mtp-responder Summary: Media Transfer Protocol daemon (responder) -Version: 0.0.22 +Version: 0.0.23 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/entity/mtp_object.c b/src/entity/mtp_object.c index 358af3c..d8279e5 100755 --- a/src/entity/mtp_object.c +++ b/src/entity/mtp_object.c @@ -507,6 +507,7 @@ mtp_bool _entity_check_child_obj_path(mtp_obj_t *obj, if (_util_is_file_opened(child_obj->file_path) == TRUE) { ERR_SECURE("File [%s] is already opened\n", child_obj->file_path); + _prop_deinit_ptparray(&child_arr); return FALSE; } -- 2.7.4 From 1fb7cbb7a3b4dec4c02c230817369dc91a9130ed Mon Sep 17 00:00:00 2001 From: Hyihong Chae Date: Mon, 24 Apr 2017 19:02:21 +0900 Subject: [PATCH 04/16] fix security issue(TSAM-13252) Change-Id: I99c089e88868edbad0532510be7e838e4d570f5f Signed-off-by: HyiHong Chae --- include/util/mtp_support.h | 1 + packaging/mtp-responder.spec | 2 +- src/entity/mtp_store.c | 6 ++++-- src/transport/mtp_transport.c | 1 + src/util/mtp_support.c | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/include/util/mtp_support.h b/include/util/mtp_support.h index b0cc9c4..a67325b 100755 --- a/include/util/mtp_support.h +++ b/include/util/mtp_support.h @@ -54,5 +54,6 @@ void _util_get_parent_path(const mtp_char *fullpath, mtp_char *p_path); void _util_conv_wstr_to_guid(mtp_wchar *wstr, mtp_uint64 *guid); mtp_bool _util_get_unique_dir_path(const mtp_char *exist_path, mtp_char *new_path, mtp_uint32 new_path_buf_len); +mtp_int32 _util_system_cmd_wait(const mtp_char *cmd); #endif /* _MTP_SUPPORT_H_ */ diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index d333fb0..8317525 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -5,7 +5,7 @@ ExcludeArch: %arm aarch64 Name: mtp-responder Summary: Media Transfer Protocol daemon (responder) -Version: 0.0.23 +Version: 0.0.24 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/entity/mtp_store.c b/src/entity/mtp_store.c index 7297d8b..c3ae8ac 100755 --- a/src/entity/mtp_store.c +++ b/src/entity/mtp_store.c @@ -1199,7 +1199,8 @@ void _entity_list_modified_files(mtp_uint32 minutes) inter_path, minutes, MTP_FILES_MODIFIED_FILES); DBG("find query is [%s]\n", command); - ret = system(command); + ret = _util_system_cmd_wait(command); + if (WIFSIGNALED(ret) && (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) { @@ -1215,7 +1216,8 @@ void _entity_list_modified_files(mtp_uint32 minutes) ext_path, minutes, MTP_FILES_MODIFIED_FILES); DBG("find query is [%s]\n", command); - ret = system(command); + ret = _util_system_cmd_wait(command); + if (WIFSIGNALED(ret) && (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) { diff --git a/src/transport/mtp_transport.c b/src/transport/mtp_transport.c index 3eee08b..7c73bbd 100755 --- a/src/transport/mtp_transport.c +++ b/src/transport/mtp_transport.c @@ -419,6 +419,7 @@ void _transport_usb_finalize(void) sizeof(msgq_ptr_t) - sizeof(long), 0)) { ERR("_util_msgq_send() Fail"); } + g_free(pkt.buffer); res = _util_thread_join(g_data_rcv, &th_result); if (res == FALSE) diff --git a/src/util/mtp_support.c b/src/util/mtp_support.c index 83108d6..8517146 100755 --- a/src/util/mtp_support.c +++ b/src/util/mtp_support.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "mtp_support.h" #include "ptp_datacodes.h" #include "mtp_util.h" @@ -643,3 +644,38 @@ SUCCESS: DBG_SECURE("Unique dir name[%s]\n", new_path); return TRUE; } + +mtp_int32 _util_system_cmd_wait(const mtp_char *cmd) +{ + + int pid = 0; + int status = 0; + + if (cmd == NULL) + return -1; + + pid = fork(); + + if (pid == -1) + return -1; + + if (pid == 0) { + char *argv[4]; + argv[0] = "sh"; + argv[1] = "-c"; + argv[2] = (char*)cmd; + argv[3] = 0; + execv("/bin/sh", argv); + exit(127); + } + + do { + if (waitpid(pid, &status, 0) == -1) { + if (errno != EINTR) + return -1; + } else { + return status; + } + } while (1); +} + -- 2.7.4 From ecf1e402b9c9f316fa6952265d8932e7f20fad49 Mon Sep 17 00:00:00 2001 From: Hyihong Chae Date: Tue, 30 May 2017 20:19:38 +0900 Subject: [PATCH 05/16] fix SENSYS issue. (TNEXT-744) Change-Id: Ia1fb37801da4e72306c9b1522156dda2547bdeec Signed-off-by: HyiHong Chae --- packaging/mtp-responder.spec | 2 +- src/util/mtp_thread.c | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index 8317525..4d27366 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -5,7 +5,7 @@ ExcludeArch: %arm aarch64 Name: mtp-responder Summary: Media Transfer Protocol daemon (responder) -Version: 0.0.24 +Version: 0.0.25 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/util/mtp_thread.c b/src/util/mtp_thread.c index e96d111..e4ba76e 100755 --- a/src/util/mtp_thread.c +++ b/src/util/mtp_thread.c @@ -76,9 +76,14 @@ mtp_bool _util_thread_cancel(pthread_t tid) { mtp_int32 res; + if (tid == 0) { + ERR("tid is NULL\n"); + return FALSE; + } + res = pthread_cancel(tid); if (res != 0) { - ERR("pthread_cancel Fail [%d] errno [%d]\n", tid, errno); + ERR("pthread_cancel Fail [%d] errno [%d]\n", tid, errno); return FALSE; } -- 2.7.4 From e37eb57d8c6adbf23cb646fadd62aa539d506937 Mon Sep 17 00:00:00 2001 From: "jh8801.jung" Date: Tue, 18 Jul 2017 20:02:24 +0900 Subject: [PATCH 06/16] Change daemon user owner to network_fw Signed-off-by: jh8801.jung Change-Id: I6306fc3b24cb4824251248c7f09b806a894a6faf --- CMakeLists.txt | 2 +- include/mtp_config.h | 4 +- include/util/mtp_util.h | 5 +++ mtp-responder.service | 5 +-- packaging/mtp-responder.spec | 1 + src/mtp_cmd_handler_util.c | 2 - src/mtp_event_handler.c | 4 +- src/mtp_init.c | 8 ++-- src/util/mtp_util.c | 100 +++++++++++++++++++++++++++++++++---------- 9 files changed, 95 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5dc968a..4e39cfd 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR}/src/util SRCS) INCLUDE(FindPkgConfig) pkg_check_modules(pkgs REQUIRED glib-2.0 capi-content-media-content - capi-media-metadata-extractor vconf dlog tapi capi-system-info storage) + capi-media-metadata-extractor vconf dlog tapi capi-system-info storage libsystemd-daemon) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/include/mtp_config.h b/include/mtp_config.h index d7052a9..eac1d2e 100755 --- a/include/mtp_config.h +++ b/include/mtp_config.h @@ -77,7 +77,7 @@ * Transport related configuration */ /* Internal Storage */ -#define MTP_STORE_PATH_CHAR "/opt/home/owner/content" +#define MTP_INTERNAL_PATH_CHAR "/opt/usr/home/" /* External Storage */ #define MTP_EXTERNAL_PATH_CHAR "/opt/media/SDCardA1" @@ -87,7 +87,7 @@ #define MTP_FILES_MODIFIED_FILES "/tmp/mtp_mod_files.log" /* Base MTP USER Directory */ -#define MTP_USER_DIRECTORY "/opt/usr/media/user" +#define MTP_USER_DIRECTORY "/opt/usr/home/owner/media" /* Hidden Root directory */ #define MTP_HIDDEN_PHONE ".HiddenPhone" diff --git a/include/util/mtp_util.h b/include/util/mtp_util.h index b0c7bb2..c5d56fd 100755 --- a/include/util/mtp_util.h +++ b/include/util/mtp_util.h @@ -25,6 +25,7 @@ #define LOG_TAG "MTP-RESPONDER" #endif /* LOG_TAG */ #include +#include #define FIND_CMD_LEN 300 #define FIND_CMD "/usr/bin/find %s \\( -iname '*.jpg' -o -iname '*.gif' " \ @@ -148,4 +149,8 @@ phone_status_t _util_get_local_lock_status(void); void _util_set_local_lock_status(const phone_status_t val); void _util_get_external_path(char *external_path); void _util_get_internal_path(char *internal_path); +mtp_bool _util_media_content_connect(); +void _util_media_content_disconnect(); + + #endif /* _MTP_UTIL_H_ */ diff --git a/mtp-responder.service b/mtp-responder.service index 8941a90..2019999 100755 --- a/mtp-responder.service +++ b/mtp-responder.service @@ -2,9 +2,8 @@ Description=MTP responder [Service] -User=owner -Group=users -SmackProcessLabel=System +User=network_fw +Group=network_fw SupplementaryGroups=priv_mediastorage priv_externalstorage Type=simple ExecStart=/usr/bin/mtp-responder diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index 4d27366..5edf60a 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -21,6 +21,7 @@ BuildRequires: pkgconfig(capi-content-media-content) BuildRequires: pkgconfig(capi-media-metadata-extractor) BuildRequires: pkgconfig(capi-system-info) Buildrequires: pkgconfig(storage) +BuildRequires: pkgconfig(libsystemd-daemon) Requires(post): /usr/bin/vconftool %define upgrade_script_path /usr/share/upgrade/scripts diff --git a/src/mtp_cmd_handler_util.c b/src/mtp_cmd_handler_util.c index 8f821dd..2b4d495 100755 --- a/src/mtp_cmd_handler_util.c +++ b/src/mtp_cmd_handler_util.c @@ -1893,8 +1893,6 @@ mtp_err_t _hutil_construct_object_entry_prop_list(mtp_uint32 store_id, g_free(alb_buf); #endif /* MTP_SUPPORT_ALBUM_ART */ *obj_ptr = obj; - if (obj_info != NULL) - _entity_dealloc_obj_info(obj_info); return MTP_ERROR_NONE; diff --git a/src/mtp_event_handler.c b/src/mtp_event_handler.c index fa549c8..0dbf921 100755 --- a/src/mtp_event_handler.c +++ b/src/mtp_event_handler.c @@ -390,14 +390,14 @@ void _handle_lock_status_notification(keynode_t *key, void *data) __send_events_from_device_to_pc(MTP_INTERNAL_STORE_ID, PTP_EVENTCODE_STOREADDED, 0, 0); - media_content_connect(); + _util_media_content_connect(); } else if (MTP_PHONE_LOCK_ON == current_val) { _device_uninstall_storage(MTP_ADDREM_INTERNAL); __send_events_from_device_to_pc(MTP_INTERNAL_STORE_ID, PTP_EVENTCODE_STOREREMOVED, 0, 0); - media_content_disconnect(); + _util_media_content_disconnect(); } return; diff --git a/src/mtp_init.c b/src/mtp_init.c index 1d79d17..2f749a1 100755 --- a/src/mtp_init.c +++ b/src/mtp_init.c @@ -155,9 +155,9 @@ void _mtp_init(add_rem_store_t sel) mtp_int32 ret; char inter_path[MTP_MAX_PATHNAME_SIZE + 1] = { 0 }; - ret = media_content_connect(); - if (MEDIA_CONTENT_ERROR_NONE != ret) { - ERR("media_content_connect() Fail(%d)", ret); + ret = _util_media_content_connect(); + if (FALSE == ret) { + ERR("media_content_connect() Fail"); goto MTP_INIT_FAIL; } @@ -495,7 +495,7 @@ int main(int argc, char *argv[]) if (MTP_ERROR_NONE != ret) { ERR("_main_init() Fail(%d)", ret); _eh_deregister_notification_callbacks(); - media_content_disconnect(); + _util_media_content_disconnect(); return MTP_ERROR_GENERAL; } DBG("MTP UID = [%u] and GID = [%u]\n", getuid(), getgid()); diff --git a/src/util/mtp_util.c b/src/util/mtp_util.c index a039945..273fd13 100755 --- a/src/util/mtp_util.c +++ b/src/util/mtp_util.c @@ -30,6 +30,11 @@ #include "mtp_fs.h" #include #include +#include +#include +//#include +#include +#include static phone_state_t g_ph_status = { 0 }; @@ -346,43 +351,94 @@ static bool _util_device_external_supported_cb(int storage_id, storage_type_e ty void _util_get_external_path(char *external_path) { int error = STORAGE_ERROR_NONE; + error = storage_foreach_device_supported(_util_device_external_supported_cb, external_path); if (error != STORAGE_ERROR_NONE) { ERR("get external storage path Fail"); - strncpy(external_path, MTP_EXTERNAL_PATH_CHAR, strlen(MTP_EXTERNAL_PATH_CHAR)); + if (external_path != NULL) + strncpy(external_path, MTP_EXTERNAL_PATH_CHAR, strlen(MTP_EXTERNAL_PATH_CHAR) + 1); } } - -static bool _util_device_internal_supported_cb(int storage_id, storage_type_e type, - storage_state_e state, const char *path, void *user_data) +uid_t _util_get_active_user() { - char *storage_path = (char *)user_data; + uid_t *active_user_list = NULL; + uid_t active_user = 0; + int user_cnt = 0; - //DBG("storage id: %d, path: %s", storage_id, path); + user_cnt = sd_get_active_uids(&active_user_list); - if (type == STORAGE_TYPE_INTERNAL && path != NULL) { - strncpy(storage_path, path, strlen(path)); - //DBG("internal storage path : %s", storage_path); - - if (storage_get_root_directory(storage_id, &storage_path) != STORAGE_ERROR_NONE) { - ERR("get internal storage path Fail"); - return FALSE; - } else { - //DBG("get internal storage path : %s", storage_path); - } + if (user_cnt <= 0) { + ERR("Active user not exists : %d", user_cnt); + + if (active_user_list != NULL) + free(active_user_list); + + return -1; } - return TRUE; + if (active_user_list == NULL) { + ERR("active_user_list is NULL"); + return -1; + } + + active_user = active_user_list[0]; + + DBG("Active UID : %d", active_user); + + free(active_user_list); + + if (active_user <= 0) { + ERR("UID is not proper value : %d", active_user); + return -1; + } + + return active_user; } void _util_get_internal_path(char *internal_path) { - int error = STORAGE_ERROR_NONE; - error = storage_foreach_device_supported(_util_device_internal_supported_cb, internal_path); + struct passwd *pwd; + uid_t active_user = 0; + char *active_name = NULL; - if (error != STORAGE_ERROR_NONE) { - ERR("get internal storage path Fail"); - strncpy(internal_path, MTP_STORE_PATH_CHAR, strlen(MTP_STORE_PATH_CHAR)); + active_user = _util_get_active_user(); + pwd = getpwuid(active_user); + active_name = pwd->pw_name; + + if (active_name == NULL) { + ERR("active_name is NULL"); + strncpy(internal_path, MTP_USER_DIRECTORY, strlen(MTP_USER_DIRECTORY) + 1); + return; } + + if (internal_path != NULL) { + strncpy(internal_path, MTP_INTERNAL_PATH_CHAR, strlen(MTP_INTERNAL_PATH_CHAR) + 1); + strncat(internal_path, active_name, strlen(active_name) + 1); + strncat(internal_path, "/media", 7); + } + + ERR("internal path is %s", internal_path); } + +mtp_bool _util_media_content_connect() +{ + mtp_int32 ret = 0; + uid_t active_user = 0; + + active_user = _util_get_active_user(); + + ret = media_content_connect_with_uid(active_user); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + ERR("media_content_connect() failed : %d", ret); + return FALSE; + } + + return TRUE; +} + +void _util_media_content_disconnect() +{ + media_content_disconnect(); +} + -- 2.7.4 From 45af7e931e9d1b1115ebb2d6816240c17725111d Mon Sep 17 00:00:00 2001 From: "jh8801.jung" Date: Tue, 8 Aug 2017 21:40:55 +0900 Subject: [PATCH 07/16] Add SmackLabel in service file Signed-off-by: jh8801.jung Change-Id: Ia8a841275806ffa5029fe46a9ee716cfa8db5e4b --- mtp-responder.service | 1 + 1 file changed, 1 insertion(+) diff --git a/mtp-responder.service b/mtp-responder.service index 2019999..24acdfe 100755 --- a/mtp-responder.service +++ b/mtp-responder.service @@ -8,3 +8,4 @@ SupplementaryGroups=priv_mediastorage priv_externalstorage Type=simple ExecStart=/usr/bin/mtp-responder KillMode=process +SmackProcessLabel=System -- 2.7.4 From afb8592010c7910d298baf2b4d7a670f5a9e9eeb Mon Sep 17 00:00:00 2001 From: "jh8801.jung" Date: Wed, 13 Sep 2017 11:13:23 +0900 Subject: [PATCH 08/16] Fix for svace issue (29398) Signed-off-by: jh8801.jung Change-Id: Ia07ec5c4d18238a614df39adbab464e0c34cff30 --- include/mtp_cmd_handler.h | 2 +- src/mtp_cmd_handler.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/mtp_cmd_handler.h b/include/mtp_cmd_handler.h index 90559cf..9e809fe 100755 --- a/include/mtp_cmd_handler.h +++ b/include/mtp_cmd_handler.h @@ -73,7 +73,7 @@ typedef struct { */ typedef struct { mtp_char cmd_buf[MTP_MAX_CMD_BLOCK_SIZE]; - mtp_char header_buf[MTP_USB_HEADER_LENGTH]; + mtp_char header_buf[MTP_USB_HEADER_LENGTH + 1]; mtp_uint32 cmd_size; mtp_uint32 data_size; mtp_uint32 data_count; diff --git a/src/mtp_cmd_handler.c b/src/mtp_cmd_handler.c index 4e20d44..cce170e 100755 --- a/src/mtp_cmd_handler.c +++ b/src/mtp_cmd_handler.c @@ -2131,7 +2131,7 @@ static void __set_object_prop_list(mtp_handler_t *hdlr) mtp_uint32 max_bytes = 0; mtp_uint32 h_obj = 0; mtp_uint32 prop_id = 0; - mtp_uint32 data_type = 0; + mtp_uint16 data_type = 0; mtp_uchar *temp = NULL; mtp_int32 bytes_left = 0; mtp_uint32 prop_val_sz = 0; @@ -2217,7 +2217,7 @@ static void __set_object_prop_list(mtp_handler_t *hdlr) /* Update property*/ ret = _hutil_update_object_property(h_obj, prop_id, - (mtp_uint16 *)&data_type, temp, bytes_left, + &data_type, temp, bytes_left, &prop_val_sz); switch (ret) { -- 2.7.4 From ab087ad7a34dfb99a1d02ac215e5270f6a4e09c7 Mon Sep 17 00:00:00 2001 From: Taesoo Jun Date: Thu, 14 Sep 2017 18:54:36 +0900 Subject: [PATCH 09/16] [Fix] potential memory leak Change-Id: I59abc1f0a7342a0fd9e5304742953641cd2907c6 --- src/entity/mtp_store.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/entity/mtp_store.c b/src/entity/mtp_store.c index c3ae8ac..7105fa3 100755 --- a/src/entity/mtp_store.c +++ b/src/entity/mtp_store.c @@ -738,6 +738,7 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, format operation is cancelled."); *response = PTP_RESPONSE_PARTIAL_DELETION; + _prop_deinit_ptparray(&child_arr); return FALSE; } @@ -764,13 +765,12 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, operation is cancelled."); *response = PTP_RESPONSE_PARTIAL_DELETION; + _prop_deinit_ptparray(&child_arr); return FALSE; } } } - _prop_deinit_ptparray(&child_arr); - } else { /* Non-Enumerated Folder */ mtp_uint32 num_of_deleted_file = 0; @@ -786,6 +786,7 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, if (MTP_ERROR_ACCESS_DENIED == ret) *response = PTP_RESPONSE_ACCESSDENIED; + _prop_deinit_ptparray(&child_arr); return FALSE; } if (num_of_file == 0) @@ -803,6 +804,8 @@ mtp_bool _entity_remove_object_mtp_store(mtp_store_t *store, mtp_obj_t *obj, all_del = FALSE; } } + _prop_deinit_ptparray(&child_arr); + _util_scan_folder_contents_in_db(obj->file_path); if (all_del) { g_snprintf(g_last_deleted, -- 2.7.4 From 54f96c0332104118b2a7e70be316aaa421b60268 Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Tue, 26 Sep 2017 13:54:34 +0900 Subject: [PATCH 10/16] Fix build waring problem due to readdir_r Change-Id: I63e0fdb15366c88d04a85eb85544270647e3ef28 Signed-off-by: saerome.kim --- packaging/mtp-responder.spec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index 5edf60a..b1f1861 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -36,6 +36,8 @@ cp %{SOURCE1001} . %build +export CFLAGS="$CFLAGS -Wno-deprecated-declarations" + %cmake . make %{?jobs:-j%jobs} -- 2.7.4 From 2bede2d1b6c1e84d6215b7e6e1c4da4b3b1b4935 Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Wed, 6 Dec 2017 21:14:59 +0900 Subject: [PATCH 11/16] Fix WGID 264373 Change-Id: I161c9e52b4cbef70da98c4a3dedfae35f8e133d0 Signed-off-by: saerome.kim --- src/util/mtp_thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 src/util/mtp_thread.c diff --git a/src/util/mtp_thread.c b/src/util/mtp_thread.c old mode 100755 new mode 100644 index e4ba76e..2ea7380 --- a/src/util/mtp_thread.c +++ b/src/util/mtp_thread.c @@ -46,7 +46,7 @@ mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname, error = pthread_create(tid, &attr, thread_func, arg); if (error != 0) { - ERR("[%s] Thread creation Fail errno [%d]\n", tname, errno); + ERR("Thread creation Fail [%d], errno [%d]\n", error, errno); pthread_attr_destroy(&attr); return FALSE; } -- 2.7.4 From 1376148afbbb98e3ccad6c5002bf07a2fe958700 Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Thu, 15 Mar 2018 14:19:11 +0900 Subject: [PATCH 12/16] fix WGID 110834 - Resource leak problem Change-Id: Ie2985204bc1d44261040d1074521f7eaf0718f90 Signed-off-by: saerome.kim --- src/entity/mtp_store.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/entity/mtp_store.c b/src/entity/mtp_store.c index 7105fa3..882fac5 100755 --- a/src/entity/mtp_store.c +++ b/src/entity/mtp_store.c @@ -472,8 +472,13 @@ mtp_uint32 _entity_get_objects_from_store_till_depth(mtp_store_t *store, mtp_uint32 ii = 0; child_arr = _prop_alloc_ptparray(UINT32_TYPE); - if (child_arr == NULL || child_arr->array_entry == NULL) + if (child_arr == NULL) { return 0; + } + if (child_arr->array_entry == NULL) { + _prop_destroy_ptparray(child_arr); + return 0; + } depth--; -- 2.7.4 From 1aec6bfd5856cc9f13b0818ae983703044d3ed17 Mon Sep 17 00:00:00 2001 From: Taesoo Jun Date: Fri, 30 Mar 2018 09:15:22 +0900 Subject: [PATCH 13/16] Add return value checking for media_info_clone Change-Id: I09dc1c5c96b2f49c52e6baa99971d86f7d77fcbe --- src/util/mtp_media_info.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/util/mtp_media_info.c b/src/util/mtp_media_info.c index 6e7b34f..e1b3bea 100755 --- a/src/util/mtp_media_info.c +++ b/src/util/mtp_media_info.c @@ -28,11 +28,14 @@ static bool __fill_media_id_cb(media_info_h media, void *user_data) { + mtp_int32 ret = MEDIA_CONTENT_ERROR_NONE; media_info_h *media_id = (media_info_h *)user_data; DBG("INTO MEdia id retrieval callback"); - media_info_clone(media_id, media); - - return FALSE; + ret = media_info_clone(media_id, media); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + return FALSE; + } + return TRUE; } static void __scan_folder_cb(media_content_error_e err, void *user_data) -- 2.7.4 From 43cc6de937afdc383b6279c949950995627510fe Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Tue, 3 Apr 2018 19:12:08 +0900 Subject: [PATCH 14/16] Apply partial relro security hardening Change-Id: Ic768751ec01780ae5029be23f76997076f8cbe30 Signed-off-by: saerome.kim --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e39cfd..f2ce402 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror-implicit-function-declaration") SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fexceptions -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE") -SET(CMAKE_EXE_LINKER_FLAGS " -Wl,--as-needed -pie -Wl,--hash-style=both") +SET(CMAKE_EXE_LINKER_FLAGS " -Wl,--as-needed -pie -Wl,--hash-style=both,-z,relro") ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} pthread rt gcrypt) -- 2.7.4 From 6263c60a6d11c64a07668e181a414b13f4e1a7d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Stelmach?= Date: Tue, 6 Feb 2018 19:04:18 +0100 Subject: [PATCH 15/16] Implement FunctionFS transport MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit FunctionFS is a generic interface between the kernel and userland processes that allows implementation of USB functions as userspace processes. There may be many instances of FunctionFS mounted each one for a different USB functions. Authored-by: Łukasz Stelmach [Backported from other repository] Signed-off-by: Krzysztof Opasiak [Rebased] Signed-off-by: Paweł Szewczyk Change-Id: Idba4fd657006d445bf1399218784f1c40e3cf626 --- include/transport/mtp_usb_driver.h | 38 ++ src/transport/mtp_transport.c | 31 ++ src/transport/mtp_usb_driver.c | 412 ++----------------- src/transport/mtp_usb_driver_ffs.c | 784 +++++++++++++++++++++++++++++++++++++ src/transport/mtp_usb_driver_slp.c | 482 +++++++++++++++++++++++ 5 files changed, 1361 insertions(+), 386 deletions(-) mode change 100755 => 100644 src/transport/mtp_usb_driver.c create mode 100755 src/transport/mtp_usb_driver_ffs.c create mode 100644 src/transport/mtp_usb_driver_slp.c diff --git a/include/transport/mtp_usb_driver.h b/include/transport/mtp_usb_driver.h index cc63874..6320983 100755 --- a/include/transport/mtp_usb_driver.h +++ b/include/transport/mtp_usb_driver.h @@ -17,12 +17,22 @@ #ifndef _MTP_USB_DRIVER_H_ #define _MTP_USB_DRIVER_H_ +#include "mtp_config.h" #include "mtp_datatype.h" #include "mtp_msgq.h" /* Start of driver related defines */ #define MTP_DRIVER_PATH "/dev/usb_mtp_gadget" +/* FunctionFS endpoint paths */ +#ifndef MTP_FFS_PATH +#define MTP_FFS_PATH "/dev/usb-funcs/mtp" +#endif +#define MTP_EP0_PATH MTP_FFS_PATH "/ep0" +#define MTP_EP_IN_PATH MTP_FFS_PATH "/ep1" +#define MTP_EP_OUT_PATH MTP_FFS_PATH "/ep2" +#define MTP_EP_STATUS_PATH MTP_FFS_PATH "/ep3" + /* These values come from f_mtp_slp.h of kernel source */ #define MTP_IOCTL_LETTER 'Z' #define MTP_GET_HIGH_FULL_SPEED _IOR(MTP_IOCTL_LETTER, 1, int) @@ -44,6 +54,32 @@ typedef struct mtp_max_pkt_size { mtp_uint32 rx; } mtp_max_pkt_size_t; +/* Transport driver interface */ +typedef struct mtp_usb_driver { + mtp_bool (*transport_init_usb_device)(void); + void *(*transport_thread_fake_usb_read)(void *arg); + void *(*transport_thread_fake_usb_write)(void *arg); + void (*transport_deinit_usb_device)(void); + void *(*transport_thread_usb_write)(void *arg); + void *(*transport_thread_usb_read)(void *arg); + void *(*transport_thread_usb_control)(void *arg); + mtp_int32 (*transport_mq_init)(msgq_id_t *rx_ipc, msgq_id_t *tx_ipc); + mtp_bool (*transport_mq_deinit)(msgq_id_t *rx_ipc, msgq_id_t *tx_ipc); + mtp_uint32 (*transport_get_usb_packet_len)(void); + mtp_uint32 (*get_tx_pkt_size)(void); + mtp_uint32 (*get_rx_pkt_size)(void); +} mtp_usb_driver_t; + +extern const mtp_usb_driver_t mtp_usb_driver_slp; +extern const mtp_usb_driver_t mtp_usb_driver_ffs; + +typedef enum { + MTP_TRANSPORT_SLP = 0, + MTP_TRANSPORT_FFS, + MTP_TRANSPORT_NUMBER, /* number of supported transports */ + MTP_TRANSPORT_UNKNOWN, +} mtp_transport_type_t; + /* Maximum repeat count for USB error recovery */ #define MTP_USB_ERROR_MAX_RETRY 5 @@ -51,10 +87,12 @@ mtp_bool _transport_init_usb_device(void); void _transport_deinit_usb_device(void); void *_transport_thread_usb_write(void *arg); void *_transport_thread_usb_read(void *arg); +void *_transport_thread_usb_control(void *arg); mtp_int32 _transport_mq_init(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid); mtp_bool _transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid); mtp_uint32 _transport_get_usb_packet_len(void); mtp_uint32 _get_tx_pkt_size(void); mtp_uint32 _get_rx_pkt_size(void); +mtp_transport_type_t _transport_get_type(void); #endif /* _MTP_USB_DRIVER_H_ */ diff --git a/src/transport/mtp_transport.c b/src/transport/mtp_transport.c index 7c73bbd..c178c51 100755 --- a/src/transport/mtp_transport.c +++ b/src/transport/mtp_transport.c @@ -40,6 +40,7 @@ static mtp_mgr_t *g_mgr = &g_mtp_mgr; static mtp_bool g_usb_threads_created = FALSE; static pthread_t g_tx_thrd = 0; static pthread_t g_rx_thrd = 0; +static pthread_t g_ctrl_thrd = 0; static pthread_t g_data_rcv = 0; static msgq_id_t mtp_to_usb_mqid; static msgq_id_t g_usb_to_mtp_mqid; @@ -290,6 +291,7 @@ static mtp_err_t __transport_init_io() mtp_int32 res = 0; thread_func_t usb_write_thread = _transport_thread_usb_write; thread_func_t usb_read_thread = _transport_thread_usb_read; + thread_func_t usb_control_thread = _transport_thread_usb_control; res = _util_thread_create(&g_tx_thrd, "usb write thread", PTHREAD_CREATE_JOINABLE, usb_write_thread, @@ -307,6 +309,17 @@ static mtp_err_t __transport_init_io() goto cleanup; } + if (_transport_get_type() == MTP_TRANSPORT_FFS) { + res = _util_thread_create(&g_ctrl_thrd, "usb control thread", + PTHREAD_CREATE_JOINABLE, + usb_control_thread, + NULL); + if (FALSE == res) { + ERR("CTRL thread creation failed\n"); + goto cleanup; + } + } + g_usb_threads_created = TRUE; return MTP_ERROR_NONE; @@ -314,6 +327,11 @@ static mtp_err_t __transport_init_io() cleanup: _util_print_error(); + if (g_ctrl_thrd) { + res = _util_thread_cancel(g_ctrl_thrd); + DBG("pthread_cancel [%d]\n", res); + g_ctrl_thrd = 0; + } if (g_rx_thrd) { res = _util_thread_cancel(g_rx_thrd); DBG("pthread_cancel [%d]\n", res); @@ -337,6 +355,19 @@ static void __transport_deinit_io() } errno = 0; + if (_transport_get_type() == MTP_TRANSPORT_FFS) { + if (FALSE == _util_thread_cancel(g_ctrl_thrd)) { + ERR("Fail to cancel pthread of g_ctrl_thrd\n"); + } else { + DBG("Succeed to cancel pthread of g_ctrl_thrd\n"); + } + + if (_util_thread_join(g_ctrl_thrd, 0) == FALSE) + ERR("pthread_join of g_ctrl_thrd failed\n"); + + g_ctrl_thrd = 0; + } + if (FALSE == _util_thread_cancel(g_rx_thrd)) ERR("_util_thread_cancel(rx) Fail"); diff --git a/src/transport/mtp_usb_driver.c b/src/transport/mtp_usb_driver.c old mode 100755 new mode 100644 index e392bb9..5576c55 --- a/src/transport/mtp_usb_driver.c +++ b/src/transport/mtp_usb_driver.c @@ -14,107 +14,41 @@ * limitations under the License. */ -#include -#include #include -#include -#include #include "mtp_usb_driver.h" -#include "mtp_device.h" -#include "ptp_datacodes.h" -#include "mtp_support.h" -#include "ptp_container.h" -#include "mtp_msgq.h" -#include "mtp_util.h" -#include "mtp_thread.h" -#include "mtp_transport.h" -#include "mtp_event_handler.h" -/* - * GLOBAL AND EXTERN VARIABLES - */ -extern mtp_config_t g_conf; - -/* - * STATIC VARIABLES AND FUNCTIONS - */ -static mtp_int32 g_usb_fd = -1; -static mtp_max_pkt_size_t pkt_size; -static mtp_uint32 rx_mq_sz; -static mtp_uint32 tx_mq_sz; - -static mtp_int32 __handle_usb_read_err(mtp_int32 err, - mtp_uchar *buf, mtp_int32 buf_len); -static void __clean_up_msg_queue(void *pmsqid); -static void __handle_control_request(mtp_int32 request); -static void __receive_signal(mtp_int32 n, siginfo_t *info, void *unused); +static const mtp_usb_driver_t *usb_driver; /* * FUNCTIONS */ mtp_bool _transport_init_usb_device(void) { - mtp_int32 status = 0; - struct sigaction sig; - pid_t mtp_pid = 0; - int msg_size; - - /* Kernel will inform to User Space using signal. */ - memset(&sig, 0, sizeof(sig)); - sig.sa_sigaction = __receive_signal; - sig.sa_flags = SA_SIGINFO; - sigaction(SIG_SETUP, &sig, NULL); - - if (g_usb_fd > 0) { - DBG("Device Already open"); - return TRUE; - } - - g_usb_fd = open(MTP_DRIVER_PATH, O_RDWR); - if (g_usb_fd < 0) { - ERR("Device node [%s] open Fail,errno [%d]\n", MTP_DRIVER_PATH, errno); - return FALSE; - } - - mtp_pid = getpid(); - status = ioctl(g_usb_fd, MTP_SET_USER_PID, &mtp_pid); - if (status < 0) { - ERR("IOCTL MTP_SET_USER_PID Fail = [%d]\n", status); - _transport_deinit_usb_device(); + if (access(MTP_DRIVER_PATH, F_OK) == 0) { + usb_driver = &mtp_usb_driver_slp; + } else if (access(MTP_EP0_PATH, F_OK) == 0) { + usb_driver = &mtp_usb_driver_ffs; + } else { + ERR("No suport for USB gadgets in kernel"); return FALSE; } - pkt_size.rx = g_conf.read_usb_size; - pkt_size.tx = g_conf.write_usb_size; - - DBG("Final : Tx pkt size:[%u], Rx pkt size:[%u]\n", pkt_size.tx, pkt_size.rx); - - msg_size = sizeof(msgq_ptr_t) - sizeof(long); - rx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_rx_ipc_size) * msg_size; - tx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_tx_ipc_size) * msg_size; - - DBG("RX MQ size :[%u], TX MQ size:[%u]\n", rx_mq_sz, tx_mq_sz); - - return TRUE; + return usb_driver->transport_init_usb_device(); } void _transport_deinit_usb_device(void) { - if (g_usb_fd >= 0) - close(g_usb_fd); - g_usb_fd = -1; - - return; + usb_driver->transport_deinit_usb_device(); } mtp_uint32 _get_tx_pkt_size(void) { - return pkt_size.tx; + return usb_driver->get_rx_pkt_size(); } mtp_uint32 _get_rx_pkt_size(void) { - return pkt_size.rx; + return usb_driver->get_rx_pkt_size(); } /* @@ -127,294 +61,22 @@ mtp_uint32 _get_rx_pkt_size(void) */ mtp_int32 _transport_mq_init(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) { - if (_util_msgq_init(rx_mqid, 0) == FALSE) { - ERR("RX MQ init Fail [%d]\n", errno); - return FALSE; - } - - if (_util_msgq_set_size(*rx_mqid, rx_mq_sz) == FALSE) - ERR("RX MQ setting size Fail [%d]\n", errno); - - if (_util_msgq_init(tx_mqid, 0) == FALSE) { - ERR("TX MQ init Fail [%d]\n", errno); - _util_msgq_deinit(rx_mqid); - *rx_mqid = -1; - return FALSE; - } - - if (_util_msgq_set_size(*tx_mqid, tx_mq_sz) == FALSE) - ERR("TX MQ setting size Fail [%d]\n", errno); - - return TRUE; + return usb_driver->transport_mq_init(rx_mqid, tx_mqid); } void *_transport_thread_usb_write(void *arg) { - mtp_int32 status = 0; - mtp_uint32 len = 0; - unsigned char *mtp_buf = NULL; - msg_type_t mtype = MTP_UNDEFINED_PACKET; - msgq_id_t *mqid = (msgq_id_t *)arg; - - pthread_cleanup_push(__clean_up_msg_queue, mqid); - - do { - /* original LinuxThreads cancelation didn't work right - * so test for it explicitly. - */ - pthread_testcancel(); - - _util_rcv_msg_from_mq(*mqid, &mtp_buf, &len, &mtype); - - if (mtype == MTP_BULK_PACKET || mtype == MTP_DATA_PACKET) { - status = write(g_usb_fd, mtp_buf, len); - if (status < 0) { - ERR("USB write fail : %d\n", errno); - if (errno == ENOMEM || errno == ECANCELED) { - status = 0; - __clean_up_msg_queue(mqid); - } - } - g_free(mtp_buf); - mtp_buf = NULL; - } else if (MTP_EVENT_PACKET == mtype) { - /* Handling the MTP Asynchronous Events */ - DBG("Send Interrupt data to kernel by IOCTL "); - status = ioctl(g_usb_fd, MTP_WRITE_INT_DATA, mtp_buf); - g_free(mtp_buf); - mtp_buf = NULL; - } else if (MTP_ZLP_PACKET == mtype) { - DBG("Send ZLP data to kernel by IOCTL "); - status = ioctl(g_usb_fd, MTP_SET_ZLP_DATA, NULL); - } else { - DBG("mtype = %d is not valid\n", mtype); - status = -1; - } - - if (status < 0) { - ERR("write data to the device node Fail:\ - status = %d\n", status); - break; - } - } while (status >= 0); - - DBG("exited Source thread with status %d\n", status); - pthread_cleanup_pop(1); - g_free(mtp_buf); - - return NULL; + return usb_driver->transport_thread_usb_write(arg); } void *_transport_thread_usb_read(void *arg) { - mtp_int32 status = 0; - msgq_ptr_t pkt = {MTP_DATA_PACKET, 0, 0, NULL}; - msgq_id_t *mqid = (msgq_id_t *)arg; - mtp_uint32 rx_size = _get_rx_pkt_size(); - - pthread_cleanup_push(__clean_up_msg_queue, mqid); - - do { - pthread_testcancel(); - - pkt.buffer = (mtp_uchar *)g_malloc(rx_size); - if (NULL == pkt.buffer) { - ERR("Sink thread: memalloc Fail."); - break; - } - - status = read(g_usb_fd, pkt.buffer, rx_size); - if (status <= 0) { - status = __handle_usb_read_err(status, pkt.buffer, rx_size); - if (status <= 0) { - ERR("__handle_usb_read_err Fail"); - g_free(pkt.buffer); - break; - } - } - - pkt.length = status; - if (FALSE == _util_msgq_send(*mqid, (void *)&pkt, - sizeof(msgq_ptr_t) - sizeof(long), 0)) { - ERR("msgsnd Fail"); - g_free(pkt.buffer); - } - } while (status > 0); - - DBG("status[%d] errno[%d]\n", status, errno); - pthread_cleanup_pop(1); - - return NULL; -} - -static mtp_int32 __handle_usb_read_err(mtp_int32 err, - mtp_uchar *buf, mtp_int32 buf_len) -{ - mtp_int32 retry = 0; - mtp_bool ret; - - while (retry++ < MTP_USB_ERROR_MAX_RETRY) { - if (err == 0) { - DBG("ZLP(Zero Length Packet). Skip"); - } else if (err < 0 && errno == EINTR) { - DBG("read () is interrupted. Skip"); - } else if (err < 0 && errno == EIO) { - DBG("EIO"); - - if (MTP_PHONE_USB_CONNECTED != - _util_get_local_usb_status()) { - ERR("USB is disconnected"); - break; - } - - _transport_deinit_usb_device(); - ret = _transport_init_usb_device(); - if (ret == FALSE) { - ERR("_transport_init_usb_device Fail"); - continue; - } - } else { - ERR("Unknown error : %d, errno [%d] \n", err, errno); - break; - } - - err = read(g_usb_fd, buf, buf_len); - if (err > 0) - break; - } - - if (err <= 0) - ERR("USB error handling Fail"); - - return err; + return usb_driver->transport_thread_usb_read(arg); } -static void __clean_up_msg_queue(void *mq_id) +void *_transport_thread_usb_control(void *arg) { - mtp_int32 len = 0; - msgq_ptr_t pkt = { 0 }; - msgq_id_t l_mqid = *(msgq_id_t *)mq_id; - - ret_if(mq_id == NULL); - - _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); - while (TRUE == _util_msgq_receive(l_mqid, (void *)&pkt, - sizeof(msgq_ptr_t) - sizeof(long), 1, &len)) { - g_free(pkt.buffer); - memset(&pkt, 0, sizeof(msgq_ptr_t)); - } - - return; -} - -static void __handle_control_request(mtp_int32 request) -{ - static mtp_bool kernel_reset = FALSE; - static mtp_bool host_cancel = FALSE; - mtp_int32 status = 0; - - switch (request) { - case USB_PTPREQUEST_CANCELIO: - DBG("USB_PTPREQUEST_CANCELIO"); - cancel_req_t cancelreq_data; - mtp_byte buffer[USB_PTPREQUEST_CANCELIO_SIZE + 1] = { 0 }; - - host_cancel = TRUE; - _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); - status = ioctl(g_usb_fd, MTP_GET_SETUP_DATA, buffer); - if (status < 0) { - ERR("IOCTL GET_SETUP_DATA Fail [%d]\n", status); - return; - } - - memcpy(&(cancelreq_data.io_code), buffer, sizeof(mtp_word)); - memcpy(&(cancelreq_data.tid), &buffer[2], sizeof(mtp_dword)); - DBG("cancel io code [%d], transaction id [%ld]\n", - cancelreq_data.io_code, cancelreq_data.tid); - break; - - case USB_PTPREQUEST_RESET: - - DBG("USB_PTPREQUEST_RESET"); - _reset_mtp_device(); - if (kernel_reset == FALSE) - kernel_reset = TRUE; - - status = ioctl(g_usb_fd, MTP_SEND_RESET_ACK, NULL); - if (status < 0) { - ERR("IOCTL MTP_SEND_RESET_ACK Fail [%d]\n", - status); - } - break; - - case USB_PTPREQUEST_GETSTATUS: - - DBG("USB_PTPREQUEST_GETSTATUS"); - - /* Send busy status response just once. This flag is also for - * the case that mtp misses the cancel request packet. - */ - static mtp_bool sent_busy = FALSE; - usb_status_req_t statusreq_data = { 0 }; - mtp_dword num_param = 0; - - memset(&statusreq_data, 0x00, sizeof(usb_status_req_t)); - if (host_cancel == TRUE || (sent_busy == FALSE && - kernel_reset == FALSE)) { - DBG("Send busy response, set host_cancel to FALSE"); - statusreq_data.len = 0x08; - statusreq_data.code = PTP_RESPONSE_DEVICEBUSY; - host_cancel = FALSE; - } else if (_device_get_phase() == DEVICE_PHASE_NOTREADY) { - statusreq_data.code = - PTP_RESPONSE_TRANSACTIONCANCELLED; - DBG("PTP_RESPONSE_TRANSACTIONCANCELLED"); - statusreq_data.len = (mtp_word)(sizeof(usb_status_req_t) + - (num_param - 2) * sizeof(mtp_dword)); - } else if (_device_get_status() == DEVICE_STATUSOK) { - DBG("PTP_RESPONSE_OK"); - statusreq_data.len = 0x08; - statusreq_data.code = PTP_RESPONSE_OK; - - if (kernel_reset == TRUE) - kernel_reset = FALSE; - } else { - DBG("PTP_RESPONSE_GEN_ERROR"); - statusreq_data.len = 0x08; - statusreq_data.code = PTP_RESPONSE_GEN_ERROR; - } - - if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) - sent_busy = TRUE; - else - sent_busy = FALSE; - - status = ioctl(g_usb_fd, MTP_SET_SETUP_DATA, &statusreq_data); - if (status < 0) { - DBG("IOCTL MTP_SET_SETUP_DATA Fail [%d]\n", - status); - return; - } - break; - - case USB_PTPREQUEST_GETEVENT: - DBG("USB_PTPREQUEST_GETEVENT"); - break; - - default: - DBG("Invalid class specific setup request"); - break; - } - return; -} - -static void __receive_signal(mtp_int32 n, siginfo_t *info, void *arg) -{ - mtp_int32 request = info->si_int; - - DBG("Received SIgnal From Kernel"); - __handle_control_request(request); - return; + return usb_driver->transport_thread_usb_control(arg); } /* @@ -425,43 +87,21 @@ static void __receive_signal(mtp_int32 n, siginfo_t *info, void *arg) */ mtp_bool _transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) { - mtp_int32 res = TRUE; - - if (*rx_mqid) { - res = _util_msgq_deinit(rx_mqid); - if (res == FALSE) - ERR("rx_mqid deinit Fail [%d]\n", errno); - else - *rx_mqid = 0; - } - - if (*tx_mqid) { - res = _util_msgq_deinit(tx_mqid); - if (res == FALSE) - ERR("tx_mqid deinit fail [%d]\n", errno); - else - *tx_mqid = 0; - } - - return res; + return usb_driver->transport_mq_deinit(rx_mqid, tx_mqid); } mtp_uint32 _transport_get_usb_packet_len(void) { - mtp_int32 status = 0; - static mtp_int32 usb_speed = 0; - - if (usb_speed == 0) { + return usb_driver->transport_get_usb_packet_len(); +} - status = ioctl(g_usb_fd, MTP_GET_HIGH_FULL_SPEED, &usb_speed); - if (status < 0) { - ERR("MTP_GET_HIGH_FULL_SPEED Fail [%d]\n", status); - return MTP_MAX_PACKET_SIZE_SEND_FS; - } - } +mtp_transport_type_t _transport_get_type(void) +{ + if (usb_driver == &mtp_usb_driver_slp) + return MTP_TRANSPORT_SLP; - if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) - return MTP_MAX_PACKET_SIZE_SEND_FS; + if (usb_driver == &mtp_usb_driver_ffs) + return MTP_TRANSPORT_FFS; - return MTP_MAX_PACKET_SIZE_SEND_HS; + return MTP_TRANSPORT_UNKNOWN; } diff --git a/src/transport/mtp_usb_driver_ffs.c b/src/transport/mtp_usb_driver_ffs.c new file mode 100755 index 0000000..e972756 --- /dev/null +++ b/src/transport/mtp_usb_driver_ffs.c @@ -0,0 +1,784 @@ +/* -*- mode: C; c-file-style: "linux" -*- + * + * mtp-responder + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * PROPRIETARY/CONFIDENTIAL + * + * This software is the confidential and proprietary information of + * SAMSUNG ELECTRONICS ("Confidential Information"). You agree and acknowledge + * that this software is owned by Samsung and you shall not disclose + * such Confidential Information and shall use it only in accordance with + * the terms of the license agreement you entered into with SAMSUNG ELECTRONICS. + * SAMSUNG make no representations or warranties about the suitability of + * the software, either express or implied, including but not limited to + * the implied warranties of merchantability, fitness for a particular purpose, + * or non-infringement. SAMSUNG shall not be liable for any damages suffered by + * licensee arising out of or related to this software. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "mtp_usb_driver.h" +#include "mtp_device.h" +#include "ptp_datacodes.h" +#include "mtp_support.h" +#include "ptp_container.h" +#include "mtp_msgq.h" +#include "mtp_thread.h" +#include "mtp_transport.h" +#include "mtp_event_handler.h" +#include +#include +#include + +/* + * GLOBAL AND EXTERN VARIABLES + */ +extern mtp_config_t g_conf; + +/* + * STATIC VARIABLES AND FUNCTIONS + */ +#ifndef FUNCTIONFS_DESCRIPTORS_MAGIC_V2 +#define FUNCTIONFS_DESCRIPTORS_MAGIC_V2 3 +enum functionfs_flags { + FUNCTIONFS_HAS_FS_DESC = 1, + FUNCTIONFS_HAS_HS_DESC = 2, + FUNCTIONFS_HAS_SS_DESC = 4, + FUNCTIONFS_HAS_MS_OS_DESC = 8, +}; +#endif + +/*PIMA15740-2000 spec*/ +#define USB_PTPREQUEST_CANCELIO 0x64 /* Cancel request */ +#define USB_PTPREQUEST_GETEVENT 0x65 /* Get extened event data */ +#define USB_PTPREQUEST_RESET 0x66 /* Reset Device */ +#define USB_PTPREQUEST_GETSTATUS 0x67 /* Get Device Status */ +#define USB_PTPREQUEST_CANCELIO_SIZE 6 +#define USB_PTPREQUEST_GETSTATUS_SIZE 12 + +#define cpu_to_le16(x) htole16(x) +#define cpu_to_le32(x) htole32(x) +#define le32_to_cpu(x) le32toh(x) +#define le16_to_cpu(x) le16toh(x) + +static const struct { + struct { + __le32 magic; + __le32 length; + __le32 flags; + __le32 fs_count; + __le32 hs_count; + // __le32 os_count; + } header; + struct { + struct usb_interface_descriptor intf; + struct usb_endpoint_descriptor_no_audio bulk_in; + struct usb_endpoint_descriptor_no_audio bulk_out; + struct usb_endpoint_descriptor_no_audio int_in; + } __attribute__((packed)) fs_descs, hs_descs; + // struct {} __attribute__((packed)) os_descs; +} __attribute__((packed)) descriptors = { + .header = { + .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), + .length = cpu_to_le32(sizeof(descriptors)), + .flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC, // | FUNCTIONFS_HAS_MS_OS_DESC, + .fs_count = 4, + .hs_count = 4, + // .os_count = 0; + }, + .fs_descs = { + // drivers/usb/gadget/f_mtp_slp.c:207 + .intf = { + .bLength = sizeof(descriptors.fs_descs.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bNumEndpoints = 3, + .bInterfaceClass = USB_CLASS_STILL_IMAGE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 1, + }, + .bulk_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(64), + }, + .bulk_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(64), + }, + .int_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 3 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16(64), + .bInterval = 6, + }, + }, + .hs_descs = { + .intf = { + .bLength = sizeof(descriptors.fs_descs.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bNumEndpoints = 3, + .bInterfaceClass = USB_CLASS_STILL_IMAGE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 1, + }, + .bulk_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(512), + }, + .bulk_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(512), + }, + .int_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 3 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16(64), + .bInterval = 6, + }, + }, +}; + +#define STR_INTERFACE "Samsung MTP" + +static const struct { + struct usb_functionfs_strings_head header; + struct { + __le16 code; + const char str1[sizeof(STR_INTERFACE)]; + } __attribute__((packed)) lang0; +} __attribute__((packed)) strings = { + .header = { + .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), + .length = cpu_to_le32(sizeof(strings)), + .str_count = cpu_to_le32(1), + .lang_count = cpu_to_le32(1), + }, + .lang0 = { + cpu_to_le16(0x0409), /* en-us */ + STR_INTERFACE, + }, +}; + +static mtp_int32 g_usb_ep0 = -1; /* read (g_usb_ep0, ...) */ +static mtp_int32 g_usb_ep_in = -1; /* write (g_usb_ep_in, ...) */ +static mtp_int32 g_usb_ep_out = -1; /* read (g_usb_ep_out, ...) */ +static mtp_int32 g_usb_ep_status = -1; /* write (g_usb_ep_status, ...) */ + +static mtp_max_pkt_size_t pkt_size; +static mtp_uint32 rx_mq_sz; +static mtp_uint32 tx_mq_sz; +static mtp_int32 __handle_usb_read_err(mtp_int32 err, + mtp_uchar *buf, mtp_int32 buf_len); +static void __clean_up_msg_queue(void *param); +static void __handle_control_request(mtp_int32 request); + +/* + * FUNCTIONS + */ + +static mtp_bool __io_init() +{ + int ret; + + g_usb_ep0 = open(MTP_EP0_PATH, O_RDWR); + if (g_usb_ep0 < 0) + goto out; + + DBG("Writing USB descriptors"); + ret = write(g_usb_ep0, &descriptors, sizeof(descriptors)); + if (ret < 0) { + ERR("Error writing descriptors"); + goto cleanup; + } + + DBG("Writing USB strings"); + ret = write(g_usb_ep0, &strings, sizeof(strings)); + if (ret < 0) { + ERR("Error writing strings"); + goto cleanup; + } + + g_usb_ep_in = open(MTP_EP_IN_PATH, O_RDWR); + if (g_usb_ep_in < 0) { + ERR("Error opening bulk-in"); + goto cleanup; + } + + g_usb_ep_out = open(MTP_EP_OUT_PATH, O_RDWR); + if (g_usb_ep_out < 0) { + ERR("Error opening bulk-out"); + goto cleanup_in; + } + + g_usb_ep_status = open(MTP_EP_STATUS_PATH, O_RDWR); + if (g_usb_ep_status < 0) { + ERR("Error opening status"); + goto cleanup_out; + } + + return TRUE; + +cleanup_out: + close(g_usb_ep_out); +cleanup_in: + close(g_usb_ep_in); +cleanup: + close(g_usb_ep0); +out: + return FALSE; +} + +static mtp_bool ffs_transport_init_usb_device(void) +{ + mtp_int32 status = 0; + int msg_size; + + if (g_usb_ep0 > 0) { + DBG("Device Already open\n"); + return TRUE; + } + + status = __io_init(); + if (!status) { + char error[256]; + ERR("Device node [%s] open failed, errno [%s]\n", + MTP_EP0_PATH, strerror_r(errno, error, sizeof(error))); + return FALSE; + } + + pkt_size.rx = g_conf.read_usb_size; + pkt_size.tx = g_conf.write_usb_size; + + DBG("Final : Tx pkt size:[%u], Rx pkt size:[%u]\n", pkt_size.tx, pkt_size.rx); + + msg_size = sizeof(msgq_ptr_t) - sizeof(long); + rx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_rx_ipc_size) * msg_size; + tx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_tx_ipc_size) * msg_size; + + DBG("RX MQ size :[%u], TX MQ size:[%u]\n", rx_mq_sz, tx_mq_sz); + + return TRUE; +} + +static void ffs_transport_deinit_usb_device(void) +{ + if (g_usb_ep0 >= 0) + close(g_usb_ep0); + g_usb_ep0 = -1; + + if (g_usb_ep_in >= 0) + close(g_usb_ep_in); + g_usb_ep_in = -1; + + if (g_usb_ep_out >= 0) + close(g_usb_ep_out); + g_usb_ep_out = -1; + + if (g_usb_ep_status >= 0) + close(g_usb_ep_status); + g_usb_ep_status = -1; + + return; +} + +static mtp_uint32 ffs_get_tx_pkt_size(void) +{ + return pkt_size.tx; +} + +static mtp_uint32 ffs_get_rx_pkt_size(void) +{ + return pkt_size.rx; +} + +/* + * static mtp_int32 ffs_transport_mq_init() + * This function create a message queue for MTP, + * A created message queue will be used to help data transfer between + * MTP module and usb buffer. + * @return This function returns TRUE on success or + * returns FALSE on failure. + */ +static mtp_int32 ffs_transport_mq_init(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) +{ + if (_util_msgq_init(rx_mqid, 0) == FALSE) { + ERR("RX MQ init Fail [%d]\n", errno); + return FALSE; + } + + if (_util_msgq_set_size(*rx_mqid, rx_mq_sz) == FALSE) + ERR("RX MQ setting size Fail [%d]\n", errno); + + if (_util_msgq_init(tx_mqid, 0) == FALSE) { + ERR("TX MQ init Fail [%d]\n", errno); + _util_msgq_deinit(rx_mqid); + *rx_mqid = -1; + return FALSE; + } + + if (_util_msgq_set_size(*tx_mqid, tx_mq_sz) == FALSE) + ERR("TX MQ setting size Fail [%d]\n", errno); + + return TRUE; +} + +static void *ffs_transport_thread_usb_write(void *arg) +{ + mtp_int32 status = 0; + mtp_uint32 len = 0; + unsigned char *mtp_buf = NULL; + msg_type_t mtype = MTP_UNDEFINED_PACKET; + msgq_id_t *mqid = (msgq_id_t *)arg; + + pthread_cleanup_push(__clean_up_msg_queue, mqid); + + do { + /* original LinuxThreads cancelation didn't work right + * so test for it explicitly. + */ + pthread_testcancel(); + + _util_rcv_msg_from_mq(*mqid, &mtp_buf, &len, &mtype); + + if (mtype == MTP_BULK_PACKET || mtype == MTP_DATA_PACKET) { + status = write(g_usb_ep_in, mtp_buf, len); + if (status < 0) { + ERR("USB write fail : %d\n", errno); + if (errno == ENOMEM || errno == ECANCELED) { + status = 0; + __clean_up_msg_queue(mqid); + } + } + g_free(mtp_buf); + mtp_buf = NULL; + } else if (MTP_EVENT_PACKET == mtype) { + /* Handling the MTP Asynchronous Events */ + DBG("Send Interrupt data to kernel via g_usb_ep_status\n"); + status = write(g_usb_ep_status, mtp_buf, len); + g_free(mtp_buf); + mtp_buf = NULL; + } else if (MTP_ZLP_PACKET == mtype) { + DBG("Send ZLP data to kerne via g_usb_ep_in\n"); + status = write(g_usb_ep_in, (void*)0xFEE1DEAD, 0); + } else { + DBG("mtype = %d is not valid\n", mtype); + status = -1; + } + + if (status < 0) { + ERR("write data to the device node Fail:\ + status = %d\n", status); + break; + } + } while (status >= 0); + + DBG("exited Source thread with status %d\n", status); + pthread_cleanup_pop(1); + g_free(mtp_buf); + + return NULL; +} + +static int __setup(int ep0, struct usb_ctrlrequest *ctrl) +{ + const char* requests[] = { + "CANCELIO", /* 0x64 */ + "GETEVENT", /* 0x65 */ + "RESET", /* 0x66 */ + "GETSTATUS", /* 0x67 */ + }; + __u16 wIndex = le16_to_cpu(ctrl->wIndex); + __u16 wValue = le16_to_cpu(ctrl->wValue); + __u16 wLength = le16_to_cpu(ctrl->wLength); + int rc = -EOPNOTSUPP; + int status = 0; + + if ((ctrl->bRequestType & 0x7f) != (USB_TYPE_CLASS | USB_RECIP_INTERFACE)) { + DBG(__FILE__ "(%s):%d: Invalid request type: %d", + __func__, __LINE__, ctrl->bRequestType); + goto stall; + } + + switch (((ctrl->bRequestType & 0x80) << 8) | ctrl->bRequest) { + case ((USB_DIR_OUT << 8) | USB_PTPREQUEST_CANCELIO): + + DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s", + __func__, __LINE__, requests[ctrl->bRequest-0x64]); + if (wValue != 0 || wIndex != 0 || wLength != 6) { + DBG("Invalid request parameters: wValue:%d wIndex:%d wLength:%d\n"); + rc = -EINVAL; + goto stall; + } + __handle_control_request(ctrl->bRequest); + break; + + case ((USB_DIR_IN << 8) | USB_PTPREQUEST_GETSTATUS): + case ((USB_DIR_OUT << 8) | USB_PTPREQUEST_RESET): + + DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s", + __func__, __LINE__, requests[ctrl->bRequest-0x64]); + __handle_control_request(ctrl->bRequest); + break; + + case ((USB_DIR_IN << 8) | USB_PTPREQUEST_GETEVENT): + + /* Optional, may stall */ + DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s", + __func__, __LINE__, requests[ctrl->bRequest-0x64]); + rc = -EOPNOTSUPP; + goto stall; + break; + + default: + DBG(__FILE__ "(%s):%d: Invalid request: %d", __func__, + __LINE__, ctrl->bRequest); + goto stall; + } + return 0; + +stall: + + DBG(__FILE__"(%s):%d:stall %0x2x.%02x\n", + __func__, __LINE__, ctrl->bRequestType, ctrl->bRequest); + if ((ctrl->bRequestType & 0x80) == USB_DIR_IN) + status = read(g_usb_ep0, NULL, 0); + else + status = write(g_usb_ep0, NULL, 0); + + if (status != -1 || errno != EL2HLT) { + ERR(__FILE__"(%s):%d:stall error\n", + __func__, __LINE__, ctrl->bRequestType, ctrl->bRequest); + rc = errno; + } + return rc; +} + +static void *ffs_transport_thread_usb_read(void *arg) +{ + mtp_int32 status = 0; + msgq_ptr_t pkt = {MTP_DATA_PACKET, 0, 0, NULL}; + msgq_id_t *mqid = (msgq_id_t *)arg; + mtp_uint32 rx_size = _get_rx_pkt_size(); + + pthread_cleanup_push(__clean_up_msg_queue, mqid); + + do { + pthread_testcancel(); + + pkt.buffer = (mtp_uchar *)g_malloc(rx_size); + if (NULL == pkt.buffer) { + ERR("Sink thread: memalloc failed.\n"); + break; + } + + status = read(g_usb_ep_out, pkt.buffer, rx_size); + if (status <= 0) { + status = __handle_usb_read_err(status, pkt.buffer, rx_size); + if (status <= 0) { + ERR("__handle_usb_read_err is failed\n"); + g_free(pkt.buffer); + break; + } + } + + pkt.length = status; + if (FALSE == _util_msgq_send(*mqid, (void *)&pkt, + sizeof(msgq_ptr_t) - sizeof(long), 0)) { + ERR("msgsnd Fail"); + g_free(pkt.buffer); + } + } while (status > 0); + + DBG("status[%d] errno[%d]\n", status, errno); + pthread_cleanup_pop(1); + + return NULL; +} + +static void *ffs_transport_thread_usb_control(void *arg) +{ + mtp_int32 status = 0; + struct usb_functionfs_event event; + msgq_id_t *mqid = (msgq_id_t *)arg; + + pthread_cleanup_push(__clean_up_msg_queue, mqid); + + do { + pthread_testcancel(); + + status = read(g_usb_ep0, &event, sizeof(event)); + if (status < 0) { + char error[256]; + ERR("read from ep0 failed: %s", + strerror_r(errno, error, sizeof(error))); + continue; + } + DBG("FUNCTIONFS event received: %d", event.type); + + switch (event.type) { + case FUNCTIONFS_SETUP: + DBG("SETUP: bmRequestType:%d bRequest:%d wValue:%d wIndex:%d wLength:%d\n", + event.u.setup.bRequestType, + event.u.setup.bRequest, + event.u.setup.wValue, + event.u.setup.wIndex, + event.u.setup.wLength); + __setup(g_usb_ep0, &event.u.setup); + break; + } + } while (status > 0); + + DBG("status[%d] errno[%d]\n", status, errno); + pthread_cleanup_pop(1); + + return NULL; +} + +static mtp_int32 __handle_usb_read_err(mtp_int32 err, + mtp_uchar *buf, mtp_int32 buf_len) +{ + mtp_int32 retry = 0; + mtp_bool ret; + + while (retry++ < MTP_USB_ERROR_MAX_RETRY) { + if (err == 0) { + DBG("ZLP(Zero Length Packet). Skip"); + } else if (err < 0 && errno == EINTR) { + DBG("read () is interrupted. Skip"); + } else if (err < 0 && errno == EIO) { + DBG("EIO"); + + if (MTP_PHONE_USB_CONNECTED != + _util_get_local_usb_status()) { + ERR("USB is disconnected"); + break; + } + + _transport_deinit_usb_device(); + ret = _transport_init_usb_device(); + if (ret == FALSE) { + ERR("_transport_init_usb_device Fail"); + continue; + } + } else { + ERR("Unknown error : %d, errno [%d] \n", err, errno); + break; + } + + err = read(g_usb_ep_out, buf, buf_len); + if (err > 0) + break; + } + + if (err <= 0) + ERR("USB error handling Fail"); + + return err; +} + +static void __clean_up_msg_queue(void *mq_id) +{ + mtp_int32 len = 0; + msgq_ptr_t pkt = { 0 }; + msgq_id_t l_mqid = *(msgq_id_t *)mq_id; + + ret_if(mq_id == NULL); + + _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); + while (TRUE == _util_msgq_receive(l_mqid, (void *)&pkt, + sizeof(msgq_ptr_t) - sizeof(long), 1, &len)) { + g_free(pkt.buffer); + memset(&pkt, 0, sizeof(msgq_ptr_t)); + } + + return; +} + +static void __handle_control_request(mtp_int32 request) +{ + static mtp_bool kernel_reset = FALSE; + static mtp_bool host_cancel = FALSE; + mtp_int32 status = 0; + + switch (request) { + case USB_PTPREQUEST_CANCELIO: + // XXX: Convert cancel request data from little-endian + // before use: le32_to_cpu(x), le16_to_cpu(x). + DBG("USB_PTPREQUEST_CANCELIO\n"); + cancel_req_t cancelreq_data; + + host_cancel = TRUE; + _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); + status = read(g_usb_ep0, &cancelreq_data, sizeof(cancelreq_data)); + if (status < 0) { + char error[256]; + ERR("Failed to read data for CANCELIO request\n: %s", + strerror_r(errno, error, sizeof(error))); + } + break; + + case USB_PTPREQUEST_RESET: + + DBG("USB_PTPREQUEST_RESET\n"); + _reset_mtp_device(); + if (kernel_reset == FALSE) { + kernel_reset = TRUE; + } + + status = read(g_usb_ep0, NULL, 0); + if (status < 0) { + ERR("IOCTL MTP_SEND_RESET_ACK Failed [%d]\n", + status); + } + break; + case USB_PTPREQUEST_GETSTATUS: + + DBG("USB_PTPREQUEST_GETSTATUS"); + + /* Send busy status response just once. This flag is also for + * the case that mtp misses the cancel request packet. + */ + static mtp_bool sent_busy = FALSE; + usb_status_req_t statusreq_data = { 0 }; + mtp_dword num_param = 0; + + memset(&statusreq_data, 0x00, sizeof(usb_status_req_t)); + if (host_cancel == TRUE || (sent_busy == FALSE && + kernel_reset == FALSE)) { + DBG("Send busy response, set host_cancel to FALSE"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_DEVICEBUSY; + host_cancel = FALSE; + } else if (_device_get_phase() == DEVICE_PHASE_NOTREADY) { + statusreq_data.code = + PTP_RESPONSE_TRANSACTIONCANCELLED; + DBG("PTP_RESPONSE_TRANSACTIONCANCELLED"); + statusreq_data.len = (mtp_word)(sizeof(usb_status_req_t) + + (num_param - 2) * sizeof(mtp_dword)); + } else if (_device_get_status() == DEVICE_STATUSOK) { + DBG("PTP_RESPONSE_OK"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_OK; + + if (kernel_reset == TRUE) + kernel_reset = FALSE; + } else { + DBG("PTP_RESPONSE_GEN_ERROR"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_GEN_ERROR; + } + + if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) { + sent_busy = TRUE; + } else { + sent_busy = FALSE; + } + + /* status = ioctl(g_usb_fd, MTP_SET_SETUP_DATA, &statusreq_data); + if (status < 0) { + DBG("IOCTL MTP_SET_SETUP_DATA Fail [%d]\n", + status); + return; + } */ + + break; + + case USB_PTPREQUEST_GETEVENT: + DBG("USB_PTPREQUEST_GETEVENT"); + break; + + default: + DBG("Invalid class specific setup request"); + break; + } + return; +} + +/* + * mtp_bool ffs_transport_mq_deinit() + * This function destroy a message queue for MTP, + * @return This function returns TRUE on success or + * returns FALSE on failure. + */ +static mtp_bool ffs_transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) +{ + mtp_int32 res = TRUE; + + if (*rx_mqid) { + res = _util_msgq_deinit(rx_mqid); + if (res == FALSE) { + ERR("rx_mqid deinit Fail [%d]\n", errno); + } else { + *rx_mqid = 0; + } + } + + if (*tx_mqid) { + res = _util_msgq_deinit(tx_mqid); + if (res == FALSE) { + ERR("tx_mqid deinit fail [%d]\n", errno); + } else { + *tx_mqid = 0; + } + } + + return res; +} + +static mtp_uint32 ffs_transport_get_usb_packet_len(void) +{ + mtp_int32 status = -1; + static mtp_int32 usb_speed = 0; + + if (usb_speed == 0) { + + //status = ioctl(g_usb_fd, MTP_GET_HIGH_FULL_SPEED, &usb_speed); + if (status < 0) { + ERR("MTP_GET_HIGH_FULL_SPEED Failed [%d]\n", status); + return MTP_MAX_PACKET_SIZE_SEND_FS; + } + } + + if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) { + return MTP_MAX_PACKET_SIZE_SEND_FS; + } + + return MTP_MAX_PACKET_SIZE_SEND_HS; +} + + +const mtp_usb_driver_t mtp_usb_driver_ffs = { + .transport_init_usb_device = ffs_transport_init_usb_device, + .transport_deinit_usb_device = ffs_transport_deinit_usb_device, + .transport_thread_usb_write = ffs_transport_thread_usb_write, + .transport_thread_usb_read = ffs_transport_thread_usb_read, + .transport_thread_usb_control = ffs_transport_thread_usb_control, + .transport_mq_init = ffs_transport_mq_init, + .transport_mq_deinit = ffs_transport_mq_deinit, + .transport_get_usb_packet_len = ffs_transport_get_usb_packet_len, + .get_tx_pkt_size = ffs_get_tx_pkt_size, + .get_rx_pkt_size = ffs_get_rx_pkt_size, +}; diff --git a/src/transport/mtp_usb_driver_slp.c b/src/transport/mtp_usb_driver_slp.c new file mode 100644 index 0000000..79f07b0 --- /dev/null +++ b/src/transport/mtp_usb_driver_slp.c @@ -0,0 +1,482 @@ +/* + * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include "mtp_usb_driver.h" +#include "mtp_device.h" +#include "ptp_datacodes.h" +#include "mtp_support.h" +#include "ptp_container.h" +#include "mtp_msgq.h" +#include "mtp_thread.h" +#include "mtp_transport.h" +#include "mtp_event_handler.h" +#include + +/* + * GLOBAL AND EXTERN VARIABLES + */ +extern mtp_config_t g_conf; + +/* + * STATIC VARIABLES AND FUNCTIONS + */ +static mtp_int32 g_usb_fd = -1; +static mtp_max_pkt_size_t pkt_size; +static mtp_uint32 rx_mq_sz; +static mtp_uint32 tx_mq_sz; + +static mtp_int32 __handle_usb_read_err(mtp_int32 err, + mtp_uchar *buf, mtp_int32 buf_len); +static void __clean_up_msg_queue(void *pmsqid); +static void __handle_control_request(mtp_int32 request); +static void __receive_signal(mtp_int32 n, siginfo_t *info, void *unused); + +/* + * FUNCTIONS + */ +static mtp_bool slp_transport_init_usb_device(void) +{ + mtp_int32 status = 0; + struct sigaction sig; + pid_t mtp_pid = 0; + int msg_size; + + /* Kernel will inform to User Space using signal. */ + memset(&sig, 0, sizeof(sig)); + sig.sa_sigaction = __receive_signal; + sig.sa_flags = SA_SIGINFO; + sigaction(SIG_SETUP, &sig, NULL); + + if (g_usb_fd > 0) { + DBG("Device Already open\n"); + return TRUE; + } + + g_usb_fd = open(MTP_DRIVER_PATH, O_RDWR); + if (g_usb_fd < 0) { + ERR("Device node [%s] open failed,errno [%d]\n", MTP_DRIVER_PATH, errno); + return FALSE; + } + + mtp_pid = getpid(); + status = ioctl(g_usb_fd, MTP_SET_USER_PID, &mtp_pid); + if (status < 0) { + ERR("IOCTL MTP_SET_USER_PID failed = [%d]\n", status); + _transport_deinit_usb_device(); + return FALSE; + } + + pkt_size.rx = g_conf.read_usb_size; + pkt_size.tx = g_conf.write_usb_size; + + DBG("Final : Tx pkt size:[%u], Rx pkt size:[%u]\n", pkt_size.tx, pkt_size.rx); + + msg_size = sizeof(msgq_ptr_t) - sizeof(long); + rx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_rx_ipc_size) * msg_size; + tx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_tx_ipc_size) * msg_size; + + DBG("RX MQ size :[%u], TX MQ size:[%u]\n", rx_mq_sz, tx_mq_sz); + + return TRUE; +} + +static void slp_transport_deinit_usb_device(void) +{ + if (g_usb_fd >= 0) + close(g_usb_fd); + g_usb_fd = -1; + + return; +} + +static mtp_uint32 slp_get_tx_pkt_size(void) +{ + return pkt_size.tx; +} + +static mtp_uint32 slp_get_rx_pkt_size(void) +{ + return pkt_size.rx; +} + +/* + * static mtp_int32 slp_transport_mq_init() + * This function create a message queue for MTP, + * A created message queue will be used to help data transfer between + * MTP module and usb buffer. + * @return This function returns TRUE on success or + * returns FALSE on failure. + */ +static mtp_int32 slp_transport_mq_init(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) +{ + if (_util_msgq_init(rx_mqid, 0) == FALSE) { + ERR("RX MQ init Fail [%d]\n", errno); + return FALSE; + } + + if (_util_msgq_set_size(*rx_mqid, rx_mq_sz) == FALSE) + ERR("RX MQ setting size Fail [%d]\n", errno); + + if (_util_msgq_init(tx_mqid, 0) == FALSE) { + ERR("TX MQ init Fail [%d]\n", errno); + _util_msgq_deinit(rx_mqid); + *rx_mqid = -1; + return FALSE; + } + + if (_util_msgq_set_size(*tx_mqid, tx_mq_sz) == FALSE) + ERR("TX MQ setting size Fail [%d]\n", errno); + + return TRUE; +} + +static void *slp_transport_thread_usb_write(void *arg) +{ + mtp_int32 status = 0; + mtp_uint32 len = 0; + unsigned char *mtp_buf = NULL; + msg_type_t mtype = MTP_UNDEFINED_PACKET; + msgq_id_t *mqid = (msgq_id_t *)arg; + + pthread_cleanup_push(__clean_up_msg_queue, mqid); + + do { + /* original LinuxThreads cancelation didn't work right + * so test for it explicitly. + */ + pthread_testcancel(); + + _util_rcv_msg_from_mq(*mqid, &mtp_buf, &len, &mtype); + + if (mtype == MTP_BULK_PACKET || mtype == MTP_DATA_PACKET) { + status = write(g_usb_fd, mtp_buf, len); + if (status < 0) { + ERR("USB write fail : %d\n", errno); + if (errno == ENOMEM || errno == ECANCELED) { + status = 0; + __clean_up_msg_queue(mqid); + } + } + g_free(mtp_buf); + mtp_buf = NULL; + } else if (MTP_EVENT_PACKET == mtype) { + /* Handling the MTP Asynchronous Events */ + DBG("Send Interrupt data to kernel by IOCTL "); + status = ioctl(g_usb_fd, MTP_WRITE_INT_DATA, mtp_buf); + g_free(mtp_buf); + mtp_buf = NULL; + } else if (MTP_ZLP_PACKET == mtype) { + DBG("Send ZLP data to kernel by IOCTL "); + status = ioctl(g_usb_fd, MTP_SET_ZLP_DATA, NULL); + } else { + DBG("mtype = %d is not valid\n", mtype); + status = -1; + } + + if (status < 0) { + ERR("write data to the device node Fail:\ + status = %d\n", status); + break; + } + } while (status >= 0); + + DBG("exited Source thread with status %d\n", status); + pthread_cleanup_pop(1); + g_free(mtp_buf); + + return NULL; +} + +static void *slp_transport_thread_usb_read(void *arg) +{ + mtp_int32 status = 0; + msgq_ptr_t pkt = {MTP_DATA_PACKET, 0, 0, NULL}; + msgq_id_t *mqid = (msgq_id_t *)arg; + mtp_uint32 rx_size = _get_rx_pkt_size(); + + pthread_cleanup_push(__clean_up_msg_queue, mqid); + + do { + pthread_testcancel(); + + pkt.buffer = (mtp_uchar *)g_malloc(rx_size); + if (NULL == pkt.buffer) { + ERR("Sink thread: memalloc Fail."); + break; + } + + status = read(g_usb_fd, pkt.buffer, rx_size); + if (status <= 0) { + status = __handle_usb_read_err(status, pkt.buffer, rx_size); + if (status <= 0) { + ERR("__handle_usb_read_err Fail"); + g_free(pkt.buffer); + break; + } + } + + pkt.length = status; + if (FALSE == _util_msgq_send(*mqid, (void *)&pkt, + sizeof(msgq_ptr_t) - sizeof(long), 0)) { + ERR("msgsnd Fail"); + g_free(pkt.buffer); + } + } while (status > 0); + + DBG("status[%d] errno[%d]\n", status, errno); + pthread_cleanup_pop(1); + + return NULL; +} + +static mtp_int32 __handle_usb_read_err(mtp_int32 err, + mtp_uchar *buf, mtp_int32 buf_len) +{ + mtp_int32 retry = 0; + mtp_bool ret; + + while (retry++ < MTP_USB_ERROR_MAX_RETRY) { + if (err == 0) { + DBG("ZLP(Zero Length Packet). Skip"); + } else if (err < 0 && errno == EINTR) { + DBG("read () is interrupted. Skip"); + } else if (err < 0 && errno == EIO) { + DBG("EIO"); + + if (MTP_PHONE_USB_CONNECTED != + _util_get_local_usb_status()) { + ERR("USB is disconnected"); + break; + } + + _transport_deinit_usb_device(); + ret = _transport_init_usb_device(); + if (ret == FALSE) { + ERR("_transport_init_usb_device Fail"); + continue; + } + } else { + ERR("Unknown error : %d, errno [%d] \n", err, errno); + break; + } + + err = read(g_usb_fd, buf, buf_len); + if (err > 0) + break; + } + + if (err <= 0) + ERR("USB error handling Fail"); + + return err; +} + +static void __clean_up_msg_queue(void *mq_id) +{ + mtp_int32 len = 0; + msgq_ptr_t pkt = { 0 }; + msgq_id_t l_mqid = *(msgq_id_t *)mq_id; + + ret_if(mq_id == NULL); + + _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); + while (TRUE == _util_msgq_receive(l_mqid, (void *)&pkt, + sizeof(msgq_ptr_t) - sizeof(long), 1, &len)) { + g_free(pkt.buffer); + memset(&pkt, 0, sizeof(msgq_ptr_t)); + } + + return; +} + +static void __handle_control_request(mtp_int32 request) +{ + static mtp_bool kernel_reset = FALSE; + static mtp_bool host_cancel = FALSE; + mtp_int32 status = 0; + + switch (request) { + case USB_PTPREQUEST_CANCELIO: + DBG("USB_PTPREQUEST_CANCELIO"); + cancel_req_t cancelreq_data; + mtp_byte buffer[USB_PTPREQUEST_CANCELIO_SIZE + 1] = { 0 }; + + host_cancel = TRUE; + _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION); + status = ioctl(g_usb_fd, MTP_GET_SETUP_DATA, buffer); + if (status < 0) { + ERR("IOCTL GET_SETUP_DATA Fail [%d]\n", status); + return; + } + + memcpy(&(cancelreq_data.io_code), buffer, sizeof(mtp_word)); + memcpy(&(cancelreq_data.tid), &buffer[2], sizeof(mtp_dword)); + DBG("cancel io code [%d], transaction id [%ld]\n", + cancelreq_data.io_code, cancelreq_data.tid); + break; + + case USB_PTPREQUEST_RESET: + + DBG("USB_PTPREQUEST_RESET"); + _reset_mtp_device(); + if (kernel_reset == FALSE) + kernel_reset = TRUE; + + status = ioctl(g_usb_fd, MTP_SEND_RESET_ACK, NULL); + if (status < 0) { + ERR("IOCTL MTP_SEND_RESET_ACK Fail [%d]\n", + status); + } + break; + + case USB_PTPREQUEST_GETSTATUS: + + DBG("USB_PTPREQUEST_GETSTATUS"); + + /* Send busy status response just once. This flag is also for + * the case that mtp misses the cancel request packet. + */ + static mtp_bool sent_busy = FALSE; + usb_status_req_t statusreq_data = { 0 }; + mtp_dword num_param = 0; + + memset(&statusreq_data, 0x00, sizeof(usb_status_req_t)); + if (host_cancel == TRUE || (sent_busy == FALSE && + kernel_reset == FALSE)) { + DBG("Send busy response, set host_cancel to FALSE"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_DEVICEBUSY; + host_cancel = FALSE; + } else if (_device_get_phase() == DEVICE_PHASE_NOTREADY) { + statusreq_data.code = + PTP_RESPONSE_TRANSACTIONCANCELLED; + DBG("PTP_RESPONSE_TRANSACTIONCANCELLED"); + statusreq_data.len = (mtp_word)(sizeof(usb_status_req_t) + + (num_param - 2) * sizeof(mtp_dword)); + } else if (_device_get_status() == DEVICE_STATUSOK) { + DBG("PTP_RESPONSE_OK"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_OK; + + if (kernel_reset == TRUE) + kernel_reset = FALSE; + } else { + DBG("PTP_RESPONSE_GEN_ERROR"); + statusreq_data.len = 0x08; + statusreq_data.code = PTP_RESPONSE_GEN_ERROR; + } + + if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) + sent_busy = TRUE; + else + sent_busy = FALSE; + + status = ioctl(g_usb_fd, MTP_SET_SETUP_DATA, &statusreq_data); + if (status < 0) { + DBG("IOCTL MTP_SET_SETUP_DATA Fail [%d]\n", + status); + return; + } + break; + + case USB_PTPREQUEST_GETEVENT: + DBG("USB_PTPREQUEST_GETEVENT"); + break; + + default: + DBG("Invalid class specific setup request"); + break; + } + return; +} + +static void __receive_signal(mtp_int32 n, siginfo_t *info, void *arg) +{ + mtp_int32 request = info->si_int; + + DBG("Received SIgnal From Kernel\n"); + __handle_control_request(request); + return; +} + +/* + * mtp_bool slp_transport_mq_deinit() + * This function destroy a message queue for MTP, + * @return This function returns TRUE on success or + * returns FALSE on failure. + */ +static mtp_bool slp_transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid) +{ + mtp_int32 res = TRUE; + + if (*rx_mqid) { + res = _util_msgq_deinit(rx_mqid); + if (res == FALSE) + ERR("rx_mqid deinit Fail [%d]\n", errno); + else + *rx_mqid = 0; + } + + if (*tx_mqid) { + res = _util_msgq_deinit(tx_mqid); + if (res == FALSE) { + ERR("tx_mqid deinit fail [%d]\n", errno); + } else { + *tx_mqid = 0; + } + } + + return res; +} + +static mtp_uint32 slp_transport_get_usb_packet_len(void) +{ + mtp_int32 status = 0; + static mtp_int32 usb_speed = 0; + + if (usb_speed == 0) { + + status = ioctl(g_usb_fd, MTP_GET_HIGH_FULL_SPEED, &usb_speed); + if (status < 0) { + ERR("MTP_GET_HIGH_FULL_SPEED Failed [%d]\n", status); + return MTP_MAX_PACKET_SIZE_SEND_FS; + } + } + + if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) + return MTP_MAX_PACKET_SIZE_SEND_FS; + + return MTP_MAX_PACKET_SIZE_SEND_HS; +} + + +const mtp_usb_driver_t mtp_usb_driver_slp = { + .transport_init_usb_device = slp_transport_init_usb_device, + .transport_deinit_usb_device = slp_transport_deinit_usb_device, + .transport_thread_usb_write = slp_transport_thread_usb_write, + .transport_thread_usb_read = slp_transport_thread_usb_read, + .transport_thread_usb_control = NULL, + .transport_mq_init = slp_transport_mq_init, + .transport_mq_deinit = slp_transport_mq_deinit, + .transport_get_usb_packet_len = slp_transport_get_usb_packet_len, + .get_tx_pkt_size = slp_get_tx_pkt_size, + .get_rx_pkt_size = slp_get_rx_pkt_size, +}; -- 2.7.4 From d0a595500968bd7d1b0b7e3a7a271646d8effc89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 8 Feb 2018 14:33:19 +0100 Subject: [PATCH 16/16] Use systemd functionfs socket activation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This includes creating binary files with descriptors and strings at compile time and systemd configuration of ffs socket. Change-Id: Ib38dc151e5e5d047ddb873661505ca13ddfc3306 Signed-off-by: Paweł Szewczyk --- CMakeLists.txt | 12 ++- include/transport/mtp_descs_strings.h | 62 +++++++++++++ mtp-responder.service | 2 + mtp-responder.socket | 6 ++ packaging/mtp-responder.spec | 5 + src/extract_descs/mtp_extract_descs_strs.c | 55 +++++++++++ src/transport/mtp_descs_strings.c | 108 ++++++++++++++++++++++ src/transport/mtp_usb_driver.c | 3 +- src/transport/mtp_usb_driver_ffs.c | 142 +++-------------------------- 9 files changed, 262 insertions(+), 133 deletions(-) create mode 100644 include/transport/mtp_descs_strings.h create mode 100755 mtp-responder.socket create mode 100644 src/extract_descs/mtp_extract_descs_strs.c create mode 100644 src/transport/mtp_descs_strings.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f2ce402..2b384e2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR}/src/util SRCS) INCLUDE(FindPkgConfig) pkg_check_modules(pkgs REQUIRED glib-2.0 capi-content-media-content - capi-media-metadata-extractor vconf dlog tapi capi-system-info storage libsystemd-daemon) + capi-media-metadata-extractor vconf dlog tapi capi-system-info storage libsystemd-daemon libsystemd) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -30,3 +30,13 @@ TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} pthread rt gcrypt) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin) INSTALL(FILES mtp-responder.conf DESTINATION /opt/var/lib/misc) + +ADD_EXECUTABLE(extract_descs_strs ${CMAKE_SOURCE_DIR}/src/extract_descs/mtp_extract_descs_strs.c + ${CMAKE_SOURCE_DIR}/src/transport/mtp_descs_strings.c) +ADD_CUSTOM_COMMAND(OUTPUT descs strs + COMMAND ./extract_descs_strs + DEPENDS extract_descs_strs) +ADD_CUSTOM_TARGET(descs_strs ALL DEPENDS descs strs) + +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/descs DESTINATION /etc/mtp-responder) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/strs DESTINATION /etc/mtp-responder) diff --git a/include/transport/mtp_descs_strings.h b/include/transport/mtp_descs_strings.h new file mode 100644 index 0000000..37747ba --- /dev/null +++ b/include/transport/mtp_descs_strings.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2012, 2013, 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#ifndef FUNCTIONFS_DESCRIPTORS_MAGIC_V2 +#define FUNCTIONFS_DESCRIPTORS_MAGIC_V2 3 +enum functionfs_flags { + FUNCTIONFS_HAS_FS_DESC = 1, + FUNCTIONFS_HAS_HS_DESC = 2, + FUNCTIONFS_HAS_SS_DESC = 4, + FUNCTIONFS_HAS_MS_OS_DESC = 8, +}; +#endif + +#define cpu_to_le16(x) htole16(x) +#define cpu_to_le32(x) htole32(x) +#define le32_to_cpu(x) le32toh(x) +#define le16_to_cpu(x) le16toh(x) + +extern struct mtp_usb_descs{ + struct { + __le32 magic; + __le32 length; + __le32 flags; + __le32 fs_count; + __le32 hs_count; + // __le32 os_count; + } header; + struct { + struct usb_interface_descriptor intf; + struct usb_endpoint_descriptor_no_audio bulk_in; + struct usb_endpoint_descriptor_no_audio bulk_out; + struct usb_endpoint_descriptor_no_audio int_in; + } __attribute__((packed)) fs_descs, hs_descs; + // struct {} __attribute__((packed)) os_descs; +} __attribute__((packed)) descriptors; + +#define STR_INTERFACE "Samsung MTP" + +extern struct mtp_usb_strs { + struct usb_functionfs_strings_head header; + struct { + __le16 code; + const char str1[sizeof(STR_INTERFACE)]; + } __attribute__((packed)) lang0; +} __attribute__((packed)) strings; diff --git a/mtp-responder.service b/mtp-responder.service index 24acdfe..69433c4 100755 --- a/mtp-responder.service +++ b/mtp-responder.service @@ -9,3 +9,5 @@ Type=simple ExecStart=/usr/bin/mtp-responder KillMode=process SmackProcessLabel=System +USBFunctionDescriptors=/etc/mtp-responder/descs +USBFunctionStrings=/etc/mtp-responder/strs diff --git a/mtp-responder.socket b/mtp-responder.socket new file mode 100755 index 0000000..0abae50 --- /dev/null +++ b/mtp-responder.socket @@ -0,0 +1,6 @@ +[Unit] +Description=MTP responder functionfs socket + +[Socket] +ListenUSBFunction=/dev/usb-funcs/mtp +Service=mtp-responder.service diff --git a/packaging/mtp-responder.spec b/packaging/mtp-responder.spec index b1f1861..93187ae 100755 --- a/packaging/mtp-responder.spec +++ b/packaging/mtp-responder.spec @@ -22,6 +22,7 @@ BuildRequires: pkgconfig(capi-media-metadata-extractor) BuildRequires: pkgconfig(capi-system-info) Buildrequires: pkgconfig(storage) BuildRequires: pkgconfig(libsystemd-daemon) +BuildRequires: pkgconfig(libsystemd) Requires(post): /usr/bin/vconftool %define upgrade_script_path /usr/share/upgrade/scripts @@ -53,6 +54,7 @@ mkdir -p %{buildroot}%{upgrade_script_path} cp -f scripts/500.%{name}-upgrade.sh %{buildroot}%{upgrade_script_path} install -D -m 0644 mtp-responder.service %{buildroot}/%{_unitdir}/mtp-responder.service +install -D -m 0644 mtp-responder.socket %{buildroot}/%{_unitdir}/mtp-responder.socket %post mkdir -p %{_sysconfdir}/systemd/default-extra-dependencies/ignore-units.d/ @@ -64,6 +66,9 @@ ln -sf %{_unitdir}/mtp-responder.service %{_sysconfdir}/systemd/default-extra-de %defattr(-,root,root,-) %{_bindir}/mtp-responder %{_unitdir}/mtp-responder.service +%{_unitdir}/mtp-responder.socket %{_prefix}/lib/udev/rules.d/99-mtp-responder.rules /opt/var/lib/misc/mtp-responder.conf %{upgrade_script_path}/500.%{name}-upgrade.sh +/etc/mtp-responder/descs +/etc/mtp-responder/strs diff --git a/src/extract_descs/mtp_extract_descs_strs.c b/src/extract_descs/mtp_extract_descs_strs.c new file mode 100644 index 0000000..947c4e2 --- /dev/null +++ b/src/extract_descs/mtp_extract_descs_strs.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2012, 2013, 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "mtp_descs_strings.h" + +static int write_blob(const char *filename, const void *blob, size_t size) +{ + int ret; + FILE *fp; + + fp = fopen(filename, "w"); + if (!fp) { + fprintf(stderr, "Could not open %s: %m\n", filename); + return -errno; + } + + ret = fwrite(blob, size, 1, fp); + if (ret < 0) { + fprintf(stderr, "Could not write to %s\n"); + goto out; + } + + ret = 0; + +out: + fclose(fp); + return ret; +} + +int main() +{ + int ret; + + ret = write_blob("descs", &descriptors, sizeof(descriptors)); + if (ret < 0) + return ret; + + ret = write_blob("strs", &strings, sizeof(strings)); + return ret; +} diff --git a/src/transport/mtp_descs_strings.c b/src/transport/mtp_descs_strings.c new file mode 100644 index 0000000..f5d90e2 --- /dev/null +++ b/src/transport/mtp_descs_strings.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2012, 2013, 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mtp_descs_strings.h" + +struct mtp_usb_descs descriptors = { + .header = { + .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), + .length = cpu_to_le32(sizeof(descriptors)), + .flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC, // | FUNCTIONFS_HAS_MS_OS_DESC, + .fs_count = 4, + .hs_count = 4, + // .os_count = 0; + }, + .fs_descs = { + // drivers/usb/gadget/f_mtp_slp.c:207 + .intf = { + .bLength = sizeof(descriptors.fs_descs.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bNumEndpoints = 3, + .bInterfaceClass = USB_CLASS_STILL_IMAGE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 1, + }, + .bulk_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(64), + }, + .bulk_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(64), + }, + .int_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 3 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16(64), + .bInterval = 6, + }, + }, + .hs_descs = { + .intf = { + .bLength = sizeof(descriptors.fs_descs.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bNumEndpoints = 3, + .bInterfaceClass = USB_CLASS_STILL_IMAGE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 1, + }, + .bulk_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(512), + }, + .bulk_out = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16(512), + }, + .int_in = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 3 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16(64), + .bInterval = 6, + }, + }, +}; + +struct mtp_usb_strs strings = { + .header = { + .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), + .length = cpu_to_le32(sizeof(strings)), + .str_count = cpu_to_le32(1), + .lang_count = cpu_to_le32(1), + }, + .lang0 = { + cpu_to_le16(0x0409), /* en-us */ + STR_INTERFACE, + }, +}; diff --git a/src/transport/mtp_usb_driver.c b/src/transport/mtp_usb_driver.c index 5576c55..6fde025 100644 --- a/src/transport/mtp_usb_driver.c +++ b/src/transport/mtp_usb_driver.c @@ -15,6 +15,7 @@ */ #include +#include #include "mtp_usb_driver.h" static const mtp_usb_driver_t *usb_driver; @@ -26,7 +27,7 @@ mtp_bool _transport_init_usb_device(void) { if (access(MTP_DRIVER_PATH, F_OK) == 0) { usb_driver = &mtp_usb_driver_slp; - } else if (access(MTP_EP0_PATH, F_OK) == 0) { + } else if (access(MTP_EP0_PATH, F_OK) == 0 || sd_listen_fds(0) >= 4) { usb_driver = &mtp_usb_driver_ffs; } else { ERR("No suport for USB gadgets in kernel"); diff --git a/src/transport/mtp_usb_driver_ffs.c b/src/transport/mtp_usb_driver_ffs.c index e972756..091daa8 100755 --- a/src/transport/mtp_usb_driver_ffs.c +++ b/src/transport/mtp_usb_driver_ffs.c @@ -25,6 +25,7 @@ #include #include "mtp_usb_driver.h" #include "mtp_device.h" +#include "mtp_descs_strings.h" #include "ptp_datacodes.h" #include "mtp_support.h" #include "ptp_container.h" @@ -33,8 +34,7 @@ #include "mtp_transport.h" #include "mtp_event_handler.h" #include -#include -#include +#include /* * GLOBAL AND EXTERN VARIABLES @@ -44,15 +44,6 @@ extern mtp_config_t g_conf; /* * STATIC VARIABLES AND FUNCTIONS */ -#ifndef FUNCTIONFS_DESCRIPTORS_MAGIC_V2 -#define FUNCTIONFS_DESCRIPTORS_MAGIC_V2 3 -enum functionfs_flags { - FUNCTIONFS_HAS_FS_DESC = 1, - FUNCTIONFS_HAS_HS_DESC = 2, - FUNCTIONFS_HAS_SS_DESC = 4, - FUNCTIONFS_HAS_MS_OS_DESC = 8, -}; -#endif /*PIMA15740-2000 spec*/ #define USB_PTPREQUEST_CANCELIO 0x64 /* Cancel request */ @@ -62,126 +53,6 @@ enum functionfs_flags { #define USB_PTPREQUEST_CANCELIO_SIZE 6 #define USB_PTPREQUEST_GETSTATUS_SIZE 12 -#define cpu_to_le16(x) htole16(x) -#define cpu_to_le32(x) htole32(x) -#define le32_to_cpu(x) le32toh(x) -#define le16_to_cpu(x) le16toh(x) - -static const struct { - struct { - __le32 magic; - __le32 length; - __le32 flags; - __le32 fs_count; - __le32 hs_count; - // __le32 os_count; - } header; - struct { - struct usb_interface_descriptor intf; - struct usb_endpoint_descriptor_no_audio bulk_in; - struct usb_endpoint_descriptor_no_audio bulk_out; - struct usb_endpoint_descriptor_no_audio int_in; - } __attribute__((packed)) fs_descs, hs_descs; - // struct {} __attribute__((packed)) os_descs; -} __attribute__((packed)) descriptors = { - .header = { - .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), - .length = cpu_to_le32(sizeof(descriptors)), - .flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC, // | FUNCTIONFS_HAS_MS_OS_DESC, - .fs_count = 4, - .hs_count = 4, - // .os_count = 0; - }, - .fs_descs = { - // drivers/usb/gadget/f_mtp_slp.c:207 - .intf = { - .bLength = sizeof(descriptors.fs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bNumEndpoints = 3, - .bInterfaceClass = USB_CLASS_STILL_IMAGE, - .bInterfaceSubClass = 1, - .bInterfaceProtocol = 1, - .iInterface = 1, - }, - .bulk_in = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16(64), - }, - .bulk_out = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16(64), - }, - .int_in = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 3 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = __constant_cpu_to_le16(64), - .bInterval = 6, - }, - }, - .hs_descs = { - .intf = { - .bLength = sizeof(descriptors.fs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bNumEndpoints = 3, - .bInterfaceClass = USB_CLASS_STILL_IMAGE, - .bInterfaceSubClass = 1, - .bInterfaceProtocol = 1, - .iInterface = 1, - }, - .bulk_in = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16(512), - }, - .bulk_out = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16(512), - }, - .int_in = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 3 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = __constant_cpu_to_le16(64), - .bInterval = 6, - }, - }, -}; - -#define STR_INTERFACE "Samsung MTP" - -static const struct { - struct usb_functionfs_strings_head header; - struct { - __le16 code; - const char str1[sizeof(STR_INTERFACE)]; - } __attribute__((packed)) lang0; -} __attribute__((packed)) strings = { - .header = { - .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), - .length = cpu_to_le32(sizeof(strings)), - .str_count = cpu_to_le32(1), - .lang_count = cpu_to_le32(1), - }, - .lang0 = { - cpu_to_le16(0x0409), /* en-us */ - STR_INTERFACE, - }, -}; - static mtp_int32 g_usb_ep0 = -1; /* read (g_usb_ep0, ...) */ static mtp_int32 g_usb_ep_in = -1; /* write (g_usb_ep_in, ...) */ static mtp_int32 g_usb_ep_out = -1; /* read (g_usb_ep_out, ...) */ @@ -203,6 +74,15 @@ static mtp_bool __io_init() { int ret; + if (sd_listen_fds(0) >= 4) { + g_usb_ep0 = SD_LISTEN_FDS_START; + g_usb_ep_in = SD_LISTEN_FDS_START + 1; + g_usb_ep_out = SD_LISTEN_FDS_START + 2; + g_usb_ep_status = SD_LISTEN_FDS_START + 3; + + return TRUE; + } + g_usb_ep0 = open(MTP_EP0_PATH, O_RDWR); if (g_usb_ep0 < 0) goto out; -- 2.7.4