File: vmc-emu.cpp

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (124 lines) | stat: -rw-r--r-- 4,107 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2019 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <glib.h>

#include "vmc-emu.h"

// handle writes to the device
static int vmc_write(SpiceCharDeviceInstance *sin,
                     const uint8_t *buf, int len)
{
    VmcEmu *const vmc = SPICE_CONTAINEROF(sin, VmcEmu, instance);

    // just copy into the buffer
    unsigned copy = MIN(sizeof(vmc->write_buf) - vmc->write_pos, len);
    memcpy(vmc->write_buf+vmc->write_pos, buf, copy);
    vmc->write_pos += copy;
    if (copy && vmc->data_written_cb) {
        vmc->data_written_cb(vmc);
    }
    return len;
}

static int vmc_read(SpiceCharDeviceInstance *sin,
                    uint8_t *buf, int len)
{
    VmcEmu *const vmc = SPICE_CONTAINEROF(sin, VmcEmu, instance);
    int ret;

    if (vmc->pos >= *vmc->message_sizes_curr && vmc->message_sizes_curr < vmc->message_sizes_end) {
        ++vmc->message_sizes_curr;
    }
    if (vmc->message_sizes_curr >= vmc->message_sizes_end || vmc->pos >= *vmc->message_sizes_curr) {
        return 0;
    }
    ret = MIN(*vmc->message_sizes_curr - vmc->pos, len);
    memcpy(buf, &vmc->message[vmc->pos], ret);
    vmc->pos += ret;
    // kick off next message read
    // currently Qemu kicks the device so we need to do it manually
    // here. If not all data are read, the device goes into blocking
    // state and we get the wake only when we read from the device
    // again
    if (vmc->pos >= *vmc->message_sizes_curr) {
        spice_server_char_device_wakeup(&vmc->instance);
    }
    return ret;
}

static void vmc_state(SpiceCharDeviceInstance *sin,
                      int connected)
{
    VmcEmu *const vmc = SPICE_CONTAINEROF(sin, VmcEmu, instance);
    vmc->device_enabled = !!connected;
}

static const SpiceCharDeviceInterface vmc_interface = {
    .base = {
        .type          = SPICE_INTERFACE_CHAR_DEVICE,
        .description   = "test spice virtual channel char device",
        .major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
        .minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
    },
    .state              = vmc_state,
    .write              = vmc_write,
    .read               = vmc_read,
};

VmcEmu *vmc_emu_new(const char *subtype, const char *portname)
{
    auto vmc = g_new0(VmcEmu, 1);
    vmc->vmc_interface = vmc_interface;
    vmc->instance.base.sif = &vmc->vmc_interface.base;
    vmc->instance.subtype = g_strdup(subtype);
    if (portname) {
        vmc->instance.portname = g_strdup(portname);
    }
    vmc_emu_reset(vmc);
    return vmc;
}

void vmc_emu_destroy(VmcEmu *vmc)
{
    g_free(const_cast<char *>(vmc->instance.portname));
    g_free(const_cast<char *>(vmc->instance.subtype));
    g_free(vmc);
}

void vmc_emu_reset(VmcEmu *vmc)
{
    vmc->pos = 0;
    vmc->write_pos = 0;
    vmc->message_sizes_curr = vmc->message_sizes;
    vmc->message_sizes_end = vmc->message_sizes;
}

void vmc_emu_add_read_till(VmcEmu *vmc, uint8_t *end)
{
    g_assert(vmc->message_sizes_end - vmc->message_sizes < G_N_ELEMENTS(vmc->message_sizes));
    g_assert(end >= vmc->message);
    g_assert(end - vmc->message <= G_N_ELEMENTS(vmc->message));
    unsigned prev_size =
        vmc->message_sizes_end > vmc->message_sizes ? vmc->message_sizes_end[-1] : 0;
    unsigned size = end - vmc->message;
    g_assert(size >= prev_size);
    *vmc->message_sizes_end = size;
    ++vmc->message_sizes_end;
}