From 9066e67eff3e8f882dd2ea248015041b61c0dbba Mon Sep 17 00:00:00 2001 From: Filip Jagodzinski Date: Tue, 28 Apr 2020 17:08:42 +0200 Subject: [PATCH] Tests: USBSerial: Handle minimal printf limitations In a line coding test, host changes the line coding based on data received from DUT via the USBSerial::printf(). A fixed-size payload, equal to LINE_CODING_STRLEN, is required by the host side. When the minimal printf is used, fixed-width messages can not be easily generated on the fly. Zero-fill or width specifiers are not supported by the minimal printf. Update the host side to stop reading on a defined payload delimiter and allow variable-width messages. --- TESTS/host_tests/usb_device_serial.py | 13 +++++++------ TESTS/usb_device/serial/main.cpp | 9 +++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/TESTS/host_tests/usb_device_serial.py b/TESTS/host_tests/usb_device_serial.py index c271176aa0..d462d761e9 100644 --- a/TESTS/host_tests/usb_device_serial.py +++ b/TESTS/host_tests/usb_device_serial.py @@ -47,8 +47,8 @@ TERM_CLOSE_DELAY = 0.01 # during terminal reopen test. TERM_REOPEN_DELAY = 0.1 -# 6 (baud) + 2 (bits) + 1 (parity) + 1 (stop) + 3 * comma -LINE_CODING_STRLEN = 13 +LINE_CODING_SEP = ',' +LINE_CODING_DELIM = ';' def usb_serial_name(serial_number): @@ -269,16 +269,17 @@ class USBSerialTest(mbed_host_tests.BaseHostTest): mbed_serial.reset_output_buffer() mbed_serial.dtr = True try: - payload = six.ensure_str(mbed_serial.read(LINE_CODING_STRLEN)) - while len(payload) == LINE_CODING_STRLEN: - baud, bits, parity, stop = (int(i) for i in payload.split(',')) + payload = six.ensure_str(mbed_serial.read_until(LINE_CODING_DELIM)) + while len(payload) > 0: + baud, bits, parity, stop = ( + int(i) for i in payload.strip(LINE_CODING_DELIM).split(LINE_CODING_SEP)) new_line_coding = { 'baudrate': baud, 'bytesize': self._BYTESIZES[bits], 'parity': self._PARITIES[parity], 'stopbits': self._STOPBITS[stop]} mbed_serial.apply_settings(new_line_coding) - payload = six.ensure_str(mbed_serial.read(LINE_CODING_STRLEN)) + payload = six.ensure_str(mbed_serial.read_until(LINE_CODING_DELIM)) except serial.SerialException as exc: self.log('TEST ERROR: {}'.format(exc)) self.notify_complete(False) diff --git a/TESTS/usb_device/serial/main.cpp b/TESTS/usb_device/serial/main.cpp index 0d56736b03..9dc1df4098 100644 --- a/TESTS/usb_device/serial/main.cpp +++ b/TESTS/usb_device/serial/main.cpp @@ -84,7 +84,8 @@ #define SERIAL_LOOPBACK_REPS 100 #define USB_RECONNECT_DELAY_MS 1 -#define LINE_CODING_STRLEN 13 // 6 + 2 + 1 + 1 + 3 * comma +#define LINE_CODING_SEP (',') +#define LINE_CODING_DELIM (';') #define USB_DEV_SN_LEN (32) // 32 hex digit UUID #define NONASCII_CHAR ('?') @@ -776,9 +777,9 @@ void test_serial_line_coding_change() for (size_t i = 0; i < num_line_codings; i++) { lc_expected = &(test_codings[i]); num_expected_callbacks = lc_prev->get_num_diffs(*lc_expected); - rc = usb_serial.printf("%06i,%02i,%01i,%01i", lc_expected->baud, lc_expected->bits, lc_expected->parity, - lc_expected->stop); - TEST_ASSERT_EQUAL_INT(LINE_CODING_STRLEN, rc); + rc = usb_serial.printf("%06i,%02i,%01i,%01i%c", lc_expected->baud, lc_expected->bits, lc_expected->parity, + lc_expected->stop, LINE_CODING_DELIM); + TEST_ASSERT(rc > 0); // The pyserial Python module does not update all line coding params // at once. It updates params one by one instead, and since every // update is followed by port reconfiguration we get multiple