blob: ecef1828355121c28a2c597aa2b5b2f16de3e207 [file] [log] [blame]
Marc-André Lureau4d43a602017-01-26 18:26:44 +04001#ifndef QEMU_CHAR_FE_H
2#define QEMU_CHAR_FE_H
3
4#include "chardev/char.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +02005#include "qemu/main-loop.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +04006
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01007typedef void IOEventHandler(void *opaque, QEMUChrEvent event);
Anton Nefedov81517ba2017-07-06 15:08:49 +03008typedef int BackendChangeHandler(void *opaque);
Marc-André Lureau4d43a602017-01-26 18:26:44 +04009
Alex Bennée67b55952023-12-11 14:59:59 +000010/**
11 * struct CharBackend - back end as seen by front end
12 * @fe_is_open: the front end is ready for IO
13 *
14 * The actual backend is Chardev
15 */
Marc-André Lureau4d43a602017-01-26 18:26:44 +040016struct CharBackend {
17 Chardev *chr;
18 IOEventHandler *chr_event;
19 IOCanReadHandler *chr_can_read;
20 IOReadHandler *chr_read;
Anton Nefedov81517ba2017-07-06 15:08:49 +030021 BackendChangeHandler *chr_be_change;
Marc-André Lureau4d43a602017-01-26 18:26:44 +040022 void *opaque;
23 int tag;
Alex Bennée67b55952023-12-11 14:59:59 +000024 bool fe_is_open;
Marc-André Lureau4d43a602017-01-26 18:26:44 +040025};
26
27/**
Marc-André Lureau56625762018-08-24 16:22:47 +020028 * qemu_chr_fe_init:
Marc-André Lureau4d43a602017-01-26 18:26:44 +040029 *
30 * Initializes a front end for the given CharBackend and
31 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
32 * release the driver.
33 *
34 * Returns: false on error.
35 */
36bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp);
37
38/**
Marc-André Lureau56625762018-08-24 16:22:47 +020039 * qemu_chr_fe_deinit:
Marc-André Lureau1ce26102017-01-27 00:49:13 +040040 * @b: a CharBackend
41 * @del: if true, delete the chardev backend
42*
Marc-André Lureau4d43a602017-01-26 18:26:44 +040043 * Dissociate the CharBackend from the Chardev.
44 *
45 * Safe to call without associated Chardev.
46 */
Marc-André Lureau1ce26102017-01-27 00:49:13 +040047void qemu_chr_fe_deinit(CharBackend *b, bool del);
Marc-André Lureau4d43a602017-01-26 18:26:44 +040048
49/**
Marc-André Lureau56625762018-08-24 16:22:47 +020050 * qemu_chr_fe_get_driver:
Marc-André Lureau4d43a602017-01-26 18:26:44 +040051 *
Marc-André Lureau56625762018-08-24 16:22:47 +020052 * Returns: the driver associated with a CharBackend or NULL if no
Marc-André Lureau4d43a602017-01-26 18:26:44 +040053 * associated Chardev.
Anton Nefedov7c44a2a2017-07-06 15:08:51 +030054 * Note: avoid this function as the driver should never be accessed directly,
55 * especially by the frontends that support chardevice hotswap.
56 * Consider qemu_chr_fe_backend_connected() to check for driver existence
Marc-André Lureau4d43a602017-01-26 18:26:44 +040057 */
58Chardev *qemu_chr_fe_get_driver(CharBackend *be);
59
60/**
Marc-André Lureau56625762018-08-24 16:22:47 +020061 * qemu_chr_fe_backend_connected:
Anton Nefedov7c44a2a2017-07-06 15:08:51 +030062 *
Marc-André Lureau56625762018-08-24 16:22:47 +020063 * Returns: true if there is a chardevice associated with @be.
Anton Nefedov7c44a2a2017-07-06 15:08:51 +030064 */
65bool qemu_chr_fe_backend_connected(CharBackend *be);
66
67/**
Marc-André Lureau56625762018-08-24 16:22:47 +020068 * qemu_chr_fe_backend_open:
Anton Nefedov30650702017-07-06 15:08:52 +030069 *
Marc-André Lureau56625762018-08-24 16:22:47 +020070 * Returns: true if chardevice associated with @be is open.
Anton Nefedov30650702017-07-06 15:08:52 +030071 */
72bool qemu_chr_fe_backend_open(CharBackend *be);
73
74/**
Artem Pisarenko7a9657e2018-11-06 18:40:51 +060075 * qemu_chr_fe_set_handlers_full:
Marc-André Lureau4d43a602017-01-26 18:26:44 +040076 * @b: a CharBackend
77 * @fd_can_read: callback to get the amount of data the frontend may
78 * receive
79 * @fd_read: callback to receive data from char
80 * @fd_event: event callback
Anton Nefedov81517ba2017-07-06 15:08:49 +030081 * @be_change: backend change callback; passing NULL means hot backend change
82 * is not supported and will not be attempted
Marc-André Lureau4d43a602017-01-26 18:26:44 +040083 * @opaque: an opaque pointer for the callbacks
84 * @context: a main loop context or NULL for the default
Michael Tokareva1a62ce2023-07-14 14:33:02 +030085 * @set_open: whether to call qemu_chr_fe_set_open() implicitly when
Marc-André Lureau4d43a602017-01-26 18:26:44 +040086 * any of the handler is non-NULL
Artem Pisarenko7a9657e2018-11-06 18:40:51 +060087 * @sync_state: whether to issue event callback with updated state
Marc-André Lureau4d43a602017-01-26 18:26:44 +040088 *
89 * Set the front end char handlers. The front end takes the focus if
90 * any of the handler is non-NULL.
91 *
92 * Without associated Chardev, nothing is changed.
93 */
Artem Pisarenko7a9657e2018-11-06 18:40:51 +060094void qemu_chr_fe_set_handlers_full(CharBackend *b,
95 IOCanReadHandler *fd_can_read,
96 IOReadHandler *fd_read,
97 IOEventHandler *fd_event,
98 BackendChangeHandler *be_change,
99 void *opaque,
100 GMainContext *context,
101 bool set_open,
102 bool sync_state);
103
104/**
105 * qemu_chr_fe_set_handlers:
106 *
107 * Version of qemu_chr_fe_set_handlers_full() with sync_state = true.
108 */
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400109void qemu_chr_fe_set_handlers(CharBackend *b,
110 IOCanReadHandler *fd_can_read,
111 IOReadHandler *fd_read,
112 IOEventHandler *fd_event,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300113 BackendChangeHandler *be_change,
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400114 void *opaque,
115 GMainContext *context,
116 bool set_open);
117
118/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200119 * qemu_chr_fe_take_focus:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400120 *
121 * Take the focus (if the front end is muxed).
122 *
123 * Without associated Chardev, nothing is changed.
124 */
125void qemu_chr_fe_take_focus(CharBackend *b);
126
127/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200128 * qemu_chr_fe_accept_input:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400129 *
130 * Notify that the frontend is ready to receive data
131 */
132void qemu_chr_fe_accept_input(CharBackend *be);
133
134/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200135 * qemu_chr_fe_disconnect:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400136 *
Julia Suvorova73516812018-08-13 12:34:02 +0300137 * Close a fd accepted by character backend.
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400138 * Without associated Chardev, do nothing.
139 */
140void qemu_chr_fe_disconnect(CharBackend *be);
141
142/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200143 * qemu_chr_fe_wait_connected:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400144 *
Michael Tokareva1a62ce2023-07-14 14:33:02 +0300145 * Wait for character backend to be connected, return < 0 on error or
Julia Suvorova73516812018-08-13 12:34:02 +0300146 * if no associated Chardev.
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400147 */
148int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp);
149
150/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200151 * qemu_chr_fe_set_echo:
152 * @echo: true to enable echo, false to disable echo
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400153 *
154 * Ask the backend to override its normal echo setting. This only really
155 * applies to the stdio backend and is used by the QMP server such that you
156 * can see what you type if you try to type QMP commands.
157 * Without associated Chardev, do nothing.
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400158 */
159void qemu_chr_fe_set_echo(CharBackend *be, bool echo);
160
161/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200162 * qemu_chr_fe_set_open:
Alex Bennée67b55952023-12-11 14:59:59 +0000163 * @be: a CharBackend
164 * @is_open: the front end open status
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400165 *
Alex Bennée67b55952023-12-11 14:59:59 +0000166 * This is an indication that the front end is ready (or not) to begin
167 * doing I/O. Without associated Chardev, do nothing.
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400168 */
Alex Bennée67b55952023-12-11 14:59:59 +0000169void qemu_chr_fe_set_open(CharBackend *be, bool is_open);
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400170
171/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200172 * qemu_chr_fe_printf:
173 * @fmt: see #printf
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400174 *
175 * Write to a character backend using a printf style interface. This
176 * function is thread-safe. It does nothing without associated
177 * Chardev.
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400178 */
179void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400180 G_GNUC_PRINTF(2, 3);
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400181
Marc-André Lureaubf7b1ea2021-08-04 17:01:14 +0400182
Philippe Mathieu-Daudéb3a10902023-07-05 13:21:11 +0200183/**
184 * FEWatchFunc: a #GSourceFunc called when any conditions requested by
185 * qemu_chr_fe_add_watch() is satisfied.
186 * @do_not_use: depending on the underlying chardev, a GIOChannel or a
187 * QIOChannel. DO NOT USE!
188 * @cond: bitwise combination of conditions watched and satisfied
189 * before calling this callback.
190 * @data: user data passed at creation to qemu_chr_fe_add_watch(). Can
191 * be NULL.
192 *
193 * Returns: G_SOURCE_REMOVE if the GSource should be removed from the
194 * main loop, or G_SOURCE_CONTINUE to leave the GSource in
195 * the main loop.
196 */
Marc-André Lureaubf7b1ea2021-08-04 17:01:14 +0400197typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition condition, void *data);
198
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400199/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200200 * qemu_chr_fe_add_watch:
201 * @cond: the condition to poll for
202 * @func: the function to call when the condition happens
203 * @user_data: the opaque pointer to pass to @func
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400204 *
205 * If the backend is connected, create and add a #GSource that fires
206 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
207 * is active; return the #GSource's tag. If it is disconnected,
208 * or without associated Chardev, return 0.
209 *
Marc-André Lureau64c3f262019-02-06 18:43:26 +0100210 * Note that you are responsible to update the front-end sources if
211 * you are switching the main context with qemu_chr_fe_set_handlers().
212 *
Marc-André Lureaubf7b1ea2021-08-04 17:01:14 +0400213 * Warning: DO NOT use the first callback argument (it may be either
214 * a GIOChannel or a QIOChannel, depending on the underlying chardev)
215 *
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400216 * Returns: the source tag
217 */
218guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
Marc-André Lureaubf7b1ea2021-08-04 17:01:14 +0400219 FEWatchFunc func, void *user_data);
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400220
221/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200222 * qemu_chr_fe_write:
223 * @buf: the data
224 * @len: the number of bytes to send
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400225 *
226 * Write data to a character backend from the front end. This function
227 * will send data from the front end to the back end. This function
228 * is thread-safe.
229 *
Julia Suvorova73516812018-08-13 12:34:02 +0300230 * Returns: the number of bytes consumed (0 if no associated Chardev)
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400231 */
232int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
233
234/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200235 * qemu_chr_fe_write_all:
236 * @buf: the data
237 * @len: the number of bytes to send
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400238 *
239 * Write data to a character backend from the front end. This function will
240 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
241 * this function will block if the back end cannot consume all of the data
242 * attempted to be written. This function is thread-safe.
243 *
Julia Suvorova73516812018-08-13 12:34:02 +0300244 * Returns: the number of bytes consumed (0 if no associated Chardev)
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400245 */
246int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
247
248/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200249 * qemu_chr_fe_read_all:
250 * @buf: the data buffer
251 * @len: the number of bytes to read
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400252 *
253 * Read data to a buffer from the back end.
254 *
Julia Suvorova73516812018-08-13 12:34:02 +0300255 * Returns: the number of bytes read (0 if no associated Chardev)
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400256 */
257int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
258
259/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200260 * qemu_chr_fe_ioctl:
261 * @cmd: see CHR_IOCTL_*
262 * @arg: the data associated with @cmd
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400263 *
264 * Issue a device specific ioctl to a backend. This function is thread-safe.
265 *
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400266 * Returns: if @cmd is not supported by the backend or there is no
267 * associated Chardev, -ENOTSUP, otherwise the return
268 * value depends on the semantics of @cmd
269 */
270int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg);
271
272/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200273 * qemu_chr_fe_get_msgfd:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400274 *
275 * For backends capable of fd passing, return the latest file descriptor passed
276 * by a client.
277 *
278 * Returns: -1 if fd passing isn't supported or there is no pending file
279 * descriptor. If a file descriptor is returned, subsequent calls to
280 * this function will return -1 until a client sends a new file
281 * descriptor.
282 */
283int qemu_chr_fe_get_msgfd(CharBackend *be);
284
285/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200286 * qemu_chr_fe_get_msgfds:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400287 *
288 * For backends capable of fd passing, return the number of file received
289 * descriptors and fills the fds array up to num elements
290 *
291 * Returns: -1 if fd passing isn't supported or there are no pending file
292 * descriptors. If file descriptors are returned, subsequent calls to
293 * this function will return -1 until a client sends a new set of file
294 * descriptors.
295 */
296int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num);
297
298/**
Marc-André Lureau56625762018-08-24 16:22:47 +0200299 * qemu_chr_fe_set_msgfds:
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400300 *
301 * For backends capable of fd passing, set an array of fds to be passed with
302 * the next send operation.
303 * A subsequent call to this function before calling a write function will
304 * result in overwriting the fd array with the new value without being send.
305 * Upon writing the message the fd array is freed.
306 *
307 * Returns: -1 if fd passing isn't supported or no associated Chardev.
308 */
309int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num);
310
311#endif /* QEMU_CHAR_FE_H */