pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 1 | #ifndef QEMU_CHAR_H |
| 2 | #define QEMU_CHAR_H |
| 3 | |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 4 | #include "qapi/qapi-types-char.h" |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 5 | #include "qemu/bitmap.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 6 | #include "qemu/thread.h" |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 7 | #include "qom/object.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 8 | |
Jing Liu | ae92cbd | 2016-09-23 08:06:11 +0200 | [diff] [blame] | 9 | #define IAC_EOR 239 |
| 10 | #define IAC_SE 240 |
| 11 | #define IAC_NOP 241 |
| 12 | #define IAC_BREAK 243 |
| 13 | #define IAC_IP 244 |
| 14 | #define IAC_SB 250 |
| 15 | #define IAC 255 |
| 16 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 17 | /* character device */ |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 18 | typedef struct CharBackend CharBackend; |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 19 | |
Marc-André Lureau | 8c260cb | 2016-10-22 13:09:39 +0300 | [diff] [blame] | 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 */ |
Peter Xu | d886101 | 2018-06-20 15:32:17 +0800 | [diff] [blame] | 25 | CHR_EVENT_CLOSED /* connection closed. NOTE: currently this event |
| 26 | * is only bound to the read port of the chardev. |
| 27 | * Normally the read port and write port of a |
| 28 | * chardev should be the same, but it can be |
| 29 | * different, e.g., for fd chardevs, when the two |
| 30 | * fds are different. So when we received the |
| 31 | * CLOSED event it's still possible that the out |
| 32 | * port is still open. TODO: we should only send |
| 33 | * the CLOSED event when both ports are closed. |
| 34 | */ |
Marc-André Lureau | 8c260cb | 2016-10-22 13:09:39 +0300 | [diff] [blame] | 35 | } QEMUChrEvent; |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 36 | |
Marc-André Lureau | f612143 | 2016-12-12 16:39:35 +0300 | [diff] [blame] | 37 | #define CHR_READ_BUF_LEN 4096 |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 38 | |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 39 | typedef enum { |
| 40 | /* Whether the chardev peer is able to close and |
| 41 | * reopen the data channel, thus requiring support |
| 42 | * for qemu_chr_wait_connected() to wait for a |
| 43 | * valid connection */ |
| 44 | QEMU_CHAR_FEATURE_RECONNECTABLE, |
| 45 | /* Whether it is possible to send/recv file descriptors |
| 46 | * over the data channel */ |
| 47 | QEMU_CHAR_FEATURE_FD_PASS, |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 48 | /* Whether replay or record mode is enabled */ |
| 49 | QEMU_CHAR_FEATURE_REPLAY, |
Marc-André Lureau | 9ac3788 | 2018-12-06 00:37:33 +0400 | [diff] [blame] | 50 | /* Whether the gcontext can be changed after calling |
| 51 | * qemu_chr_be_update_read_handlers() */ |
| 52 | QEMU_CHAR_FEATURE_GCONTEXT, |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 53 | |
| 54 | QEMU_CHAR_FEATURE_LAST, |
Marc-André Lureau | 279b066 | 2016-12-14 13:27:58 +0300 | [diff] [blame] | 55 | } ChardevFeature; |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 56 | |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 57 | #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY) |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 58 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 59 | struct Chardev { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 60 | Object parent_obj; |
| 61 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 62 | QemuMutex chr_write_lock; |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 63 | CharBackend *be; |
aliguori | 5ccfae1 | 2008-10-31 17:31:29 +0000 | [diff] [blame] | 64 | char *label; |
| 65 | char *filename; |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 66 | int logfd; |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 67 | int be_open; |
Lukas Straub | feb774c | 2021-03-30 20:13:31 +0200 | [diff] [blame] | 68 | /* used to coordinate the chardev-change special-case: */ |
| 69 | bool handover_yank_instance; |
zhanghailiang | b19456d | 2017-04-19 09:15:32 +0800 | [diff] [blame] | 70 | GSource *gsource; |
Peter Xu | 95eeeba | 2017-09-21 14:35:52 +0800 | [diff] [blame] | 71 | GMainContext *gcontext; |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 72 | DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST); |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 75 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 76 | * qemu_chr_new_from_opts: |
| 77 | * @opts: see qemu-config.c for a list of valid options |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 78 | * @context: the #GMainContext to be used at initialization time |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 79 | * |
| 80 | * Create a new character backend from a QemuOpts list. |
| 81 | * |
Anton Nefedov | 0ec846b | 2017-07-25 13:04:41 +0300 | [diff] [blame] | 82 | * Returns: on success: a new character backend |
| 83 | * otherwise: NULL; @errp specifies the error |
| 84 | * or left untouched in case of help option |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 85 | */ |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 86 | Chardev *qemu_chr_new_from_opts(QemuOpts *opts, |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 87 | GMainContext *context, |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 88 | Error **errp); |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 89 | |
| 90 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 91 | * qemu_chr_parse_common: |
| 92 | * @opts: the options that still need parsing |
| 93 | * @backend: a new backend |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 94 | * |
| 95 | * Parse the common options available to all character backends. |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 96 | */ |
| 97 | void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend); |
| 98 | |
| 99 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 100 | * qemu_chr_parse_opts: |
Anton Nefedov | 75b6016 | 2017-07-06 15:08:57 +0300 | [diff] [blame] | 101 | * |
| 102 | * Parse the options to the ChardevBackend struct. |
| 103 | * |
| 104 | * Returns: a new backend or NULL on error |
| 105 | */ |
| 106 | ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, |
| 107 | Error **errp); |
| 108 | |
| 109 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 110 | * qemu_chr_new: |
| 111 | * @label: the name of the backend |
| 112 | * @filename: the URI |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 113 | * @context: the #GMainContext to be used at initialization time |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 114 | * |
| 115 | * Create a new character backend from a URI. |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 116 | * Do not implicitly initialize a monitor if the chardev is muxed. |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 117 | * |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 118 | * Returns: a new character backend |
| 119 | */ |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 120 | Chardev *qemu_chr_new(const char *label, const char *filename, |
| 121 | GMainContext *context); |
Marc-André Lureau | 94a40fc | 2016-10-22 12:52:49 +0300 | [diff] [blame] | 122 | |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 123 | /** |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 124 | * qemu_chr_new_mux_mon: |
| 125 | * @label: the name of the backend |
| 126 | * @filename: the URI |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 127 | * @context: the #GMainContext to be used at initialization time |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 128 | * |
| 129 | * Create a new character backend from a URI. |
| 130 | * Implicitly initialize a monitor if the chardev is muxed. |
| 131 | * |
| 132 | * Returns: a new character backend |
| 133 | */ |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 134 | Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename, |
| 135 | GMainContext *context); |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * qemu_chr_change: |
| 139 | * @opts: the new backend options |
Anton Nefedov | 7bb8608 | 2017-07-06 15:08:50 +0300 | [diff] [blame] | 140 | * |
| 141 | * Change an existing character backend |
Anton Nefedov | 7bb8608 | 2017-07-06 15:08:50 +0300 | [diff] [blame] | 142 | */ |
| 143 | void qemu_chr_change(QemuOpts *opts, Error **errp); |
| 144 | |
| 145 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 146 | * qemu_chr_cleanup: |
Marc-André Lureau | aa5cb7f | 2016-07-04 17:38:23 +0200 | [diff] [blame] | 147 | * |
| 148 | * Delete all chardevs (when leaving qemu) |
| 149 | */ |
| 150 | void qemu_chr_cleanup(void); |
| 151 | |
| 152 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 153 | * qemu_chr_new_noreplay: |
| 154 | * @label: the name of the backend |
| 155 | * @filename: the URI |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 156 | * @permit_mux_mon: if chardev is muxed, initialize a monitor |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 157 | * @context: the #GMainContext to be used at initialization time |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 158 | * |
| 159 | * Create a new character backend from a URI. |
| 160 | * Character device communications are not written |
| 161 | * into the replay log. |
| 162 | * |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 163 | * Returns: a new character backend |
| 164 | */ |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 165 | Chardev *qemu_chr_new_noreplay(const char *label, const char *filename, |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 166 | bool permit_mux_mon, GMainContext *context); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 167 | |
| 168 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 169 | * qemu_chr_be_can_write: |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 170 | * |
| 171 | * Determine how much data the front end can currently accept. This function |
| 172 | * returns the number of bytes the front end can accept. If it returns 0, the |
| 173 | * front end cannot receive data at the moment. The function must be polled |
| 174 | * to determine when data can be received. |
| 175 | * |
| 176 | * Returns: the number of bytes the front end can receive via @qemu_chr_be_write |
| 177 | */ |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 178 | int qemu_chr_be_can_write(Chardev *s); |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 179 | |
| 180 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 181 | * qemu_chr_be_write: |
| 182 | * @buf: a buffer to receive data from the front end |
| 183 | * @len: the number of bytes to receive from the front end |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 184 | * |
| 185 | * Write data from the back end to the front end. Before issuing this call, |
| 186 | * the caller should call @qemu_chr_be_can_write to determine how much data |
| 187 | * the front end can currently accept. |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 188 | */ |
Arwed Meyer | 8f9abdf | 2022-09-11 20:18:37 +0200 | [diff] [blame] | 189 | void qemu_chr_be_write(Chardev *s, const uint8_t *buf, int len); |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 190 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 191 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 192 | * qemu_chr_be_write_impl: |
| 193 | * @buf: a buffer to receive data from the front end |
| 194 | * @len: the number of bytes to receive from the front end |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 195 | * |
| 196 | * Implementation of back end writing. Used by replay module. |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 197 | */ |
Arwed Meyer | 8f9abdf | 2022-09-11 20:18:37 +0200 | [diff] [blame] | 198 | void qemu_chr_be_write_impl(Chardev *s, const uint8_t *buf, int len); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 199 | |
| 200 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 201 | * qemu_chr_be_update_read_handlers: |
| 202 | * @context: the gcontext that will be used to attach the watch sources |
Peter Xu | 07241c2 | 2017-09-21 14:35:51 +0800 | [diff] [blame] | 203 | * |
| 204 | * Invoked when frontend read handlers are setup |
Peter Xu | 07241c2 | 2017-09-21 14:35:51 +0800 | [diff] [blame] | 205 | */ |
| 206 | void qemu_chr_be_update_read_handlers(Chardev *s, |
| 207 | GMainContext *context); |
| 208 | |
| 209 | /** |
Marc-André Lureau | 5662576 | 2018-08-24 16:22:47 +0200 | [diff] [blame] | 210 | * qemu_chr_be_event: |
| 211 | * @event: the event to send |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 212 | * |
| 213 | * Send an event from the back end to the front end. |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 214 | */ |
Paolo Bonzini | 2fa9044 | 2020-01-08 11:14:29 +0100 | [diff] [blame] | 215 | void qemu_chr_be_event(Chardev *s, QEMUChrEvent event); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 216 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 217 | int qemu_chr_add_client(Chardev *s, int fd); |
| 218 | Chardev *qemu_chr_find(const char *name); |
Marc-André Lureau | ad5c679 | 2016-10-21 22:38:41 +0300 | [diff] [blame] | 219 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 220 | bool qemu_chr_has_feature(Chardev *chr, |
Marc-André Lureau | 279b066 | 2016-12-14 13:27:58 +0300 | [diff] [blame] | 221 | ChardevFeature feature); |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 222 | void qemu_chr_set_feature(Chardev *chr, |
Marc-André Lureau | 279b066 | 2016-12-14 13:27:58 +0300 | [diff] [blame] | 223 | ChardevFeature feature); |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 224 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename, |
| 225 | bool permit_mux_mon); |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 226 | int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all); |
| 227 | #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true) |
Marc-André Lureau | d24ca4b | 2016-12-12 18:41:00 +0300 | [diff] [blame] | 228 | int qemu_chr_wait_connected(Chardev *chr, Error **errp); |
Anthony Liguori | 2011fe5 | 2011-08-15 11:17:41 -0500 | [diff] [blame] | 229 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 230 | #define TYPE_CHARDEV "chardev" |
Eduardo Habkost | a489d19 | 2020-09-16 14:25:18 -0400 | [diff] [blame] | 231 | OBJECT_DECLARE_TYPE(Chardev, ChardevClass, CHARDEV) |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 232 | |
| 233 | #define TYPE_CHARDEV_NULL "chardev-null" |
| 234 | #define TYPE_CHARDEV_MUX "chardev-mux" |
| 235 | #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf" |
| 236 | #define TYPE_CHARDEV_PTY "chardev-pty" |
| 237 | #define TYPE_CHARDEV_CONSOLE "chardev-console" |
| 238 | #define TYPE_CHARDEV_STDIO "chardev-stdio" |
| 239 | #define TYPE_CHARDEV_PIPE "chardev-pipe" |
| 240 | #define TYPE_CHARDEV_MEMORY "chardev-memory" |
| 241 | #define TYPE_CHARDEV_PARALLEL "chardev-parallel" |
| 242 | #define TYPE_CHARDEV_FILE "chardev-file" |
| 243 | #define TYPE_CHARDEV_SERIAL "chardev-serial" |
| 244 | #define TYPE_CHARDEV_SOCKET "chardev-socket" |
| 245 | #define TYPE_CHARDEV_UDP "chardev-udp" |
| 246 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 247 | #define CHARDEV_IS_RINGBUF(chr) \ |
| 248 | object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF) |
| 249 | #define CHARDEV_IS_PTY(chr) \ |
| 250 | object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY) |
| 251 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 252 | struct ChardevClass { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 253 | ObjectClass parent_class; |
| 254 | |
| 255 | bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */ |
Lukas Straub | feb774c | 2021-03-30 20:13:31 +0200 | [diff] [blame] | 256 | bool supports_yank; |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 257 | |
| 258 | /* parse command line options and populate QAPI @backend */ |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 259 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 260 | |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 261 | /* called after construction, open/starts the backend */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 262 | void (*open)(Chardev *chr, ChardevBackend *backend, |
| 263 | bool *be_opened, Error **errp); |
| 264 | |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 265 | /* write buf to the backend */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 266 | int (*chr_write)(Chardev *s, const uint8_t *buf, int len); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Read from the backend (blocking). A typical front-end will instead rely |
| 270 | * on chr_can_read/chr_read being called when polling/looping. |
| 271 | */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 272 | int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 273 | |
| 274 | /* create a watch on the backend */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 275 | GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 276 | |
| 277 | /* update the backend internal sources */ |
Peter Xu | bb86d05 | 2017-09-21 14:35:54 +0800 | [diff] [blame] | 278 | void (*chr_update_read_handler)(Chardev *s); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 279 | |
| 280 | /* send an ioctl to the backend */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 281 | int (*chr_ioctl)(Chardev *s, int cmd, void *arg); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 282 | |
| 283 | /* get ancillary-received fds during last read */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 284 | int (*get_msgfds)(Chardev *s, int* fds, int num); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 285 | |
| 286 | /* set ancillary fds to be sent with next write */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 287 | int (*set_msgfds)(Chardev *s, int *fds, int num); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 288 | |
| 289 | /* accept the given fd */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 290 | int (*chr_add_client)(Chardev *chr, int fd); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 291 | |
| 292 | /* wait for a connection */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 293 | int (*chr_wait_connected)(Chardev *chr, Error **errp); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 294 | |
| 295 | /* disconnect a connection */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 296 | void (*chr_disconnect)(Chardev *chr); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 297 | |
| 298 | /* called by frontend when it can read */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 299 | void (*chr_accept_input)(Chardev *chr); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 300 | |
| 301 | /* set terminal echo */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 302 | void (*chr_set_echo)(Chardev *chr, bool echo); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 303 | |
| 304 | /* notify the backend of frontend open state */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 305 | void (*chr_set_fe_open)(Chardev *chr, int fe_open); |
Marc-André Lureau | 78e3e1d | 2021-08-04 19:48:41 +0400 | [diff] [blame] | 306 | |
| 307 | /* handle various events */ |
Paolo Bonzini | 2fa9044 | 2020-01-08 11:14:29 +0100 | [diff] [blame] | 308 | void (*chr_be_event)(Chardev *s, QEMUChrEvent event); |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 309 | }; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 310 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 311 | Chardev *qemu_chardev_new(const char *id, const char *typename, |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 312 | ChardevBackend *backend, GMainContext *context, |
| 313 | Error **errp); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 314 | |
aliguori | 0e82f34 | 2008-10-31 18:44:40 +0000 | [diff] [blame] | 315 | extern int term_escape_char; |
| 316 | |
Peter Xu | 2c716ba | 2018-01-04 22:18:35 +0800 | [diff] [blame] | 317 | GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms, |
| 318 | GSourceFunc func, void *private); |
| 319 | |
Paolo Bonzini | 5a1ee60 | 2020-11-30 13:44:49 -0500 | [diff] [blame] | 320 | void suspend_mux_open(void); |
| 321 | void resume_mux_open(void); |
| 322 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 323 | #endif |