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