Re: [PATCH v4 2/2] pylibfdt: Allow reading integer values from properties

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Wed, Jun 06, 2018 at 01:58:13PM -0600, Simon Glass wrote:
> Extend the Properties class with some functions to read a single integer
> property. Add a new getprop_obj() function to return a Property object
> instead of the raw data.
> 
> This suggested approach can be extended to handle other types, as well as
> arrays.
> 
> Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx>
> ---
> 
> Changes in v4:
> - Make the as_...() functions non-members of Property

Sorry to flipflop on you, but your messages elsewhere convinced me
your original approach of having those as members of Property was
preferable after all.  In the long run, having both options might
actually be desirable, but I think go with members of Property first,
and see if manually using struct is good enough for the other case.

> - Drop getprop() and rename getprop_obj() to getprop()
> 
> Changes in v3:
> - Rename the functions with an 'as_' prefix instead of 'to_'
> 
>  pylibfdt/libfdt.i       | 25 ++++++++++++++++++++++---
>  tests/pylibfdt_tests.py | 13 +++++++++++++
>  tests/run_tests.sh      |  1 +
>  tests/test_props.dts    | 11 +++++++++++
>  4 files changed, 47 insertions(+), 3 deletions(-)
>  create mode 100644 tests/test_props.dts
> 
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index 170005f..0c13c5f 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -153,6 +153,24 @@ def check_err_null(val, quiet=()):
>              raise FdtException(val)
>      return val
>  
> +# Functions which convert a bytestring (as obtained by Fdt.getprop(), for
> +# example. to various supported values
> +def as_cell(bytes, fmt):
> +    return struct.unpack('>' + fmt, bytes)[0]
> +
> +def as_uint32(bytes):
> +        return as_cell(bytes, 'L')
> +
> +def as_int32(bytes):
> +    return as_cell(bytes, 'l')
> +
> +def as_uint64(bytes):
> +    return as_cell(bytes, 'Q')
> +
> +def as_int64(bytes):
> +    return as_cell(bytes, 'q')
> +
> +
>  class Fdt:
>      """Device tree class, supporting all operations
>  
> @@ -365,7 +383,9 @@ class Fdt:
>              quiet: Errors to ignore (empty to raise on all errors)
>  
>          Returns:
> -            Value of property as a string of bytes, or -ve error number
> +            Value of property as a Property object (which can be used as a
> +               bytearray/string), or -ve error number. On failure, returns an
> +               integer error
>  
>          Raises:
>              FdtError if any error occurs (e.g. the property is not found)
> @@ -374,8 +394,7 @@ class Fdt:
>                                 quiet)
>          if isinstance(pdata, (int)):
>              return pdata
> -        # Use bytes() rather than string(). This works on both Python 2 and 3
> -        return bytes(pdata[0])
> +        return Property(prop_name, bytearray(pdata[0]))
>  
>      def get_phandle(self, nodeoffset):
>          """Get the phandle of a node
> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> index 343a1e4..c8c6da6 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -89,6 +89,7 @@ class PyLibfdtTests(unittest.TestCase):
>      def setUp(self):
>          """Read in the device tree we use for testing"""
>          self.fdt = _ReadFdt('test_tree1.dtb')
> +        self.fdt2 = _ReadFdt('test_props.dtb')
>  
>      def GetPropList(self, node_path):
>          """Read a list of properties from a node
> @@ -330,6 +331,18 @@ class PyLibfdtTests(unittest.TestCase):
>          node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
>          self.assertEquals(node2, self.fdt.node_offset_by_phandle(0x2001))
>  
> +    def get_prop(self, name):
> +        return self.fdt2.getprop(0, name)
> +
> +    def testGetIntProperties(self):
> +        """Test that we can access properties as integers"""
> +        self.assertEquals(0xdeadbeef,
> +                          libfdt.as_uint32(self.get_prop("prop-hex32")))
> +        self.assertEquals(123, libfdt.as_uint32(self.get_prop("prop-uint32")))
> +        self.assertEquals(-2, libfdt.as_int32(self.get_prop("prop-int32")))
> +        self.assertEquals(9223372036854775807,
> +                          libfdt.as_uint64(self.get_prop("prop-uint64")))
> +        self.assertEquals(-2, libfdt.as_int64(self.get_prop("prop-int64")))
>  
>  if __name__ == "__main__":
>      unittest.main()
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index 28e528f..21b633e 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -888,6 +888,7 @@ fdtoverlay_tests() {
>  }
>  
>  pylibfdt_tests () {
> +    run_dtc_test -I dts -O dtb -o test_props.dtb test_props.dts
>      TMP=/tmp/tests.stderr.$$
>      python pylibfdt_tests.py -v 2> $TMP
>  
> diff --git a/tests/test_props.dts b/tests/test_props.dts
> new file mode 100644
> index 0000000..7e59bd1
> --- /dev/null
> +++ b/tests/test_props.dts
> @@ -0,0 +1,11 @@
> +/dts-v1/;
> +
> +/ {
> +	compatible = "test_props";
> +	prop-hex32 = <0xdeadbeef>;
> +	prop-uint32 = <123>;
> +	prop-int32 = <0xfffffffe>;
> +	prop-hex64 = /bits/ 64 <0xdeadbeef01abcdef>;
> +	prop-uint64 = /bits/ 64 <9223372036854775807>;
> +	prop-int64 = /bits/ 64 <0xfffffffffffffffe>;
> +};

-- 
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

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux