net: hub: suppress warnings of no host network for qtest
[qemu/kevin.git] / include / chardev / char.h
blob7becd8c80c820f7f8ff9d03c2f02c882daa8361b
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
4 #include "qapi/qapi-types-char.h"
5 #include "qemu/main-loop.h"
6 #include "qemu/bitmap.h"
7 #include "qom/object.h"
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
17 /* character device */
18 typedef struct CharBackend CharBackend;
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. 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.
35 } QEMUChrEvent;
37 #define CHR_READ_BUF_LEN 4096
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,
48 /* Whether replay or record mode is enabled */
49 QEMU_CHAR_FEATURE_REPLAY,
51 QEMU_CHAR_FEATURE_LAST,
52 } ChardevFeature;
54 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
56 struct Chardev {
57 Object parent_obj;
59 QemuMutex chr_write_lock;
60 CharBackend *be;
61 char *label;
62 char *filename;
63 int logfd;
64 int be_open;
65 GSource *gsource;
66 GMainContext *gcontext;
67 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
70 /**
71 * qemu_chr_new_from_opts:
72 * @opts: see qemu-config.c for a list of valid options
74 * Create a new character backend from a QemuOpts list.
76 * Returns: on success: a new character backend
77 * otherwise: NULL; @errp specifies the error
78 * or left untouched in case of help option
80 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
81 Error **errp);
83 /**
84 * qemu_chr_parse_common:
85 * @opts: the options that still need parsing
86 * @backend: a new backend
88 * Parse the common options available to all character backends.
90 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
92 /**
93 * qemu_chr_parse_opts:
95 * Parse the options to the ChardevBackend struct.
97 * Returns: a new backend or NULL on error
99 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
100 Error **errp);
103 * qemu_chr_new:
104 * @label: the name of the backend
105 * @filename: the URI
107 * Create a new character backend from a URI.
108 * Do not implicitly initialize a monitor if the chardev is muxed.
110 * Returns: a new character backend
112 Chardev *qemu_chr_new(const char *label, const char *filename);
115 * qemu_chr_new_mux_mon:
116 * @label: the name of the backend
117 * @filename: the URI
119 * Create a new character backend from a URI.
120 * Implicitly initialize a monitor if the chardev is muxed.
122 * Returns: a new character backend
124 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename);
127 * qemu_chr_change:
128 * @opts: the new backend options
130 * Change an existing character backend
132 void qemu_chr_change(QemuOpts *opts, Error **errp);
135 * qemu_chr_cleanup:
137 * Delete all chardevs (when leaving qemu)
139 void qemu_chr_cleanup(void);
142 * qemu_chr_new_noreplay:
143 * @label: the name of the backend
144 * @filename: the URI
145 * @permit_mux_mon: if chardev is muxed, initialize a monitor
147 * Create a new character backend from a URI.
148 * Character device communications are not written
149 * into the replay log.
151 * Returns: a new character backend
153 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
154 bool permit_mux_mon);
157 * qemu_chr_be_can_write:
159 * Determine how much data the front end can currently accept. This function
160 * returns the number of bytes the front end can accept. If it returns 0, the
161 * front end cannot receive data at the moment. The function must be polled
162 * to determine when data can be received.
164 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
166 int qemu_chr_be_can_write(Chardev *s);
169 * qemu_chr_be_write:
170 * @buf: a buffer to receive data from the front end
171 * @len: the number of bytes to receive from the front end
173 * Write data from the back end to the front end. Before issuing this call,
174 * the caller should call @qemu_chr_be_can_write to determine how much data
175 * the front end can currently accept.
177 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
180 * qemu_chr_be_write_impl:
181 * @buf: a buffer to receive data from the front end
182 * @len: the number of bytes to receive from the front end
184 * Implementation of back end writing. Used by replay module.
186 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
189 * qemu_chr_be_update_read_handlers:
190 * @context: the gcontext that will be used to attach the watch sources
192 * Invoked when frontend read handlers are setup
194 void qemu_chr_be_update_read_handlers(Chardev *s,
195 GMainContext *context);
198 * qemu_chr_be_event:
199 * @event: the event to send
201 * Send an event from the back end to the front end.
203 void qemu_chr_be_event(Chardev *s, int event);
205 int qemu_chr_add_client(Chardev *s, int fd);
206 Chardev *qemu_chr_find(const char *name);
208 bool qemu_chr_has_feature(Chardev *chr,
209 ChardevFeature feature);
210 void qemu_chr_set_feature(Chardev *chr,
211 ChardevFeature feature);
212 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
213 bool permit_mux_mon);
214 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
215 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
216 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
218 #define TYPE_CHARDEV "chardev"
219 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
220 #define CHARDEV_CLASS(klass) \
221 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
222 #define CHARDEV_GET_CLASS(obj) \
223 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
225 #define TYPE_CHARDEV_NULL "chardev-null"
226 #define TYPE_CHARDEV_MUX "chardev-mux"
227 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
228 #define TYPE_CHARDEV_PTY "chardev-pty"
229 #define TYPE_CHARDEV_CONSOLE "chardev-console"
230 #define TYPE_CHARDEV_STDIO "chardev-stdio"
231 #define TYPE_CHARDEV_PIPE "chardev-pipe"
232 #define TYPE_CHARDEV_MEMORY "chardev-memory"
233 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
234 #define TYPE_CHARDEV_FILE "chardev-file"
235 #define TYPE_CHARDEV_SERIAL "chardev-serial"
236 #define TYPE_CHARDEV_SOCKET "chardev-socket"
237 #define TYPE_CHARDEV_UDP "chardev-udp"
239 #define CHARDEV_IS_RINGBUF(chr) \
240 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
241 #define CHARDEV_IS_PTY(chr) \
242 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
244 typedef struct ChardevClass {
245 ObjectClass parent_class;
247 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
248 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
250 void (*open)(Chardev *chr, ChardevBackend *backend,
251 bool *be_opened, Error **errp);
253 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
254 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
255 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
256 void (*chr_update_read_handler)(Chardev *s);
257 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
258 int (*get_msgfds)(Chardev *s, int* fds, int num);
259 int (*set_msgfds)(Chardev *s, int *fds, int num);
260 int (*chr_add_client)(Chardev *chr, int fd);
261 int (*chr_wait_connected)(Chardev *chr, Error **errp);
262 void (*chr_disconnect)(Chardev *chr);
263 void (*chr_accept_input)(Chardev *chr);
264 void (*chr_set_echo)(Chardev *chr, bool echo);
265 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
266 void (*chr_be_event)(Chardev *s, int event);
267 /* Return 0 if succeeded, 1 if failed */
268 int (*chr_machine_done)(Chardev *chr);
269 } ChardevClass;
271 Chardev *qemu_chardev_new(const char *id, const char *typename,
272 ChardevBackend *backend, Error **errp);
274 extern int term_escape_char;
276 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
277 GSourceFunc func, void *private);
279 /* console.c */
280 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
282 #endif