feat: implement Windows serial platform backend

Add cel_serial_platform_open/close/read/write/flush for
Windows using CreateFileA, DCB for baud/8N1, and
SetCommTimeouts for non-blocking reads.

serial.c now delegates all operations to the platform
backend via a cel_serial_platform_handle, and
test_serial.c mocks that backend with CMock.
This commit is contained in:
2026-06-14 20:37:38 +02:00
parent a1ea02771c
commit 794ee9989a
4 changed files with 145 additions and 30 deletions
+48 -11
View File
@@ -8,8 +8,11 @@ void setUp(void) { Mockserial_internal_Init(); }
void tearDown(void) { Mockserial_internal_Verify(); Mockserial_internal_Destroy(); }
void test_open_valid_path(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_close_Expect(1);
cel_serial_close(port);
}
@@ -17,10 +20,18 @@ 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);
}
@@ -29,30 +40,53 @@ void test_close_null(void) {
cel_serial_close(NULL);
}
void test_read_returns_zero_stub(void) {
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];
/* Stub implementation returns 0 */
cel_serial_platform_read_ExpectAndReturn(1, buf, sizeof(buf), 4);
size_t n = cel_serial_read(port, buf, sizeof(buf));
TEST_ASSERT_EQUAL_UINT(0, n);
TEST_ASSERT_EQUAL_UINT(4, n);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_write_returns_zero_stub(void) {
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};
/* Stub implementation returns 0 */
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(0, n);
TEST_ASSERT_EQUAL_UINT(sizeof(buf), n);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
void test_flush_no_crash(void) {
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_flush(port); /* should not crash */
cel_serial_platform_flush_Expect(1);
cel_serial_flush(port);
cel_serial_platform_close_Expect(1);
cel_serial_close(port);
}
@@ -91,11 +125,14 @@ 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_returns_zero_stub);
RUN_TEST(test_write_returns_zero_stub);
RUN_TEST(test_flush_no_crash);
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);