qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 1/2] qdev: add string and binary properties.
Date: Fri, 29 May 2009 00:44:38 +0200	[thread overview]
Message-ID: <1243550679-9027-1-git-send-email-kraxel@redhat.com> (raw)


Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/qdev.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/qdev.h |    5 +++++
 2 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/hw/qdev.c b/hw/qdev.c
index f271b7f..d723b3e 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -35,12 +35,17 @@ struct DeviceProperty {
     const char *name;
     enum {
         PROP_INT,
+        PROP_STR,
+        PROP_BIN,
         PROP_PTR,
     } type;
     union {
         uint64_t i;
+        char *str;
+        uint8_t *bin;
         void *ptr;
     } value;
+    size_t len;
     DeviceProperty *next;
 };
 
@@ -146,6 +151,26 @@ void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
     prop->value.i = value;
 }
 
+void qdev_set_prop_str(DeviceState *dev, const char *name, const char *str)
+{
+    DeviceProperty *prop;
+
+    prop = create_prop(dev, name);
+    prop->type = PROP_STR;
+    prop->value.str = qemu_strdup(str);
+}
+
+void qdev_set_prop_bin(DeviceState *dev, const char *name, const uint8_t *bin, size_t len)
+{
+    DeviceProperty *prop;
+
+    prop = create_prop(dev, name);
+    prop->type = PROP_BIN;
+    prop->value.bin = qemu_malloc(len);
+    prop->len = len;
+    memcpy(prop->value.bin, bin, len);
+}
+
 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
 {
     DeviceProperty *prop;
@@ -203,6 +228,30 @@ uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
     return prop->value.i;
 }
 
+const char *qdev_get_prop_str(DeviceState *dev, const char *name, const char *def)
+{
+    DeviceProperty *prop;
+
+    prop = find_prop(dev, name);
+    if (!prop || prop->type != PROP_STR)
+        return def;
+
+    return prop->value.str;
+}
+
+int qdev_get_prop_bin(DeviceState *dev, const char *name, uint8_t *dest, size_t size)
+{
+    DeviceProperty *prop;
+
+    prop = find_prop(dev, name);
+    if (!prop || prop->type != PROP_BIN)
+        return -1;
+    if (size < prop->len)
+        return -1;
+    memcpy(dest, prop->value.bin, prop->len);
+    return 0;
+}
+
 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
 {
     DeviceProperty *prop;
@@ -326,6 +375,7 @@ void qbus_print(Monitor *mon, BusState *bus, int indent)
     struct DeviceState *dev;
     struct BusState *child;
     struct DeviceProperty *prop;
+    int i;
 
     monitor_printf(mon, "%*sbus: name %s, type %d\n", indent, "",
                    bus->name, bus->type);
@@ -336,6 +386,17 @@ void qbus_print(Monitor *mon, BusState *bus, int indent)
             case PROP_INT:
                 monitor_printf(mon, ", %s=%" PRId64, prop->name, prop->value.i);
                 break;
+            case PROP_STR:
+                monitor_printf(mon, ", %s=\"%s\"", prop->name, prop->value.str);
+                break;
+            case PROP_BIN:
+                monitor_printf(mon, ", %s=", prop->name);
+                for (i = 0; i < prop->len && i < 8; i++) {
+                    monitor_printf(mon, "%s%02x", 0 == i ? "" : ",",
+                                   prop->value.bin[i]);
+                }
+                monitor_printf(mon, "%s", i == prop->len ? "" : ",...");
+                break;
             case PROP_PTR:
                 monitor_printf(mon, ", %s=%p", prop->name, prop->value.ptr);
                 break;
diff --git a/hw/qdev.h b/hw/qdev.h
index ab670de..2f5d9dc 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -50,6 +50,9 @@ void qdev_free(DeviceState *dev);
 
 /* Set properties between creation and init.  */
 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value);
+void qdev_set_prop_str(DeviceState *dev, const char *name, const char *str);
+void qdev_set_prop_bin(DeviceState *dev, const char *name, const uint8_t *bin,
+                       size_t len);
 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value);
 void qdev_set_netdev(DeviceState *dev, NICInfo *nd);
 
@@ -84,6 +87,8 @@ CharDriverState *qdev_init_chardev(DeviceState *dev);
 
 BusState *qdev_get_parent_bus(DeviceState *dev);
 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def);
+const char *qdev_get_prop_str(DeviceState *dev, const char *name, const char *def);
+int qdev_get_prop_bin(DeviceState *dev, const char *name, uint8_t *dest, size_t size);
 void *qdev_get_prop_ptr(DeviceState *dev, const char *name);
 
 /* Convery from a base type to a parent type, with compile time checking.  */
-- 
1.6.2.2

             reply	other threads:[~2009-05-28 22:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28 22:44 Gerd Hoffmann [this message]
2009-05-28 22:44 ` [Qemu-devel] [PATCH 2/2] qdev: kill DeviceState->nd, use properties instead Gerd Hoffmann
2009-05-29 12:10   ` Paul Brook
2009-05-29 13:17     ` Gerd Hoffmann

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=1243550679-9027-1-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).