summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2025-09-21 21:39:27 +0700
committerhathach <[email protected]>2025-09-21 21:39:27 +0700
commit4c818998d499a5a22c27ddfafc618e04ddcb823f (patch)
tree0847555c60caf49730f84ba81dad1a853b7a8ceb
parentf5a3f25456bcfae65de9b60c8073986f4cdc8f16 (diff)
implement get object info
-rw-r--r--examples/device/mtp/src/mtp_fs_example.c85
-rw-r--r--src/class/mtp/mtp.h35
-rw-r--r--src/class/mtp/mtp_device.c25
-rw-r--r--src/class/mtp/mtp_device_storage.h4
4 files changed, 60 insertions, 89 deletions
diff --git a/examples/device/mtp/src/mtp_fs_example.c b/examples/device/mtp/src/mtp_fs_example.c
index add7d6bf5..20d09508f 100644
--- a/examples/device/mtp/src/mtp_fs_example.c
+++ b/examples/device/mtp/src/mtp_fs_example.c
@@ -65,7 +65,7 @@ typedef struct {
static fs_object_info_t fs_objects[FS_MAX_NODES] = {
{
.handle = 1,
- .parent = 0xffffffff,
+ .parent = 0,
.allocated = true,
.association = false,
.name = "readme.txt",
@@ -80,7 +80,7 @@ static fs_object_info_t fs_objects[FS_MAX_NODES] = {
#define STORAGE_DESCRIPTRION { 'd', 'i', 's', 'k', 0 }
#define VOLUME_IDENTIFIER { 'v', 'o', 'l', 0 }
-typedef MTP_STORAGE_INFO_TYPEDEF(TU_ARRAY_SIZE((uint16_t[]) STORAGE_DESCRIPTRION),
+typedef MTP_STORAGE_INFO_STRUCT(TU_ARRAY_SIZE((uint16_t[]) STORAGE_DESCRIPTRION),
TU_ARRAY_SIZE(((uint16_t[])VOLUME_IDENTIFIER))
) storage_info_t;
@@ -261,7 +261,8 @@ int32_t tud_mtp_command_received_cb(uint8_t idx, mtp_generic_container_t* cmd_bl
uint32_t handles[FS_MAX_NODES] = { 0 };
uint32_t count = 0;
for (uint8_t i = 0, h = 0; i < FS_MAX_NODES; i++) {
- if (fs_objects[i].allocated && parent_handle == fs_objects[i].parent) {
+ if (fs_objects[i].allocated && parent_handle == fs_objects[i].parent ||
+ (parent_handle == 0xFFFFFFFF && fs_objects[i].parent == 0)) {
handles[count++] = fs_objects[i].handle;
}
}
@@ -270,6 +271,40 @@ int32_t tud_mtp_command_received_cb(uint8_t idx, mtp_generic_container_t* cmd_bl
break;
}
+ case MTP_OP_GET_OBJECT_INFO: {
+ const uint32_t object_handle = cmd_block->data[0];
+ fs_object_info_t* obj = fs_object_get_from_handle(object_handle);
+ if (obj == NULL) {
+ TU_LOG1("ERR: Object with handle %ld does not exist\r\n", object_handle);
+ return MTP_RESP_INVALID_OBJECT_HANDLE;
+ }
+ mtp_object_info_header_t object_info_header = {
+ .storage_id = SUPPORTED_STORAGE_ID,
+ .object_format = MTP_OBJ_FORMAT_TEXT,
+ .protection_status = MTP_PROTECTION_STATUS_NO_PROTECTION,
+ .object_compressed_size = obj->size,
+ .thumb_format = MTP_OBJ_FORMAT_UNDEFINED,
+ .thumb_compressed_size = 0,
+ .thumb_pix_width = 0,
+ .thumb_pix_height = 0,
+ .image_pix_width = 0,
+ .image_pix_height = 0,
+ .image_bit_depth = 0,
+ .parent_object = obj->parent,
+ .association_type = MTP_ASSOCIATION_UNDEFINED,
+ .association_desc = 0,
+ .sequence_number = 0
+ };
+ mtp_container_add_raw(out_block, &object_info_header, sizeof(object_info_header));
+ mtp_container_add_cstring(out_block, obj->name);
+ mtp_container_add_cstring(out_block, obj->created);
+ mtp_container_add_cstring(out_block, obj->modified);
+ mtp_container_add_cstring(out_block, ""); // keywords, not used
+
+ tud_mtp_data_send(out_block);
+ break;
+ }
+
default: return MTP_RESP_OPERATION_NOT_SUPPORTED;
}
@@ -346,7 +381,7 @@ mtp_response_t tud_mtp_storage_association_get_object_handle(uint32_t storage_id
}
mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t parent_object,
- uint32_t* new_object_handle, const mtp_object_info_t* info) {
+ uint32_t* new_object_handle, const mtp_object_info_header_t* info) {
fs_object_info_t* obj = NULL;
if (_fs_operation.session_id == 0) {
@@ -402,7 +437,7 @@ mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t p
obj->association = info->object_format == MTP_OBJ_FORMAT_ASSOCIATION;
// Extract variable data
- uint16_t offset_data = sizeof(mtp_object_info_t);
+ uint16_t offset_data = sizeof(mtp_object_info_header_t);
mtpd_gct_get_string(&offset_data, obj->name, FS_MAX_NODE_NAME_LEN);
mtpd_gct_get_string(&offset_data, obj->created, FS_ISODATETIME_LEN);
mtpd_gct_get_string(&offset_data, obj->modified, FS_ISODATETIME_LEN);
@@ -417,46 +452,6 @@ mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t p
return MTP_RESP_OK;
}
-mtp_response_t tud_mtp_storage_object_read_info(uint32_t object_handle, mtp_object_info_t* info) {
- const fs_object_info_t* obj;
-
- if (_fs_operation.session_id == 0) {
- TU_LOG1("ERR: Session not open\r\n");
- return MTP_RESP_SESSION_NOT_OPEN;
- }
-
- obj = fs_object_get_from_handle(object_handle);
- if (obj == NULL) {
- TU_LOG1("ERR: Object with handle %ld does not exist\r\n", object_handle);
- return MTP_RESP_INVALID_OBJECT_HANDLE;
- }
-
- memset(info, 0, sizeof(mtp_object_info_t));
- info->storage_id = STORAGE_ID(0x0001, 0x0001);
- if (obj->association) {
- info->object_format = MTP_OBJ_FORMAT_ASSOCIATION;
- info->protection_status = MTP_PROTECTION_STATUS_NO_PROTECTION;
- info->object_compressed_size = 0;
- info->association_type = MTP_ASSOCIATION_UNDEFINED;
- } else {
- info->object_format = MTP_OBJ_FORMAT_UNDEFINED;
- info->protection_status = MTP_PROTECTION_STATUS_NO_PROTECTION;
- info->object_compressed_size = obj->size;
- info->association_type = MTP_ASSOCIATION_UNDEFINED;
- }
- info->thumb_format = MTP_OBJ_FORMAT_UNDEFINED;
- info->parent_object = obj->parent;
-
- mtpd_gct_append_wstring(obj->name);
- mtpd_gct_append_wstring(obj->created); // date_created
- mtpd_gct_append_wstring(obj->modified); // date_modified
- mtpd_gct_append_wstring(""); // keywords, not used
-
- TU_LOG1("Retrieve object %s with handle %ld\r\n", obj->name, obj->handle);
-
- return MTP_RESP_OK;
-}
-
mtp_response_t tud_mtp_storage_object_write(uint32_t object_handle, const uint8_t* buffer, uint32_t size) {
fs_object_info_t* obj;
diff --git a/src/class/mtp/mtp.h b/src/class/mtp/mtp.h
index c6ff2946a..61adc29e4 100644
--- a/src/class/mtp/mtp.h
+++ b/src/class/mtp/mtp.h
@@ -647,11 +647,12 @@ typedef enum {
typedef enum {
MTP_ASSOCIATION_UNDEFINED = 0x0000u,
MTP_ASSOCIATION_GENERIC_FOLDER = 0x0001u,
- MTP_ASSOCIATION_GENERIC_ALBUM = 0x0002u,
+ MTP_ASSOCIATION_ALBUM = 0x0002u,
MTP_ASSOCIATION_TIME_SEQUENCE = 0x0003u,
MTP_ASSOCIATION_HORIZONTAL_PANORAMIC = 0x0004u,
MTP_ASSOCIATION_VERTICAL_PANORAMIC = 0x0005u,
MTP_ASSOCIATION_2D_PANORAMIC = 0x0006u,
+ MTP_ASSOCIATION_ANCILLARY_DATA = 0x0007u,
} mtp_association_t;
//--------------------------------------------------------------------+
@@ -705,7 +706,7 @@ typedef union TU_ATTR_PACKED {
uint32_t id;
} mtp_storage_id_t;
-#define MTP_STORAGE_INFO_TYPEDEF(_storage_desc_chars, _volume_id_chars) \
+#define MTP_STORAGE_INFO_STRUCT(_storage_desc_chars, _volume_id_chars) \
struct TU_ATTR_PACKED { \
uint16_t storage_type; \
uint16_t filesystem_type; \
@@ -717,29 +718,24 @@ typedef union TU_ATTR_PACKED {
mtp_string_t(_volume_id_chars) volume_identifier; \
}
-// ObjectInfo Dataset
+// Object Info Dataset without dynamic string: filename, date_created, date_modified, keywords
typedef struct TU_ATTR_PACKED {
uint32_t storage_id;
uint16_t object_format;
uint16_t protection_status;
uint32_t object_compressed_size;
- uint16_t thumb_format; // unused
- uint32_t thumb_compressed_size; // unused
- uint32_t thumb_pix_width; // unused
- uint32_t thumb_pix_height; // unused
- uint32_t image_pix_width; // unused
- uint32_t image_pix_height; // unused
- uint32_t image_bit_depth; // unused
+ uint16_t thumb_format;
+ uint32_t thumb_compressed_size;
+ uint32_t thumb_pix_width;
+ uint32_t thumb_pix_height;
+ uint32_t image_pix_width;
+ uint32_t image_pix_height;
+ uint32_t image_bit_depth;
uint32_t parent_object; // 0: root
uint16_t association_type;
- uint32_t association_description; // not used
- uint32_t sequence_number; // not used
-} mtp_object_info_t;
-// The following fields will be dynamically added to the struct at runtime:
-// - wstring filename;
-// - datetime_wstring date_created;
-// - datetime_wstring date_modified;
-// - wstring keywords;
+ uint32_t association_desc;
+ uint32_t sequence_number;
+} mtp_object_info_header_t;
// Device property desc up to get/set
typedef struct TU_ATTR_PACKED {
@@ -754,7 +750,7 @@ typedef struct TU_ATTR_PACKED {
// - uint8_t form_flag;
// no form
-#define MTP_DEVICE_PROPERTIES_TYPEDEF(_type) \
+#define MTP_DEVICE_PROPERTIES_STRUCT(_type) \
struct TU_ATTR_PACKED { \
uint16_t device_property_code; \
uint16_t datatype; \
@@ -764,7 +760,6 @@ typedef struct TU_ATTR_PACKED {
uint8_t form_flag; /* 0: none, 1: range, 2: enum */ \
};
-
typedef struct TU_ATTR_PACKED {
uint16_t wLength;
uint16_t code;
diff --git a/src/class/mtp/mtp_device.c b/src/class/mtp/mtp_device.c
index 8e403f9ac..22ef45cd9 100644
--- a/src/class/mtp/mtp_device.c
+++ b/src/class/mtp/mtp_device.c
@@ -85,7 +85,6 @@ static mtp_phase_type_t mtpd_chk_session_open(const char *func_name);
static mtp_phase_type_t mtpd_handle_cmd(mtpd_interface_t* p_mtp);
static mtp_phase_type_t mtpd_handle_data(void);
static mtp_phase_type_t mtpd_handle_cmd_close_session(void);
-static mtp_phase_type_t mtpd_handle_cmd_get_object_info(void);
static mtp_phase_type_t mtpd_handle_cmd_get_object(void);
static mtp_phase_type_t mtpd_handle_dti_get_object(void);
static mtp_phase_type_t mtpd_handle_cmd_delete_object(void);
@@ -454,7 +453,8 @@ mtp_phase_type_t mtpd_handle_cmd(mtpd_interface_t* p_mtp) {
case MTP_OP_GET_OBJECT_INFO:
TU_LOG_DRV(" MTP command: MTP_OP_GET_OBJECT_INFO\n");
- return mtpd_handle_cmd_get_object_info();
+ break;
+
case MTP_OP_GET_OBJECT:
TU_LOG_DRV(" MTP command: MTP_OP_GET_OBJECT\n");
return mtpd_handle_cmd_get_object();
@@ -528,25 +528,6 @@ mtp_phase_type_t mtpd_handle_cmd_close_session(void)
return MTP_PHASE_RESPONSE;
}
-mtp_phase_type_t mtpd_handle_cmd_get_object_info(void)
-{
- TU_VERIFY_STATIC(sizeof(mtp_object_info_t) < MTP_MAX_PACKET_SIZE, "mtp_object_info_t shall fit in MTP_MAX_PACKET_SIZE");
- mtp_generic_container_t* p_container = &_mtpd_epbuf.container;
- uint32_t object_handle = p_container->data[0];
-
- p_container->len = MTP_CONTAINER_HEADER_LENGTH + sizeof(mtp_object_info_t);
- p_container->type = MTP_CONTAINER_TYPE_DATA_BLOCK;
- p_container->code = MTP_OP_GET_OBJECT_INFO;
- mtp_response_t res = tud_mtp_storage_object_read_info(object_handle, (mtp_object_info_t *)p_container->data);
- mtp_phase_type_t phase;
- if ((phase = mtpd_chk_generic(__func__, (res != MTP_RESP_OK), res, "")) != MTP_PHASE_NONE) {
- return phase;
- }
-
- _mtpd_itf.queued_len = p_container->len;
- return MTP_PHASE_DATA_IN;
-}
-
mtp_phase_type_t mtpd_handle_cmd_get_object(void)
{
mtp_generic_container_t* p_container = &_mtpd_epbuf.container;
@@ -634,7 +615,7 @@ mtp_phase_type_t mtpd_handle_dto_send_object_info(void)
{
mtp_generic_container_t* p_container = &_mtpd_epbuf.container;
uint32_t new_object_handle = 0;
- mtp_response_t res = tud_mtp_storage_object_write_info(_mtpd_soi.storage_id, _mtpd_soi.parent_object_handle, &new_object_handle, (mtp_object_info_t *)p_container->data);
+ mtp_response_t res = tud_mtp_storage_object_write_info(_mtpd_soi.storage_id, _mtpd_soi.parent_object_handle, &new_object_handle, (mtp_object_info_header_t *)p_container->data);
mtp_phase_type_t phase;
if ((phase = mtpd_chk_generic(__func__, (res != MTP_RESP_OK), res, "")) != MTP_PHASE_NONE) return phase;
diff --git a/src/class/mtp/mtp_device_storage.h b/src/class/mtp/mtp_device_storage.h
index 6c12d38e3..29d4b0bd5 100644
--- a/src/class/mtp/mtp_device_storage.h
+++ b/src/class/mtp/mtp_device_storage.h
@@ -77,7 +77,7 @@ mtp_response_t tud_mtp_storage_association_get_object_handle(uint32_t session_ha
// The handle of the new object shall be returned in new_object_handle.
// The structure info contains the information to be used for file creation, as passted by the host.
// Note that the variable information (e.g. wstring file name, dates and tags shall be retrieved by using the library functions)
-mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t parent_object, uint32_t *new_object_handle, const mtp_object_info_t *info);
+mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t parent_object, uint32_t *new_object_handle, const mtp_object_info_header_t *info);
// Get object information related to a given object handle
//
@@ -89,7 +89,7 @@ mtp_response_t tud_mtp_storage_object_write_info(uint32_t storage_id, uint32_t p
// - Date modified (string, use tud_gct_append_date or empty string)
// - Keywords (string containing list of kw, separated by space, use tud_mtp_gct_append_wstring)
// Note that the variable information (e.g. wstring file name, dates and tags shall be written by using the library functions)
-mtp_response_t tud_mtp_storage_object_read_info(uint32_t object_handle, mtp_object_info_t *info);
+mtp_response_t tud_mtp_storage_object_read_info(uint32_t object_handle, mtp_object_info_header_t *info);
// Get the object size.
//