4 #include "chardev/char.h"
5 #include "qemu/main-loop.h"
7 typedef void IOEventHandler(void *opaque
, QEMUChrEvent event
);
8 typedef int BackendChangeHandler(void *opaque
);
10 /* This is the backend as seen by frontend, the actual backend is
14 IOEventHandler
*chr_event
;
15 IOCanReadHandler
*chr_can_read
;
16 IOReadHandler
*chr_read
;
17 BackendChangeHandler
*chr_be_change
;
26 * Initializes a front end for the given CharBackend and
27 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
30 * Returns: false on error.
32 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
);
37 * @del: if true, delete the chardev backend
39 * Dissociate the CharBackend from the Chardev.
41 * Safe to call without associated Chardev.
43 void qemu_chr_fe_deinit(CharBackend
*b
, bool del
);
46 * qemu_chr_fe_get_driver:
48 * Returns: the driver associated with a CharBackend or NULL if no
50 * Note: avoid this function as the driver should never be accessed directly,
51 * especially by the frontends that support chardevice hotswap.
52 * Consider qemu_chr_fe_backend_connected() to check for driver existence
54 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
);
57 * qemu_chr_fe_backend_connected:
59 * Returns: true if there is a chardevice associated with @be.
61 bool qemu_chr_fe_backend_connected(CharBackend
*be
);
64 * qemu_chr_fe_backend_open:
66 * Returns: true if chardevice associated with @be is open.
68 bool qemu_chr_fe_backend_open(CharBackend
*be
);
71 * qemu_chr_fe_set_handlers_full:
73 * @fd_can_read: callback to get the amount of data the frontend may
75 * @fd_read: callback to receive data from char
76 * @fd_event: event callback
77 * @be_change: backend change callback; passing NULL means hot backend change
78 * is not supported and will not be attempted
79 * @opaque: an opaque pointer for the callbacks
80 * @context: a main loop context or NULL for the default
81 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
82 * any of the handler is non-NULL
83 * @sync_state: whether to issue event callback with updated state
85 * Set the front end char handlers. The front end takes the focus if
86 * any of the handler is non-NULL.
88 * Without associated Chardev, nothing is changed.
90 void qemu_chr_fe_set_handlers_full(CharBackend
*b
,
91 IOCanReadHandler
*fd_can_read
,
92 IOReadHandler
*fd_read
,
93 IOEventHandler
*fd_event
,
94 BackendChangeHandler
*be_change
,
96 GMainContext
*context
,
101 * qemu_chr_fe_set_handlers:
103 * Version of qemu_chr_fe_set_handlers_full() with sync_state = true.
105 void qemu_chr_fe_set_handlers(CharBackend
*b
,
106 IOCanReadHandler
*fd_can_read
,
107 IOReadHandler
*fd_read
,
108 IOEventHandler
*fd_event
,
109 BackendChangeHandler
*be_change
,
111 GMainContext
*context
,
115 * qemu_chr_fe_take_focus:
117 * Take the focus (if the front end is muxed).
119 * Without associated Chardev, nothing is changed.
121 void qemu_chr_fe_take_focus(CharBackend
*b
);
124 * qemu_chr_fe_accept_input:
126 * Notify that the frontend is ready to receive data
128 void qemu_chr_fe_accept_input(CharBackend
*be
);
131 * qemu_chr_fe_disconnect:
133 * Close a fd accepted by character backend.
134 * Without associated Chardev, do nothing.
136 void qemu_chr_fe_disconnect(CharBackend
*be
);
139 * qemu_chr_fe_wait_connected:
141 * Wait for characted backend to be connected, return < 0 on error or
142 * if no associated Chardev.
144 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
);
147 * qemu_chr_fe_set_echo:
148 * @echo: true to enable echo, false to disable echo
150 * Ask the backend to override its normal echo setting. This only really
151 * applies to the stdio backend and is used by the QMP server such that you
152 * can see what you type if you try to type QMP commands.
153 * Without associated Chardev, do nothing.
155 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
);
158 * qemu_chr_fe_set_open:
160 * Set character frontend open status. This is an indication that the
161 * front end is ready (or not) to begin doing I/O.
162 * Without associated Chardev, do nothing.
164 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
);
167 * qemu_chr_fe_printf:
170 * Write to a character backend using a printf style interface. This
171 * function is thread-safe. It does nothing without associated
174 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
178 typedef gboolean (*FEWatchFunc
)(void *do_not_use
, GIOCondition condition
, void *data
);
181 * qemu_chr_fe_add_watch:
182 * @cond: the condition to poll for
183 * @func: the function to call when the condition happens
184 * @user_data: the opaque pointer to pass to @func
186 * If the backend is connected, create and add a #GSource that fires
187 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
188 * is active; return the #GSource's tag. If it is disconnected,
189 * or without associated Chardev, return 0.
191 * Note that you are responsible to update the front-end sources if
192 * you are switching the main context with qemu_chr_fe_set_handlers().
194 * Warning: DO NOT use the first callback argument (it may be either
195 * a GIOChannel or a QIOChannel, depending on the underlying chardev)
197 * Returns: the source tag
199 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
200 FEWatchFunc func
, void *user_data
);
205 * @len: the number of bytes to send
207 * Write data to a character backend from the front end. This function
208 * will send data from the front end to the back end. This function
211 * Returns: the number of bytes consumed (0 if no associated Chardev)
213 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
);
216 * qemu_chr_fe_write_all:
218 * @len: the number of bytes to send
220 * Write data to a character backend from the front end. This function will
221 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
222 * this function will block if the back end cannot consume all of the data
223 * attempted to be written. This function is thread-safe.
225 * Returns: the number of bytes consumed (0 if no associated Chardev)
227 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
);
230 * qemu_chr_fe_read_all:
231 * @buf: the data buffer
232 * @len: the number of bytes to read
234 * Read data to a buffer from the back end.
236 * Returns: the number of bytes read (0 if no associated Chardev)
238 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
);
242 * @cmd: see CHR_IOCTL_*
243 * @arg: the data associated with @cmd
245 * Issue a device specific ioctl to a backend. This function is thread-safe.
247 * Returns: if @cmd is not supported by the backend or there is no
248 * associated Chardev, -ENOTSUP, otherwise the return
249 * value depends on the semantics of @cmd
251 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
);
254 * qemu_chr_fe_get_msgfd:
256 * For backends capable of fd passing, return the latest file descriptor passed
259 * Returns: -1 if fd passing isn't supported or there is no pending file
260 * descriptor. If a file descriptor is returned, subsequent calls to
261 * this function will return -1 until a client sends a new file
264 int qemu_chr_fe_get_msgfd(CharBackend
*be
);
267 * qemu_chr_fe_get_msgfds:
269 * For backends capable of fd passing, return the number of file received
270 * descriptors and fills the fds array up to num elements
272 * Returns: -1 if fd passing isn't supported or there are no pending file
273 * descriptors. If file descriptors are returned, subsequent calls to
274 * this function will return -1 until a client sends a new set of file
277 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int num
);
280 * qemu_chr_fe_set_msgfds:
282 * For backends capable of fd passing, set an array of fds to be passed with
283 * the next send operation.
284 * A subsequent call to this function before calling a write function will
285 * result in overwriting the fd array with the new value without being send.
286 * Upon writing the message the fd array is freed.
288 * Returns: -1 if fd passing isn't supported or no associated Chardev.
290 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
);
292 #endif /* QEMU_CHAR_FE_H */