From d60742dbcb6342735a421fda2bdf2e70a21f2387 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:31:17 +1300 Subject: [PATCH 1/9] Added a list/describe test Doesn't work on macos --- simple_uart_test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/simple_uart_test.c b/simple_uart_test.c index 540276e..0c3bdee 100644 --- a/simple_uart_test.c +++ b/simple_uart_test.c @@ -258,6 +258,47 @@ void test_logfile(void) unlink(logfile); } +void test_list(void) +{ + // Describe only works on Linux/Windows +#if defined(__linux__) || defined(_WIN32) + char **names = NULL; + ssize_t count; + + /* Test with loopback devices */ + pid_t pid = setup_loopback(); + + count = simple_uart_list(&names); + + /* Count should be non-negative and should have the loopback devices or at least + * verify that the list function works without crashing */ + TEST_ASSERT(count > 0); + printf("Device count: %zd\n", count); + + TEST_ASSERT(names != NULL); + int ndescs = 0; + + /* Just verify that we can iterate through the list and free it safely */ + for (ssize_t i = 0; i < count; i++) { + char description[256]; + TEST_ASSERT(names[i] != NULL); + TEST_ASSERT(strlen(names[i]) > 0); + TEST_ASSERT(simple_uart_describe(names[i], description, sizeof(description)) == 0); + printf("%zd: name=%s desc=%s\n", i, names[i], description); + if (strlen(description) > 0) { + ndescs++; + } + + free(names[i]); + } + free(names); + + // At least one has to have a description + TEST_ASSERT(ndescs > 0); + shutdown_loopback(pid); +#endif +} + TEST_LIST = { {"open", test_open}, {"loopback", test_loopback}, @@ -265,5 +306,6 @@ TEST_LIST = { {"read_line", test_read_line}, {"read_timeout", test_read_timeout}, {"logfile", test_logfile}, + {"list", test_list}, {NULL, NULL}, }; \ No newline at end of file From ff168c110395d2f12bec4e5c03fd8f0962d54b7a Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:41:33 +1300 Subject: [PATCH 2/9] Add some description info for /dev/ttySx uarts --- simple_uart.c | 7 +++++++ simple_uart_test.c | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/simple_uart.c b/simple_uart.c index bcc30fd..a504410 100644 --- a/simple_uart.c +++ b/simple_uart.c @@ -746,6 +746,13 @@ int simple_uart_describe(const char *uart, char *description, size_t max_desc_le if (readfile(basePath, "serial", chrBuf, sizeof(chrBuf))) { snprintf(description + strlen(description), max_desc_len - strlen(description), "serial=%s,", chrBuf); } + + // These only work on /dev/ttySx style UARTs + snprintf(unresolvedPath, sizeof(unresolvedPath), "/sys/class/tty/%s", basename((char *)uart)); + if (readfile(unresolvedPath, "port", chrBuf, sizeof(chrBuf))) { + snprintf(description + strlen(description), max_desc_len - strlen(description), "port=%s", chrBuf); + } + // MacOS #else // Volunteers for MacOS wanted diff --git a/simple_uart_test.c b/simple_uart_test.c index 0c3bdee..66b8590 100644 --- a/simple_uart_test.c +++ b/simple_uart_test.c @@ -265,9 +265,6 @@ void test_list(void) char **names = NULL; ssize_t count; - /* Test with loopback devices */ - pid_t pid = setup_loopback(); - count = simple_uart_list(&names); /* Count should be non-negative and should have the loopback devices or at least @@ -295,7 +292,6 @@ void test_list(void) // At least one has to have a description TEST_ASSERT(ndescs > 0); - shutdown_loopback(pid); #endif } From 860c9645d1e7d2ee408a982acc9f40496b953a82 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:46:26 +1300 Subject: [PATCH 3/9] Fix up readfile --- simple_uart.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simple_uart.c b/simple_uart.c index a504410..3b1097b 100644 --- a/simple_uart.c +++ b/simple_uart.c @@ -435,7 +435,10 @@ static bool readfile(const char *dir, const char *filename, char *result, size_t if ((fp = fopen(path, "rb")) != NULL) { len = fread(result, 1, max_result_len - 1, fp); if (len) { - result[len - 1] = '\0'; + result[len] = '\0'; + while (isspace(result[len - 1])) + len--; + result[len] = '\0'; } fclose(fp); } From 6773d6610d12ba97a0ec0930582c663a68c136e7 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:47:00 +1300 Subject: [PATCH 4/9] New checkout action --- .github/workflows/ccpp.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index b988402..182265b 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -10,7 +10,7 @@ jobs: ubuntu: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: setup run: sudo apt-get update && sudo apt-get -f install socat cppcheck clang-format-19 valgrind - name: build @@ -26,7 +26,7 @@ jobs: windows: runs-on: windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: build shell: cmd run: | @@ -35,7 +35,7 @@ jobs: macos: runs-on: macos-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: setup run: brew install socat - name: build From b91c1b1dd557fe878ba365893edc190ce91a6324 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:48:47 +1300 Subject: [PATCH 5/9] Missing last ',' fixup --- simple_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple_uart.c b/simple_uart.c index 3b1097b..eee9ea1 100644 --- a/simple_uart.c +++ b/simple_uart.c @@ -753,7 +753,7 @@ int simple_uart_describe(const char *uart, char *description, size_t max_desc_le // These only work on /dev/ttySx style UARTs snprintf(unresolvedPath, sizeof(unresolvedPath), "/sys/class/tty/%s", basename((char *)uart)); if (readfile(unresolvedPath, "port", chrBuf, sizeof(chrBuf))) { - snprintf(description + strlen(description), max_desc_len - strlen(description), "port=%s", chrBuf); + snprintf(description + strlen(description), max_desc_len - strlen(description), "port=%s,", chrBuf); } // MacOS From 26a1aae2be7b8380fdb6c2cbc31419d26d8da180 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:51:39 +1300 Subject: [PATCH 6/9] cleanup --- simple_uart.c | 3 +-- simple_uart_test.c | 11 +---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/simple_uart.c b/simple_uart.c index eee9ea1..09c9188 100644 --- a/simple_uart.c +++ b/simple_uart.c @@ -434,8 +434,7 @@ static bool readfile(const char *dir, const char *filename, char *result, size_t result[0] = '\0'; if ((fp = fopen(path, "rb")) != NULL) { len = fread(result, 1, max_result_len - 1, fp); - if (len) { - result[len] = '\0'; + if (len > 0) { while (isspace(result[len - 1])) len--; result[len] = '\0'; diff --git a/simple_uart_test.c b/simple_uart_test.c index 66b8590..52c3de3 100644 --- a/simple_uart_test.c +++ b/simple_uart_test.c @@ -270,8 +270,6 @@ void test_list(void) /* Count should be non-negative and should have the loopback devices or at least * verify that the list function works without crashing */ TEST_ASSERT(count > 0); - printf("Device count: %zd\n", count); - TEST_ASSERT(names != NULL); int ndescs = 0; @@ -281,17 +279,10 @@ void test_list(void) TEST_ASSERT(names[i] != NULL); TEST_ASSERT(strlen(names[i]) > 0); TEST_ASSERT(simple_uart_describe(names[i], description, sizeof(description)) == 0); - printf("%zd: name=%s desc=%s\n", i, names[i], description); - if (strlen(description) > 0) { - ndescs++; - } - + TEST_ASSERT(strlen(description) > 0); free(names[i]); } free(names); - - // At least one has to have a description - TEST_ASSERT(ndescs > 0); #endif } From 08be0f391c3ad13bc8ccda076ea4e6ec9e0de3c5 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:52:53 +1300 Subject: [PATCH 7/9] unused --- simple_uart_test.c | 1 - 1 file changed, 1 deletion(-) diff --git a/simple_uart_test.c b/simple_uart_test.c index 52c3de3..5a80915 100644 --- a/simple_uart_test.c +++ b/simple_uart_test.c @@ -271,7 +271,6 @@ void test_list(void) * verify that the list function works without crashing */ TEST_ASSERT(count > 0); TEST_ASSERT(names != NULL); - int ndescs = 0; /* Just verify that we can iterate through the list and free it safely */ for (ssize_t i = 0; i < count; i++) { From 8fc987c1ab9dde0bfefe6d6c85b32f749bc1c81f Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:53:42 +1300 Subject: [PATCH 8/9] Fix up comments --- simple_uart_test.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/simple_uart_test.c b/simple_uart_test.c index 5a80915..ca94593 100644 --- a/simple_uart_test.c +++ b/simple_uart_test.c @@ -267,12 +267,10 @@ void test_list(void) count = simple_uart_list(&names); - /* Count should be non-negative and should have the loopback devices or at least - * verify that the list function works without crashing */ + /* We assume the test machine has at least one serial port */ TEST_ASSERT(count > 0); TEST_ASSERT(names != NULL); - /* Just verify that we can iterate through the list and free it safely */ for (ssize_t i = 0; i < count; i++) { char description[256]; TEST_ASSERT(names[i] != NULL); From d0f6066537c8e836946d64d4d5b73261c58d391c Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Wed, 5 Nov 2025 09:56:34 +1300 Subject: [PATCH 9/9] simple_uart_term: describe things during listing --- simple_uart_term.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/simple_uart_term.c b/simple_uart_term.c index bf49c2d..cb3f89e 100644 --- a/simple_uart_term.c +++ b/simple_uart_term.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -27,9 +28,13 @@ int main(int argc, char *argv[]) case 'l': { char **names; ssize_t nuarts = simple_uart_list(&names); + char description[256] = {0}; printf("Found %zd ports\n", nuarts); for (ssize_t i = 0; i < nuarts; i++) { - printf("Port %zd: %s\n", i, names[i]); + printf("Port %zd: %s", i, names[i]); + if (simple_uart_describe(names[i], description, sizeof(description)) == 0 && strlen(description) > 0) + printf(" Description: %s", description); + printf("\n"); } for (ssize_t i = 0; i < nuarts; i++) {