ui: mix misleading comments & return types of VNC I/O helper methods
[qemu/ar7.git] / include / chardev / char.h
blob778d61029587758c7282c9ab3a6f5cef53540838
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
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"
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
18 /* character device */
19 typedef struct CharBackend CharBackend;
21 typedef enum {
22 CHR_EVENT_BREAK, /* serial break char */
23 CHR_EVENT_OPENED, /* new connection established */
24 CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
25 CHR_EVENT_MUX_OUT, /* mux-focus will move on */
26 CHR_EVENT_CLOSED /* connection closed */
27 } QEMUChrEvent;
29 #define CHR_READ_BUF_LEN 4096
31 typedef enum {
32 /* Whether the chardev peer is able to close and
33 * reopen the data channel, thus requiring support
34 * for qemu_chr_wait_connected() to wait for a
35 * valid connection */
36 QEMU_CHAR_FEATURE_RECONNECTABLE,
37 /* Whether it is possible to send/recv file descriptors
38 * over the data channel */
39 QEMU_CHAR_FEATURE_FD_PASS,
40 /* Whether replay or record mode is enabled */
41 QEMU_CHAR_FEATURE_REPLAY,
43 QEMU_CHAR_FEATURE_LAST,
44 } ChardevFeature;
46 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
48 struct Chardev {
49 Object parent_obj;
51 QemuMutex chr_write_lock;
52 CharBackend *be;
53 char *label;
54 char *filename;
55 int logfd;
56 int be_open;
57 GSource *gsource;
58 GMainContext *gcontext;
59 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
62 /**
63 * @qemu_chr_new_from_opts:
65 * Create a new character backend from a QemuOpts list.
67 * @opts see qemu-config.c for a list of valid options
69 * Returns: on success: a new character backend
70 * otherwise: NULL; @errp specifies the error
71 * or left untouched in case of help option
73 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
74 Error **errp);
76 /**
77 * @qemu_chr_parse_common:
79 * Parse the common options available to all character backends.
81 * @opts the options that still need parsing
82 * @backend a new backend
84 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
86 /**
87 * @qemu_chr_parse_opts:
89 * Parse the options to the ChardevBackend struct.
91 * Returns: a new backend or NULL on error
93 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
94 Error **errp);
96 /**
97 * @qemu_chr_new:
99 * Create a new character backend from a URI.
101 * @label the name of the backend
102 * @filename the URI
104 * Returns: a new character backend
106 Chardev *qemu_chr_new(const char *label, const char *filename);
109 * @qemu_chr_change:
111 * Change an existing character backend
113 * @opts the new backend options
115 void qemu_chr_change(QemuOpts *opts, Error **errp);
118 * @qemu_chr_cleanup:
120 * Delete all chardevs (when leaving qemu)
122 void qemu_chr_cleanup(void);
125 * @qemu_chr_new_noreplay:
127 * Create a new character backend from a URI.
128 * Character device communications are not written
129 * into the replay log.
131 * @label the name of the backend
132 * @filename the URI
134 * Returns: a new character backend
136 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
139 * @qemu_chr_be_can_write:
141 * Determine how much data the front end can currently accept. This function
142 * returns the number of bytes the front end can accept. If it returns 0, the
143 * front end cannot receive data at the moment. The function must be polled
144 * to determine when data can be received.
146 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
148 int qemu_chr_be_can_write(Chardev *s);
151 * @qemu_chr_be_write:
153 * Write data from the back end to the front end. Before issuing this call,
154 * the caller should call @qemu_chr_be_can_write to determine how much data
155 * the front end can currently accept.
157 * @buf a buffer to receive data from the front end
158 * @len the number of bytes to receive from the front end
160 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
163 * @qemu_chr_be_write_impl:
165 * Implementation of back end writing. Used by replay module.
167 * @buf a buffer to receive data from the front end
168 * @len the number of bytes to receive from the front end
170 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
173 * @qemu_chr_be_update_read_handlers:
175 * Invoked when frontend read handlers are setup
177 * @context the gcontext that will be used to attach the watch sources
179 void qemu_chr_be_update_read_handlers(Chardev *s,
180 GMainContext *context);
183 * @qemu_chr_be_event:
185 * Send an event from the back end to the front end.
187 * @event the event to send
189 void qemu_chr_be_event(Chardev *s, int event);
191 int qemu_chr_add_client(Chardev *s, int fd);
192 Chardev *qemu_chr_find(const char *name);
194 bool qemu_chr_has_feature(Chardev *chr,
195 ChardevFeature feature);
196 void qemu_chr_set_feature(Chardev *chr,
197 ChardevFeature feature);
198 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
199 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
200 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
201 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
203 #define TYPE_CHARDEV "chardev"
204 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
205 #define CHARDEV_CLASS(klass) \
206 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
207 #define CHARDEV_GET_CLASS(obj) \
208 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
210 #define TYPE_CHARDEV_NULL "chardev-null"
211 #define TYPE_CHARDEV_MUX "chardev-mux"
212 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
213 #define TYPE_CHARDEV_PTY "chardev-pty"
214 #define TYPE_CHARDEV_CONSOLE "chardev-console"
215 #define TYPE_CHARDEV_STDIO "chardev-stdio"
216 #define TYPE_CHARDEV_PIPE "chardev-pipe"
217 #define TYPE_CHARDEV_MEMORY "chardev-memory"
218 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
219 #define TYPE_CHARDEV_FILE "chardev-file"
220 #define TYPE_CHARDEV_SERIAL "chardev-serial"
221 #define TYPE_CHARDEV_SOCKET "chardev-socket"
222 #define TYPE_CHARDEV_UDP "chardev-udp"
224 #define CHARDEV_IS_RINGBUF(chr) \
225 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
226 #define CHARDEV_IS_PTY(chr) \
227 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
229 typedef struct ChardevClass {
230 ObjectClass parent_class;
232 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
233 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
235 void (*open)(Chardev *chr, ChardevBackend *backend,
236 bool *be_opened, Error **errp);
238 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
239 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
240 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
241 void (*chr_update_read_handler)(Chardev *s);
242 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
243 int (*get_msgfds)(Chardev *s, int* fds, int num);
244 int (*set_msgfds)(Chardev *s, int *fds, int num);
245 int (*chr_add_client)(Chardev *chr, int fd);
246 int (*chr_wait_connected)(Chardev *chr, Error **errp);
247 void (*chr_disconnect)(Chardev *chr);
248 void (*chr_accept_input)(Chardev *chr);
249 void (*chr_set_echo)(Chardev *chr, bool echo);
250 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
251 void (*chr_be_event)(Chardev *s, int event);
252 } ChardevClass;
254 Chardev *qemu_chardev_new(const char *id, const char *typename,
255 ChardevBackend *backend, Error **errp);
257 extern int term_escape_char;
259 /* console.c */
260 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
262 #endif