From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:48978) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rqjfd-0006dJ-8S for qemu-devel@nongnu.org; Fri, 27 Jan 2012 06:08:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rqjfa-0002ZE-7y for qemu-devel@nongnu.org; Fri, 27 Jan 2012 06:08:41 -0500 Received: from cantor2.suse.de ([195.135.220.15]:54795 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqjfZ-0002Yy-TD for qemu-devel@nongnu.org; Fri, 27 Jan 2012 06:08:38 -0500 Message-ID: <4F22852E.6070703@suse.de> Date: Fri, 27 Jan 2012 12:06:22 +0100 From: =?UTF-8?B?QW5kcmVhcyBGw6RyYmVy?= MIME-Version: 1.0 References: <1327593288-22521-1-git-send-email-afaerber@suse.de> In-Reply-To: <1327593288-22521-1-git-send-email-afaerber@suse.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v8] qdev: Add support for property type bool List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Vasilis Liaskovitis , Anthony Liguori , Juan Quintela , Jan Kiszka , Markus Armbruster , Blue Swirl , Amit Shah , Paolo Bonzini Am 26.01.2012 16:54, schrieb Andreas F=C3=A4rber: > From: Andreas F=C3=A4rber >=20 > 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(). >=20 > 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). >=20 > Signed-off-by: Andreas F=C3=A4rber > Cc: Juan Quintela > Cc: Markus Armbruster > Cc: Jan Kiszka > Cc: Vasilis Liaskovitis > --- > v7 -> v8: > * Add missing extern qdev_prop_bool_switch for DEFINE_PROP_SWITCH(). >=20 > 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/fals= e input). >=20 > 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().] >=20 > v4 -> v5 (40P): > * Parse on/off in addition to yes/no for both bit and bool, print yes/= no for bool. >=20 > v4 (ISA): > * Introduced. > hw/qdev-properties.c | 102 ++++++++++++++++++++++++++++++++++++++++++= +++++++- > hw/qdev.h | 8 ++++ > 2 files changed, 109 insertions(+), 1 deletions(-) >=20 > 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, voi= d *opaque, > } > =20 > PropertyInfo qdev_prop_bit =3D { > - .name =3D "boolean", > + .name =3D "bit", > .legacy_name =3D "on/off", > .type =3D PROP_TYPE_BIT, > .size =3D sizeof(uint32_t), > @@ -96,6 +96,101 @@ PropertyInfo qdev_prop_bit =3D { > .set =3D set_bit, > }; > =20 > +/* --- bool --- */ > + > +static int parse_bool(DeviceState *dev, Property *prop, const char *st= r) > +{ > + bool *ptr =3D qdev_get_prop_ptr(dev, prop); > + if (strcmp(str, "true") =3D=3D 0 || strcmp(str, "yes") =3D=3D 0) { > + *ptr =3D true; > + } else if (strcmp(str, "false") =3D=3D 0 || strcmp(str, "no") =3D=3D= 0) { I just noticed that Jan's patch does strncasecmp() for bit, so if we want those semantics here too (foo=3DTRUE,bar=3DFalse,foobar=3DyES) I can= send a v9. I really hate replying to myself though, so I'll wait a bit for other review comments to avoid a further surge in patch revisions. Andreas > + *ptr =3D false; > + } else { > + return -EINVAL; > + } > + > + return 0; > +} > + > +static int parse_bool_switch(DeviceState *dev, Property *prop, > + const char *str) > +{ > + bool *ptr =3D qdev_get_prop_ptr(dev, prop); > + if (strcmp(str, "on") =3D=3D 0) { > + *ptr =3D true; > + } else if (strcmp(str, "off") =3D=3D 0) { > + *ptr =3D false; > + } else { > + return -EINVAL; > + } > + > + return 0; > +} > + > +static int print_bool(DeviceState *dev, Property *prop, > + char *dest, size_t len) > +{ > + bool *ptr =3D 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 =3D 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 =3D opaque; > + bool *ptr =3D 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 =3D opaque; > + bool *ptr =3D qdev_get_prop_ptr(dev, prop); > + Error *local_err =3D NULL; > + bool value; > + > + if (dev->state !=3D 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 =3D value; > +} > + > +PropertyInfo qdev_prop_bool =3D { > + .name =3D "boolean", > + .type =3D PROP_TYPE_BOOL, > + .size =3D sizeof(bool), > + .parse =3D parse_bool, > + .print =3D print_bool, > + .get =3D get_bool, > + .set =3D set_bool, > +}; > + > +PropertyInfo qdev_prop_bool_switch =3D { > + .name =3D "switch", > + .type =3D PROP_TYPE_BOOL, > + .size =3D sizeof(bool), > + .parse =3D parse_bool_switch, > + .print =3D print_bool_switch, > + .get =3D get_bool, > + .set =3D set_bool, > +}; > + > /* --- 8bit integer --- */ > =20 > static int parse_uint8(DeviceState *dev, Property *prop, const char *s= tr) > @@ -1055,6 +1150,11 @@ void qdev_prop_set_bit(DeviceState *dev, const c= har *name, bool value) > qdev_prop_set(dev, name, &value, PROP_TYPE_BIT); > } > =20 > +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 v= alue) > { > qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8); > diff --git a/hw/qdev.h b/hw/qdev.h > index 6b58dd8..cef20f6 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, > }; > =20 > struct PropertyInfo { > @@ -276,6 +277,8 @@ int do_device_del(Monitor *mon, const QDict *qdict,= QObject **ret_data); > /*** qdev-properties.c ***/ > =20 > extern PropertyInfo qdev_prop_bit; > +extern PropertyInfo qdev_prop_bool; > +extern PropertyInfo qdev_prop_bool_switch; > extern PropertyInfo qdev_prop_uint8; > extern PropertyInfo qdev_prop_uint16; > extern PropertyInfo qdev_prop_uint32; > @@ -315,6 +318,10 @@ extern PropertyInfo qdev_prop_pci_devfn; > .defval =3D (bool[]) { (_defval) }, \ > } > =20 > +#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 +365,7 @@ int qdev_prop_exists(DeviceState *dev, const char *= name); > int qdev_prop_parse(DeviceState *dev, const char *name, const char *va= lue); > 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 v= alue); > 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); --=20 SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 N=C3=BCrnberg, Germany GF: Jeff Hawn, Jennifer Guild, Felix Imend=C3=B6rffer; HRB 16746 AG N=C3=BC= rnberg