From 5e937c72c42086bc0ea09086e5476efad8410c92 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Sat, 14 Mar 2026 16:21:36 +0100 Subject: [PATCH 1/4] Add modem control and configuration interfaces - Introduced new headers for modem line control, including functions for setting DTR, RTS, and querying CTS, DSR, DCD, and RI states. - Added extended configuration options with functions for flow control, sending break conditions, updating baud rate, and retrieving port configuration. - Implemented error handling codes for new functionalities in status_codes.h and updated the corresponding message retrieval in status_codes.hpp. - Enhanced strong_types.hpp with a new FlowControl enum for better type safety in flow control settings. - Updated serial.h to include new interface headers for improved modularity and organization. --- .../cpp_core/interface/serial_get_config.h | 44 +++++++++++++++++++ include/cpp_core/interface/serial_get_cts.h | 26 +++++++++++ include/cpp_core/interface/serial_get_dcd.h | 26 +++++++++++ include/cpp_core/interface/serial_get_dsr.h | 25 +++++++++++ include/cpp_core/interface/serial_get_ri.h | 25 +++++++++++ .../cpp_core/interface/serial_monitor_ports.h | 44 +++++++++++++++++++ .../cpp_core/interface/serial_send_break.h | 33 ++++++++++++++ include/cpp_core/interface/serial_set_dtr.h | 28 ++++++++++++ .../interface/serial_set_flow_control.h | 38 ++++++++++++++++ include/cpp_core/interface/serial_set_rts.h | 28 ++++++++++++ .../interface/serial_update_baudrate.h | 38 ++++++++++++++++ include/cpp_core/serial.h | 17 +++++++ include/cpp_core/status_codes.h | 8 ++++ include/cpp_core/status_codes.hpp | 16 +++++++ include/cpp_core/strong_types.hpp | 7 +++ 15 files changed, 403 insertions(+) create mode 100644 include/cpp_core/interface/serial_get_config.h create mode 100644 include/cpp_core/interface/serial_get_cts.h create mode 100644 include/cpp_core/interface/serial_get_dcd.h create mode 100644 include/cpp_core/interface/serial_get_dsr.h create mode 100644 include/cpp_core/interface/serial_get_ri.h create mode 100644 include/cpp_core/interface/serial_monitor_ports.h create mode 100644 include/cpp_core/interface/serial_send_break.h create mode 100644 include/cpp_core/interface/serial_set_dtr.h create mode 100644 include/cpp_core/interface/serial_set_flow_control.h create mode 100644 include/cpp_core/interface/serial_set_rts.h create mode 100644 include/cpp_core/interface/serial_update_baudrate.h diff --git a/include/cpp_core/interface/serial_get_config.h b/include/cpp_core/interface/serial_get_config.h new file mode 100644 index 0000000..4c8c459 --- /dev/null +++ b/include/cpp_core/interface/serial_get_config.h @@ -0,0 +1,44 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current line settings of an open serial port. + * + * The OS-level configuration is read back and reported via the supplied + * callback. This is useful for verifying that serialOpen() or + * serialUpdateBaudrate() applied the requested values, or for inspecting a + * port that was configured externally. + * + * @code{.c} + * void onConfig(int baud, int data_bits, int parity, int stop_bits, int flow_control) { + * printf("baud=%d data=%d par=%d stop=%d flow=%d\n", + * baud, data_bits, parity, stop_bits, flow_control); + * } + * serialGetConfig(handle, onConfig); + * @endcode + * + * @param handle Port handle obtained from serialOpen(). + * @param callback_fn Callback receiving the current configuration values: + * - @p baudrate Current baud rate in bit/s. + * - @p data_bits Number of data bits (5-8). + * - @p parity 0 = none, 1 = even, 2 = odd. + * - @p stop_bits 0 = 1 stop bit, 2 = 2 stop bits. + * - @p flow_control 0 = none, 1 = RTS/CTS, 2 = XON/XOFF. + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialGetConfig(int64_t handle, + void (*callback_fn)(int baudrate, int data_bits, int parity, int stop_bits, + int flow_control), + ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_cts.h b/include/cpp_core/interface/serial_get_cts.h new file mode 100644 index 0000000..8e5df9f --- /dev/null +++ b/include/cpp_core/interface/serial_get_cts.h @@ -0,0 +1,26 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Read the current state of the Clear To Send (CTS) input line. + * + * CTS is asserted by the remote device to indicate it is ready to receive + * data. Polling this line is useful when manual flow-control logic is + * required instead of (or in addition to) automatic RTS/CTS flow control. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetCts(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_dcd.h b/include/cpp_core/interface/serial_get_dcd.h new file mode 100644 index 0000000..86b23ad --- /dev/null +++ b/include/cpp_core/interface/serial_get_dcd.h @@ -0,0 +1,26 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Read the current state of the Data Carrier Detect (DCD) input line. + * + * DCD is asserted by a modem when a carrier signal has been detected on the + * telephone line. For direct serial links it can serve as a general-purpose + * "connection alive" indicator. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetDcd(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_dsr.h b/include/cpp_core/interface/serial_get_dsr.h new file mode 100644 index 0000000..ff239f6 --- /dev/null +++ b/include/cpp_core/interface/serial_get_dsr.h @@ -0,0 +1,25 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Read the current state of the Data Set Ready (DSR) input line. + * + * DSR is asserted by the remote device to indicate it is powered on and + * ready to communicate. It is the counterpart to DTR. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetDsr(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_ri.h b/include/cpp_core/interface/serial_get_ri.h new file mode 100644 index 0000000..13f1f9a --- /dev/null +++ b/include/cpp_core/interface/serial_get_ri.h @@ -0,0 +1,25 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Read the current state of the Ring Indicator (RI) input line. + * + * RI is asserted by a modem to signal an incoming call. On non-modem + * hardware it can be repurposed as a general-purpose input signal. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetRi(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_monitor_ports.h b/include/cpp_core/interface/serial_monitor_ports.h new file mode 100644 index 0000000..ed5f64a --- /dev/null +++ b/include/cpp_core/interface/serial_monitor_ports.h @@ -0,0 +1,44 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Start monitoring for serial-port hot-plug events. + * + * Registers a callback that is invoked whenever a serial port appears or + * disappears on the system (e.g. a USB-to-serial adapter is plugged in or + * removed). The monitoring runs on a background thread managed by the + * library. + * + * Only **one** monitor can be active at a time. Calling this function again + * replaces the previous callback. Pass `nullptr` as @p callback_fn to stop + * monitoring and release the background thread. + * + * @code{.c} + * void onChange(int event, const char *port) { + * if (event == 1) printf("connected: %s\n", port); + * else printf("disconnected: %s\n", port); + * } + * serialMonitorPorts(onChange); + * // ... later ... + * serialMonitorPorts(nullptr); // stop + * @endcode + * + * @param callback_fn Callback receiving: + * - @p event 1 = port appeared (connected), 0 = port disappeared (disconnected). + * - @p port Null-terminated device identifier (e.g. "COM3", "/dev/ttyUSB0"). + * Valid only for the duration of the callback. + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialMonitorPorts(void (*callback_fn)(int event, const char *port), + ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_send_break.h b/include/cpp_core/interface/serial_send_break.h new file mode 100644 index 0000000..046dd3d --- /dev/null +++ b/include/cpp_core/interface/serial_send_break.h @@ -0,0 +1,33 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Send a break condition on the serial line. + * + * A break is a sustained logic-LOW that lasts longer than a normal + * character frame. Many protocols rely on it: + * + * - **DMX512**: A break of >= 88 us marks the start of a new frame. + * - **LIN bus**: The master starts each frame with a 13-bit break. + * - **MODBUS RTU**: Some implementations use break for frame sync. + * + * The @p duration_ms parameter is a *minimum* - the actual break may be + * slightly longer due to OS scheduling. + * + * @param handle Port handle obtained from serialOpen(). + * @param duration_ms Break duration in milliseconds (> 0). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSendBreak(int64_t handle, int duration_ms, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_set_dtr.h b/include/cpp_core/interface/serial_set_dtr.h new file mode 100644 index 0000000..7a84021 --- /dev/null +++ b/include/cpp_core/interface/serial_set_dtr.h @@ -0,0 +1,28 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Set or clear the Data Terminal Ready (DTR) modem control line. + * + * DTR is commonly used for: + * - Signalling readiness to the remote device. + * - Triggering a board reset on Arduino-compatible hardware. + * - Half-duplex direction control on RS-485 adapters. + * + * @param handle Port handle obtained from serialOpen(). + * @param state Non-zero to assert (HIGH), zero to de-assert (LOW). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetDtr(int64_t handle, int state, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_set_flow_control.h b/include/cpp_core/interface/serial_set_flow_control.h new file mode 100644 index 0000000..39435c2 --- /dev/null +++ b/include/cpp_core/interface/serial_set_flow_control.h @@ -0,0 +1,38 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Configure the flow-control mode for an open serial port. + * + * Flow control prevents buffer overruns when one side is slower than the + * other. Three modes are supported: + * + * | @p mode | Meaning | + * |---------|-----------------------------------------------| + * | 0 | None - no flow control (default after open). | + * | 1 | Hardware (RTS/CTS) - the UART automatically | + * | | de-asserts RTS when the RX buffer is full and | + * | | pauses TX when CTS is de-asserted. | + * | 2 | Software (XON/XOFF) - in-band control chars | + * | | `0x11` (XON) and `0x13` (XOFF) are sent to | + * | | pause/resume the remote transmitter. | + * + * Changing the mode on an already-open port takes effect immediately. + * + * @param handle Port handle obtained from serialOpen(). + * @param mode Flow-control mode: 0 = none, 1 = RTS/CTS, 2 = XON/XOFF. + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetFlowControl(int64_t handle, int mode, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_set_rts.h b/include/cpp_core/interface/serial_set_rts.h new file mode 100644 index 0000000..cda45c9 --- /dev/null +++ b/include/cpp_core/interface/serial_set_rts.h @@ -0,0 +1,28 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Set or clear the Request To Send (RTS) modem control line. + * + * RTS is typically used for hardware flow control (RTS/CTS) or + * as a transmit-enable signal in half-duplex RS-485 setups. When + * hardware flow control is **not** enabled via serialSetFlowControl(), + * this function gives manual control over the line. + * + * @param handle Port handle obtained from serialOpen(). + * @param state Non-zero to assert (HIGH), zero to de-assert (LOW). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetRts(int64_t handle, int state, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_update_baudrate.h b/include/cpp_core/interface/serial_update_baudrate.h new file mode 100644 index 0000000..950198a --- /dev/null +++ b/include/cpp_core/interface/serial_update_baudrate.h @@ -0,0 +1,38 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Change the baud rate of an already-open serial port. + * + * The new rate takes effect immediately without closing and re-opening the + * port. All other line settings (data bits, parity, stop bits, flow control) + * remain unchanged. + * + * Typical use-case: a bootloader handshake starts at a safe 9600 baud and + * then both sides switch to a higher speed for the actual data transfer. + * + * @code{.c} + * intptr_t h = serialOpen("/dev/ttyUSB0", 9600, 8); + * // ... bootloader handshake at 9600 ... + * serialUpdateBaudrate(h, 115200); + * // ... fast data transfer ... + * @endcode + * + * @param handle Port handle obtained from serialOpen(). + * @param baudrate New baud rate in bit/s (>= 300). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialUpdateBaudrate(int64_t handle, int baudrate, + ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/serial.h b/include/cpp_core/serial.h index 9401a7a..fafe328 100644 --- a/include/cpp_core/serial.h +++ b/include/cpp_core/serial.h @@ -25,3 +25,20 @@ #include "interface/serial_set_read_callback.h" #include "interface/serial_set_write_callback.h" #include "interface/serial_write.h" + +// Modem line control +#include "interface/serial_set_dtr.h" +#include "interface/serial_set_rts.h" +#include "interface/serial_get_cts.h" +#include "interface/serial_get_dsr.h" +#include "interface/serial_get_dcd.h" +#include "interface/serial_get_ri.h" + +// Extended configuration +#include "interface/serial_set_flow_control.h" +#include "interface/serial_send_break.h" +#include "interface/serial_update_baudrate.h" +#include "interface/serial_get_config.h" + +// Port discovery +#include "interface/serial_monitor_ports.h" diff --git a/include/cpp_core/status_codes.h b/include/cpp_core/status_codes.h index 46c5bc3..0b5771d 100644 --- a/include/cpp_core/status_codes.h +++ b/include/cpp_core/status_codes.h @@ -18,5 +18,13 @@ enum class StatusCodes kClearBufferOutError = -11, kAbortReadError = -12, kAbortWriteError = -13, + kSetDtrError = -14, + kSetRtsError = -15, + kGetModemStatusError = -16, + kSendBreakError = -17, + kSetFlowControlError = -18, + kUpdateBaudrateError = -19, + kGetConfigError = -20, + kMonitorError = -21, }; } // namespace cpp_core diff --git a/include/cpp_core/status_codes.hpp b/include/cpp_core/status_codes.hpp index 569b769..40f2962 100644 --- a/include/cpp_core/status_codes.hpp +++ b/include/cpp_core/status_codes.hpp @@ -41,6 +41,22 @@ namespace cpp_core return "AbortReadError"; case StatusCodes::kAbortWriteError: return "AbortWriteError"; + case StatusCodes::kSetDtrError: + return "SetDtrError"; + case StatusCodes::kSetRtsError: + return "SetRtsError"; + case StatusCodes::kGetModemStatusError: + return "GetModemStatusError"; + case StatusCodes::kSendBreakError: + return "SendBreakError"; + case StatusCodes::kSetFlowControlError: + return "SetFlowControlError"; + case StatusCodes::kUpdateBaudrateError: + return "UpdateBaudrateError"; + case StatusCodes::kGetConfigError: + return "GetConfigError"; + case StatusCodes::kMonitorError: + return "MonitorError"; } return "Unknown"; } diff --git a/include/cpp_core/strong_types.hpp b/include/cpp_core/strong_types.hpp index bb93bd8..330f571 100644 --- a/include/cpp_core/strong_types.hpp +++ b/include/cpp_core/strong_types.hpp @@ -59,4 +59,11 @@ enum class StopBits : int kTwo = 2, }; +enum class FlowControl : int +{ + kNone = 0, + kRtsCts = 1, + kXonXoff = 2, +}; + } // namespace cpp_core From 1635249ef9aa886faea9f2b6e7171084474caeb4 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Mon, 16 Mar 2026 18:46:47 +0100 Subject: [PATCH 2/4] Refactor serial interface for line settings management - Introduced new headers for getting and setting baud rate, data bits, parity, stop bits, and flow control in the serial interface. - Removed deprecated configuration retrieval header to streamline the API. - Updated status codes to reflect new error handling for setting line parameters. - Enhanced documentation for new functions to improve clarity and usability. --- .../cpp_core/interface/serial_get_baudrate.h | 26 +++++++++++ .../cpp_core/interface/serial_get_config.h | 44 ------------------- .../cpp_core/interface/serial_get_data_bits.h | 22 ++++++++++ .../interface/serial_get_flow_control.h | 22 ++++++++++ .../cpp_core/interface/serial_get_parity.h | 22 ++++++++++ .../cpp_core/interface/serial_get_stop_bits.h | 22 ++++++++++ ...pdate_baudrate.h => serial_set_baudrate.h} | 11 +---- .../cpp_core/interface/serial_set_data_bits.h | 26 +++++++++++ .../cpp_core/interface/serial_set_parity.h | 25 +++++++++++ .../cpp_core/interface/serial_set_stop_bits.h | 26 +++++++++++ include/cpp_core/serial.h | 17 +++++-- include/cpp_core/status_codes.h | 6 ++- include/cpp_core/status_codes.hpp | 12 +++-- 13 files changed, 219 insertions(+), 62 deletions(-) create mode 100644 include/cpp_core/interface/serial_get_baudrate.h delete mode 100644 include/cpp_core/interface/serial_get_config.h create mode 100644 include/cpp_core/interface/serial_get_data_bits.h create mode 100644 include/cpp_core/interface/serial_get_flow_control.h create mode 100644 include/cpp_core/interface/serial_get_parity.h create mode 100644 include/cpp_core/interface/serial_get_stop_bits.h rename include/cpp_core/interface/{serial_update_baudrate.h => serial_set_baudrate.h} (71%) create mode 100644 include/cpp_core/interface/serial_set_data_bits.h create mode 100644 include/cpp_core/interface/serial_set_parity.h create mode 100644 include/cpp_core/interface/serial_set_stop_bits.h diff --git a/include/cpp_core/interface/serial_get_baudrate.h b/include/cpp_core/interface/serial_get_baudrate.h new file mode 100644 index 0000000..922bcee --- /dev/null +++ b/include/cpp_core/interface/serial_get_baudrate.h @@ -0,0 +1,26 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current baud rate of an open serial port. + * + * Reads back the baud rate that the OS driver is currently using. Useful for + * verifying that serialOpen() or serialSetBaudrate() applied the requested + * value. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return Current baud rate in bit/s (>= 300) or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetBaudrate(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_config.h b/include/cpp_core/interface/serial_get_config.h deleted file mode 100644 index 4c8c459..0000000 --- a/include/cpp_core/interface/serial_get_config.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "../error_callback.h" -#include "../module_api.h" -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - - /** - * @brief Query the current line settings of an open serial port. - * - * The OS-level configuration is read back and reported via the supplied - * callback. This is useful for verifying that serialOpen() or - * serialUpdateBaudrate() applied the requested values, or for inspecting a - * port that was configured externally. - * - * @code{.c} - * void onConfig(int baud, int data_bits, int parity, int stop_bits, int flow_control) { - * printf("baud=%d data=%d par=%d stop=%d flow=%d\n", - * baud, data_bits, parity, stop_bits, flow_control); - * } - * serialGetConfig(handle, onConfig); - * @endcode - * - * @param handle Port handle obtained from serialOpen(). - * @param callback_fn Callback receiving the current configuration values: - * - @p baudrate Current baud rate in bit/s. - * - @p data_bits Number of data bits (5-8). - * - @p parity 0 = none, 1 = even, 2 = odd. - * - @p stop_bits 0 = 1 stop bit, 2 = 2 stop bits. - * - @p flow_control 0 = none, 1 = RTS/CTS, 2 = XON/XOFF. - * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. - * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. - */ - MODULE_API auto serialGetConfig(int64_t handle, - void (*callback_fn)(int baudrate, int data_bits, int parity, int stop_bits, - int flow_control), - ErrorCallbackT error_callback = nullptr) -> int; - -#ifdef __cplusplus -} -#endif diff --git a/include/cpp_core/interface/serial_get_data_bits.h b/include/cpp_core/interface/serial_get_data_bits.h new file mode 100644 index 0000000..097faf4 --- /dev/null +++ b/include/cpp_core/interface/serial_get_data_bits.h @@ -0,0 +1,22 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current number of data bits of an open serial port. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return Current data bits (5-8) or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetDataBits(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_flow_control.h b/include/cpp_core/interface/serial_get_flow_control.h new file mode 100644 index 0000000..4e10d38 --- /dev/null +++ b/include/cpp_core/interface/serial_get_flow_control.h @@ -0,0 +1,22 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current flow-control mode of an open serial port. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 = none, 1 = RTS/CTS, 2 = XON/XOFF, or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetFlowControl(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_parity.h b/include/cpp_core/interface/serial_get_parity.h new file mode 100644 index 0000000..ad9c82f --- /dev/null +++ b/include/cpp_core/interface/serial_get_parity.h @@ -0,0 +1,22 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current parity setting of an open serial port. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 = none, 1 = even, 2 = odd, or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetParity(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_get_stop_bits.h b/include/cpp_core/interface/serial_get_stop_bits.h new file mode 100644 index 0000000..df40b61 --- /dev/null +++ b/include/cpp_core/interface/serial_get_stop_bits.h @@ -0,0 +1,22 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Query the current stop-bit setting of an open serial port. + * + * @param handle Port handle obtained from serialOpen(). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 = 1 stop bit, 2 = 2 stop bits, or a negative error code from ::cpp_core::StatusCodes. + */ + MODULE_API auto serialGetStopBits(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_update_baudrate.h b/include/cpp_core/interface/serial_set_baudrate.h similarity index 71% rename from include/cpp_core/interface/serial_update_baudrate.h rename to include/cpp_core/interface/serial_set_baudrate.h index 950198a..69f6c4c 100644 --- a/include/cpp_core/interface/serial_update_baudrate.h +++ b/include/cpp_core/interface/serial_set_baudrate.h @@ -18,20 +18,13 @@ extern "C" * Typical use-case: a bootloader handshake starts at a safe 9600 baud and * then both sides switch to a higher speed for the actual data transfer. * - * @code{.c} - * intptr_t h = serialOpen("/dev/ttyUSB0", 9600, 8); - * // ... bootloader handshake at 9600 ... - * serialUpdateBaudrate(h, 115200); - * // ... fast data transfer ... - * @endcode - * * @param handle Port handle obtained from serialOpen(). * @param baudrate New baud rate in bit/s (>= 300). * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. */ - MODULE_API auto serialUpdateBaudrate(int64_t handle, int baudrate, - ErrorCallbackT error_callback = nullptr) -> int; + MODULE_API auto serialSetBaudrate(int64_t handle, int baudrate, + ErrorCallbackT error_callback = nullptr) -> int; #ifdef __cplusplus } diff --git a/include/cpp_core/interface/serial_set_data_bits.h b/include/cpp_core/interface/serial_set_data_bits.h new file mode 100644 index 0000000..d1550ce --- /dev/null +++ b/include/cpp_core/interface/serial_set_data_bits.h @@ -0,0 +1,26 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Change the number of data bits on an already-open serial port. + * + * Takes effect immediately. All other line settings remain unchanged. + * + * @param handle Port handle obtained from serialOpen(). + * @param data_bits Number of data bits (5-8). + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetDataBits(int64_t handle, int data_bits, + ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_set_parity.h b/include/cpp_core/interface/serial_set_parity.h new file mode 100644 index 0000000..ef3edb9 --- /dev/null +++ b/include/cpp_core/interface/serial_set_parity.h @@ -0,0 +1,25 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Change the parity setting on an already-open serial port. + * + * Takes effect immediately. All other line settings remain unchanged. + * + * @param handle Port handle obtained from serialOpen(). + * @param parity 0 = none, 1 = even, 2 = odd. + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetParity(int64_t handle, int parity, ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/interface/serial_set_stop_bits.h b/include/cpp_core/interface/serial_set_stop_bits.h new file mode 100644 index 0000000..0dbf2ce --- /dev/null +++ b/include/cpp_core/interface/serial_set_stop_bits.h @@ -0,0 +1,26 @@ +#pragma once +#include "../error_callback.h" +#include "../module_api.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @brief Change the stop-bit setting on an already-open serial port. + * + * Takes effect immediately. All other line settings remain unchanged. + * + * @param handle Port handle obtained from serialOpen(). + * @param stop_bits 0 = 1 stop bit, 2 = 2 stop bits. + * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. + * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. + */ + MODULE_API auto serialSetStopBits(int64_t handle, int stop_bits, + ErrorCallbackT error_callback = nullptr) -> int; + +#ifdef __cplusplus +} +#endif diff --git a/include/cpp_core/serial.h b/include/cpp_core/serial.h index fafe328..e0c0c40 100644 --- a/include/cpp_core/serial.h +++ b/include/cpp_core/serial.h @@ -34,11 +34,22 @@ #include "interface/serial_get_dcd.h" #include "interface/serial_get_ri.h" -// Extended configuration +// Line-setting getters +#include "interface/serial_get_baudrate.h" +#include "interface/serial_get_data_bits.h" +#include "interface/serial_get_parity.h" +#include "interface/serial_get_stop_bits.h" +#include "interface/serial_get_flow_control.h" + +// Line-setting setters +#include "interface/serial_set_baudrate.h" +#include "interface/serial_set_data_bits.h" +#include "interface/serial_set_parity.h" +#include "interface/serial_set_stop_bits.h" #include "interface/serial_set_flow_control.h" + +// Extended control #include "interface/serial_send_break.h" -#include "interface/serial_update_baudrate.h" -#include "interface/serial_get_config.h" // Port discovery #include "interface/serial_monitor_ports.h" diff --git a/include/cpp_core/status_codes.h b/include/cpp_core/status_codes.h index 0b5771d..4f0c2c8 100644 --- a/include/cpp_core/status_codes.h +++ b/include/cpp_core/status_codes.h @@ -23,8 +23,10 @@ enum class StatusCodes kGetModemStatusError = -16, kSendBreakError = -17, kSetFlowControlError = -18, - kUpdateBaudrateError = -19, - kGetConfigError = -20, + kSetBaudrateError = -19, + kSetDataBitsError = -20, + kSetParityError = -22, + kSetStopBitsError = -23, kMonitorError = -21, }; } // namespace cpp_core diff --git a/include/cpp_core/status_codes.hpp b/include/cpp_core/status_codes.hpp index 40f2962..7390b9e 100644 --- a/include/cpp_core/status_codes.hpp +++ b/include/cpp_core/status_codes.hpp @@ -51,10 +51,14 @@ namespace cpp_core return "SendBreakError"; case StatusCodes::kSetFlowControlError: return "SetFlowControlError"; - case StatusCodes::kUpdateBaudrateError: - return "UpdateBaudrateError"; - case StatusCodes::kGetConfigError: - return "GetConfigError"; + case StatusCodes::kSetBaudrateError: + return "SetBaudrateError"; + case StatusCodes::kSetDataBitsError: + return "SetDataBitsError"; + case StatusCodes::kSetParityError: + return "SetParityError"; + case StatusCodes::kSetStopBitsError: + return "SetStopBitsError"; case StatusCodes::kMonitorError: return "MonitorError"; } From e1fbaefc7388e493ecb1519795cd38c58455945d Mon Sep 17 00:00:00 2001 From: Katze719 Date: Mon, 16 Mar 2026 18:51:02 +0100 Subject: [PATCH 3/4] Update status codes in status_codes.h to correct the position of kMonitorError and maintain consistency in error handling definitions. --- include/cpp_core/status_codes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cpp_core/status_codes.h b/include/cpp_core/status_codes.h index 4f0c2c8..58f0fcc 100644 --- a/include/cpp_core/status_codes.h +++ b/include/cpp_core/status_codes.h @@ -25,8 +25,8 @@ enum class StatusCodes kSetFlowControlError = -18, kSetBaudrateError = -19, kSetDataBitsError = -20, + kMonitorError = -21, kSetParityError = -22, kSetStopBitsError = -23, - kMonitorError = -21, }; } // namespace cpp_core From b8341faf646e950bd7a283ec68a2fa195c575929 Mon Sep 17 00:00:00 2001 From: Katze719 Date: Tue, 17 Mar 2026 18:48:35 +0100 Subject: [PATCH 4/4] refactor: remove serialMonitorPorts from this branch Moved to a dedicated feature/serial-monitor-ports branch for a separate PR. --- .../cpp_core/interface/serial_monitor_ports.h | 44 ------------------- include/cpp_core/serial.h | 3 -- include/cpp_core/status_codes.h | 1 - include/cpp_core/status_codes.hpp | 2 - 4 files changed, 50 deletions(-) delete mode 100644 include/cpp_core/interface/serial_monitor_ports.h diff --git a/include/cpp_core/interface/serial_monitor_ports.h b/include/cpp_core/interface/serial_monitor_ports.h deleted file mode 100644 index ed5f64a..0000000 --- a/include/cpp_core/interface/serial_monitor_ports.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "../error_callback.h" -#include "../module_api.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - - /** - * @brief Start monitoring for serial-port hot-plug events. - * - * Registers a callback that is invoked whenever a serial port appears or - * disappears on the system (e.g. a USB-to-serial adapter is plugged in or - * removed). The monitoring runs on a background thread managed by the - * library. - * - * Only **one** monitor can be active at a time. Calling this function again - * replaces the previous callback. Pass `nullptr` as @p callback_fn to stop - * monitoring and release the background thread. - * - * @code{.c} - * void onChange(int event, const char *port) { - * if (event == 1) printf("connected: %s\n", port); - * else printf("disconnected: %s\n", port); - * } - * serialMonitorPorts(onChange); - * // ... later ... - * serialMonitorPorts(nullptr); // stop - * @endcode - * - * @param callback_fn Callback receiving: - * - @p event 1 = port appeared (connected), 0 = port disappeared (disconnected). - * - @p port Null-terminated device identifier (e.g. "COM3", "/dev/ttyUSB0"). - * Valid only for the duration of the callback. - * @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`. - * @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error. - */ - MODULE_API auto serialMonitorPorts(void (*callback_fn)(int event, const char *port), - ErrorCallbackT error_callback = nullptr) -> int; - -#ifdef __cplusplus -} -#endif diff --git a/include/cpp_core/serial.h b/include/cpp_core/serial.h index e0c0c40..06e9c58 100644 --- a/include/cpp_core/serial.h +++ b/include/cpp_core/serial.h @@ -50,6 +50,3 @@ // Extended control #include "interface/serial_send_break.h" - -// Port discovery -#include "interface/serial_monitor_ports.h" diff --git a/include/cpp_core/status_codes.h b/include/cpp_core/status_codes.h index 58f0fcc..4da48ce 100644 --- a/include/cpp_core/status_codes.h +++ b/include/cpp_core/status_codes.h @@ -25,7 +25,6 @@ enum class StatusCodes kSetFlowControlError = -18, kSetBaudrateError = -19, kSetDataBitsError = -20, - kMonitorError = -21, kSetParityError = -22, kSetStopBitsError = -23, }; diff --git a/include/cpp_core/status_codes.hpp b/include/cpp_core/status_codes.hpp index 7390b9e..3573de2 100644 --- a/include/cpp_core/status_codes.hpp +++ b/include/cpp_core/status_codes.hpp @@ -59,8 +59,6 @@ namespace cpp_core return "SetParityError"; case StatusCodes::kSetStopBitsError: return "SetStopBitsError"; - case StatusCodes::kMonitorError: - return "MonitorError"; } return "Unknown"; }