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