a1ea02771c
Drop timeout_ms; read now returns immediately with whatever data is available (0 if none), so callers don't block the rest of their loop waiting on serial I/O. telemetry's poll loop now sleeps interval_ms itself between empty reads via a small sleep_ms helper.
74 lines
1.8 KiB
C
74 lines
1.8 KiB
C
#ifdef _MSC_VER
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
#endif
|
|
|
|
#include "celrs/serial.h"
|
|
#include "celrs/platform/serial_internal.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct cel_serial_port {
|
|
char path[256];
|
|
int baud_rate;
|
|
int fd; /* platform-specific handle (HANDLE on Win, int on POSIX) */
|
|
};
|
|
|
|
cel_serial_port* cel_serial_open(char const* path, int baud_rate) {
|
|
if (path == NULL) return NULL;
|
|
|
|
cel_serial_port* port = (cel_serial_port*)calloc(1, sizeof(cel_serial_port));
|
|
if (port == NULL) 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;
|
|
|
|
return port;
|
|
}
|
|
|
|
void cel_serial_close(cel_serial_port* port) {
|
|
if (port == NULL) return;
|
|
/* TODO: platform-specific close */
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void cel_serial_flush(cel_serial_port* port) {
|
|
(void)port;
|
|
/* TODO: platform-specific flush */
|
|
}
|
|
|
|
int cel_serial_list_ports(char*** out_ports, int max_ports) {
|
|
if (out_ports == NULL) return -1;
|
|
if (max_ports <= 0) max_ports = 64;
|
|
return cel_serial_platform_list_ports(out_ports, max_ports);
|
|
}
|
|
|
|
void cel_serial_free_ports(char** ports, int count) {
|
|
if (ports == NULL) return;
|
|
for (int i = 0; i < count; i++) {
|
|
free(ports[i]);
|
|
}
|
|
free(ports);
|
|
}
|