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
24 #include "qemu/osdep.h"
25 #include "sysemu/char.h"
26 #include "qmp-commands.h"
27 #include "qemu/base64.h"
29 /* Ring buffer chardev */
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
);
54 if (!buf
|| (len
< 0)) {
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
;
68 static int ringbuf_chr_read(Chardev
*chr
, uint8_t *buf
, int len
)
70 RingBufChardev
*d
= RINGBUF_CHARDEV(chr
);
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
);
82 static void char_ringbuf_finalize(Object
*obj
)
84 RingBufChardev
*d
= RINGBUF_CHARDEV(obj
);
89 static void qemu_chr_open_ringbuf(Chardev
*chr
,
90 ChardevBackend
*backend
,
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");
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
,
115 const uint8_t *write_data
;
119 chr
= qemu_chr_find(device
);
121 error_setg(errp
, "Device '%s' not found", device
);
125 if (!CHARDEV_IS_RINGBUF(chr
)) {
126 error_setg(errp
, "%s is not a ringbuf device", device
);
130 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
131 write_data
= qbase64_decode(data
, -1,
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
);
149 error_setg(errp
, "Failed to write to device %s", device
);
154 char *qmp_ringbuf_read(const char *device
, int64_t size
,
155 bool has_format
, enum DataFormat format
,
163 chr
= qemu_chr_find(device
);
165 error_setg(errp
, "Device '%s' not found", device
);
169 if (!CHARDEV_IS_RINGBUF(chr
)) {
170 error_setg(errp
, "%s is not a ringbuf device", device
);
175 error_setg(errp
, "size must be greater than zero");
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
);
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.
197 data
= (char *)read_data
;
203 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
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);
215 ringbuf
->has_size
= true;
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
);