uploaded spice-vdagent
[platform/adaptation/emulator/spice-vdagent.git] / src / vdagent-virtio-port.h
1 /*  vdagent-virtio-port.h virtio port communication header
2
3     Copyright 2010 Red Hat, Inc.
4
5     Red Hat Authors:
6     Hans de Goede <hdegoede@redhat.com>
7
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or   
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of 
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef __VIRTIO_PORT_H
23 #define __VIRTIO_PORT_H
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <sys/select.h>
28 #include <spice/vd_agent.h>
29
30 struct vdagent_virtio_port;
31
32 /* Callbacks with this type will be called when a complete message has been
33    received. Sometimes the callback may want to close the port, in this
34    case do *not* call vdagent_virtio_port_destroy from the callback. The desire
35    to close the port can be indicated be returning -1 from the callback,
36    in other cases return 0. */
37 typedef int (*vdagent_virtio_port_read_callback)(
38     struct vdagent_virtio_port *vport,
39     int port_nr,
40     VDAgentMessage *message_header,
41     uint8_t *data);
42
43 /* Callbacks with this type will be called when the port is disconnected.
44    Note:
45    1) vdagent_virtio_port will destroy the port in question itself after
46       this callback has completed!
47    2) This callback is always called, even if the disconnect is initiated
48       by the vdagent_virtio_port user through returning -1 from a read
49       callback, or by explictly calling vdagent_virtio_port_destroy */
50 typedef void (*vdagent_virtio_port_disconnect_callback)(
51     struct vdagent_virtio_port *conn);
52
53
54 /* Create a vdagent virtio port object for port portname */
55 struct vdagent_virtio_port *vdagent_virtio_port_create(const char *portname,
56     vdagent_virtio_port_read_callback read_callback,
57     vdagent_virtio_port_disconnect_callback disconnect_callback);
58     
59 /* The contents of portp will be made NULL */
60 void vdagent_virtio_port_destroy(struct vdagent_virtio_port **vportp);
61
62
63 /* Given a vdagent_virtio_port fill the fd_sets pointed to by readfds and
64    writefds for select() usage.
65
66    Return value: value of the highest fd + 1 */
67 int vdagent_virtio_port_fill_fds(struct vdagent_virtio_port *vport,
68         fd_set *readfds, fd_set *writefds);
69
70 /* Handle any events flagged by select for the given vdagent_virtio_port.
71    Note the port may be destroyed (when disconnected) by this call
72    in this case the disconnect calllback will get called before the
73    destruction and the contents of connp will be made NULL */
74 void vdagent_virtio_port_handle_fds(struct vdagent_virtio_port **vportp,
75         fd_set *readfds, fd_set *writefds);
76
77
78 /* Queue a message for delivery, either bit by bit, or all at once
79
80    Returns 0 on success -1 on error (only happens when malloc fails) */
81 int vdagent_virtio_port_write_start(
82         struct vdagent_virtio_port *vport,
83         uint32_t port_nr,
84         uint32_t message_type,
85         uint32_t message_opaque,
86         uint32_t data_size);
87
88 int vdagent_virtio_port_write_append(
89         struct vdagent_virtio_port *vport,
90         const uint8_t *data,
91         uint32_t size);
92
93 int vdagent_virtio_port_write(
94         struct vdagent_virtio_port *vport,
95         uint32_t port_nr,
96         uint32_t message_type,
97         uint32_t message_opaque,
98         const uint8_t *data,
99         uint32_t data_size);
100
101 void vdagent_virtio_port_flush(struct vdagent_virtio_port **vportp);
102 void vdagent_virtio_port_reset(struct vdagent_virtio_port *vport, int port);
103
104 #endif