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
+23
View File
@@ -1,5 +1,28 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
/* Platform serial handle (HANDLE on Windows, fd on POSIX) */
typedef intptr_t cel_serial_platform_handle;
#define CEL_SERIAL_PLATFORM_INVALID_HANDLE ((cel_serial_platform_handle)-1)
/* Open the platform serial device at path with the given baud rate.
* Returns CEL_SERIAL_PLATFORM_INVALID_HANDLE on failure. */
cel_serial_platform_handle cel_serial_platform_open(char const* path, int baud_rate);
/* Close the platform serial device. */
void cel_serial_platform_close(cel_serial_platform_handle handle);
/* Read up to len bytes without blocking.
* Returns bytes read immediately available, or 0 if none/error. */
size_t cel_serial_platform_read(cel_serial_platform_handle handle, uint8_t* buf, size_t len);
/* Write data. Returns bytes written, 0 on error. */
size_t cel_serial_platform_write(cel_serial_platform_handle handle, uint8_t const* buf, size_t len);
/* Flush output buffer. */
void cel_serial_platform_flush(cel_serial_platform_handle handle);
/* Platform-specific port enumeration.
* Returns the number of ports found, or -1 on error.
+58
View File
@@ -1,9 +1,67 @@
#include "celrs/platform/serial_internal.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
cel_serial_platform_handle cel_serial_platform_open(char const* path, int baud_rate) {
char full_path[300];
snprintf(full_path, sizeof(full_path), "\\\\.\\%s", path);
HANDLE h = CreateFileA(full_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) return CEL_SERIAL_PLATFORM_INVALID_HANDLE;
DCB dcb = {0};
dcb.DCBlength = sizeof(dcb);
if (!GetCommState(h, &dcb)) {
CloseHandle(h);
return CEL_SERIAL_PLATFORM_INVALID_HANDLE;
}
dcb.BaudRate = (DWORD)baud_rate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(h, &dcb)) {
CloseHandle(h);
return CEL_SERIAL_PLATFORM_INVALID_HANDLE;
}
/* Non-blocking reads: ReadFile returns immediately with whatever
* bytes are already buffered (possibly zero). */
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = MAXDWORD;
if (!SetCommTimeouts(h, &timeouts)) {
CloseHandle(h);
return CEL_SERIAL_PLATFORM_INVALID_HANDLE;
}
return (cel_serial_platform_handle)(intptr_t)h;
}
void cel_serial_platform_close(cel_serial_platform_handle handle) {
CloseHandle((HANDLE)(intptr_t)handle);
}
size_t cel_serial_platform_read(cel_serial_platform_handle handle, uint8_t* buf, size_t len) {
DWORD bytes_read = 0;
if (!ReadFile((HANDLE)(intptr_t)handle, buf, (DWORD)len, &bytes_read, NULL)) return 0;
return (size_t)bytes_read;
}
size_t cel_serial_platform_write(cel_serial_platform_handle handle, uint8_t const* buf, size_t len) {
DWORD bytes_written = 0;
if (!WriteFile((HANDLE)(intptr_t)handle, buf, (DWORD)len, &bytes_written, NULL)) return 0;
return (size_t)bytes_written;
}
void cel_serial_platform_flush(cel_serial_platform_handle handle) {
FlushFileBuffers((HANDLE)(intptr_t)handle);
}
/* Enumerate active COM ports via HKLM\HARDWARE\DEVICEMAP\SERIALCOMM.
* Windows keeps one value per active port in this key, so this is a
* single registry read instead of probing COM1..COM255 with CreateFile. */
+16 -19
View File
@@ -11,51 +11,48 @@
struct cel_serial_port {
char path[256];
int baud_rate;
int fd; /* platform-specific handle (HANDLE on Win, int on POSIX) */
cel_serial_platform_handle handle;
};
cel_serial_port* cel_serial_open(char const* path, int baud_rate) {
if (path == NULL) return NULL;
cel_serial_platform_handle handle = cel_serial_platform_open(path, baud_rate);
if (handle == CEL_SERIAL_PLATFORM_INVALID_HANDLE) return NULL;
cel_serial_port* port = (cel_serial_port*)calloc(1, sizeof(cel_serial_port));
if (port == NULL) return NULL;
if (port == NULL) {
cel_serial_platform_close(handle);
return NULL;
}
strncpy(port->path, path, sizeof(port->path) - 1);
port->path[sizeof(port->path) - 1] = '\0';
port->baud_rate = baud_rate;
port->fd = -1;
/* TODO: platform-specific open (CreateFile on Win, open+termios on POSIX) */
(void)baud_rate;
port->handle = handle;
return port;
}
void cel_serial_close(cel_serial_port* port) {
if (port == NULL) return;
/* TODO: platform-specific close */
cel_serial_platform_close(port->handle);
free(port);
}
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len) {
(void)port;
(void)buf;
(void)len;
/* TODO: platform-specific non-blocking read */
return 0;
if (port == NULL) return 0;
return cel_serial_platform_read(port->handle, buf, len);
}
size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len) {
(void)port;
(void)buf;
(void)len;
/* TODO: platform-specific write */
return 0;
if (port == NULL) return 0;
return cel_serial_platform_write(port->handle, buf, len);
}
void cel_serial_flush(cel_serial_port* port) {
(void)port;
/* TODO: platform-specific flush */
if (port == NULL) return;
cel_serial_platform_flush(port->handle);
}
int cel_serial_list_ports(char*** out_ports, int max_ports) {