]>
Commit | Line | Data |
---|---|---|
4d43a603 MAL |
1 | #ifndef QEMU_CHAR_FE_H |
2 | #define QEMU_CHAR_FE_H | |
3 | ||
4 | #include "chardev/char.h" | |
db725815 | 5 | #include "qemu/main-loop.h" |
4d43a603 MAL |
6 | |
7 | typedef void IOEventHandler(void *opaque, int event); | |
81517ba3 | 8 | typedef int BackendChangeHandler(void *opaque); |
4d43a603 MAL |
9 | |
10 | /* This is the backend as seen by frontend, the actual backend is | |
11 | * Chardev */ | |
12 | struct CharBackend { | |
13 | Chardev *chr; | |
14 | IOEventHandler *chr_event; | |
15 | IOCanReadHandler *chr_can_read; | |
16 | IOReadHandler *chr_read; | |
81517ba3 | 17 | BackendChangeHandler *chr_be_change; |
4d43a603 MAL |
18 | void *opaque; |
19 | int tag; | |
20 | int fe_open; | |
21 | }; | |
22 | ||
23 | /** | |
5662576a | 24 | * qemu_chr_fe_init: |
4d43a603 MAL |
25 | * |
26 | * Initializes a front end for the given CharBackend and | |
27 | * Chardev. Call qemu_chr_fe_deinit() to remove the association and | |
28 | * release the driver. | |
29 | * | |
30 | * Returns: false on error. | |
31 | */ | |
32 | bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp); | |
33 | ||
34 | /** | |
5662576a | 35 | * qemu_chr_fe_deinit: |
1ce2610c MAL |
36 | * @b: a CharBackend |
37 | * @del: if true, delete the chardev backend | |
38 | * | |
4d43a603 MAL |
39 | * Dissociate the CharBackend from the Chardev. |
40 | * | |
41 | * Safe to call without associated Chardev. | |
42 | */ | |
1ce2610c | 43 | void qemu_chr_fe_deinit(CharBackend *b, bool del); |
4d43a603 MAL |
44 | |
45 | /** | |
5662576a | 46 | * qemu_chr_fe_get_driver: |
4d43a603 | 47 | * |
5662576a | 48 | * Returns: the driver associated with a CharBackend or NULL if no |
4d43a603 | 49 | * associated Chardev. |
7c44a2a9 AN |
50 | * Note: avoid this function as the driver should never be accessed directly, |
51 | * especially by the frontends that support chardevice hotswap. | |
52 | * Consider qemu_chr_fe_backend_connected() to check for driver existence | |
4d43a603 MAL |
53 | */ |
54 | Chardev *qemu_chr_fe_get_driver(CharBackend *be); | |
55 | ||
7c44a2a9 | 56 | /** |
5662576a | 57 | * qemu_chr_fe_backend_connected: |
7c44a2a9 | 58 | * |
5662576a | 59 | * Returns: true if there is a chardevice associated with @be. |
7c44a2a9 AN |
60 | */ |
61 | bool qemu_chr_fe_backend_connected(CharBackend *be); | |
62 | ||
30650701 | 63 | /** |
5662576a | 64 | * qemu_chr_fe_backend_open: |
30650701 | 65 | * |
5662576a | 66 | * Returns: true if chardevice associated with @be is open. |
30650701 AN |
67 | */ |
68 | bool qemu_chr_fe_backend_open(CharBackend *be); | |
69 | ||
4d43a603 | 70 | /** |
7a9657ef | 71 | * qemu_chr_fe_set_handlers_full: |
4d43a603 MAL |
72 | * @b: a CharBackend |
73 | * @fd_can_read: callback to get the amount of data the frontend may | |
74 | * receive | |
75 | * @fd_read: callback to receive data from char | |
76 | * @fd_event: event callback | |
81517ba3 AN |
77 | * @be_change: backend change callback; passing NULL means hot backend change |
78 | * is not supported and will not be attempted | |
4d43a603 MAL |
79 | * @opaque: an opaque pointer for the callbacks |
80 | * @context: a main loop context or NULL for the default | |
81 | * @set_open: whether to call qemu_chr_fe_set_open() implicitely when | |
82 | * any of the handler is non-NULL | |
7a9657ef | 83 | * @sync_state: whether to issue event callback with updated state |
4d43a603 MAL |
84 | * |
85 | * Set the front end char handlers. The front end takes the focus if | |
86 | * any of the handler is non-NULL. | |
87 | * | |
88 | * Without associated Chardev, nothing is changed. | |
89 | */ | |
7a9657ef AP |
90 | void qemu_chr_fe_set_handlers_full(CharBackend *b, |
91 | IOCanReadHandler *fd_can_read, | |
92 | IOReadHandler *fd_read, | |
93 | IOEventHandler *fd_event, | |
94 | BackendChangeHandler *be_change, | |
95 | void *opaque, | |
96 | GMainContext *context, | |
97 | bool set_open, | |
98 | bool sync_state); | |
99 | ||
100 | /** | |
101 | * qemu_chr_fe_set_handlers: | |
102 | * | |
103 | * Version of qemu_chr_fe_set_handlers_full() with sync_state = true. | |
104 | */ | |
4d43a603 MAL |
105 | void qemu_chr_fe_set_handlers(CharBackend *b, |
106 | IOCanReadHandler *fd_can_read, | |
107 | IOReadHandler *fd_read, | |
108 | IOEventHandler *fd_event, | |
81517ba3 | 109 | BackendChangeHandler *be_change, |
4d43a603 MAL |
110 | void *opaque, |
111 | GMainContext *context, | |
112 | bool set_open); | |
113 | ||
114 | /** | |
5662576a | 115 | * qemu_chr_fe_take_focus: |
4d43a603 MAL |
116 | * |
117 | * Take the focus (if the front end is muxed). | |
118 | * | |
119 | * Without associated Chardev, nothing is changed. | |
120 | */ | |
121 | void qemu_chr_fe_take_focus(CharBackend *b); | |
122 | ||
123 | /** | |
5662576a | 124 | * qemu_chr_fe_accept_input: |
4d43a603 MAL |
125 | * |
126 | * Notify that the frontend is ready to receive data | |
127 | */ | |
128 | void qemu_chr_fe_accept_input(CharBackend *be); | |
129 | ||
130 | /** | |
5662576a | 131 | * qemu_chr_fe_disconnect: |
4d43a603 | 132 | * |
7351681e | 133 | * Close a fd accepted by character backend. |
4d43a603 MAL |
134 | * Without associated Chardev, do nothing. |
135 | */ | |
136 | void qemu_chr_fe_disconnect(CharBackend *be); | |
137 | ||
138 | /** | |
5662576a | 139 | * qemu_chr_fe_wait_connected: |
4d43a603 MAL |
140 | * |
141 | * Wait for characted backend to be connected, return < 0 on error or | |
7351681e | 142 | * if no associated Chardev. |
4d43a603 MAL |
143 | */ |
144 | int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp); | |
145 | ||
146 | /** | |
5662576a MAL |
147 | * qemu_chr_fe_set_echo: |
148 | * @echo: true to enable echo, false to disable echo | |
4d43a603 MAL |
149 | * |
150 | * Ask the backend to override its normal echo setting. This only really | |
151 | * applies to the stdio backend and is used by the QMP server such that you | |
152 | * can see what you type if you try to type QMP commands. | |
153 | * Without associated Chardev, do nothing. | |
4d43a603 MAL |
154 | */ |
155 | void qemu_chr_fe_set_echo(CharBackend *be, bool echo); | |
156 | ||
157 | /** | |
5662576a | 158 | * qemu_chr_fe_set_open: |
4d43a603 MAL |
159 | * |
160 | * Set character frontend open status. This is an indication that the | |
161 | * front end is ready (or not) to begin doing I/O. | |
162 | * Without associated Chardev, do nothing. | |
163 | */ | |
164 | void qemu_chr_fe_set_open(CharBackend *be, int fe_open); | |
165 | ||
166 | /** | |
5662576a MAL |
167 | * qemu_chr_fe_printf: |
168 | * @fmt: see #printf | |
4d43a603 MAL |
169 | * |
170 | * Write to a character backend using a printf style interface. This | |
171 | * function is thread-safe. It does nothing without associated | |
172 | * Chardev. | |
4d43a603 MAL |
173 | */ |
174 | void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) | |
175 | GCC_FMT_ATTR(2, 3); | |
176 | ||
177 | /** | |
5662576a MAL |
178 | * qemu_chr_fe_add_watch: |
179 | * @cond: the condition to poll for | |
180 | * @func: the function to call when the condition happens | |
181 | * @user_data: the opaque pointer to pass to @func | |
4d43a603 MAL |
182 | * |
183 | * If the backend is connected, create and add a #GSource that fires | |
184 | * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP) | |
185 | * is active; return the #GSource's tag. If it is disconnected, | |
186 | * or without associated Chardev, return 0. | |
187 | * | |
64c3f266 MAL |
188 | * Note that you are responsible to update the front-end sources if |
189 | * you are switching the main context with qemu_chr_fe_set_handlers(). | |
190 | * | |
4d43a603 MAL |
191 | * Returns: the source tag |
192 | */ | |
193 | guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, | |
194 | GIOFunc func, void *user_data); | |
195 | ||
196 | /** | |
5662576a MAL |
197 | * qemu_chr_fe_write: |
198 | * @buf: the data | |
199 | * @len: the number of bytes to send | |
4d43a603 MAL |
200 | * |
201 | * Write data to a character backend from the front end. This function | |
202 | * will send data from the front end to the back end. This function | |
203 | * is thread-safe. | |
204 | * | |
7351681e | 205 | * Returns: the number of bytes consumed (0 if no associated Chardev) |
4d43a603 MAL |
206 | */ |
207 | int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len); | |
208 | ||
209 | /** | |
5662576a MAL |
210 | * qemu_chr_fe_write_all: |
211 | * @buf: the data | |
212 | * @len: the number of bytes to send | |
4d43a603 MAL |
213 | * |
214 | * Write data to a character backend from the front end. This function will | |
215 | * send data from the front end to the back end. Unlike @qemu_chr_fe_write, | |
216 | * this function will block if the back end cannot consume all of the data | |
217 | * attempted to be written. This function is thread-safe. | |
218 | * | |
7351681e | 219 | * Returns: the number of bytes consumed (0 if no associated Chardev) |
4d43a603 MAL |
220 | */ |
221 | int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len); | |
222 | ||
223 | /** | |
5662576a MAL |
224 | * qemu_chr_fe_read_all: |
225 | * @buf: the data buffer | |
226 | * @len: the number of bytes to read | |
4d43a603 MAL |
227 | * |
228 | * Read data to a buffer from the back end. | |
229 | * | |
7351681e | 230 | * Returns: the number of bytes read (0 if no associated Chardev) |
4d43a603 MAL |
231 | */ |
232 | int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len); | |
233 | ||
234 | /** | |
5662576a MAL |
235 | * qemu_chr_fe_ioctl: |
236 | * @cmd: see CHR_IOCTL_* | |
237 | * @arg: the data associated with @cmd | |
4d43a603 MAL |
238 | * |
239 | * Issue a device specific ioctl to a backend. This function is thread-safe. | |
240 | * | |
4d43a603 MAL |
241 | * Returns: if @cmd is not supported by the backend or there is no |
242 | * associated Chardev, -ENOTSUP, otherwise the return | |
243 | * value depends on the semantics of @cmd | |
244 | */ | |
245 | int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg); | |
246 | ||
247 | /** | |
5662576a | 248 | * qemu_chr_fe_get_msgfd: |
4d43a603 MAL |
249 | * |
250 | * For backends capable of fd passing, return the latest file descriptor passed | |
251 | * by a client. | |
252 | * | |
253 | * Returns: -1 if fd passing isn't supported or there is no pending file | |
254 | * descriptor. If a file descriptor is returned, subsequent calls to | |
255 | * this function will return -1 until a client sends a new file | |
256 | * descriptor. | |
257 | */ | |
258 | int qemu_chr_fe_get_msgfd(CharBackend *be); | |
259 | ||
260 | /** | |
5662576a | 261 | * qemu_chr_fe_get_msgfds: |
4d43a603 MAL |
262 | * |
263 | * For backends capable of fd passing, return the number of file received | |
264 | * descriptors and fills the fds array up to num elements | |
265 | * | |
266 | * Returns: -1 if fd passing isn't supported or there are no pending file | |
267 | * descriptors. If file descriptors are returned, subsequent calls to | |
268 | * this function will return -1 until a client sends a new set of file | |
269 | * descriptors. | |
270 | */ | |
271 | int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num); | |
272 | ||
273 | /** | |
5662576a | 274 | * qemu_chr_fe_set_msgfds: |
4d43a603 MAL |
275 | * |
276 | * For backends capable of fd passing, set an array of fds to be passed with | |
277 | * the next send operation. | |
278 | * A subsequent call to this function before calling a write function will | |
279 | * result in overwriting the fd array with the new value without being send. | |
280 | * Upon writing the message the fd array is freed. | |
281 | * | |
282 | * Returns: -1 if fd passing isn't supported or no associated Chardev. | |
283 | */ | |
284 | int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num); | |
285 | ||
286 | #endif /* QEMU_CHAR_FE_H */ |