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#include "qemu/osdep.h"
27#include "qemu-common.h"
28#include "chardev/char.h"
29
30#define BUF_SIZE 32
31
32typedef struct {
33 Chardev parent;
34
35 uint8_t in_buf[32];
36 int in_buf_used;
37} TestdevChardev;
38
39#define TYPE_CHARDEV_TESTDEV "chardev-testdev"
40#define TESTDEV_CHARDEV(obj) \
41 OBJECT_CHECK(TestdevChardev, (obj), TYPE_CHARDEV_TESTDEV)
42
43
44static int testdev_eat_packet(TestdevChardev *testdev)
45{
46 const uint8_t *cur = testdev->in_buf;
47 int len = testdev->in_buf_used;
48 uint8_t c;
49 int arg;
50
51#define EAT(c) do { \
52 if (!len--) { \
53 return 0; \
54 } \
55 c = *cur++; \
56} while (0)
57
58 EAT(c);
59
60 while (isspace(c)) {
61 EAT(c);
62 }
63
64 arg = 0;
65 while (isdigit(c)) {
66 arg = arg * 10 + c - '0';
67 EAT(c);
68 }
69
70 while (isspace(c)) {
71 EAT(c);
72 }
73
74 switch (c) {
75 case 'q':
76 exit((arg << 1) | 1);
77 break;
78 default:
79 break;
80 }
81 return cur - testdev->in_buf;
82}
83
84
85static int testdev_chr_write(Chardev *chr, const uint8_t *buf, int len)
86{
87 TestdevChardev *testdev = TESTDEV_CHARDEV(chr);
88 int tocopy, eaten, orig_len = len;
89
90 while (len) {
91
92 tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
93
94 memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
95 testdev->in_buf_used += tocopy;
96 buf += tocopy;
97 len -= tocopy;
98
99
100 while (testdev->in_buf_used > 0 &&
101 (eaten = testdev_eat_packet(testdev)) > 0) {
102 memmove(testdev->in_buf, testdev->in_buf + eaten,
103 testdev->in_buf_used - eaten);
104 testdev->in_buf_used -= eaten;
105 }
106 }
107 return orig_len;
108}
109
110static void char_testdev_class_init(ObjectClass *oc, void *data)
111{
112 ChardevClass *cc = CHARDEV_CLASS(oc);
113
114 cc->chr_write = testdev_chr_write;
115}
116
117static const TypeInfo char_testdev_type_info = {
118 .name = TYPE_CHARDEV_TESTDEV,
119 .parent = TYPE_CHARDEV,
120 .instance_size = sizeof(TestdevChardev),
121 .class_init = char_testdev_class_init,
122};
123
124static void register_types(void)
125{
126 type_register_static(&char_testdev_type_info);
127}
128
129type_init(register_types);
130