qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Victor CLEMENT <victor.clement@openwide.fr>
To: qemu-devel@nongnu.org
Cc: victor.clement@outlook.com,
	Victor CLEMENT <victor.clement@openwide.fr>,
	julien.viarddegalbert@openwide.fr
Subject: [Qemu-devel] [PATCH 3/7] chardev: add a scenario engine backend
Date: Fri, 11 Sep 2015 14:50:26 +0200	[thread overview]
Message-ID: <1441975830-11828-4-git-send-email-victor.clement@openwide.fr> (raw)
In-Reply-To: <1441975830-11828-1-git-send-email-victor.clement@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 <victor.clement@openwide.fr>
---
 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 += rng.o rng-egd.o
 common-obj-$(CONFIG_POSIX) += rng-random.o
 
+common-obj-$(CONFIG_SCENARIO) += scenario.o
 common-obj-y += msmouse.o testdev.o
 common-obj-$(CONFIG_BRLAPI) += baum.o
 baum.o-cflags := $(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énierie
+ *
+ * Author: Victor Clément
+ *
+ * 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 included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <stdlib.h>
+#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 = true;
+    }
+    return len;
+}
+
+
+static bool scenario_create_chardev(const char *name)
+{
+    ChardevBackend *backend = g_new0(ChardevBackend, 1);
+    backend->kind = 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 = qemu_chr_find(name);
+    if (chr) {
+        chr->chr_write = cb;
+    } else {
+        if (create) {
+            printf("scenario: WARNING: chardev \"%s\" not found, creating it\n",
+                    name);
+            if (scenario_create_chardev(name)) {
+                chr = qemu_chr_find(name);
+                chr->chr_write = cb;
+                printf("scenario: INFO: chardev \"%s\" created\n", name);
+            } else {
+                printf("scenario: ERROR: chardev \"%s\" cannot be created\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 = qemu_chr_alloc();
+    chr->chr_write = scenario_chr_write;
+    chr->chr_close = scenario_chr_close;
+    chr->explicit_be_open = 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);
 
 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 compatibility
-                                       'memory' : 'ChardevRingbuf' } }
+                                       'memory' : 'ChardevRingbuf',
+                                       'scenario': 'ChardevDummy' } }
 
 ##
 # @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, ChardevBackend *backend,
     case CHARDEV_BACKEND_KIND_MEMORY:
         chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
         break;
+#ifdef CONFIG_SCENARIO
+    case CHARDEV_BACKEND_KIND_SCENARIO:
+        chr = qemu_chr_open_scenario();
+        break;
+#endif
     default:
         error_setg(errp, "unknown chardev backend (%d)", backend->kind);
         break;
-- 
2.5.1

  parent reply	other threads:[~2015-09-11 12:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-11 12:50 [Qemu-devel] [PATCH 0/7] Qemu scenario engine Victor CLEMENT
2015-09-11 12:50 ` [Qemu-devel] [PATCH 1/7] configure: add --enable-scenario-engine option Victor CLEMENT
2015-09-11 12:50 ` [Qemu-devel] [PATCH 2/7] gpio-pl061: add a scenario engine interaction API Victor CLEMENT
2015-09-11 12:50 ` Victor CLEMENT [this message]
2015-09-11 13:11   ` [Qemu-devel] [PATCH 3/7] chardev: add a scenario engine backend Eric Blake
2015-09-11 12:50 ` [Qemu-devel] [PATCH 4/7] scenario-engine: add utilities Victor CLEMENT
2015-09-11 12:50 ` [Qemu-devel] [PATCH 5/7] scenario-engine: add a time based event scheduler Victor CLEMENT
2015-09-11 12:50 ` [Qemu-devel] [PATCH 6/7] scenario engine: provide a scenario file template Victor CLEMENT
2015-09-11 12:50 ` [Qemu-devel] [PATCH 7/7] scenario engine: add a Qemu option to start it Victor CLEMENT
2015-09-11 13:11 ` [Qemu-devel] [PATCH 0/7] Qemu scenario engine Andreas Färber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1441975830-11828-4-git-send-email-victor.clement@openwide.fr \
    --to=victor.clement@openwide.fr \
    --cc=julien.viarddegalbert@openwide.fr \
    --cc=qemu-devel@nongnu.org \
    --cc=victor.clement@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).