1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 Ennebi Elettronica (https://ennebielettronica.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "bsp/board_api.h"
#include "tusb.h"
#include "tinyusb_logo_png.h"
//--------------------------------------------------------------------+
// Dataset
//--------------------------------------------------------------------+
//------------- device info -------------//
#define DEV_INFO_MANUFACTURER "TinyUSB"
#define DEV_INFO_MODEL "MTP Example"
#define DEV_INFO_VERSION "1.0"
#define DEV_PROP_FRIENDLY_NAME "TinyUSB MTP"
//------------- storage info -------------//
#define STORAGE_DESCRIPTION { 'd', 'i', 's', 'k', 0 }
#define VOLUME_IDENTIFIER { 'v', 'o', 'l', 0 }
enum {
STORAGE_DESC_LEN = TU_ARRAY_SIZE((uint16_t[]) STORAGE_DESCRIPTION),
VOLUME_ID_LEN = TU_ARRAY_SIZE((uint16_t[])VOLUME_IDENTIFIER)
};
typedef MTP_STORAGE_INFO_STRUCT(STORAGE_DESC_LEN, VOLUME_ID_LEN) storage_info_t;
storage_info_t storage_info = {
#ifdef CFG_EXAMPLE_MTP_READONLY
.storage_type = MTP_STORAGE_TYPE_FIXED_ROM,
#else
.storage_type = MTP_STORAGE_TYPE_FIXED_RAM,
#endif
.filesystem_type = MTP_FILESYSTEM_TYPE_GENERIC_HIERARCHICAL,
.access_capability = MTP_ACCESS_CAPABILITY_READ_WRITE,
.max_capacity_in_bytes = 0, // calculated at runtime
.free_space_in_bytes = 0, // calculated at runtime
.free_space_in_objects = 0, // calculated at runtime
.storage_description = {
.count = (TU_FIELD_SIZE(storage_info_t, storage_description)-1) / sizeof(uint16_t),
.utf16 = STORAGE_DESCRIPTION
},
.volume_identifier = {
.count = (TU_FIELD_SIZE(storage_info_t, volume_identifier)-1) / sizeof(uint16_t),
.utf16 = VOLUME_IDENTIFIER
}
};
//--------------------------------------------------------------------+
// MTP FILESYSTEM
//--------------------------------------------------------------------+
// only allow to add 1 more object to make it simpler to manage memory
#define FS_MAX_FILE_COUNT 3UL
#define FS_MAX_FILENAME_LEN 16
#ifdef CFG_EXAMPLE_MTP_READONLY
#define FS_MAX_CAPACITY_BYTES 0
#else
#define FS_MAX_CAPACITY_BYTES (4 * 1024UL)
// object data buffer (excluding 2 predefined files) with simple allocation pointer
uint8_t fs_buf[FS_MAX_CAPACITY_BYTES];
#endif
#define FS_FIXED_DATETIME "20250808T173500.0" // "YYYYMMDDTHHMMSS.s"
#define README_TXT_CONTENT "TinyUSB MTP Filesystem example"
typedef struct {
uint16_t name[FS_MAX_FILENAME_LEN];
uint16_t object_format;
uint16_t protection_status;
uint32_t image_pix_width;
uint32_t image_pix_height;
uint32_t image_bit_depth;
uint32_t parent;
uint16_t association_type;
uint32_t size;
uint8_t* data;
} fs_file_t;
// Files system, handle is index + 1
static fs_file_t fs_objects[FS_MAX_FILE_COUNT] = {
{
.name = { 'r', 'e', 'a', 'd', 'm', 'e', '.', 't', 'x', 't', 0 }, // readme.txt
.object_format = MTP_OBJ_FORMAT_TEXT,
.protection_status = MTP_PROTECTION_STATUS_READ_ONLY,
.image_pix_width = 0,
.image_pix_height = 0,
.image_bit_depth = 0,
.parent = 0,
.association_type = MTP_ASSOCIATION_UNDEFINED,
.size = sizeof(README_TXT_CONTENT)-1,
.data = (uint8_t*) (uintptr_t) README_TXT_CONTENT,
},
{
.name = { 't', 'i', 'n', 'y', 'u', 's', 'b', '.', 'p', 'n', 'g', 0 }, // "tinyusb.png"
.object_format = MTP_OBJ_FORMAT_PNG,
.protection_status = MTP_PROTECTION_STATUS_READ_ONLY,
.image_pix_width = 128,
.image_pix_height = 64,
.image_bit_depth = 32,
.parent = 0,
.association_type = MTP_ASSOCIATION_UNDEFINED,
.size = LOGO_LEN,
.data = (uint8_t*) (uintptr_t) logo_bin
}
};
enum {
SUPPORTED_STORAGE_ID = 0x00010001u // physical = 1, logical = 1
};
static int32_t fs_get_device_info(tud_mtp_cb_data_t* cb_data);
static int32_t fs_open_close_session(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_storage_ids(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_storage_info(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_device_properties(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_object_handles(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_object_info(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_object(tud_mtp_cb_data_t* cb_data);
static int32_t fs_get_partial_object(tud_mtp_cb_data_t* cb_data);
static int32_t fs_delete_object(tud_mtp_cb_data_t* cb_data);
static int32_t fs_send_object_info(tud_mtp_cb_data_t* cb_data);
static int32_t fs_send_object(tud_mtp_cb_data_t* cb_data);
typedef int32_t (*fs_op_handler_t)(tud_mtp_cb_data_t* cb_data);
typedef struct {
uint32_t op_code;
fs_op_handler_t handler;
}fs_op_handler_dict_t;
fs_op_handler_dict_t fs_op_handler_dict[] = {
{ MTP_OP_GET_DEVICE_INFO, fs_get_device_info },
{ MTP_OP_OPEN_SESSION, fs_open_close_session },
{ MTP_OP_CLOSE_SESSION, fs_open_close_session },
{ MTP_OP_GET_STORAGE_IDS, fs_get_storage_ids },
{ MTP_OP_GET_STORAGE_INFO, fs_get_storage_info },
{ MTP_OP_GET_DEVICE_PROP_DESC, fs_get_device_properties },
{ MTP_OP_GET_DEVICE_PROP_VALUE, fs_get_device_properties },
{ MTP_OP_GET_OBJECT_HANDLES, fs_get_object_handles },
{ MTP_OP_GET_OBJECT_INFO, fs_get_object_info },
{ MTP_OP_GET_OBJECT, fs_get_object },
{ MTP_OP_GET_PARTIAL_OBJECT, fs_get_partial_object },
{ MTP_OP_DELETE_OBJECT, fs_delete_object },
{ MTP_OP_SEND_OBJECT_INFO, fs_send_object_info },
{ MTP_OP_SEND_OBJECT, fs_send_object },
};
static bool is_session_opened = false;
static uint32_t send_obj_handle = 0;
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
// Get pointer to object info from handle
static inline fs_file_t* fs_get_file(uint32_t handle) {
if (handle == 0 || handle > FS_MAX_FILE_COUNT) {
return NULL;
}
return &fs_objects[handle-1];
}
static inline bool fs_file_exist(fs_file_t* f) {
return f->name[0] != 0;
}
// Get the number of allocated nodes in filesystem
static uint32_t fs_get_file_count(void) {
uint32_t count = 0;
for (size_t i = 0; i < FS_MAX_FILE_COUNT; i++) {
if (fs_file_exist(&fs_objects[i])) {
count++;
}
}
return count;
}
static inline fs_file_t* fs_create_file(void) {
for (size_t i = 0; i < FS_MAX_FILE_COUNT; i++) {
fs_file_t* f = &fs_objects[i];
if (!fs_file_exist(f)) {
send_obj_handle = i + 1;
return f;
}
}
return NULL;
}
// simple malloc
static inline uint8_t* fs_malloc(size_t size) {
#ifdef CFG_EXAMPLE_MTP_READONLY
(void) size;
return NULL;
#else
if (size > FS_MAX_CAPACITY_BYTES) {
return NULL;
}
return fs_buf;
#endif
}
//--------------------------------------------------------------------+
// Control Request callback
//--------------------------------------------------------------------+
bool tud_mtp_request_cancel_cb(tud_mtp_request_cb_data_t* cb_data) {
mtp_request_reset_cancel_data_t cancel_data;
memcpy(&cancel_data, cb_data->buf, sizeof(cancel_data));
(void) cancel_data.code;
(void ) cancel_data.transaction_id;
return true;
}
// Invoked when received Device Reset request
// return false to stall the request
bool tud_mtp_request_device_reset_cb(tud_mtp_request_cb_data_t* cb_data) {
(void) cb_data;
return true;
}
// Invoked when received Get Extended Event request. Application fill callback data's buffer for response
// return negative to stall the request
int32_t tud_mtp_request_get_extended_event_cb(tud_mtp_request_cb_data_t* cb_data) {
(void) cb_data;
return false; // not implemented yet
}
// Invoked when received Get DeviceStatus request. Application fill callback data's buffer for response
// return negative to stall the request
int32_t tud_mtp_request_get_device_status_cb(tud_mtp_request_cb_data_t* cb_data) {
uint16_t* buf16 = (uint16_t*)(uintptr_t) cb_data->buf;
buf16[0] = 4; // length
buf16[1] = MTP_RESP_OK; // status
return 4;
}
//--------------------------------------------------------------------+
// Bulk Only Protocol
//--------------------------------------------------------------------+
int32_t tud_mtp_command_received_cb(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
fs_op_handler_t handler = NULL;
for (size_t i = 0; i < TU_ARRAY_SIZE(fs_op_handler_dict); i++) {
if (fs_op_handler_dict[i].op_code == command->header.code) {
handler = fs_op_handler_dict[i].handler;
break;
}
}
int32_t resp_code;
if (handler == NULL) {
resp_code = MTP_RESP_OPERATION_NOT_SUPPORTED;
} else {
resp_code = handler(cb_data);
}
if (resp_code > MTP_RESP_UNDEFINED) {
// send response if needed
io_container->header->code = (uint16_t)resp_code;
tud_mtp_response_send(io_container);
}
return resp_code;
}
int32_t tud_mtp_data_xfer_cb(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
fs_op_handler_t handler = NULL;
for (size_t i = 0; i < TU_ARRAY_SIZE(fs_op_handler_dict); i++) {
if (fs_op_handler_dict[i].op_code == command->header.code) {
handler = fs_op_handler_dict[i].handler;
break;
}
}
int32_t resp_code;
if (handler == NULL) {
resp_code = MTP_RESP_OPERATION_NOT_SUPPORTED;
} else {
resp_code = handler(cb_data);
}
if (resp_code > MTP_RESP_UNDEFINED) {
// send response if needed
io_container->header->code = (uint16_t)resp_code;
tud_mtp_response_send(io_container);
}
return 0;
}
int32_t tud_mtp_data_complete_cb(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* resp = &cb_data->io_container;
switch (command->header.code) {
case MTP_OP_SEND_OBJECT_INFO: {
fs_file_t* f = fs_get_file(send_obj_handle);
if (f == NULL) {
resp->header->code = MTP_RESP_GENERAL_ERROR;
break;
}
// parameter is: storage id, parent handle, new handle
(void) mtp_container_add_uint32(resp, SUPPORTED_STORAGE_ID);
(void) mtp_container_add_uint32(resp, f->parent);
(void) mtp_container_add_uint32(resp, send_obj_handle);
resp->header->code = MTP_RESP_OK;
break;
}
case MTP_OP_GET_PARTIAL_OBJECT: {
// response parameter: actual length of data sent excluding container header
const uint32_t len = cb_data->total_xferred_bytes - sizeof(mtp_container_header_t);
(void) mtp_container_add_uint32(resp, len);
resp->header->code = MTP_RESP_OK;
break;
}
default:
resp->header->code = (cb_data->xfer_result == XFER_RESULT_SUCCESS) ? MTP_RESP_OK : MTP_RESP_GENERAL_ERROR;
break;
}
tud_mtp_response_send(resp);
return 0;
}
int32_t tud_mtp_response_complete_cb(tud_mtp_cb_data_t* cb_data) {
(void) cb_data;
return 0; // nothing to do
}
//--------------------------------------------------------------------+
// File System Handlers
//--------------------------------------------------------------------+
static int32_t fs_get_device_info(tud_mtp_cb_data_t* cb_data) {
// Device info is already prepared up to playback formats. Application only need to add string fields
int32_t resp_code = 0;
mtp_container_info_t* io_container = &cb_data->io_container;
(void) mtp_container_add_cstring(io_container, DEV_INFO_MANUFACTURER);
(void) mtp_container_add_cstring(io_container, DEV_INFO_MODEL);
(void) mtp_container_add_cstring(io_container, DEV_INFO_VERSION);
enum { MAX_SERIAL_NCHARS = 32 };
uint16_t serial_utf16[MAX_SERIAL_NCHARS+1];
size_t nchars = board_usb_get_serial(serial_utf16, MAX_SERIAL_NCHARS);
serial_utf16[tu_min32(nchars, MAX_SERIAL_NCHARS)] = 0; // ensure null termination
(void) mtp_container_add_string(io_container, serial_utf16);
if (!tud_mtp_data_send(io_container)) {
resp_code = MTP_RESP_DEVICE_BUSY;
}
return resp_code;
}
static int32_t fs_open_close_session(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
if (command->header.code == MTP_OP_OPEN_SESSION) {
if (is_session_opened) {
return MTP_RESP_SESSION_ALREADY_OPEN;
}
is_session_opened = true;
} else { // close session
if (!is_session_opened) {
return MTP_RESP_SESSION_NOT_OPEN;
}
is_session_opened = false;
}
return MTP_RESP_OK;
}
static int32_t fs_get_storage_ids(tud_mtp_cb_data_t* cb_data) {
mtp_container_info_t* io_container = &cb_data->io_container;
uint32_t storage_ids [] = { SUPPORTED_STORAGE_ID };
(void) mtp_container_add_auint32(io_container, 1, storage_ids);
tud_mtp_data_send(io_container);
return 0;
}
static int32_t fs_get_storage_info(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t storage_id = command->params[0];
TU_VERIFY(SUPPORTED_STORAGE_ID == storage_id, -1);
// update storage info with current free space
storage_info.max_capacity_in_bytes = sizeof(README_TXT_CONTENT) + LOGO_LEN + FS_MAX_CAPACITY_BYTES;
storage_info.free_space_in_objects = FS_MAX_FILE_COUNT - fs_get_file_count();
storage_info.free_space_in_bytes = storage_info.free_space_in_objects ? FS_MAX_CAPACITY_BYTES : 0;
(void) mtp_container_add_raw(io_container, &storage_info, sizeof(storage_info));
tud_mtp_data_send(io_container);
return 0;
}
static int32_t fs_get_device_properties(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint16_t dev_prop_code = (uint16_t) command->params[0];
if (command->header.code == MTP_OP_GET_DEVICE_PROP_DESC) {
// get describing dataset
mtp_device_prop_desc_header_t device_prop_header;
device_prop_header.device_property_code = dev_prop_code;
switch (dev_prop_code) {
case MTP_DEV_PROP_DEVICE_FRIENDLY_NAME:
device_prop_header.datatype = MTP_DATA_TYPE_STR;
device_prop_header.get_set = MTP_MODE_GET;
(void) mtp_container_add_raw(io_container, &device_prop_header, sizeof(device_prop_header));
(void) mtp_container_add_cstring(io_container, DEV_PROP_FRIENDLY_NAME); // factory
(void) mtp_container_add_cstring(io_container, DEV_PROP_FRIENDLY_NAME); // current
(void) mtp_container_add_uint8(io_container, 0); // no form
tud_mtp_data_send(io_container);
break;
default:
return MTP_RESP_PARAMETER_NOT_SUPPORTED;
}
} else {
// get value
switch (dev_prop_code) {
case MTP_DEV_PROP_DEVICE_FRIENDLY_NAME:
(void) mtp_container_add_cstring(io_container, DEV_PROP_FRIENDLY_NAME);
tud_mtp_data_send(io_container);
break;
default:
return MTP_RESP_PARAMETER_NOT_SUPPORTED;
}
}
return 0;
}
static int32_t fs_get_object_handles(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t storage_id = command->params[0];
const uint32_t obj_format = command->params[1]; // optional
const uint32_t parent_handle = command->params[2]; // folder handle, 0xFFFFFFFF is root
(void)obj_format;
if (storage_id != 0xFFFFFFFFu && storage_id != SUPPORTED_STORAGE_ID) {
return MTP_RESP_INVALID_STORAGE_ID;
}
uint32_t handles[FS_MAX_FILE_COUNT] = { 0 };
uint32_t count = 0u;
for (uint8_t i = 0u; i < FS_MAX_FILE_COUNT; i++) {
fs_file_t* f = &fs_objects[i];
if (fs_file_exist(f) &&
(parent_handle == f->parent || (parent_handle == 0xFFFFFFFFu && f->parent == 0u))) {
handles[count++] = (uint32_t) i + 1u; // handle is index + 1
}
}
(void) mtp_container_add_auint32(io_container, count, handles);
tud_mtp_data_send(io_container);
return 0;
}
static int32_t fs_get_object_info(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t obj_handle = command->params[0];
fs_file_t* f = fs_get_file(obj_handle);
if (f == NULL) {
return MTP_RESP_INVALID_OBJECT_HANDLE;
}
mtp_object_info_header_t obj_info_header = {
.storage_id = SUPPORTED_STORAGE_ID,
.object_format = f->object_format,
.protection_status = f->protection_status,
.object_compressed_size = f->size,
.thumb_format = MTP_OBJ_FORMAT_UNDEFINED,
.thumb_compressed_size = 0,
.thumb_pix_width = 0,
.thumb_pix_height = 0,
.image_pix_width = f->image_pix_width,
.image_pix_height = f->image_pix_height,
.image_bit_depth = f->image_bit_depth,
.parent_object = f->parent,
.association_type = f->association_type,
.association_desc = 0,
.sequence_number = 0
};
(void) mtp_container_add_raw(io_container, &obj_info_header, sizeof(obj_info_header));
(void) mtp_container_add_string(io_container, f->name);
(void) mtp_container_add_cstring(io_container, FS_FIXED_DATETIME);
(void) mtp_container_add_cstring(io_container, FS_FIXED_DATETIME);
(void) mtp_container_add_cstring(io_container, ""); // keywords, not used
tud_mtp_data_send(io_container);
return 0;
}
static int32_t fs_get_object(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t obj_handle = command->params[0];
const fs_file_t* f = fs_get_file(obj_handle);
if (f == NULL) {
return MTP_RESP_INVALID_OBJECT_HANDLE;
}
if (cb_data->phase == MTP_PHASE_COMMAND) {
// If file contents is larger than CFG_TUD_MTP_EP_BUFSIZE, data may only partially is added here
// the rest will be sent in tud_mtp_data_more_cb
(void) mtp_container_add_raw(io_container, f->data, f->size);
tud_mtp_data_send(io_container);
} else if (cb_data->phase == MTP_PHASE_DATA) {
// continue sending remaining data: file contents offset is xferred byte minus header size
const uint32_t offset = cb_data->total_xferred_bytes - sizeof(mtp_container_header_t);
const uint32_t xact_len = tu_min32(f->size - offset, io_container->payload_bytes);
if (xact_len > 0) {
memcpy(io_container->payload, f->data + offset, xact_len);
tud_mtp_data_send(io_container);
}
} else {
// nothing to do
}
return 0;
}
static int32_t fs_get_partial_object(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t obj_handle = command->params[0];
const uint32_t req_offset = command->params[1];
const uint32_t req_max = command->params[2];
const fs_file_t* f = fs_get_file(obj_handle);
if (f == NULL) {
return MTP_RESP_INVALID_OBJECT_HANDLE;
}
const uint32_t avail = (req_offset >= f->size) ? 0u : (f->size - req_offset);
const uint32_t to_send = tu_min32(avail, req_max);
if (cb_data->phase == MTP_PHASE_COMMAND) {
// If file contents is larger than CFG_TUD_MTP_EP_BUFSIZE, data may only partially be added here
// the rest will be sent in tud_mtp_data_more_cb
(void) mtp_container_add_raw(io_container, f->data + req_offset, to_send);
tud_mtp_data_send(io_container);
} else if (cb_data->phase == MTP_PHASE_DATA) {
// continue sending remaining data: file contents offset is xferred byte minus header size
const uint32_t offset = cb_data->total_xferred_bytes - sizeof(mtp_container_header_t);
const uint32_t xact_len = tu_min32(to_send - offset, io_container->payload_bytes);
if (xact_len > 0) {
memcpy(io_container->payload, f->data + offset + req_offset, xact_len);
tud_mtp_data_send(io_container);
}
} else {
// nothing to do
}
return 0;
}
static int32_t fs_send_object_info(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
mtp_container_info_t* io_container = &cb_data->io_container;
const uint32_t storage_id = command->params[0];
const uint32_t parent_handle = command->params[1]; // folder handle, 0xFFFFFFFF is root
(void) parent_handle;
if (!is_session_opened) {
return MTP_RESP_SESSION_NOT_OPEN;
}
if (storage_id != 0xFFFFFFFFu && storage_id != SUPPORTED_STORAGE_ID) {
return MTP_RESP_INVALID_STORAGE_ID;
}
if (cb_data->phase == MTP_PHASE_COMMAND) {
(void) tud_mtp_data_receive(io_container);
} else if (cb_data->phase == MTP_PHASE_DATA) {
mtp_object_info_header_t* obj_info = (mtp_object_info_header_t*) io_container->payload;
if (obj_info->storage_id != 0 && obj_info->storage_id != SUPPORTED_STORAGE_ID) {
return MTP_RESP_INVALID_STORAGE_ID;
}
if (obj_info->parent_object != 0 && obj_info->parent_object != 0xFFFFFFFFu) { // not root
fs_file_t* parent = fs_get_file(obj_info->parent_object);
if (parent == NULL || 0u == parent->association_type) {
return MTP_RESP_INVALID_PARENT_OBJECT;
}
}
uint8_t* f_buf = fs_malloc(obj_info->object_compressed_size);
if (f_buf == NULL) {
return MTP_RESP_STORE_FULL;
}
fs_file_t* f = fs_create_file();
if (f == NULL) {
return MTP_RESP_STORE_FULL;
}
f->object_format = obj_info->object_format;
f->protection_status = obj_info->protection_status;
f->image_pix_width = obj_info->image_pix_width;
f->image_pix_height = obj_info->image_pix_height;
f->image_bit_depth = obj_info->image_bit_depth;
f->parent = obj_info->parent_object;
f->association_type = obj_info->association_type;
f->size = obj_info->object_compressed_size;
f->data = f_buf;
uint8_t* buf = io_container->payload + sizeof(mtp_object_info_header_t);
(void) mtp_container_get_string(buf, f->name);
// ignore date created/modified/keywords
} else {
// nothing to do
}
return 0;
}
static int32_t fs_send_object(tud_mtp_cb_data_t* cb_data) {
mtp_container_info_t* io_container = &cb_data->io_container;
fs_file_t* f = fs_get_file(send_obj_handle);
if (f == NULL) {
return MTP_RESP_INVALID_OBJECT_HANDLE;
}
if (cb_data->phase == MTP_PHASE_COMMAND) {
io_container->header->len += f->size;
tud_mtp_data_receive(io_container);
} else {
// file contents offset is total xferred minus header size minus last received chunk
const uint32_t offset = cb_data->total_xferred_bytes - sizeof(mtp_container_header_t) - io_container->payload_bytes;
memcpy(f->data + offset, io_container->payload, io_container->payload_bytes);
if (cb_data->total_xferred_bytes - sizeof(mtp_container_header_t) < f->size) {
tud_mtp_data_receive(io_container);
}
}
return 0;
}
static int32_t fs_delete_object(tud_mtp_cb_data_t* cb_data) {
const mtp_container_command_t* command = cb_data->command_container;
const uint32_t obj_handle = command->params[0];
const uint32_t obj_format = command->params[1]; // optional
(void) obj_format;
if (!is_session_opened) {
return MTP_RESP_SESSION_NOT_OPEN;
}
fs_file_t* f = fs_get_file(obj_handle);
if (f == NULL) {
return MTP_RESP_INVALID_OBJECT_HANDLE;
}
// delete object by clear the name
f->name[0] = 0;
return MTP_RESP_OK;
}
|