Skip to content

Commit 4a0613c

Browse files
committed
fix mtp serial
1 parent 6d32256 commit 4a0613c

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

examples/device/mtp/src/mtp_fs_example.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,10 @@ static int32_t fs_get_device_info(tud_mtp_cb_data_t* cb_data) {
351351
mtp_container_add_cstring(io_container, DEV_INFO_MODEL);
352352
mtp_container_add_cstring(io_container, DEV_INFO_VERSION);
353353

354-
uint16_t serial_utf16[32];
355-
size_t nchars = board_usb_get_serial(serial_utf16, 32);
356-
serial_utf16[tu_min32(nchars, 31u)] = 0; // ensure null termination
354+
enum { MAX_SERIAL_NCHARS = 32 };
355+
uint16_t serial_utf16[MAX_SERIAL_NCHARS+1];
356+
size_t nchars = board_usb_get_serial(serial_utf16, MAX_SERIAL_NCHARS);
357+
serial_utf16[tu_min32(nchars, MAX_SERIAL_NCHARS)] = 0; // ensure null termination
357358
mtp_container_add_string(io_container, serial_utf16);
358359

359360
tud_mtp_data_send(io_container);

test/hil/hil_test.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def open_mtp_dev(uid):
142142
mtp = MTP()
143143
timeout = ENUM_TIMEOUT
144144
while timeout > 0:
145+
# run_cmd(f"gio mount -u mtp://TinyUsb_TinyUsb_Device_{uid}/")
145146
for raw in mtp.detect_devices():
146147
mtp.device = mtp.mtp.LIBMTP_Open_Raw_Device(ctypes.byref(raw))
147148
if mtp.device and mtp.get_serialnumber().decode('utf-8') == uid:
@@ -526,42 +527,44 @@ def test_device_mtp(board):
526527
if mtp is None or mtp.device is None:
527528
assert False, 'MTP device not found'
528529

529-
assert b"TinyUSB" == mtp.get_manufacturer(), 'MTP wrong manufacturer'
530-
assert b"MTP Example" == mtp.get_modelname(), 'MTP wrong model'
531-
assert b'1.0' == mtp.get_deviceversion(), 'MTP wrong version'
532-
assert b'TinyUSB MTP' == mtp.get_devicename(), 'MTP wrong device name'
533-
534-
# read and compare readme.txt and logo.png
535-
f1_expect = b'TinyUSB MTP Filesystem example'
536-
f2_md5_expect = '40ef23fc2891018d41a05d4a0d5f822f' # md5sum of logo.png
537-
f1 = uid.encode("utf-8") + b'_file1'
538-
f2 = uid.encode("utf-8") + b'_file2'
539-
f3 = uid.encode("utf-8") + b'_file3'
540-
mtp.get_file_to_file(1, f1)
541-
with open(f1, 'rb') as file:
542-
f1_data = file.read()
543-
os.remove(f1)
544-
assert f1_data == f1_expect, 'MTP file1 wrong data'
545-
mtp.get_file_to_file(2, f2)
546-
with open(f2, 'rb') as file:
547-
f2_data = file.read()
548-
os.remove(f2)
549-
assert f2_md5_expect == hashlib.md5(f2_data).hexdigest(), 'MTP file2 wrong data'
550-
# test send file
551-
with open(f3, "wb") as file:
552-
f3_data = os.urandom(random.randint(1024, 3*1024))
553-
file.write(f3_data)
554-
file.close()
555-
fid = mtp.send_file_from_file(f3, b'file3')
556-
f3_readback = f3 + b'_readback'
557-
mtp.get_file_to_file(fid, f3_readback)
558-
with open(f3_readback, 'rb') as f:
559-
f3_rb_data = f.read()
560-
os.remove(f3_readback)
561-
assert f3_rb_data == f3_data, 'MTP file3 wrong data'
562-
os.remove(f3)
563-
mtp.delete_object(fid)
564-
530+
try:
531+
assert b"TinyUSB" == mtp.get_manufacturer(), 'MTP wrong manufacturer'
532+
assert b"MTP Example" == mtp.get_modelname(), 'MTP wrong model'
533+
assert b'1.0' == mtp.get_deviceversion(), 'MTP wrong version'
534+
assert b'TinyUSB MTP' == mtp.get_devicename(), 'MTP wrong device name'
535+
536+
# read and compare readme.txt and logo.png
537+
f1_expect = b'TinyUSB MTP Filesystem example'
538+
f2_md5_expect = '40ef23fc2891018d41a05d4a0d5f822f' # md5sum of logo.png
539+
f1 = uid.encode("utf-8") + b'_file1'
540+
f2 = uid.encode("utf-8") + b'_file2'
541+
f3 = uid.encode("utf-8") + b'_file3'
542+
mtp.get_file_to_file(1, f1)
543+
with open(f1, 'rb') as file:
544+
f1_data = file.read()
545+
os.remove(f1)
546+
assert f1_data == f1_expect, 'MTP file1 wrong data'
547+
mtp.get_file_to_file(2, f2)
548+
with open(f2, 'rb') as file:
549+
f2_data = file.read()
550+
os.remove(f2)
551+
assert f2_md5_expect == hashlib.md5(f2_data).hexdigest(), 'MTP file2 wrong data'
552+
# test send file
553+
with open(f3, "wb") as file:
554+
f3_data = os.urandom(random.randint(1024, 3*1024))
555+
file.write(f3_data)
556+
file.close()
557+
fid = mtp.send_file_from_file(f3, b'file3')
558+
f3_readback = f3 + b'_readback'
559+
mtp.get_file_to_file(fid, f3_readback)
560+
with open(f3_readback, 'rb') as f:
561+
f3_rb_data = f.read()
562+
os.remove(f3_readback)
563+
assert f3_rb_data == f3_data, 'MTP file3 wrong data'
564+
os.remove(f3)
565+
mtp.delete_object(fid)
566+
finally:
567+
mtp.disconnect()
565568

566569

567570
# -------------------------------------------------------------

0 commit comments

Comments
 (0)