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