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:
@@ -16,7 +16,7 @@ target_include_directories(celrs_serial_platform PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||
target_compile_features(celrs_serial_platform PRIVATE c_std_23)
|
||||
if (IS_WINDOWS)
|
||||
target_sources(celrs_serial_platform PRIVATE platform/serial_win.c)
|
||||
target_link_libraries(celrs_serial_platform PRIVATE advapi32)
|
||||
target_link_libraries(celrs_serial_platform PRIVATE advapi32 setupapi)
|
||||
elseif(IS_LINUX OR IS_MACOS)
|
||||
target_sources(celrs_serial_platform PRIVATE platform/serial_posix.c)
|
||||
endif()
|
||||
|
||||
@@ -28,3 +28,9 @@ void cel_serial_platform_flush(cel_serial_platform_handle handle);
|
||||
* Returns the number of ports found, or -1 on error.
|
||||
* out_ports must be freed with cel_serial_free_ports(). */
|
||||
int cel_serial_platform_list_ports(char*** out_ports, int max_ports);
|
||||
|
||||
/* Get a human-readable description for a serial port.
|
||||
* Returns 0 on success, -1 if description unavailable.
|
||||
* Always null-terminates out. */
|
||||
int cel_serial_platform_get_description(char const* path,
|
||||
char* out, size_t out_size);
|
||||
|
||||
@@ -34,3 +34,34 @@ int cel_serial_platform_list_ports(char*** out_ports, int max_ports) {
|
||||
*out_ports = ports;
|
||||
return count;
|
||||
}
|
||||
|
||||
int cel_serial_platform_get_description(char const* path,
|
||||
char* out, size_t out_size) {
|
||||
if (out == NULL || out_size == 0) return -1;
|
||||
out[0] = '\0';
|
||||
|
||||
/* Try reading from sysfs for USB device info */
|
||||
char sysfs_path[512];
|
||||
snprintf(sysfs_path, sizeof(sysfs_path),
|
||||
"/sys/class/tty/%s/device/idVendor",
|
||||
path + 5); /* skip /dev/ */
|
||||
|
||||
FILE* f = fopen(sysfs_path, "r");
|
||||
if (f == NULL) {
|
||||
/* Fallback: return the port name itself */
|
||||
strncpy(out, path, out_size - 1);
|
||||
out[out_size - 1] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
char vendor[16] = {0};
|
||||
if (fgets(vendor, sizeof(vendor), f) != NULL) {
|
||||
fclose(f);
|
||||
snprintf(out, out_size, "%s (USB)", path);
|
||||
} else {
|
||||
fclose(f);
|
||||
strncpy(out, path, out_size - 1);
|
||||
out[out_size - 1] = '\0';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "celrs/platform/serial_internal.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <setupapi.h>
|
||||
#include <initguid.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -97,3 +99,36 @@ int cel_serial_platform_list_ports(char*** out_ports, int max_ports) {
|
||||
*out_ports = ports;
|
||||
return count;
|
||||
}
|
||||
|
||||
int cel_serial_platform_get_description(char const* path,
|
||||
char* out, size_t out_size) {
|
||||
if (out == NULL || out_size == 0) return -1;
|
||||
out[0] = '\0';
|
||||
|
||||
HDEVINFO dev_info = SetupDiGetClassDevs(NULL, "USB", NULL, DIGCF_PRESENT);
|
||||
if (dev_info == INVALID_HANDLE_VALUE) return -1;
|
||||
|
||||
int found = -1;
|
||||
SP_DEVINFO_DATA dev_info_data;
|
||||
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
||||
|
||||
for (DWORD i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
||||
char friendly_name[256];
|
||||
DWORD type, req;
|
||||
if (SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data,
|
||||
SPDRP_FRIENDLYNAME, &type, (BYTE*)friendly_name,
|
||||
sizeof(friendly_name), &req)) {
|
||||
/* Check if this device uses our COM port */
|
||||
char* port_match = strstr(friendly_name, path);
|
||||
if (port_match != NULL) {
|
||||
strncpy(out, friendly_name, out_size - 1);
|
||||
out[out_size - 1] = '\0';
|
||||
found = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SetupDiDestroyDeviceInfoList(dev_info);
|
||||
return found;
|
||||
}
|
||||
|
||||
+36
-19
@@ -70,30 +70,47 @@ void cel_serial_free_ports(char** ports, int count) {
|
||||
}
|
||||
|
||||
int cel_serial_find_elrs_port(char* out, size_t out_size) {
|
||||
/* TODO: enumerate serial ports via cel_serial_list_ports().
|
||||
* Match port description against keywords (case-insensitive):
|
||||
* "silicon labs", "cp210", "elrs", "expresslrs", "bayck",
|
||||
* "ch340", "ch343", "ftdi", "uart"
|
||||
* Return first match. Copy device path into out.
|
||||
* Return 0 on success, -1 if no match found. */
|
||||
(void)out;
|
||||
(void)out_size;
|
||||
if (out == NULL || out_size == 0) return -1;
|
||||
|
||||
char** ports = NULL;
|
||||
int count = cel_serial_list_ports(&ports, 0);
|
||||
if (count < 0) return -1;
|
||||
|
||||
char const* keywords[] = {
|
||||
"silicon labs", "cp210", "elrs", "expresslrs",
|
||||
"bayck", "ch340", "ch343", "ftdi", "uart"
|
||||
};
|
||||
size_t n_keywords = sizeof(keywords) / sizeof(keywords[0]);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
char desc[256];
|
||||
if (cel_serial_platform_get_description(ports[i], desc, sizeof(desc)) == 0) {
|
||||
for (size_t k = 0; k < n_keywords; k++) {
|
||||
if (strstr(desc, keywords[k]) != NULL) {
|
||||
strncpy(out, ports[i], out_size - 1);
|
||||
out[out_size - 1] = '\0';
|
||||
cel_serial_free_ports(ports, count);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cel_serial_free_ports(ports, count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cel_serial_port* cel_serial_open_probe(char const* path,
|
||||
int const bauds[], int count,
|
||||
int* out_baud) {
|
||||
/* TODO: try opening the port at each baud rate in order.
|
||||
* Default probe order: 921600, 400000, 420000.
|
||||
* Set DTR=0, RTS=0 before opening to avoid module reset.
|
||||
* On first successful open, store baud in *out_baud and return port.
|
||||
* If all fail, return NULL.
|
||||
* NOTE: current cel_serial_open() doesn't support DTR/RTS control.
|
||||
* This needs a platform-level extension (see serial_internal.h). */
|
||||
(void)path;
|
||||
(void)bauds;
|
||||
(void)count;
|
||||
(void)out_baud;
|
||||
if (path == NULL || bauds == NULL || count <= 0) return NULL;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
cel_serial_port* port = cel_serial_open(path, bauds[i]);
|
||||
if (port != NULL) {
|
||||
if (out_baud != NULL) *out_baud = bauds[i];
|
||||
return port;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user