Patchwork [v7] qdev: Add support for property type bool

login
register
mail settings
Submitter Andreas Färber
Date Jan. 26, 2012, 3:27 p.m.
Message ID <1327591664-18442-1-git-send-email-afaerber@suse.de>
Download mbox | patch
Permalink /patch/137952/
State New
Headers show

Comments

Andreas Färber - Jan. 26, 2012, 3:27 p.m.
From: Andreas Färber <andreas.faerber@web.de>

VMState supports the type bool but qdev instead supports bit, backed by
uint32_t. Therefore let's add PROP_TYPE_BOOL and qdev_prop_set_bool().

With non-programmers in mind, instead of universal true/false provide
two different property types:
* on/off for DEFINE_PROP_SWITCH() (requested by Jan) and
* yes/no for DEFINE_PROP_BOOL() (also accepting true/false as input).

Signed-off-by: Andreas Färber <andreas.faerber@web.de>
Cc: Juan Quintela <quintela@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
---
 v6 -> v7:
 * Rename existing "boolean" type to "bit".
 * Re-introduce qdev_prop_set_bool().
 * Split "bool" into "switch" (on/off) and "boolean" (yes/no; true/false input).

 v5 -> v6:
 * Rebased onto QOM properties.
 * Parse and print true/false for bool, leave bit untouched.
 Please review, v6 untested.
 [* Accidentally dropped qdev_prop_set_bool().]

 v4 -> v5 (40P):
 * Parse on/off in addition to yes/no for both bit and bool, print yes/no for bool.

 v4 (ISA):
 * Introduced.

 hw/qdev-properties.c |  102 +++++++++++++++++++++++++++++++++++++++++++++++++-
 hw/qdev.h            |    7 +++
 2 files changed, 108 insertions(+), 1 deletions(-)
Andreas Färber - Jan. 26, 2012, 3:37 p.m.
Am 26.01.2012 16:27, schrieb Andreas Färber:
> From: Andreas Färber <andreas.faerber@web.de>
> 
> VMState supports the type bool but qdev instead supports bit, backed by
> uint32_t. Therefore let's add PROP_TYPE_BOOL and qdev_prop_set_bool().
> 
> With non-programmers in mind, instead of universal true/false provide
> two different property types:
> * on/off for DEFINE_PROP_SWITCH() (requested by Jan) and
> * yes/no for DEFINE_PROP_BOOL() (also accepting true/false as input).
> 
> Signed-off-by: Andreas Färber <andreas.faerber@web.de>
> Cc: Juan Quintela <quintela@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Jan Kiszka <jan.kiszka@siemens.com>
> Cc: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
> ---
>  v6 -> v7:
>  * Rename existing "boolean" type to "bit".
>  * Re-introduce qdev_prop_set_bool().
>  * Split "bool" into "switch" (on/off) and "boolean" (yes/no; true/false input).
> 
>  v5 -> v6:
>  * Rebased onto QOM properties.
>  * Parse and print true/false for bool, leave bit untouched.
>  Please review, v6 untested.
>  [* Accidentally dropped qdev_prop_set_bool().]
> 
>  v4 -> v5 (40P):
>  * Parse on/off in addition to yes/no for both bit and bool, print yes/no for bool.
> 
>  v4 (ISA):
>  * Introduced.
> 
>  hw/qdev-properties.c |  102 +++++++++++++++++++++++++++++++++++++++++++++++++-
>  hw/qdev.h            |    7 +++
>  2 files changed, 108 insertions(+), 1 deletions(-)

> diff --git a/hw/qdev.h b/hw/qdev.h
> index 6b58dd8..37167d4 100644
> --- a/hw/qdev.h
> +++ b/hw/qdev.h
> @@ -152,6 +152,7 @@ enum PropertyType {
>      PROP_TYPE_VLAN,
>      PROP_TYPE_PTR,
>      PROP_TYPE_BIT,
> +    PROP_TYPE_BOOL,
>  };
>  
>  struct PropertyInfo {
> @@ -276,6 +277,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;

Sorry, missing

extern PropertyInfo qdev_prop_bool_switch;

Andreas

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 02f0dae..0b1e9ea 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -86,7 +86,7 @@  static void set_bit(DeviceState *dev, Visitor *v, void *opaque,
 }
 
 PropertyInfo qdev_prop_bit = {
-    .name  = "boolean",
+    .name  = "bit",
     .legacy_name  = "on/off",
     .type  = PROP_TYPE_BIT,
     .size  = sizeof(uint32_t),
@@ -96,6 +96,101 @@  PropertyInfo qdev_prop_bit = {
     .set   = set_bit,
 };
 
+/* --- bool --- */
+
+static int parse_bool(DeviceState *dev, Property *prop, const char *str)
+{
+    bool *ptr = qdev_get_prop_ptr(dev, prop);
+    if (strcmp(str, "true") == 0 || strcmp(str, "yes") == 0) {
+        *ptr = true;
+    } else if (strcmp(str, "false") == 0 || strcmp(str, "no") == 0) {
+        *ptr = false;
+    } else {
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static int parse_bool_switch(DeviceState *dev, Property *prop,
+                             const char *str)
+{
+    bool *ptr = qdev_get_prop_ptr(dev, prop);
+    if (strcmp(str, "on") == 0) {
+        *ptr = true;
+    } else if (strcmp(str, "off") == 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");
+}
+
+static int print_bool_switch(DeviceState *dev, Property *prop,
+                             char *dest, size_t len)
+{
+    bool *ptr = qdev_get_prop_ptr(dev, prop);
+    return snprintf(dest, len, *ptr ? "on" : "off");
+}
+
+static void get_bool(DeviceState *dev, Visitor *v, void *opaque,
+                     const char *name, Error **errp)
+{
+    Property *prop = opaque;
+    bool *ptr = qdev_get_prop_ptr(dev, prop);
+
+    visit_type_bool(v, ptr, name, errp);
+}
+
+static void set_bool(DeviceState *dev, Visitor *v, void *opaque,
+                     const char *name, Error **errp)
+{
+    Property *prop = opaque;
+    bool *ptr = qdev_get_prop_ptr(dev, prop);
+    Error *local_err = NULL;
+    bool value;
+
+    if (dev->state != DEV_STATE_CREATED) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
+
+    visit_type_bool(v, &value, name, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    *ptr = value;
+}
+
+PropertyInfo qdev_prop_bool = {
+    .name = "boolean",
+    .type = PROP_TYPE_BOOL,
+    .size = sizeof(bool),
+    .parse = parse_bool,
+    .print = print_bool,
+    .get   = get_bool,
+    .set   = set_bool,
+};
+
+PropertyInfo qdev_prop_bool_switch = {
+    .name = "switch",
+    .type = PROP_TYPE_BOOL,
+    .size = sizeof(bool),
+    .parse = parse_bool_switch,
+    .print = print_bool_switch,
+    .get   = get_bool,
+    .set   = set_bool,
+};
+
 /* --- 8bit integer --- */
 
 static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
@@ -1055,6 +1150,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 6b58dd8..37167d4 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -152,6 +152,7 @@  enum PropertyType {
     PROP_TYPE_VLAN,
     PROP_TYPE_PTR,
     PROP_TYPE_BIT,
+    PROP_TYPE_BOOL,
 };
 
 struct PropertyInfo {
@@ -276,6 +277,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;
@@ -315,6 +317,10 @@  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_SWITCH(_n, _s, _f, _d)                      \
+    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_bool_switch, 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)                      \
@@ -358,6 +364,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);