Merge remote-tracking branch 'yongbok/tags/mips-20161204' into staging
[qemu/kevin.git] / backends / testdev.c
blob60156e320eb03f6ee16f5b2e2dd4e7cc0c39ee30
1 /*
2 * QEMU Char Device for testsuite control
4 * Copyright (c) 2014 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "sysemu/char.h"
30 #define BUF_SIZE 32
32 typedef struct {
33 CharDriverState *chr;
34 uint8_t in_buf[32];
35 int in_buf_used;
36 } TestdevCharState;
38 /* Try to interpret a whole incoming packet */
39 static int testdev_eat_packet(TestdevCharState *testdev)
41 const uint8_t *cur = testdev->in_buf;
42 int len = testdev->in_buf_used;
43 uint8_t c;
44 int arg;
46 #define EAT(c) do { \
47 if (!len--) { \
48 return 0; \
49 } \
50 c = *cur++; \
51 } while (0)
53 EAT(c);
55 while (isspace(c)) {
56 EAT(c);
59 arg = 0;
60 while (isdigit(c)) {
61 arg = arg * 10 + c - '0';
62 EAT(c);
65 while (isspace(c)) {
66 EAT(c);
69 switch (c) {
70 case 'q':
71 exit((arg << 1) | 1);
72 break;
73 default:
74 break;
76 return cur - testdev->in_buf;
79 /* The other end is writing some data. Store it and try to interpret */
80 static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
82 TestdevCharState *testdev = chr->opaque;
83 int tocopy, eaten, orig_len = len;
85 while (len) {
86 /* Complete our buffer as much as possible */
87 tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
89 memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
90 testdev->in_buf_used += tocopy;
91 buf += tocopy;
92 len -= tocopy;
94 /* Interpret it as much as possible */
95 while (testdev->in_buf_used > 0 &&
96 (eaten = testdev_eat_packet(testdev)) > 0) {
97 memmove(testdev->in_buf, testdev->in_buf + eaten,
98 testdev->in_buf_used - eaten);
99 testdev->in_buf_used -= eaten;
102 return orig_len;
105 static void testdev_free(struct CharDriverState *chr)
107 TestdevCharState *testdev = chr->opaque;
109 g_free(testdev);
112 static CharDriverState *chr_testdev_init(const char *id,
113 ChardevBackend *backend,
114 ChardevReturn *ret,
115 bool *be_opened,
116 Error **errp)
118 TestdevCharState *testdev;
119 CharDriverState *chr;
121 testdev = g_new0(TestdevCharState, 1);
122 testdev->chr = chr = g_new0(CharDriverState, 1);
124 chr->opaque = testdev;
125 chr->chr_write = testdev_write;
126 chr->chr_free = testdev_free;
128 return chr;
131 static void register_types(void)
133 register_char_driver("testdev", CHARDEV_BACKEND_KIND_TESTDEV, NULL,
134 chr_testdev_init);
137 type_init(register_types);