VMState supports the type bool but qdev instead supports bit, backed by uint32_t. Therefore let's add DEFINE_PROP_BOOL() and qdev_prop_set_bool().
Since, e.g., enabled=on does not look nice, parse/print "yes" and "no". Cc: Juan Quintela <quint...@redhat.com> Signed-off-by: Andreas Färber <andreas.faer...@web.de> --- hw/qdev-properties.c | 35 +++++++++++++++++++++++++++++++++++ hw/qdev.h | 5 +++++ 2 files changed, 40 insertions(+), 0 deletions(-) diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index eff2d24..e262f9a 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -63,6 +63,36 @@ PropertyInfo qdev_prop_bit = { .print = print_bit, }; +/* --- bool --- */ + +static int parse_bool(DeviceState *dev, Property *prop, const char *str) +{ + bool *ptr = qdev_get_prop_ptr(dev, prop); + + if (strncasecmp(str, "yes", 3) == 0) { + *ptr = true; + } else if (strncasecmp(str, "no", 2) == 0) { + *ptr = false; + } else { + return -EINVAL; + } + return 0; +} + +static int print_bool(DeviceState *dev, Property *prop, char *dest, size_t len) +{ + bool *ptr = qdev_get_prop_ptr(dev, prop); + return snprintf(dest, len, (*ptr) ? "yes" : "no"); +} + +PropertyInfo qdev_prop_bool = { + .name = "yes/no", + .type = PROP_TYPE_BOOL, + .size = sizeof(bool), + .parse = parse_bool, + .print = print_bool, +}; + /* --- 8bit integer --- */ static int parse_uint8(DeviceState *dev, Property *prop, const char *str) @@ -644,6 +674,11 @@ void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value) qdev_prop_set(dev, name, &value, PROP_TYPE_BIT); } +void qdev_prop_set_bool(DeviceState *dev, const char *name, bool value) +{ + qdev_prop_set(dev, name, &value, PROP_TYPE_BOOL); +} + void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value) { qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8); diff --git a/hw/qdev.h b/hw/qdev.h index 8a13ec9..f05166d 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -101,6 +101,7 @@ enum PropertyType { PROP_TYPE_VLAN, PROP_TYPE_PTR, PROP_TYPE_BIT, + PROP_TYPE_BOOL, }; struct PropertyInfo { @@ -219,6 +220,7 @@ int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data); /*** qdev-properties.c ***/ extern PropertyInfo qdev_prop_bit; +extern PropertyInfo qdev_prop_bool; extern PropertyInfo qdev_prop_uint8; extern PropertyInfo qdev_prop_uint16; extern PropertyInfo qdev_prop_uint32; @@ -257,6 +259,8 @@ extern PropertyInfo qdev_prop_pci_devfn; .defval = (bool[]) { (_defval) }, \ } +#define DEFINE_PROP_BOOL(_n, _s, _f, _d) \ + DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_bool, bool) #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t) #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \ @@ -298,6 +302,7 @@ int qdev_prop_exists(DeviceState *dev, const char *name); int qdev_prop_parse(DeviceState *dev, const char *name, const char *value); void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type); void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value); +void qdev_prop_set_bool(DeviceState *dev, const char *name, bool value); void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value); void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value); void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value); -- 1.7.5.3