]> git.proxmox.com Git - mirror_qemu.git/blob - include/sysemu/char.h
Merge remote-tracking branch 'elmarco/tags/chr-tests-pull-request' into staging
[mirror_qemu.git] / include / sysemu / char.h
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
3
4 #include "qemu-common.h"
5 #include "qemu/option.h"
6 #include "qemu/main-loop.h"
7 #include "qemu/bitmap.h"
8 #include "qom/object.h"
9
10 #define IAC_EOR 239
11 #define IAC_SE 240
12 #define IAC_NOP 241
13 #define IAC_BREAK 243
14 #define IAC_IP 244
15 #define IAC_SB 250
16 #define IAC 255
17
18 /* character device */
19
20 typedef enum {
21 CHR_EVENT_BREAK, /* serial break char */
22 CHR_EVENT_OPENED, /* new connection established */
23 CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
24 CHR_EVENT_MUX_OUT, /* mux-focus will move on */
25 CHR_EVENT_CLOSED /* connection closed */
26 } QEMUChrEvent;
27
28 #define CHR_READ_BUF_LEN 4096
29
30 #define CHR_IOCTL_SERIAL_SET_PARAMS 1
31 typedef struct {
32 int speed;
33 int parity;
34 int data_bits;
35 int stop_bits;
36 } QEMUSerialSetParams;
37
38 #define CHR_IOCTL_SERIAL_SET_BREAK 2
39
40 #define CHR_IOCTL_PP_READ_DATA 3
41 #define CHR_IOCTL_PP_WRITE_DATA 4
42 #define CHR_IOCTL_PP_READ_CONTROL 5
43 #define CHR_IOCTL_PP_WRITE_CONTROL 6
44 #define CHR_IOCTL_PP_READ_STATUS 7
45 #define CHR_IOCTL_PP_EPP_READ_ADDR 8
46 #define CHR_IOCTL_PP_EPP_READ 9
47 #define CHR_IOCTL_PP_EPP_WRITE_ADDR 10
48 #define CHR_IOCTL_PP_EPP_WRITE 11
49 #define CHR_IOCTL_PP_DATA_DIR 12
50
51 struct ParallelIOArg {
52 void *buffer;
53 int count;
54 };
55
56 #define CHR_IOCTL_SERIAL_SET_TIOCM 13
57 #define CHR_IOCTL_SERIAL_GET_TIOCM 14
58
59 #define CHR_TIOCM_CTS 0x020
60 #define CHR_TIOCM_CAR 0x040
61 #define CHR_TIOCM_DSR 0x100
62 #define CHR_TIOCM_RI 0x080
63 #define CHR_TIOCM_DTR 0x002
64 #define CHR_TIOCM_RTS 0x004
65
66 typedef void IOEventHandler(void *opaque, int event);
67
68 typedef enum {
69 /* Whether the chardev peer is able to close and
70 * reopen the data channel, thus requiring support
71 * for qemu_chr_wait_connected() to wait for a
72 * valid connection */
73 QEMU_CHAR_FEATURE_RECONNECTABLE,
74 /* Whether it is possible to send/recv file descriptors
75 * over the data channel */
76 QEMU_CHAR_FEATURE_FD_PASS,
77 /* Whether replay or record mode is enabled */
78 QEMU_CHAR_FEATURE_REPLAY,
79
80 QEMU_CHAR_FEATURE_LAST,
81 } ChardevFeature;
82
83 /* This is the backend as seen by frontend, the actual backend is
84 * Chardev */
85 typedef struct CharBackend {
86 Chardev *chr;
87 IOEventHandler *chr_event;
88 IOCanReadHandler *chr_can_read;
89 IOReadHandler *chr_read;
90 void *opaque;
91 int tag;
92 int fe_open;
93 } CharBackend;
94
95 struct Chardev {
96 Object parent_obj;
97
98 QemuMutex chr_write_lock;
99 CharBackend *be;
100 char *label;
101 char *filename;
102 int logfd;
103 int be_open;
104 guint fd_in_tag;
105 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
106 };
107
108 /**
109 * @qemu_chr_new_from_opts:
110 *
111 * Create a new character backend from a QemuOpts list.
112 *
113 * @opts see qemu-config.c for a list of valid options
114 *
115 * Returns: a new character backend
116 */
117 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
118 Error **errp);
119
120 /**
121 * @qemu_chr_parse_common:
122 *
123 * Parse the common options available to all character backends.
124 *
125 * @opts the options that still need parsing
126 * @backend a new backend
127 */
128 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
129
130 /**
131 * @qemu_chr_new:
132 *
133 * Create a new character backend from a URI.
134 *
135 * @label the name of the backend
136 * @filename the URI
137 *
138 * Returns: a new character backend
139 */
140 Chardev *qemu_chr_new(const char *label, const char *filename);
141
142
143 /**
144 * @qemu_chr_fe_disconnect:
145 *
146 * Close a fd accpeted by character backend.
147 * Without associated Chardev, do nothing.
148 */
149 void qemu_chr_fe_disconnect(CharBackend *be);
150
151 /**
152 * @qemu_chr_cleanup:
153 *
154 * Delete all chardevs (when leaving qemu)
155 */
156 void qemu_chr_cleanup(void);
157
158 /**
159 * @qemu_chr_fe_wait_connected:
160 *
161 * Wait for characted backend to be connected, return < 0 on error or
162 * if no assicated Chardev.
163 */
164 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp);
165
166 /**
167 * @qemu_chr_new_noreplay:
168 *
169 * Create a new character backend from a URI.
170 * Character device communications are not written
171 * into the replay log.
172 *
173 * @label the name of the backend
174 * @filename the URI
175 *
176 * Returns: a new character backend
177 */
178 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
179
180 /**
181 * @qemu_chr_fe_set_echo:
182 *
183 * Ask the backend to override its normal echo setting. This only really
184 * applies to the stdio backend and is used by the QMP server such that you
185 * can see what you type if you try to type QMP commands.
186 * Without associated Chardev, do nothing.
187 *
188 * @echo true to enable echo, false to disable echo
189 */
190 void qemu_chr_fe_set_echo(CharBackend *be, bool echo);
191
192 /**
193 * @qemu_chr_fe_set_open:
194 *
195 * Set character frontend open status. This is an indication that the
196 * front end is ready (or not) to begin doing I/O.
197 * Without associated Chardev, do nothing.
198 */
199 void qemu_chr_fe_set_open(CharBackend *be, int fe_open);
200
201 /**
202 * @qemu_chr_fe_printf:
203 *
204 * Write to a character backend using a printf style interface. This
205 * function is thread-safe. It does nothing without associated
206 * Chardev.
207 *
208 * @fmt see #printf
209 */
210 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
211 GCC_FMT_ATTR(2, 3);
212
213 /**
214 * @qemu_chr_fe_add_watch:
215 *
216 * If the backend is connected, create and add a #GSource that fires
217 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
218 * is active; return the #GSource's tag. If it is disconnected,
219 * or without associated Chardev, return 0.
220 *
221 * @cond the condition to poll for
222 * @func the function to call when the condition happens
223 * @user_data the opaque pointer to pass to @func
224 *
225 * Returns: the source tag
226 */
227 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
228 GIOFunc func, void *user_data);
229
230 /**
231 * @qemu_chr_fe_write:
232 *
233 * Write data to a character backend from the front end. This function
234 * will send data from the front end to the back end. This function
235 * is thread-safe.
236 *
237 * @buf the data
238 * @len the number of bytes to send
239 *
240 * Returns: the number of bytes consumed (0 if no assicated Chardev)
241 */
242 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
243
244 /**
245 * @qemu_chr_fe_write_all:
246 *
247 * Write data to a character backend from the front end. This function will
248 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
249 * this function will block if the back end cannot consume all of the data
250 * attempted to be written. This function is thread-safe.
251 *
252 * @buf the data
253 * @len the number of bytes to send
254 *
255 * Returns: the number of bytes consumed (0 if no assicated Chardev)
256 */
257 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
258
259 /**
260 * @qemu_chr_fe_read_all:
261 *
262 * Read data to a buffer from the back end.
263 *
264 * @buf the data buffer
265 * @len the number of bytes to read
266 *
267 * Returns: the number of bytes read (0 if no assicated Chardev)
268 */
269 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
270
271 /**
272 * @qemu_chr_fe_ioctl:
273 *
274 * Issue a device specific ioctl to a backend. This function is thread-safe.
275 *
276 * @cmd see CHR_IOCTL_*
277 * @arg the data associated with @cmd
278 *
279 * Returns: if @cmd is not supported by the backend or there is no
280 * associated Chardev, -ENOTSUP, otherwise the return
281 * value depends on the semantics of @cmd
282 */
283 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg);
284
285 /**
286 * @qemu_chr_fe_get_msgfd:
287 *
288 * For backends capable of fd passing, return the latest file descriptor passed
289 * by a client.
290 *
291 * Returns: -1 if fd passing isn't supported or there is no pending file
292 * descriptor. If a file descriptor is returned, subsequent calls to
293 * this function will return -1 until a client sends a new file
294 * descriptor.
295 */
296 int qemu_chr_fe_get_msgfd(CharBackend *be);
297
298 /**
299 * @qemu_chr_fe_get_msgfds:
300 *
301 * For backends capable of fd passing, return the number of file received
302 * descriptors and fills the fds array up to num elements
303 *
304 * Returns: -1 if fd passing isn't supported or there are no pending file
305 * descriptors. If file descriptors are returned, subsequent calls to
306 * this function will return -1 until a client sends a new set of file
307 * descriptors.
308 */
309 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num);
310
311 /**
312 * @qemu_chr_fe_set_msgfds:
313 *
314 * For backends capable of fd passing, set an array of fds to be passed with
315 * the next send operation.
316 * A subsequent call to this function before calling a write function will
317 * result in overwriting the fd array with the new value without being send.
318 * Upon writing the message the fd array is freed.
319 *
320 * Returns: -1 if fd passing isn't supported or no associated Chardev.
321 */
322 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num);
323
324 /**
325 * @qemu_chr_be_can_write:
326 *
327 * Determine how much data the front end can currently accept. This function
328 * returns the number of bytes the front end can accept. If it returns 0, the
329 * front end cannot receive data at the moment. The function must be polled
330 * to determine when data can be received.
331 *
332 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
333 */
334 int qemu_chr_be_can_write(Chardev *s);
335
336 /**
337 * @qemu_chr_be_write:
338 *
339 * Write data from the back end to the front end. Before issuing this call,
340 * the caller should call @qemu_chr_be_can_write to determine how much data
341 * the front end can currently accept.
342 *
343 * @buf a buffer to receive data from the front end
344 * @len the number of bytes to receive from the front end
345 */
346 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
347
348 /**
349 * @qemu_chr_be_write_impl:
350 *
351 * Implementation of back end writing. Used by replay module.
352 *
353 * @buf a buffer to receive data from the front end
354 * @len the number of bytes to receive from the front end
355 */
356 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
357
358 /**
359 * @qemu_chr_be_event:
360 *
361 * Send an event from the back end to the front end.
362 *
363 * @event the event to send
364 */
365 void qemu_chr_be_event(Chardev *s, int event);
366
367 /**
368 * @qemu_chr_fe_init:
369 *
370 * Initializes a front end for the given CharBackend and
371 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
372 * release the driver.
373 *
374 * Returns: false on error.
375 */
376 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp);
377
378 /**
379 * @qemu_chr_fe_get_driver:
380 *
381 * Returns the driver associated with a CharBackend or NULL if no
382 * associated Chardev.
383 */
384 Chardev *qemu_chr_fe_get_driver(CharBackend *be);
385
386 /**
387 * @qemu_chr_fe_deinit:
388 *
389 * Dissociate the CharBackend from the Chardev.
390 *
391 * Safe to call without associated Chardev.
392 */
393 void qemu_chr_fe_deinit(CharBackend *b);
394
395 /**
396 * @qemu_chr_fe_set_handlers:
397 * @b: a CharBackend
398 * @fd_can_read: callback to get the amount of data the frontend may
399 * receive
400 * @fd_read: callback to receive data from char
401 * @fd_event: event callback
402 * @opaque: an opaque pointer for the callbacks
403 * @context: a main loop context or NULL for the default
404 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
405 * any of the handler is non-NULL
406 *
407 * Set the front end char handlers. The front end takes the focus if
408 * any of the handler is non-NULL.
409 *
410 * Without associated Chardev, nothing is changed.
411 */
412 void qemu_chr_fe_set_handlers(CharBackend *b,
413 IOCanReadHandler *fd_can_read,
414 IOReadHandler *fd_read,
415 IOEventHandler *fd_event,
416 void *opaque,
417 GMainContext *context,
418 bool set_open);
419
420 /**
421 * @qemu_chr_fe_take_focus:
422 *
423 * Take the focus (if the front end is muxed).
424 *
425 * Without associated Chardev, nothing is changed.
426 */
427 void qemu_chr_fe_take_focus(CharBackend *b);
428
429 void qemu_chr_fe_accept_input(CharBackend *be);
430 int qemu_chr_add_client(Chardev *s, int fd);
431 Chardev *qemu_chr_find(const char *name);
432
433 bool qemu_chr_has_feature(Chardev *chr,
434 ChardevFeature feature);
435 void qemu_chr_set_feature(Chardev *chr,
436 ChardevFeature feature);
437 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
438 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len);
439 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
440
441 #define TYPE_CHARDEV "chardev"
442 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
443 #define CHARDEV_CLASS(klass) \
444 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
445 #define CHARDEV_GET_CLASS(obj) \
446 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
447
448 #define TYPE_CHARDEV_NULL "chardev-null"
449 #define TYPE_CHARDEV_MUX "chardev-mux"
450 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
451 #define TYPE_CHARDEV_PTY "chardev-pty"
452 #define TYPE_CHARDEV_CONSOLE "chardev-console"
453 #define TYPE_CHARDEV_STDIO "chardev-stdio"
454 #define TYPE_CHARDEV_PIPE "chardev-pipe"
455 #define TYPE_CHARDEV_MEMORY "chardev-memory"
456 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
457 #define TYPE_CHARDEV_FILE "chardev-file"
458 #define TYPE_CHARDEV_SERIAL "chardev-serial"
459 #define TYPE_CHARDEV_SOCKET "chardev-socket"
460 #define TYPE_CHARDEV_UDP "chardev-udp"
461
462 #define CHARDEV_IS_RINGBUF(chr) \
463 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
464 #define CHARDEV_IS_PTY(chr) \
465 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
466
467 typedef struct ChardevClass {
468 ObjectClass parent_class;
469
470 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
471 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
472
473 void (*open)(Chardev *chr, ChardevBackend *backend,
474 bool *be_opened, Error **errp);
475
476 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
477 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
478 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
479 void (*chr_update_read_handler)(Chardev *s, GMainContext *context);
480 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
481 int (*get_msgfds)(Chardev *s, int* fds, int num);
482 int (*set_msgfds)(Chardev *s, int *fds, int num);
483 int (*chr_add_client)(Chardev *chr, int fd);
484 int (*chr_wait_connected)(Chardev *chr, Error **errp);
485 void (*chr_disconnect)(Chardev *chr);
486 void (*chr_accept_input)(Chardev *chr);
487 void (*chr_set_echo)(Chardev *chr, bool echo);
488 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
489 } ChardevClass;
490
491 Chardev *qemu_chardev_new(const char *id, const char *typename,
492 ChardevBackend *backend, Error **errp);
493
494 extern int term_escape_char;
495
496 /* console.c */
497 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
498
499 #endif