From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1M9oLK-0007OX-Fu for qemu-devel@nongnu.org; Thu, 28 May 2009 18:44:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1M9oLF-0007Jp-6a for qemu-devel@nongnu.org; Thu, 28 May 2009 18:44:57 -0400 Received: from [199.232.76.173] (port=40826 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M9oLE-0007Jf-N5 for qemu-devel@nongnu.org; Thu, 28 May 2009 18:44:53 -0400 Received: from mx2.redhat.com ([66.187.237.31]:37988) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1M9oLE-0001b1-5W for qemu-devel@nongnu.org; Thu, 28 May 2009 18:44:52 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n4SMipdn029763 for ; Thu, 28 May 2009 18:44:51 -0400 From: Gerd Hoffmann Date: Fri, 29 May 2009 00:44:38 +0200 Message-Id: <1243550679-9027-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PATCH 1/2] qdev: add string and binary properties. List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann Signed-off-by: Gerd Hoffmann --- 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