[PATCH 07/10] pylibfdt: Add functions to set and get properties as strings

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



It is common to want to set a property to a nul-terminated string in a
device tree. Add python methods to handle this.

Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx>
---

 pylibfdt/libfdt.i       | 24 ++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 11 +++++++++++
 2 files changed, 35 insertions(+)

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index 75868b8..5ecddba 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -599,6 +599,27 @@ class Fdt:
         return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
                          quiet)
 
+    def setprop_str(self, nodeoffset, prop_name, val, quiet=()):
+        """Set the string value of a property
+
+        The property is set to the string, with a nul terminator added
+
+        Args:
+            nodeoffset: Node offset containing the property to create/update
+            prop_name: Name of property
+            val: Value to write (string without nul terminator)
+            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
+        """
+        val += '\0'
+        return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
+                                     val, len(val)), quiet)
+
     def delprop(self, nodeoffset, prop_name):
         """Delete a property from a node
 
@@ -656,6 +677,9 @@ class Property(bytearray):
 
     def as_int64(self):
         return self.as_cell('q')
+
+    def as_str(self):
+        return self[:-1]
 %}
 
 %rename(fdt_property) fdt_property_func;
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index f75579f..497124b 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -68,6 +68,7 @@ TEST_VALUE64_1L = 0x01abcdef
 TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
 
 TEST_STRING_1 = 'hello world'
+TEST_STRING_2 = 'hi world'
 
 
 def get_err(err_code):
@@ -440,6 +441,16 @@ class PyLibfdtTests(unittest.TestCase):
         self.assertEquals(struct.pack('>Q', TEST_VALUE64_1),
                           self.fdt.getprop(node, prop))
 
+    def testSetPropStr(self):
+        """Test that we can set a property to a particular string"""
+        node = 0
+        prop = 'prop-str'
+        self.assertEquals(TEST_STRING_1,
+                          self.fdt.getprop_obj(node, prop).as_str())
+        self.fdt.setprop_str(node, prop, TEST_STRING_2)
+        self.assertEquals(TEST_STRING_2,
+                          self.fdt.getprop_obj(node, prop).as_str())
+
 
 if __name__ == "__main__":
     unittest.main()
-- 
2.17.0.441.gb46fe60e1d-goog

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[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