chardev: Basic support for TN3270
[qemu/ar7.git] / chardev / char-ringbuf.c
blobd130069e8886f79fce593246d21dd7462da2b0ee
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "sysemu/char.h"
26 #include "qmp-commands.h"
27 #include "qemu/base64.h"
29 /* Ring buffer chardev */
31 typedef struct {
32 Chardev parent;
33 size_t size;
34 size_t prod;
35 size_t cons;
36 uint8_t *cbuf;
37 } RingBufChardev;
39 #define RINGBUF_CHARDEV(obj) \
40 OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF)
42 static size_t ringbuf_count(const Chardev *chr)
44 const RingBufChardev *d = RINGBUF_CHARDEV(chr);
46 return d->prod - d->cons;
49 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
51 RingBufChardev *d = RINGBUF_CHARDEV(chr);
52 int i;
54 if (!buf || (len < 0)) {
55 return -1;
58 for (i = 0; i < len; i++) {
59 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
60 if (d->prod - d->cons > d->size) {
61 d->cons = d->prod - d->size;
65 return len;
68 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
70 RingBufChardev *d = RINGBUF_CHARDEV(chr);
71 int i;
73 qemu_mutex_lock(&chr->chr_write_lock);
74 for (i = 0; i < len && d->cons != d->prod; i++) {
75 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
77 qemu_mutex_unlock(&chr->chr_write_lock);
79 return i;
82 static void char_ringbuf_finalize(Object *obj)
84 RingBufChardev *d = RINGBUF_CHARDEV(obj);
86 g_free(d->cbuf);
89 static void qemu_chr_open_ringbuf(Chardev *chr,
90 ChardevBackend *backend,
91 bool *be_opened,
92 Error **errp)
94 ChardevRingbuf *opts = backend->u.ringbuf.data;
95 RingBufChardev *d = RINGBUF_CHARDEV(chr);
97 d->size = opts->has_size ? opts->size : 65536;
99 /* The size must be power of 2 */
100 if (d->size & (d->size - 1)) {
101 error_setg(errp, "size of ringbuf chardev must be power of two");
102 return;
105 d->prod = 0;
106 d->cons = 0;
107 d->cbuf = g_malloc0(d->size);
110 void qmp_ringbuf_write(const char *device, const char *data,
111 bool has_format, enum DataFormat format,
112 Error **errp)
114 Chardev *chr;
115 const uint8_t *write_data;
116 int ret;
117 gsize write_count;
119 chr = qemu_chr_find(device);
120 if (!chr) {
121 error_setg(errp, "Device '%s' not found", device);
122 return;
125 if (!CHARDEV_IS_RINGBUF(chr)) {
126 error_setg(errp, "%s is not a ringbuf device", device);
127 return;
130 if (has_format && (format == DATA_FORMAT_BASE64)) {
131 write_data = qbase64_decode(data, -1,
132 &write_count,
133 errp);
134 if (!write_data) {
135 return;
137 } else {
138 write_data = (uint8_t *)data;
139 write_count = strlen(data);
142 ret = ringbuf_chr_write(chr, write_data, write_count);
144 if (write_data != (uint8_t *)data) {
145 g_free((void *)write_data);
148 if (ret < 0) {
149 error_setg(errp, "Failed to write to device %s", device);
150 return;
154 char *qmp_ringbuf_read(const char *device, int64_t size,
155 bool has_format, enum DataFormat format,
156 Error **errp)
158 Chardev *chr;
159 uint8_t *read_data;
160 size_t count;
161 char *data;
163 chr = qemu_chr_find(device);
164 if (!chr) {
165 error_setg(errp, "Device '%s' not found", device);
166 return NULL;
169 if (!CHARDEV_IS_RINGBUF(chr)) {
170 error_setg(errp, "%s is not a ringbuf device", device);
171 return NULL;
174 if (size <= 0) {
175 error_setg(errp, "size must be greater than zero");
176 return NULL;
179 count = ringbuf_count(chr);
180 size = size > count ? count : size;
181 read_data = g_malloc(size + 1);
183 ringbuf_chr_read(chr, read_data, size);
185 if (has_format && (format == DATA_FORMAT_BASE64)) {
186 data = g_base64_encode(read_data, size);
187 g_free(read_data);
188 } else {
190 * FIXME should read only complete, valid UTF-8 characters up
191 * to @size bytes. Invalid sequences should be replaced by a
192 * suitable replacement character. Except when (and only
193 * when) ring buffer lost characters since last read, initial
194 * continuation characters should be dropped.
196 read_data[size] = 0;
197 data = (char *)read_data;
200 return data;
203 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
204 Error **errp)
206 int val;
207 ChardevRingbuf *ringbuf;
209 backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
210 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
211 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
213 val = qemu_opt_get_size(opts, "size", 0);
214 if (val != 0) {
215 ringbuf->has_size = true;
216 ringbuf->size = val;
220 static void char_ringbuf_class_init(ObjectClass *oc, void *data)
222 ChardevClass *cc = CHARDEV_CLASS(oc);
224 cc->parse = qemu_chr_parse_ringbuf;
225 cc->open = qemu_chr_open_ringbuf;
226 cc->chr_write = ringbuf_chr_write;
229 static const TypeInfo char_ringbuf_type_info = {
230 .name = TYPE_CHARDEV_RINGBUF,
231 .parent = TYPE_CHARDEV,
232 .class_init = char_ringbuf_class_init,
233 .instance_size = sizeof(RingBufChardev),
234 .instance_finalize = char_ringbuf_finalize,
237 /* Bug-compatibility: */
238 static const TypeInfo char_memory_type_info = {
239 .name = TYPE_CHARDEV_MEMORY,
240 .parent = TYPE_CHARDEV_RINGBUF,
243 static void register_types(void)
245 type_register_static(&char_ringbuf_type_info);
246 type_register_static(&char_memory_type_info);
249 type_init(register_types);