Received: from eggs.gnu.org ([2001:4830:134:3::10]:56829) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZaNnN-0002VE-72 for qemu-devel@nongnu.org; Fri, 11 Sep 2015 08:51:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZaNnL-00067S-Re for qemu-devel@nongnu.org; Fri, 11 Sep 2015 08:51:13 -0400 Received: from mx1.openwide.fr ([213.162.52.98]:47225) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZaNnL-00067C-Jw for qemu-devel@nongnu.org; Fri, 11 Sep 2015 08:51:11 -0400 From: Victor CLEMENT Date: Fri, 11 Sep 2015 14:50:26 +0200 Message-Id: <1441975830-11828-4-git-send-email-victor.clement@openwide.fr> In-Reply-To: <1441975830-11828-1-git-send-email-victor.clement@openwide.fr> References: <1441975830-11828-1-git-send-email-victor.clement@openwide.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 3/7] chardev: add a scenario engine backend List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: victor.clement@outlook.com, Victor CLEMENT , julien.viarddegalbert@openwide.fr This scenario backend implements the scenario interaction API for all character device emulators. It provides a callback registration function which registers a chr_write callback. It will call the user defined callback on incoming data. To write to this character device, qemu_chr_be_write can be used. Signed-off-by: Victor CLEMENT --- backends/Makefile.objs | 1 + backends/scenario.c | 107 +++++++++++++++++++++++++++++++++++++++++++= ++++++ include/sysemu/char.h | 13 ++++++ qapi-schema.json | 3 +- qemu-char.c | 5 +++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 backends/scenario.c diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 31a3a89..6be07dc 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -1,6 +1,7 @@ common-obj-y +=3D rng.o rng-egd.o common-obj-$(CONFIG_POSIX) +=3D rng-random.o =20 +common-obj-$(CONFIG_SCENARIO) +=3D scenario.o common-obj-y +=3D msmouse.o testdev.o common-obj-$(CONFIG_BRLAPI) +=3D baum.o baum.o-cflags :=3D $(SDL_CFLAGS) diff --git a/backends/scenario.c b/backends/scenario.c new file mode 100644 index 0000000..35dd9d2 --- /dev/null +++ b/backends/scenario.c @@ -0,0 +1,107 @@ +/* + * QEMU Scenario engine chardev backend + * + * Copyright (c) 2015 Open Wide Ing=C3=A9nierie + * + * Author: Victor Cl=C3=A9ment + * + * Permission is hereby granted, free of charge, to any person obtaining= a copy + * of this software and associated documentation files (the "Software"),= to deal + * in the Software without restriction, including without limitation the= rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or = sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be includ= ed in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRE= SS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILI= TY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHA= LL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR = OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISI= NG FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING= S IN + * THE SOFTWARE. + */ +#include +#include "qemu-common.h" +#include "qmp-commands.h" +#include "sysemu/char.h" + +static int scenario_chr_write(struct CharDriverState *s, + const uint8_t *buf, + int len) +{ + static bool notified; + if (!notified) { + printf("Write to scenario chardev %s with no custom callback\n", + s->label); + notified =3D true; + } + return len; +} + + +static bool scenario_create_chardev(const char *name) +{ + ChardevBackend *backend =3D g_new0(ChardevBackend, 1); + backend->kind =3D CHARDEV_BACKEND_KIND_SCENARIO; + if (qmp_chardev_add(name, backend, NULL)) { + printf("scenario: successfull creation of scenario chardev: %s\n= ", + name); + return true; + } else { + printf("scenario: error creating scenario chardev: %s\n", name); + return false; + } + qapi_free_ChardevBackend(backend); +} + +void scenario_register_chardev_cb(const char *name, scenario_chardev_cb_= t *cb, + bool create) +{ + CharDriverState *chr; + chr =3D qemu_chr_find(name); + if (chr) { + chr->chr_write =3D cb; + } else { + if (create) { + printf("scenario: WARNING: chardev \"%s\" not found, creatin= g it\n", + name); + if (scenario_create_chardev(name)) { + chr =3D qemu_chr_find(name); + chr->chr_write =3D cb; + printf("scenario: INFO: chardev \"%s\" created\n", name)= ; + } else { + printf("scenario: ERROR: chardev \"%s\" cannot be create= d\n", + name); + } + } else { + printf("scenario: WARNING: chardev \"%s\" not found\n", name= ); + } + } +} + +static void scenario_chr_close(struct CharDriverState *chr) +{ + g_free(chr); +} + +CharDriverState *qemu_chr_open_scenario(void) +{ + CharDriverState *chr; + + chr =3D qemu_chr_alloc(); + chr->chr_write =3D scenario_chr_write; + chr->chr_close =3D scenario_chr_close; + chr->explicit_be_open =3D true; + + return chr; +} + +static void register_types(void) +{ + register_char_driver("scenario", CHARDEV_BACKEND_KIND_SCENARIO, NULL= ); +} + +type_init(register_types); diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 832b7fe..94bebf2 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -368,4 +368,17 @@ typedef CharDriverState *(VcHandler)(ChardevVC *vc); =20 void register_vc_handler(VcHandler *handler); CharDriverState *vc_init(ChardevVC *vc); + +/* scenario.c */ +#ifdef CONFIG_SCENARIO +CharDriverState *qemu_chr_open_scenario(void); + +typedef int scenario_chardev_cb_t(struct CharDriverState *s, + const uint8_t *buf, + int len); +void scenario_register_chardev_cb(const char *name, + scenario_chardev_cb_t *cb, + bool create); +#endif + #endif diff --git a/qapi-schema.json b/qapi-schema.json index 4342a08..2aed9da 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -3042,7 +3042,8 @@ 'vc' : 'ChardevVC', 'ringbuf': 'ChardevRingbuf', # next one is just for compatibil= ity - 'memory' : 'ChardevRingbuf' } } + 'memory' : 'ChardevRingbuf', + 'scenario': 'ChardevDummy' } } =20 ## # @ChardevReturn: diff --git a/qemu-char.c b/qemu-char.c index d956f8d..717fafd 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -4295,6 +4295,11 @@ ChardevReturn *qmp_chardev_add(const char *id, Cha= rdevBackend *backend, case CHARDEV_BACKEND_KIND_MEMORY: chr =3D qemu_chr_open_ringbuf(backend->ringbuf, errp); break; +#ifdef CONFIG_SCENARIO + case CHARDEV_BACKEND_KIND_SCENARIO: + chr =3D qemu_chr_open_scenario(); + break; +#endif default: error_setg(errp, "unknown chardev backend (%d)", backend->kind); break; --=20 2.5.1