Skip to contentSkip to conversation

pylibfdt: Support boolean properties

PATCH Applied Simon Glass 2 participants 2 messages Sep 12, 2023 – Sep 15, 2023 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pylibfdt: Support boolean properties

Boolean properties are unusual in that their presense or absence indicates the value of the property. This makes them a little painful to support using the existing getprop() support.

Add new methods to deal with booleans specifically.

---

Changes in v2:
- Drop debug-related Makefile change
- Drop duplicate setprop_u32() test
- Add a separate test for hasprop()
- Drop checking of no-space since testSetProp() already has that

 pylibfdt/libfdt.i       | 55 +++++++++++++++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 33 +++++++++++++++++++++++++
 tests/test_props.dts    |  1 +
 3 files changed, 89 insertions(+)
pylibfdt/libfdt.i +55 -0
index 2361e22..0b50983 100644
@@ -419,6 +419,35 @@ class FdtRo(object):
             return pdata
         return Property(prop_name, bytearray(pdata[0]))
 
+    def hasprop(self, nodeoffset, prop_name, quiet=()):
+        """Check if a node has a property
+
+        This can be used to check boolean properties
+
+        Args:
+            nodeoffset: Node offset containing property to check
+            prop_name: Name of property to check
+            quiet: Errors to ignore (empty to raise on all errors). Note that
+                NOTFOUND is added internally by this function so need not be
+                provided
+
+        Returns:
+            True if the property exists in the node, else False. If an error
+                other than -NOTFOUND is returned by fdt_getprop() then the error
+                is return (-ve integer)
+
+        Raises:
+            FdtError if any error occurs other than NOTFOUND (e.g. the
+                nodeoffset is invalid)
+        """
+        pdata = check_err_null(fdt_getprop(self._fdt, nodeoffset, prop_name),
+                               quiet + (NOTFOUND,))
+        if isinstance(pdata, (int)):
+            if pdata == -NOTFOUND:
+                return False
+            return pdata
+        return True
+
     def get_phandle(self, nodeoffset):
         """Get the phandle of a node
 
@@ -605,6 +634,32 @@ class Fdt(FdtRo):
         return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name, val,
                                      len(val)), quiet)
 
+    def setprop_bool(self, nodeoffset, prop_name, val, quiet=()):
+        """Set the boolean value of a property
+
+        Either:
+            adds the property if not already present; or
+            deletes the property if present
+
+        Args:
+            nodeoffset: Node offset containing the property to create/delete
+            prop_name: Name of property
+            val: Boolean value to write (i.e. True or False)
+            quiet: Errors to ignore (empty to raise on all errors)
+
+        Returns:
+            Error code, or 0 if OK
+
+        Raises:
+            FdtException if no parent found or other error occurs
+        """
+        exists = self.hasprop(nodeoffset, prop_name, quiet)
+        if val != exists:
+            if val:
+                return self.setprop(nodeoffset, prop_name, b'', quiet=quiet)
+            else:
+                return self.delprop(nodeoffset, prop_name, quiet=quiet)
+
     def setprop_u32(self, nodeoffset, prop_name, val, quiet=()):
         """Set the value of a property
 
tests/pylibfdt_tests.py +33 -0
index 34c2764..a4f73ed 100644
@@ -496,6 +496,39 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual(TEST_STRING_3,
                           self.fdt.getprop(node, prop).as_str())
 
+    def testHasProp(self):
+        """Test that we can check if a node has a property"""
+        node = 0
+        self.assertFalse(self.fdt2.hasprop(node, 'missing'))
+        self.assertTrue(self.fdt2.hasprop(node, 'prop-bool'))
+
+        # Test a property with a non-empty value
+        self.assertTrue(self.fdt2.hasprop(node, 'prop-uint64'))
+
+    def testSetPropBool(self):
+        """Test that we can update and create boolean properties"""
+        node = 0
+        prop = 'prop-bool'
+
+        # Make some space and then try setting a new boolean property
+        self.fdt.resize(self.fdt.totalsize() + 50)
+        self.fdt.hasprop(node, 'missing')
+        self.fdt.setprop_bool(node, 'missing', True)
+        self.assertTrue(self.fdt.hasprop(node, 'missing'))
+
+        # Trying toggling an existing boolean property. Do each operation twice
+        # to make sure that the behaviour is correct when setting the property
+        # to the same value.
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, False)
+        self.assertFalse(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, False)
+        self.assertFalse(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, True)
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, True)
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+
     def testSetName(self):
         """Test that we can update a node name"""
         node = self.fdt.path_offset('/subnode@1')
tests/test_props.dts +1 -0
index 5089023..09be197 100644
@@ -12,4 +12,5 @@
 	prop-uint32-array = <0x1>, <0x98765432>, <0xdeadbeef>;
 	prop-int64-array = /bits/ 64 <0x100000000 0xfffffffffffffffe>;
 	prop-uint64-array = /bits/ 64 <0x100000000 0x1>;
+	prop-bool;
 };
Signature · 1 line
2.42.0.283.g2d96d420d3-goog

A message that is not in the archive (sent off-list or not yet received): 20230912182716.248253-1-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org

Applied
#2 lore raw
Quoted 7 lines · Simon Glass

Boolean properties are unusual in that their presense or absence indicates the value of the property. This makes them a little painful to support using the existing getprop() support.

Add new methods to deal with booleans specifically.

Merged, thanks.

Quoted 140 lines

---

Changes in v2:
- Drop debug-related Makefile change
- Drop duplicate setprop_u32() test
- Add a separate test for hasprop()
- Drop checking of no-space since testSetProp() already has that

 pylibfdt/libfdt.i       | 55 +++++++++++++++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 33 +++++++++++++++++++++++++
 tests/test_props.dts    |  1 +
 3 files changed, 89 insertions(+)
pylibfdt/libfdt.i +55 -0
index 2361e22..0b50983 100644
@@ -419,6 +419,35 @@ class FdtRo(object):
             return pdata
         return Property(prop_name, bytearray(pdata[0]))
 
+    def hasprop(self, nodeoffset, prop_name, quiet=()):
+        """Check if a node has a property
+
+        This can be used to check boolean properties
+
+        Args:
+            nodeoffset: Node offset containing property to check
+            prop_name: Name of property to check
+            quiet: Errors to ignore (empty to raise on all errors). Note that
+                NOTFOUND is added internally by this function so need not be
+                provided
+
+        Returns:
+            True if the property exists in the node, else False. If an error
+                other than -NOTFOUND is returned by fdt_getprop() then the error
+                is return (-ve integer)
+
+        Raises:
+            FdtError if any error occurs other than NOTFOUND (e.g. the
+                nodeoffset is invalid)
+        """
+        pdata = check_err_null(fdt_getprop(self._fdt, nodeoffset, prop_name),
+                               quiet + (NOTFOUND,))
+        if isinstance(pdata, (int)):
+            if pdata == -NOTFOUND:
+                return False
+            return pdata
+        return True
+
     def get_phandle(self, nodeoffset):
         """Get the phandle of a node
 
@@ -605,6 +634,32 @@ class Fdt(FdtRo):
         return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name, val,
                                      len(val)), quiet)
 
+    def setprop_bool(self, nodeoffset, prop_name, val, quiet=()):
+        """Set the boolean value of a property
+
+        Either:
+            adds the property if not already present; or
+            deletes the property if present
+
+        Args:
+            nodeoffset: Node offset containing the property to create/delete
+            prop_name: Name of property
+            val: Boolean value to write (i.e. True or False)
+            quiet: Errors to ignore (empty to raise on all errors)
+
+        Returns:
+            Error code, or 0 if OK
+
+        Raises:
+            FdtException if no parent found or other error occurs
+        """
+        exists = self.hasprop(nodeoffset, prop_name, quiet)
+        if val != exists:
+            if val:
+                return self.setprop(nodeoffset, prop_name, b'', quiet=quiet)
+            else:
+                return self.delprop(nodeoffset, prop_name, quiet=quiet)
+
     def setprop_u32(self, nodeoffset, prop_name, val, quiet=()):
         """Set the value of a property
 
tests/pylibfdt_tests.py +33 -0
index 34c2764..a4f73ed 100644
@@ -496,6 +496,39 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual(TEST_STRING_3,
                           self.fdt.getprop(node, prop).as_str())
 
+    def testHasProp(self):
+        """Test that we can check if a node has a property"""
+        node = 0
+        self.assertFalse(self.fdt2.hasprop(node, 'missing'))
+        self.assertTrue(self.fdt2.hasprop(node, 'prop-bool'))
+
+        # Test a property with a non-empty value
+        self.assertTrue(self.fdt2.hasprop(node, 'prop-uint64'))
+
+    def testSetPropBool(self):
+        """Test that we can update and create boolean properties"""
+        node = 0
+        prop = 'prop-bool'
+
+        # Make some space and then try setting a new boolean property
+        self.fdt.resize(self.fdt.totalsize() + 50)
+        self.fdt.hasprop(node, 'missing')
+        self.fdt.setprop_bool(node, 'missing', True)
+        self.assertTrue(self.fdt.hasprop(node, 'missing'))
+
+        # Trying toggling an existing boolean property. Do each operation twice
+        # to make sure that the behaviour is correct when setting the property
+        # to the same value.
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, False)
+        self.assertFalse(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, False)
+        self.assertFalse(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, True)
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+        self.fdt2.setprop_bool(node, prop, True)
+        self.assertTrue(self.fdt2.hasprop(node, prop))
+
     def testSetName(self):
         """Test that we can update a node name"""
         node = self.fdt.path_offset('/subnode@1')
tests/test_props.dts +1 -0
index 5089023..09be197 100644
@@ -12,4 +12,5 @@
 	prop-uint32-array = <0x1>, <0x98765432>, <0xdeadbeef>;
 	prop-int64-array = /bits/ 64 <0x100000000 0xfffffffffffffffe>;
 	prop-uint64-array = /bits/ 64 <0x100000000 0x1>;
+	prop-bool;
 };
Signature · 4 lines
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