feat: add serial port listing and CLI flags
Implement cel_serial_list_ports/cel_serial_free_ports with platform backends: Windows reads HKLM\HARDWARE\DEVICEMAP\ SERIALCOMM (fast, single registry read), POSIX scans /dev for ttyUSB*/ttyACM*. telemetry tool gains --list, --port, and --baudrate flags; baud rate was previously hardcoded to 400000. Rename the tool_telemetry CMake target to telemetry. Fix test_free_ports_zero_count, which passed a stack array to cel_serial_free_ports (which calls free() on it), corrupting the heap.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
add_executable(tool_telemetry telemetry.c)
|
||||
target_include_directories(tool_telemetry PRIVATE "${CMAKE_SOURCE_DIR}")
|
||||
target_compile_features(tool_telemetry PRIVATE c_std_23)
|
||||
target_link_libraries(tool_telemetry PRIVATE celrs_crsf celrs_serial celrs_logger celrs_log_write)
|
||||
add_executable(telemetry telemetry.c)
|
||||
target_include_directories(telemetry PRIVATE "${CMAKE_SOURCE_DIR}")
|
||||
target_compile_features(telemetry PRIVATE c_std_23)
|
||||
target_link_libraries(telemetry PRIVATE celrs_crsf celrs_serial celrs_logger celrs_log_write)
|
||||
|
||||
+43
-13
@@ -32,33 +32,63 @@ static int telemetry_parse_link(int16_t* rssi, uint8_t* link_quality,
|
||||
}
|
||||
|
||||
static void print_usage(char const* prog) {
|
||||
printf("Usage: %s <serial_port> [interval_ms]\n", prog);
|
||||
printf(" serial_port : COM3 (Windows) or /dev/ttyUSB0 (Linux)\n");
|
||||
printf(" interval_ms : poll interval in ms (default 200)\n");
|
||||
printf("Usage: %s --port <serial_port> [--baudrate <rate>] [interval_ms]\n", prog);
|
||||
printf(" %s --list\n", prog);
|
||||
printf(" --port <serial_port> : COM3 (Windows) or /dev/ttyUSB0 (Linux)\n");
|
||||
printf(" --baudrate <rate> : baud rate (default 400000)\n");
|
||||
printf(" interval_ms : poll interval in ms (default 200)\n");
|
||||
printf(" --list : list available serial ports and exit\n");
|
||||
}
|
||||
|
||||
static int list_ports(void) {
|
||||
char** ports = NULL;
|
||||
int count = cel_serial_list_ports(&ports, 0);
|
||||
if (count < 0) {
|
||||
cel_log_err("Failed to list serial ports");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("%s\n", ports[i]);
|
||||
}
|
||||
|
||||
cel_serial_free_ports(ports, count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char const* argv[]) {
|
||||
if (argc < 2) {
|
||||
char const* port_path = NULL;
|
||||
int baud_rate = 400000;
|
||||
int interval_ms = 200;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--list") == 0) {
|
||||
return list_ports();
|
||||
} else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
|
||||
port_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--baudrate") == 0 && i + 1 < argc) {
|
||||
baud_rate = atoi(argv[++i]);
|
||||
if (baud_rate <= 0) baud_rate = 400000;
|
||||
} else {
|
||||
interval_ms = atoi(argv[i]);
|
||||
if (interval_ms <= 0) interval_ms = 200;
|
||||
}
|
||||
}
|
||||
|
||||
if (port_path == NULL) {
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char const* port_path = argv[1];
|
||||
int interval_ms = 200;
|
||||
if (argc >= 3) {
|
||||
interval_ms = atoi(argv[2]);
|
||||
if (interval_ms <= 0) interval_ms = 200;
|
||||
}
|
||||
|
||||
/* Open serial port */
|
||||
cel_serial_port* port = cel_serial_open(port_path, 400000);
|
||||
cel_serial_port* port = cel_serial_open(port_path, baud_rate);
|
||||
if (port == NULL) {
|
||||
cel_log_err("Failed to open serial port");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char msg[256];
|
||||
snprintf(msg, sizeof(msg), "Connected to %s (400000 baud)", port_path);
|
||||
snprintf(msg, sizeof(msg), "Connected to %s (%d baud)", port_path, baud_rate);
|
||||
cel_log_info(msg);
|
||||
|
||||
/* Send heartbeat to establish CRSF link */
|
||||
|
||||
Reference in New Issue
Block a user