Ultibo API
C/C++ API for Ultibo Core
Loading...
Searching...
No Matches
serial.h
Go to the documentation of this file.
1/*
2 * This file is part of the Ultibo project, https://ultibo.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2026 Garry Wood <garry@softoz.com.au>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#ifndef _ULTIBO_SERIAL_H
27#define _ULTIBO_SERIAL_H
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include "ultibo/globaltypes.h"
34#include "ultibo/globalconst.h"
35#include "ultibo/devices.h"
36
38#define SERIAL_NAME_PREFIX "Serial"
39
40#define SERIAL_LOGGING_DESCRIPTION "Serial Logging"
41
42#define SERIAL_RECEIVE_DEPTH_DEFAULT SIZE_2K
43#define SERIAL_TRANSMIT_DEPTH_DEFAULT SIZE_2K
44
45#define SERIAL_PUSH_TIMEOUT 50
46
48#define SERIAL_TYPE_NONE 0
49#define SERIAL_TYPE_UART 1
50#define SERIAL_TYPE_USB 2
51
52#define SERIAL_TYPE_MAX 2
53
55#define SERIAL_STATE_CLOSED 0
56#define SERIAL_STATE_CLOSING 1
57#define SERIAL_STATE_OPENING 2
58#define SERIAL_STATE_OPEN 3
59
60#define SERIAL_STATE_MAX 3
61
63#define SERIAL_FLAG_NONE 0x00000000
64#define SERIAL_FLAG_DATA_8BIT 0x00000001
65#define SERIAL_FLAG_DATA_7BIT 0x00000002
66#define SERIAL_FLAG_DATA_6BIT 0x00000004
67#define SERIAL_FLAG_DATA_5BIT 0x00000008
68#define SERIAL_FLAG_STOP_1BIT 0x00000010
69#define SERIAL_FLAG_STOP_2BIT 0x00000020
70#define SERIAL_FLAG_STOP_1BIT5 0x00000040
71#define SERIAL_FLAG_PARITY_ODD 0x00000080
72#define SERIAL_FLAG_PARITY_EVEN 0x00000100
73#define SERIAL_FLAG_PARITY_MARK 0x00000200
74#define SERIAL_FLAG_PARITY_SPACE 0x00000400
75#define SERIAL_FLAG_FLOW_RTS_CTS 0x00000800
76#define SERIAL_FLAG_FLOW_DSR_DTR 0x00001000
77#define SERIAL_FLAG_PUSH_RX 0x00002000
78#define SERIAL_FLAG_PUSH_TX 0x00004000
79
81#define SERIAL_READ_NONE 0x00000000
82#define SERIAL_READ_NON_BLOCK 0x00000001
83#define SERIAL_READ_PEEK_BUFFER 0x00000002
84
86#define SERIAL_WRITE_NONE 0x00000000
87#define SERIAL_WRITE_NON_BLOCK 0x00000001
88#define SERIAL_WRITE_PEEK_BUFFER 0x00000002
89
91#define SERIAL_WAIT_NONE 0
92#define SERIAL_WAIT_RECEIVE 1
93#define SERIAL_WAIT_TRANSMIT 2
94
96#define SERIAL_FLUSH_NONE 0x00000000
97#define SERIAL_FLUSH_RECEIVE 0x00000001
98#define SERIAL_FLUSH_TRANSMIT 0x00000002
99
101#define SERIAL_STATUS_NONE 0x00000000
102#define SERIAL_STATUS_RTS 0x00000001
103#define SERIAL_STATUS_CTS 0x00000002
104#define SERIAL_STATUS_DSR 0x00000004
105#define SERIAL_STATUS_DTR 0x00000008
106#define SERIAL_STATUS_RX_FULL 0x00000010
107#define SERIAL_STATUS_RX_EMPTY 0x00000020
108#define SERIAL_STATUS_TX_FULL 0x00000040
109#define SERIAL_STATUS_TX_EMPTY 0x00000080
110#define SERIAL_STATUS_BUSY 0x00000100
111#define SERIAL_STATUS_BREAK_ERROR 0x00000200
112#define SERIAL_STATUS_PARITY_ERROR 0x00000400
113#define SERIAL_STATUS_FRAMING_ERROR 0x00000800
114#define SERIAL_STATUS_OVERRUN_ERROR 0x00001000
115#define SERIAL_STATUS_DCD 0x00002000
116#define SERIAL_STATUS_RI 0x00004000
117
119
123{
124 uint32_t flags;
125 uint32_t minrate;
126 uint32_t maxrate;
127 uint32_t baudrate;
128 uint32_t databits;
129 uint32_t stopbits;
130 uint32_t parity;
131 uint32_t flowcontrol;
132 uint32_t receivedepth;
133 uint32_t transmitdepth;
134};
135
139{
141 uint32_t start;
142 uint32_t count;
143 uint32_t size;
144 void *data;
145};
146
149
151typedef uint32_t STDCALL (*serial_enumerate_cb)(SERIAL_DEVICE *serial, void *data);
153typedef uint32_t STDCALL (*serial_notification_cb)(DEVICE *device, void *data, uint32_t notification);
154
156typedef uint32_t STDCALL (*serial_device_open_proc)(SERIAL_DEVICE *serial, uint32_t baudrate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flowcontrol, uint32_t receivedepth, uint32_t transmitdepth);
157typedef uint32_t STDCALL (*serial_device_close_proc)(SERIAL_DEVICE *serial);
158
159typedef uint32_t STDCALL (*serial_device_read_proc)(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
160typedef uint32_t STDCALL (*serial_device_write_proc)(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
161
162typedef uint32_t STDCALL (*serial_device_wait_proc)(SERIAL_DEVICE *serial, uint32_t direction, uint32_t timeout, uint32_t *count);
163typedef uint32_t STDCALL (*serial_device_flush_proc)(SERIAL_DEVICE *serial, uint32_t flags);
164
166typedef uint32_t STDCALL (*serial_device_set_status_proc)(SERIAL_DEVICE *serial, uint32_t status);
167
170
205
207
220uint32_t STDCALL serial_device_open(SERIAL_DEVICE *serial, uint32_t baudrate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flowcontrol, uint32_t receivedepth, uint32_t transmitdepth);
221
228
238uint32_t STDCALL serial_device_read(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
239
249uint32_t STDCALL serial_device_write(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
250
259uint32_t STDCALL serial_device_wait(SERIAL_DEVICE *serial, uint32_t direction, uint32_t timeout, uint32_t *count);
260
267uint32_t STDCALL serial_device_flush(SERIAL_DEVICE *serial, uint32_t flags);
268
276
283
292uint32_t STDCALL serial_device_set_status(SERIAL_DEVICE *serial, uint32_t status);
293
302
310
318
324
331
338
345
352
359
366
373
381
390uint32_t STDCALL serial_device_notification(SERIAL_DEVICE *serial, serial_notification_cb callback, void *data, uint32_t notification, uint32_t flags);
391
398int STDCALL serial_device_printf(SERIAL_DEVICE *serial, const char *format, ...) _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
399
401
406
411
416
421
425uint32_t STDCALL serial_type_to_string(uint32_t serialtype, char *string, uint32_t len);
426
430uint32_t STDCALL serial_state_to_string(uint32_t serialstate, char *string, uint32_t len);
431
440
449
454void * STDCALL serial_buffer_read_start(SERIAL_BUFFER *buffer, uint32_t *available);
455
461
466void * STDCALL serial_buffer_write_start(SERIAL_BUFFER *buffer, uint32_t *available);
467
473
474uint32_t STDCALL serial_data_bits_to_string(uint32_t bits, char *string, uint32_t len);
475uint32_t STDCALL serial_stop_bits_to_string(uint32_t bits, char *string, uint32_t len);
476uint32_t STDCALL serial_parity_to_string(uint32_t parity, char *string, uint32_t len);
477uint32_t STDCALL serial_flow_control_to_string(uint32_t flow, char *string, uint32_t len);
478
480
485
490
495uint32_t STDCALL serial_logging_device_parameters(SERIAL_DEVICE *serial, const char *parameters, uint32_t *baudrate, uint32_t *parity, uint32_t *databits, uint32_t *stopbits);
496
497#ifdef __cplusplus
498}
499#endif
500
501#endif // _ULTIBO_SERIAL_H
struct _DEVICE DEVICE
Definition devices.h:373
HANDLE EVENT_HANDLE
Definition globaltypes.h:117
#define STDCALL
Definition globaltypes.h:45
_Bool BOOL
Built in Boolean type (1 byte) (Redeclared here for flexibility).
Definition globaltypes.h:55
HANDLE MUTEX_HANDLE
Definition globaltypes.h:105
uint32_t STDCALL serial_flow_control_to_string(uint32_t flow, char *string, uint32_t len)
SERIAL_DEVICE *STDCALL serial_device_get_default(void)
Get the current default Serial device.
uint32_t STDCALL(* serial_enumerate_cb)(SERIAL_DEVICE *serial, void *data)
Definition serial.h:151
uint32_t STDCALL serial_logging_device_add(SERIAL_DEVICE *serial)
Add a new serial logging device on receipt of a device register notification.
uint32_t STDCALL serial_device_deregister(SERIAL_DEVICE *serial)
Deregister a Serial from the Serial table.
uint32_t STDCALL(* serial_device_get_status_proc)(SERIAL_DEVICE *serial)
Definition serial.h:165
SERIAL_DEVICE *STDCALL serial_device_create(void)
Create a new Serial entry.
uint32_t STDCALL serial_device_close(SERIAL_DEVICE *serial)
Close a Serial device and terminate sending and receiving.
void *STDCALL serial_buffer_read_start(SERIAL_BUFFER *buffer, uint32_t *available)
Return a pointer to the next read from the buffer and the number of bytes that can be read.
uint32_t STDCALL(* serial_device_flush_proc)(SERIAL_DEVICE *serial, uint32_t flags)
Definition serial.h:163
uint32_t STDCALL serial_device_destroy(SERIAL_DEVICE *serial)
Destroy an existing Serial entry.
uint32_t STDCALL serial_logging_device_remove(SERIAL_DEVICE *serial)
Remove a serial logging device on receipt of a device deregister notification.
struct _SERIAL_PROPERTIES SERIAL_PROPERTIES
Definition serial.h:121
uint32_t STDCALL serial_device_set_status(SERIAL_DEVICE *serial, uint32_t status)
Set the current line status of a Serial device.
uint32_t STDCALL serial_device_properties(SERIAL_DEVICE *serial, SERIAL_PROPERTIES *properties)
Get the properties for the specified Serial device.
uint32_t STDCALL serial_device_read(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Read data from a Serial device.
uint32_t STDCALL serial_stop_bits_to_string(uint32_t bits, char *string, uint32_t len)
BOOL STDCALL serial_buffer_write_complete(SERIAL_BUFFER *buffer, uint32_t added)
Update the buffer to reflect the number of bytes added when writing.
uint32_t STDCALL serial_device_set_default(SERIAL_DEVICE *serial)
Set the current default Serial device.
uint32_t STDCALL serial_device_set_properties(SERIAL_DEVICE *serial, SERIAL_PROPERTIES *properties)
Set the properties for the specified Serial device.
uint32_t STDCALL serial_state_to_string(uint32_t serialstate, char *string, uint32_t len)
Convert a Serial state value to a string.
uint32_t STDCALL serial_device_status(SERIAL_DEVICE *serial)
Get the current line status of a Serial device.
struct _SERIAL_DEVICE SERIAL_DEVICE
Definition serial.h:148
uint32_t STDCALL serial_device_get_properties(SERIAL_DEVICE *serial, SERIAL_PROPERTIES *properties)
Get the properties for the specified Serial device.
uint32_t STDCALL(* serial_device_get_properties_proc)(SERIAL_DEVICE *serial, SERIAL_PROPERTIES *properties)
Definition serial.h:168
uint32_t STDCALL serial_device_enumerate(serial_enumerate_cb callback, void *data)
Enumerate all serial devices in the serial table.
BOOL STDCALL serial_buffer_read_complete(SERIAL_BUFFER *buffer, uint32_t removed)
Update the buffer to reflect the number of bytes removed when reading.
uint32_t STDCALL serial_device_flush(SERIAL_DEVICE *serial, uint32_t flags)
Discard the contents of the receive and/or transmit buffers of a Serial device.
SERIAL_DEVICE *STDCALL serial_device_find(uint32_t serialid)
Find a serial device by ID in the serial table.
uint32_t STDCALL(* serial_notification_cb)(DEVICE *device, void *data, uint32_t notification)
Definition serial.h:153
uint32_t STDCALL serial_parity_to_string(uint32_t parity, char *string, uint32_t len)
uint32_t STDCALL(* serial_device_open_proc)(SERIAL_DEVICE *serial, uint32_t baudrate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flowcontrol, uint32_t receivedepth, uint32_t transmitdepth)
Definition serial.h:156
uint32_t STDCALL(* serial_device_set_status_proc)(SERIAL_DEVICE *serial, uint32_t status)
Definition serial.h:166
uint32_t STDCALL serial_device_wait(SERIAL_DEVICE *serial, uint32_t direction, uint32_t timeout, uint32_t *count)
Wait for data to be available in the receive or transmit buffers of a Serial device.
uint32_t STDCALL serial_device_register(SERIAL_DEVICE *serial)
Register a new Serial in the Serial table.
uint32_t STDCALL serial_device_get_status(SERIAL_DEVICE *serial)
Get the current line status of a Serial device.
uint32_t STDCALL serial_device_notification(SERIAL_DEVICE *serial, serial_notification_cb callback, void *data, uint32_t notification, uint32_t flags)
Register a notification for serial device changes.
uint32_t STDCALL(* serial_device_wait_proc)(SERIAL_DEVICE *serial, uint32_t direction, uint32_t timeout, uint32_t *count)
Definition serial.h:162
SERIAL_DEVICE *STDCALL serial_device_find_by_name(const char *name)
Find a serial device by name in the serial table.
uint32_t STDCALL(* serial_device_read_proc)(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Definition serial.h:159
int STDCALL uint32_t STDCALL serial_get_count(void)
Get the current Serial count.
uint32_t STDCALL serial_device_open(SERIAL_DEVICE *serial, uint32_t baudrate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flowcontrol, uint32_t receivedepth, uint32_t transmitdepth)
Open a Serial device ready for sending and receiving.
uint32_t STDCALL(* serial_device_close_proc)(SERIAL_DEVICE *serial)
Definition serial.h:157
BOOL STDCALL serial_device_redirect_input(SERIAL_DEVICE *serial)
Redirect standard input to the serial device specified by Serial.
void *STDCALL serial_buffer_write_start(SERIAL_BUFFER *buffer, uint32_t *available)
Return a pointer to the next write to the buffer and the number of bytes that can be written.
BOOL STDCALL serial_device_redirect_output(SERIAL_DEVICE *serial)
Redirect standard output to the serial device specified by Serial.
struct _SERIAL_BUFFER SERIAL_BUFFER
Definition serial.h:137
SERIAL_DEVICE *STDCALL serial_device_check(SERIAL_DEVICE *serial)
Check if the supplied Serial is in the Serial table.
uint32_t STDCALL serial_data_bits_to_string(uint32_t bits, char *string, uint32_t len)
uint32_t STDCALL(* serial_device_set_properties_proc)(SERIAL_DEVICE *serial, SERIAL_PROPERTIES *properties)
Definition serial.h:169
uint32_t STDCALL serial_device_write(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Write data to a Serial device.
SERIAL_DEVICE *STDCALL serial_device_find_by_description(const char *description)
Find a serial device by description in the serial table.
uint32_t STDCALL serial_type_to_string(uint32_t serialtype, char *string, uint32_t len)
Convert a Serial type value to a string.
uint32_t STDCALL serial_logging_device_parameters(SERIAL_DEVICE *serial, const char *parameters, uint32_t *baudrate, uint32_t *parity, uint32_t *databits, uint32_t *stopbits)
Break down the serial parameters value into component parts of baud rate, parity, data bits and stop ...
int STDCALL serial_device_printf(SERIAL_DEVICE *serial, const char *format,...) _ATTRIBUTE((__format__(__printf__
Print formatted text to a Serial device.
SERIAL_DEVICE *STDCALL serial_device_create_ex(uint32_t size)
Create a new Serial entry.
uint32_t STDCALL(* serial_device_write_proc)(SERIAL_DEVICE *serial, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Definition serial.h:160
Definition serial.h:139
EVENT_HANDLE wait
Data ready / Buffer free event.
Definition serial.h:140
uint32_t start
Index of first byte in buffer.
Definition serial.h:141
void * data
Buffered data.
Definition serial.h:144
uint32_t count
Number of bytes in buffer.
Definition serial.h:142
uint32_t size
Size of buffer.
Definition serial.h:143
Definition serial.h:172
uint32_t transmitcount
Definition serial.h:198
MUTEX_HANDLE lock
Device lock.
Definition serial.h:190
uint32_t serialstate
Serial state (eg SERIAL_STATE_OPEN).
Definition serial.h:177
uint32_t receiveerrors
Definition serial.h:196
serial_device_set_properties_proc devicesetproperties
A Device specific DeviceSetProperties method implementing the standard Serial device interface (Or ni...
Definition serial.h:188
SERIAL_BUFFER receive
Serial receive buffer.
Definition serial.h:191
serial_device_close_proc deviceclose
A Device specific DeviceClose method implementing the standard Serial device interface (Mandatory).
Definition serial.h:180
uint32_t transmitoverruns
Definition serial.h:200
DEVICE device
The Device entry for this Serial.
Definition serial.h:174
SERIAL_PROPERTIES properties
Device properties.
Definition serial.h:193
serial_device_write_proc devicewrite
A Device specific DeviceWrite method implementing the standard Serial device interface (Mandatory).
Definition serial.h:182
uint32_t receivecount
Definition serial.h:195
SERIAL_BUFFER transmit
Serial transmit buffer.
Definition serial.h:192
serial_device_set_status_proc devicesetstatus
A Device specific DeviceSetStatus method implementing the standard Serial device interface (Optional)...
Definition serial.h:186
uint32_t receiveoverruns
Definition serial.h:197
SERIAL_DEVICE * prev
Previous entry in Serial table.
Definition serial.h:202
SERIAL_DEVICE * next
Next entry in Serial table.
Definition serial.h:203
serial_device_get_properties_proc devicegetproperties
A Device specific DeviceGetProperties method implementing the standard Serial device interface (Or ni...
Definition serial.h:187
serial_device_get_status_proc devicegetstatus
A Device specific DeviceGetStatus method implementing the standard Serial device interface (Or nil if...
Definition serial.h:185
serial_device_flush_proc deviceflush
A Device specific DeviceFlush method implementing the standard Serial device interface (Or nil if the...
Definition serial.h:184
uint32_t serialid
Unique Id of this Serial device in the Serial device table.
Definition serial.h:176
uint32_t transmiterrors
Definition serial.h:199
uint32_t serialstatus
Serial status (eg SERIAL_STATUS_RX_FULL)(May not be real time status depending on the driver).
Definition serial.h:178
serial_device_wait_proc devicewait
A Device specific DeviceWait method implementing the standard Serial device interface (Or nil if the ...
Definition serial.h:183
serial_device_read_proc deviceread
A Device specific DeviceRead method implementing the standard Serial device interface (Mandatory).
Definition serial.h:181
serial_device_open_proc deviceopen
A Device specific DeviceOpen method implementing the standard Serial device interface (Mandatory).
Definition serial.h:179
Definition serial.h:123
uint32_t minrate
Minimum supported baud rate (0 for any rate supported).
Definition serial.h:125
uint32_t transmitdepth
Current transmit depth setting.
Definition serial.h:133
uint32_t parity
Current parity setting.
Definition serial.h:130
uint32_t flags
Device flags (eg SERIAL_FLAG_DATA_8BIT).
Definition serial.h:124
uint32_t maxrate
Maximum supported baud rate (0 for any rate supported).
Definition serial.h:126
uint32_t stopbits
Current stop bits setting.
Definition serial.h:129
uint32_t flowcontrol
Current flow control setting.
Definition serial.h:131
uint32_t databits
Current data bits setting.
Definition serial.h:128
uint32_t baudrate
Current baud rate setting.
Definition serial.h:127
uint32_t receivedepth
Current receive depth setting.
Definition serial.h:132