4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qemu/error-report.h"
26 #include "qapi/error.h"
27 #include "qapi-visit.h"
28 #include "sysemu/replay.h"
30 #include "chardev/char-fe.h"
31 #include "chardev/char-io.h"
32 #include "chardev/char-mux.h"
34 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
)
42 return qemu_chr_write(s
, buf
, len
, false);
45 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
)
53 return qemu_chr_write(s
, buf
, len
, true);
56 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
)
59 int offset
= 0, counter
= 10;
62 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_sync_read
) {
66 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
67 return replay_char_read_all_load(buf
);
70 while (offset
< len
) {
72 res
= CHARDEV_GET_CLASS(s
)->chr_sync_read(s
, buf
+ offset
,
74 if (res
== -1 && errno
== EAGAIN
) {
84 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
85 replay_char_read_all_save_error(res
);
97 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
98 replay_char_read_all_save_buf(buf
, offset
);
103 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
)
105 Chardev
*s
= be
->chr
;
108 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_ioctl
|| qemu_chr_replay(s
)) {
111 res
= CHARDEV_GET_CLASS(s
)->chr_ioctl(s
, cmd
, arg
);
117 int qemu_chr_fe_get_msgfd(CharBackend
*be
)
119 Chardev
*s
= be
->chr
;
121 int res
= (qemu_chr_fe_get_msgfds(be
, &fd
, 1) == 1) ? fd
: -1;
122 if (s
&& qemu_chr_replay(s
)) {
123 error_report("Replay: get msgfd is not supported "
124 "for serial devices yet");
130 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int len
)
132 Chardev
*s
= be
->chr
;
138 return CHARDEV_GET_CLASS(s
)->get_msgfds
?
139 CHARDEV_GET_CLASS(s
)->get_msgfds(s
, fds
, len
) : -1;
142 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
)
144 Chardev
*s
= be
->chr
;
150 return CHARDEV_GET_CLASS(s
)->set_msgfds
?
151 CHARDEV_GET_CLASS(s
)->set_msgfds(s
, fds
, num
) : -1;
154 void qemu_chr_fe_accept_input(CharBackend
*be
)
156 Chardev
*s
= be
->chr
;
162 if (CHARDEV_GET_CLASS(s
)->chr_accept_input
) {
163 CHARDEV_GET_CLASS(s
)->chr_accept_input(s
);
168 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
170 char buf
[CHR_READ_BUF_LEN
];
173 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
174 /* XXX this blocks entire thread. Rewrite to use
175 * qemu_chr_fe_write and background I/O callbacks */
176 qemu_chr_fe_write_all(be
, (uint8_t *)buf
, strlen(buf
));
180 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
)
185 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
)
189 if (CHARDEV_IS_MUX(s
)) {
190 MuxChardev
*d
= MUX_CHARDEV(s
);
192 if (d
->mux_cnt
>= MAX_MUX
) {
196 d
->backends
[d
->mux_cnt
] = b
;
210 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->label
);
214 void qemu_chr_fe_deinit(CharBackend
*b
, bool del
)
219 qemu_chr_fe_set_handlers(b
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
220 if (b
->chr
->be
== b
) {
223 if (CHARDEV_IS_MUX(b
->chr
)) {
224 MuxChardev
*d
= MUX_CHARDEV(b
->chr
);
225 d
->backends
[b
->tag
] = NULL
;
228 object_unparent(OBJECT(b
->chr
));
234 void qemu_chr_fe_set_handlers(CharBackend
*b
,
235 IOCanReadHandler
*fd_can_read
,
236 IOReadHandler
*fd_read
,
237 IOEventHandler
*fd_event
,
239 GMainContext
*context
,
251 cc
= CHARDEV_GET_CLASS(s
);
252 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
254 remove_fd_in_watch(s
);
258 b
->chr_can_read
= fd_can_read
;
259 b
->chr_read
= fd_read
;
260 b
->chr_event
= fd_event
;
262 if (cc
->chr_update_read_handler
) {
263 cc
->chr_update_read_handler(s
, context
);
267 qemu_chr_fe_set_open(b
, fe_open
);
271 qemu_chr_fe_take_focus(b
);
272 /* We're connecting to an already opened device, so let's make sure we
273 also get the open event */
275 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
279 if (CHARDEV_IS_MUX(s
)) {
280 mux_chr_set_handlers(s
, context
);
284 void qemu_chr_fe_take_focus(CharBackend
*b
)
290 if (CHARDEV_IS_MUX(b
->chr
)) {
291 mux_set_focus(b
->chr
, b
->tag
);
295 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
)
298 error_setg(errp
, "missing associated backend");
302 return qemu_chr_wait_connected(be
->chr
, errp
);
305 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
)
307 Chardev
*chr
= be
->chr
;
309 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_set_echo
) {
310 CHARDEV_GET_CLASS(chr
)->chr_set_echo(chr
, echo
);
314 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
)
316 Chardev
*chr
= be
->chr
;
322 if (be
->fe_open
== fe_open
) {
325 be
->fe_open
= fe_open
;
326 if (CHARDEV_GET_CLASS(chr
)->chr_set_fe_open
) {
327 CHARDEV_GET_CLASS(chr
)->chr_set_fe_open(chr
, fe_open
);
331 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
332 GIOFunc func
, void *user_data
)
334 Chardev
*s
= be
->chr
;
338 if (!s
|| CHARDEV_GET_CLASS(s
)->chr_add_watch
== NULL
) {
342 src
= CHARDEV_GET_CLASS(s
)->chr_add_watch(s
, cond
);
347 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
348 tag
= g_source_attach(src
, NULL
);
354 void qemu_chr_fe_disconnect(CharBackend
*be
)
356 Chardev
*chr
= be
->chr
;
358 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_disconnect
) {
359 CHARDEV_GET_CLASS(chr
)->chr_disconnect(chr
);