Skip to content

Commit 62b5b39

Browse files
authored
Add modem control and configuration interfaces (#16)
* 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. * 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. * Update status codes in status_codes.h to correct the position of kMonitorError and maintain consistency in error handling definitions. * refactor: remove serialMonitorPorts from this branch Moved to a dedicated feature/serial-monitor-ports branch for a separate PR.
1 parent 530e66a commit 62b5b39

21 files changed

Lines changed: 510 additions & 0 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Query the current baud rate of an open serial port.
13+
*
14+
* Reads back the baud rate that the OS driver is currently using. Useful for
15+
* verifying that serialOpen() or serialSetBaudrate() applied the requested
16+
* value.
17+
*
18+
* @param handle Port handle obtained from serialOpen().
19+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
20+
* @return Current baud rate in bit/s (>= 300) or a negative error code from ::cpp_core::StatusCodes.
21+
*/
22+
MODULE_API auto serialGetBaudrate(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Read the current state of the Clear To Send (CTS) input line.
13+
*
14+
* CTS is asserted by the remote device to indicate it is ready to receive
15+
* data. Polling this line is useful when manual flow-control logic is
16+
* required instead of (or in addition to) automatic RTS/CTS flow control.
17+
*
18+
* @param handle Port handle obtained from serialOpen().
19+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
20+
* @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes.
21+
*/
22+
MODULE_API auto serialGetCts(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Query the current number of data bits of an open serial port.
13+
*
14+
* @param handle Port handle obtained from serialOpen().
15+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
16+
* @return Current data bits (5-8) or a negative error code from ::cpp_core::StatusCodes.
17+
*/
18+
MODULE_API auto serialGetDataBits(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Read the current state of the Data Carrier Detect (DCD) input line.
13+
*
14+
* DCD is asserted by a modem when a carrier signal has been detected on the
15+
* telephone line. For direct serial links it can serve as a general-purpose
16+
* "connection alive" indicator.
17+
*
18+
* @param handle Port handle obtained from serialOpen().
19+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
20+
* @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes.
21+
*/
22+
MODULE_API auto serialGetDcd(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Read the current state of the Data Set Ready (DSR) input line.
13+
*
14+
* DSR is asserted by the remote device to indicate it is powered on and
15+
* ready to communicate. It is the counterpart to DTR.
16+
*
17+
* @param handle Port handle obtained from serialOpen().
18+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
19+
* @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes.
20+
*/
21+
MODULE_API auto serialGetDsr(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Query the current flow-control mode of an open serial port.
13+
*
14+
* @param handle Port handle obtained from serialOpen().
15+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
16+
* @return 0 = none, 1 = RTS/CTS, 2 = XON/XOFF, or a negative error code from ::cpp_core::StatusCodes.
17+
*/
18+
MODULE_API auto serialGetFlowControl(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Query the current parity setting of an open serial port.
13+
*
14+
* @param handle Port handle obtained from serialOpen().
15+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
16+
* @return 0 = none, 1 = even, 2 = odd, or a negative error code from ::cpp_core::StatusCodes.
17+
*/
18+
MODULE_API auto serialGetParity(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Read the current state of the Ring Indicator (RI) input line.
13+
*
14+
* RI is asserted by a modem to signal an incoming call. On non-modem
15+
* hardware it can be repurposed as a general-purpose input signal.
16+
*
17+
* @param handle Port handle obtained from serialOpen().
18+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
19+
* @return 1 if asserted (HIGH), 0 if de-asserted (LOW), or a negative error code from ::cpp_core::StatusCodes.
20+
*/
21+
MODULE_API auto serialGetRi(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Query the current stop-bit setting of an open serial port.
13+
*
14+
* @param handle Port handle obtained from serialOpen().
15+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
16+
* @return 0 = 1 stop bit, 2 = 2 stop bits, or a negative error code from ::cpp_core::StatusCodes.
17+
*/
18+
MODULE_API auto serialGetStopBits(int64_t handle, ErrorCallbackT error_callback = nullptr) -> int;
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
#include "../error_callback.h"
3+
#include "../module_api.h"
4+
#include <cstdint>
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
/**
12+
* @brief Send a break condition on the serial line.
13+
*
14+
* A break is a sustained logic-LOW that lasts longer than a normal
15+
* character frame. Many protocols rely on it:
16+
*
17+
* - **DMX512**: A break of >= 88 us marks the start of a new frame.
18+
* - **LIN bus**: The master starts each frame with a 13-bit break.
19+
* - **MODBUS RTU**: Some implementations use break for frame sync.
20+
*
21+
* The @p duration_ms parameter is a *minimum* - the actual break may be
22+
* slightly longer due to OS scheduling.
23+
*
24+
* @param handle Port handle obtained from serialOpen().
25+
* @param duration_ms Break duration in milliseconds (> 0).
26+
* @param error_callback [optional] Callback to invoke on error. Defined in error_callback.h. Default is `nullptr`.
27+
* @return 0 on success or a negative error code from ::cpp_core::StatusCodes on error.
28+
*/
29+
MODULE_API auto serialSendBreak(int64_t handle, int duration_ms, ErrorCallbackT error_callback = nullptr) -> int;
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif

0 commit comments

Comments
 (0)