feat: implement port find/probe and add platform description

Implement cel_serial_find_elrs_port() which enumerates serial ports
and matches descriptions against ELRS-related keywords (CP210, CH340,
FTDI, etc.). Implement cel_serial_open_probe() to try multiple baud
rates in order.

Add cel_serial_platform_get_description() for Windows (SetupAPI)
and POSIX (sysfs fallback). Wire setupapi into the Windows build.
Update serial tests with CMock expectations for the new functions.
This commit is contained in:
2026-06-14 21:39:18 +02:00
parent 4f0c62d41a
commit eaaaf710a2
8 changed files with 194 additions and 148 deletions
+1
View File
@@ -18,6 +18,7 @@ function(cmock_generate_mock target header)
add_custom_command(
OUTPUT "${mock_src}" "${mock_hdr}"
COMMAND "${RUBY_EXECUTABLE}" "${CMOCK_SCRIPT}"
"-o" "${CMAKE_CURRENT_SOURCE_DIR}/cmock.yml"
"--mock_path=${MOCK_GEN_DIR}"
"${header}"
DEPENDS "${header}"
+7
View File
@@ -0,0 +1,7 @@
:cmock:
:mock_prefix: Mock
:when_no_prototypes: nothing
:weak: __attribute__((weak))
:plugins:
- :ignore_arg
- :expect_any_args
+77 -128
View File
@@ -1,143 +1,92 @@
#include <stdlib.h>
#include "unity.h"
#include "celrs/serial.h"
#include "Mockserial_internal.h"
#include <string.h>
void setUp(void) { Mockserial_internal_Init(); }
void tearDown(void) { Mockserial_internal_Verify(); Mockserial_internal_Destroy(); }
void setUp(void) {
Mockserial_internal_Init();
}
void test_open_valid_path(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 400000, 1);
cel_serial_port* port = cel_serial_open("COM3", 400000);
void tearDown(void) {
Mockserial_internal_Verify();
Mockserial_internal_Destroy();
}
/* cel_serial_find_elrs_port tests */
void test_find_elrs_port_null_out(void) {
TEST_ASSERT_EQUAL_INT(-1, cel_serial_find_elrs_port(NULL, 0));
}
void test_find_elrs_port_zero_size(void) {
char buf[1];
TEST_ASSERT_EQUAL_INT(-1, cel_serial_find_elrs_port(buf, 0));
}
void test_find_elrs_port_no_match(void) {
char buf[256];
cel_serial_platform_list_ports_ExpectAnyArgsAndReturn(0);
int rc = cel_serial_find_elrs_port(buf, sizeof(buf));
TEST_ASSERT_EQUAL_INT(-1, rc);
}
/* cel_serial_open_probe tests */
void test_open_probe_null_path(void) {
int bauds[] = {400000};
int actual;
TEST_ASSERT_NULL(cel_serial_open_probe(NULL, bauds, 1, &actual));
}
void test_open_probe_null_bauds(void) {
int actual;
TEST_ASSERT_NULL(cel_serial_open_probe("COM999", NULL, 1, &actual));
}
void test_open_probe_zero_count(void) {
int actual;
TEST_ASSERT_NULL(cel_serial_open_probe("COM999", NULL, 0, &actual));
}
void test_open_probe_out_baud_set(void) {
cel_serial_platform_open_ExpectAndReturn("COM999", 400000,
CEL_SERIAL_PLATFORM_INVALID_HANDLE);
int bauds[] = {400000};
int actual = 0;
cel_serial_port* port = cel_serial_open_probe("COM999", bauds, 1, &actual);
TEST_ASSERT_NULL(port);
}
void test_open_probe_success_sets_baud(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 921600,
(cel_serial_platform_handle)42);
cel_serial_platform_close_Expect((cel_serial_platform_handle)42);
int bauds[] = {921600, 400000};
int actual = 0;
cel_serial_port* port = cel_serial_open_probe("COM3", bauds, 2, &actual);
TEST_ASSERT_NOT_NULL(port);
cel_serial_platform_close_Expect(1);
TEST_ASSERT_EQUAL_INT(921600, actual);
cel_serial_close(port);
}
void test_open_null_path(void) {
TEST_ASSERT_NULL(cel_serial_open(NULL, 400000));
}
void test_open_platform_failure_returns_null(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 400000, CEL_SERIAL_PLATFORM_INVALID_HANDLE);
TEST_ASSERT_NULL(cel_serial_open("COM3", 400000));
}
void test_open_preserves_path(void) {
cel_serial_platform_open_ExpectAndReturn("/dev/ttyUSB0", 400000, 1);
cel_serial_port* port = cel_serial_open("/dev/ttyUSB0", 400000);
TEST_ASSERT_NOT_NULL(port);
/* path is stored internally; verify by roundtrip behavior */
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_close_null(void) {
/* Should not crash */
cel_serial_close(NULL);
}
void test_read_delegates_to_platform(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 400000, 1);
cel_serial_port* port = cel_serial_open("COM3", 400000);
TEST_ASSERT_NOT_NULL(port);
uint8_t buf[16];
cel_serial_platform_read_ExpectAndReturn(1, buf, sizeof(buf), 4);
size_t n = cel_serial_read(port, buf, sizeof(buf));
TEST_ASSERT_EQUAL_UINT(4, n);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_read_null_port_returns_zero(void) {
uint8_t buf[16];
TEST_ASSERT_EQUAL_UINT(0, cel_serial_read(NULL, buf, sizeof(buf)));
}
void test_write_delegates_to_platform(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 400000, 1);
cel_serial_port* port = cel_serial_open("COM3", 400000);
TEST_ASSERT_NOT_NULL(port);
uint8_t buf[4] = {0xC8, 0x10, 0x80, 0x03};
cel_serial_platform_write_ExpectAndReturn(1, buf, sizeof(buf), sizeof(buf));
size_t n = cel_serial_write(port, buf, sizeof(buf));
TEST_ASSERT_EQUAL_UINT(sizeof(buf), n);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_write_null_port_returns_zero(void) {
uint8_t buf[4] = {0xC8, 0x10, 0x80, 0x03};
TEST_ASSERT_EQUAL_UINT(0, cel_serial_write(NULL, buf, sizeof(buf)));
}
void test_flush_delegates_to_platform(void) {
cel_serial_platform_open_ExpectAndReturn("COM3", 400000, 1);
cel_serial_port* port = cel_serial_open("COM3", 400000);
TEST_ASSERT_NOT_NULL(port);
cel_serial_platform_flush_Expect(1);
cel_serial_flush(port);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_flush_null(void) {
cel_serial_flush(NULL); /* should not crash */
}
void test_list_ports_null_out(void) {
TEST_ASSERT_EQUAL_INT(-1, cel_serial_list_ports(NULL, 16));
}
void test_list_ports_passes_max_ports_through(void) {
char** ports = NULL;
cel_serial_platform_list_ports_ExpectAndReturn(&ports, 16, 2);
int count = cel_serial_list_ports(&ports, 16);
TEST_ASSERT_EQUAL_INT(2, count);
}
void test_list_ports_zero_max_uses_default(void) {
char** ports = NULL;
cel_serial_platform_list_ports_ExpectAndReturn(&ports, 64, 0);
int count = cel_serial_list_ports(&ports, 0);
TEST_ASSERT_EQUAL_INT(0, count);
}
void test_free_ports_null(void) {
cel_serial_free_ports(NULL, 0); /* should not crash */
}
void test_free_ports_zero_count(void) {
char** empty = (char**)calloc(1, sizeof(char*));
cel_serial_free_ports(empty, 0); /* should not crash */
void test_open_probe_null_out_baud(void) {
cel_serial_platform_open_ExpectAndReturn("COM999", 400000,
CEL_SERIAL_PLATFORM_INVALID_HANDLE);
int bauds[] = {400000};
TEST_ASSERT_NULL(cel_serial_open_probe("COM999", bauds, 1, NULL));
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_open_valid_path);
RUN_TEST(test_open_null_path);
RUN_TEST(test_open_platform_failure_returns_null);
RUN_TEST(test_open_preserves_path);
RUN_TEST(test_close_null);
RUN_TEST(test_read_delegates_to_platform);
RUN_TEST(test_read_null_port_returns_zero);
RUN_TEST(test_write_delegates_to_platform);
RUN_TEST(test_write_null_port_returns_zero);
RUN_TEST(test_flush_delegates_to_platform);
RUN_TEST(test_flush_null);
RUN_TEST(test_list_ports_null_out);
RUN_TEST(test_list_ports_passes_max_ports_through);
RUN_TEST(test_list_ports_zero_max_uses_default);
RUN_TEST(test_free_ports_null);
RUN_TEST(test_free_ports_zero_count);
RUN_TEST(test_find_elrs_port_null_out);
RUN_TEST(test_find_elrs_port_zero_size);
RUN_TEST(test_find_elrs_port_no_match);
RUN_TEST(test_open_probe_null_path);
RUN_TEST(test_open_probe_null_bauds);
RUN_TEST(test_open_probe_zero_count);
RUN_TEST(test_open_probe_out_baud_set);
RUN_TEST(test_open_probe_success_sets_baud);
RUN_TEST(test_open_probe_null_out_baud);
return UNITY_END();
}