From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Gibson Subject: Re: [PATCH v3 2/6] libfdt: Add iterator over properties Date: Thu, 21 Jul 2016 12:01:25 +1000 Message-ID: <20160721020125.GC31439@voom.fritz.box> References: <20160720142044.27527-1-maxime.ripard@free-electrons.com> <20160720142044.27527-3-maxime.ripard@free-electrons.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="JgQwtEuHJzHdouWu" Return-path: Content-Disposition: inline In-Reply-To: <20160720142044.27527-3-maxime.ripard-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org> Sender: devicetree-compiler-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Maxime Ripard Cc: Pantelis Antoniou , Simon Glass , Boris Brezillon , Alexander Kaplan , Thomas Petazzoni , devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, Antoine =?iso-8859-1?Q?T=E9nart?= , Stefan Agner , devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org List-Id: devicetree@vger.kernel.org --JgQwtEuHJzHdouWu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jul 20, 2016 at 04:20:40PM +0200, Maxime Ripard wrote: > Implement a macro based on fdt_first_property_offset and > fdt_next_property_offset that provides a convenience to iterate over all > the properties of a given node. >=20 > Signed-off-by: Maxime Ripard > Acked-by: Simon Glass > --- > libfdt/libfdt.h | 26 +++++++++++++ > tests/.gitignore | 1 + > tests/Makefile.tests | 1 + > tests/property_iterate.c | 97 ++++++++++++++++++++++++++++++++++++++++= ++++++ > tests/property_iterate.dts | 24 ++++++++++++ > tests/run_tests.sh | 3 ++ > 6 files changed, 152 insertions(+) > create mode 100644 tests/property_iterate.c > create mode 100644 tests/property_iterate.dts >=20 > diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h > index 70ec39c7104c..cb2f080c81d4 100644 > --- a/libfdt/libfdt.h > +++ b/libfdt/libfdt.h > @@ -457,6 +457,32 @@ int fdt_first_property_offset(const void *fdt, int n= odeoffset); > */ > int fdt_next_property_offset(const void *fdt, int offset); > =20 > +/** > + * fdt_for_each_property - iterate over all properties of a node Name here doesn't match the actual macro name. > + * @property_offset: property offset (int) Note (int, lvalue). > + * @fdt: FDT blob (const void *) > + * @node: node offset (int) > + * > + * This is actually a wrapper around a for loop and would be used like s= o: > + * > + * fdt_for_each_property(property, fdt, node) { > + * if ((property < 0) && (property !=3D -FDT_ERR_NOT_FOUND)) { > + * Error handling > + * } Error handling needs to go after the for block. > + * > + * Use property > + * ... > + * } > + * > + * Note that this is implemented as a macro and property is used as > + * iterator in the loop. The node variable can be constant or even a > + * literal. > + */ > +#define fdt_for_each_property_offset(property, fdt, node) \ > + for (property =3D fdt_first_property_offset(fdt, node); \ > + property >=3D 0; \ > + property =3D fdt_next_property_offset(fdt, property)) > + > /** > * fdt_get_property_by_offset - retrieve the property at a given offset > * @fdt: pointer to the device tree blob > diff --git a/tests/.gitignore b/tests/.gitignore > index e4532da30bf5..fa4616ba28c2 100644 > --- a/tests/.gitignore > +++ b/tests/.gitignore > @@ -40,6 +40,7 @@ tmp.* > /path_offset > /path_offset_aliases > /phandle_format > +/property_iterate > /propname_escapes > /references > /root_node > diff --git a/tests/Makefile.tests b/tests/Makefile.tests > index f7c3a4b00ead..196518c83eda 100644 > --- a/tests/Makefile.tests > +++ b/tests/Makefile.tests > @@ -23,6 +23,7 @@ LIB_TESTS_L =3D get_mem_rsv \ > add_subnode_with_nops path_offset_aliases \ > utilfdt_test \ > integer-expressions \ > + property_iterate \ > subnode_iterate > LIB_TESTS =3D $(LIB_TESTS_L:%=3D$(TESTS_PREFIX)%) > =20 > diff --git a/tests/property_iterate.c b/tests/property_iterate.c > new file mode 100644 > index 000000000000..0f3959cb8c22 > --- /dev/null > +++ b/tests/property_iterate.c > @@ -0,0 +1,97 @@ > +/* > + * libfdt - Flat Device Tree manipulation > + * Tests that fdt_next_subnode() works as expected > + * > + * Copyright (C) 2013 Google, Inc > + * > + * Copyright (C) 2007 David Gibson, IBM Corporation. > + * > + * This library is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public License > + * as published by the Free Software Foundation; either version 2.1 of > + * the License, or (at your option) any later version. > + * > + * This library is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with this library; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 = USA > + */ > + > +#include > +#include > +#include > +#include > + > +#include > + > +#include "tests.h" > +#include "testdata.h" > + > +static void test_node(void *fdt, int parent_offset) > +{ > + fdt32_t properties; > + const fdt32_t *prop; > + int offset, property; > + int count; > + int len; > + > + /* > + * This property indicates the number of properties in our > + * test node to expect > + */ > + prop =3D fdt_getprop(fdt, parent_offset, "test-properties", &len); > + if (!prop || len !=3D sizeof(fdt32_t)) { > + FAIL("Missing/invalid test-properties property at '%s'", > + fdt_get_name(fdt, parent_offset, NULL)); > + } > + properties =3D cpu_to_fdt32(*prop); > + > + count =3D 0; > + offset =3D fdt_first_subnode(fdt, parent_offset); > + if (offset < 0) > + FAIL("Missing test node\n"); > + > + fdt_for_each_property_offset(property, fdt, offset) > + count++; > + > + if (count !=3D properties) { > + FAIL("Node '%s': Expected %d properties, got %d\n", > + fdt_get_name(fdt, parent_offset, NULL), properties, > + count); > + } > +} > + > +static void check_fdt_next_subnode(void *fdt) > +{ > + int offset; > + int count =3D 0; > + > + fdt_for_each_subnode(offset, fdt, 0) { > + test_node(fdt, offset); > + count++; > + } > + > + if (count !=3D 2) > + FAIL("Expected %d tests, got %d\n", 2, count); > +} > + > +int main(int argc, char *argv[]) > +{ > + void *fdt; > + > + test_init(argc, argv); > + if (argc !=3D 2) > + CONFIG("Usage: %s ", argv[0]); > + > + fdt =3D load_blob(argv[1]); > + if (!fdt) > + FAIL("No device tree available"); > + > + check_fdt_next_subnode(fdt); > + > + PASS(); > +} > diff --git a/tests/property_iterate.dts b/tests/property_iterate.dts > new file mode 100644 > index 000000000000..2ed677e7e6ea > --- /dev/null > +++ b/tests/property_iterate.dts > @@ -0,0 +1,24 @@ > +/dts-v1/; > + > +/ { > + #address-cells =3D <1>; > + #size-cells =3D <0>; > + > + test1 { > + test-properties =3D <3>; > + > + test { > + linux,phandle =3D <0x1>; > + #address-cells =3D <1>; > + #size-cells =3D <0>; > + }; > + }; > + > + test2 { > + test-properties =3D <0>; > + > + test { > + }; > + }; > +}; > + > diff --git a/tests/run_tests.sh b/tests/run_tests.sh > index 7eb9b3d33108..6a2662b2abaf 100755 > --- a/tests/run_tests.sh > +++ b/tests/run_tests.sh > @@ -269,6 +269,9 @@ libfdt_tests () { > run_dtc_test -I dts -O dtb -o subnode_iterate.dtb subnode_iterate.dts > run_test subnode_iterate subnode_iterate.dtb > =20 > + run_dtc_test -I dts -O dtb -o property_iterate.dtb property_iterate.= dts > + run_test property_iterate property_iterate.dtb > + > # Tests for behaviour on various sorts of corrupted trees > run_test truncated_property > =20 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --JgQwtEuHJzHdouWu Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJXkCz1AAoJEGw4ysog2bOSV1gQAL+1r92MBYb0OZK05JTSElgt UG1QFFQLICY/jyC02F6IBPftfkoOiCpvlpunoX33WdkhhBF7tARpgDux8nkAGYcM vWcCZz9U4sUhnv/EXsMGvl5o0sA8TLfNU2f0/aNd4lVIrwtKEwXCtRn29yCJDWW0 udyVy5FFyajsHXkqOlvlnlDiIP2ioBptd0jaqnHfW9n23rxtN4cHV+/V9dafvfyo IwdThb7HfOJtymF8fx3euLDc5vJ41U0fe9943D+Yo3NFLXzyLqdEJKsGDD1JZQK4 bsf9xERtE404xjHOIkRyqgcEO32rHPW92nb1rp1cwG0H4wjz1q/JE+hiUa34gzWb 5OiTKLfh+adt2wm400r9msvJn/aYwLiJTGNgG/yFYizL9E45EHQ8mgP6rsuURV9C 83cQFEGcDugpFWjd63T7vqnWQ2tmztsCe6oCfQyJChqWqImrVHYxSyceebCvI6xw 4cOO+A6mSh9MIFVuSg7U7FlMRgYsATvM+yhskeIyifUXY7qdthMoq3YG6fx9doIw eXoQI7AjhWH9Kj6hrqqQRcRFhXAndiNrb/Rb8dBu90XZUE1Q0zDFrwmtz6tRz6BI 9k/HzfFTCLcOXqpv88cjbaVtn9RH9J6v236f6b4To9rAOA2b3B1SOtn0WKN0AxtC Q0EuLY4Pk/Qx1DlVcuV/ =3MOq -----END PGP SIGNATURE----- --JgQwtEuHJzHdouWu--