[PATCH i-g-t 2/5] lib/igt_sysfs: Add igt_sysfs_{get,set}_s32
Francois Dugast
francois.dugast at intel.com
Fri Nov 8 15:59:30 UTC 2024
Add this family of functions to read and write signed values in sysfs.
Signed-off-by: Francois Dugast <francois.dugast at intel.com>
Reviewed-by: Kamil Konieczny <kamil.konieczny at linux.intel.com>
---
lib/igt_sysfs.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_sysfs.h | 5 ++++
2 files changed, 76 insertions(+)
diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index 26a3aa31f..00d5822fd 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -873,6 +873,77 @@ void igt_sysfs_set_u32(int dir, const char *attr, uint32_t value)
"Failed to write %u to %s attribute (%s)\n", value, attr, strerror(errno));
}
+/**
+ * __igt_sysfs_get_s32:
+ * @dir: directory corresponding to attribute
+ * @attr: name of the sysfs node to read
+ * @value: pointer for storing read value
+ *
+ * Convenience wrapper to read a signed 32bit integer from a sysfs file.
+ *
+ * Returns:
+ * True if value successfully read, false otherwise.
+ */
+bool __igt_sysfs_get_s32(int dir, const char *attr, int32_t *value)
+{
+ if (igt_debug_on(igt_sysfs_scanf(dir, attr, "%d", value) != 1))
+ return false;
+
+ return true;
+}
+
+/**
+ * igt_sysfs_get_s32:
+ * @dir: directory corresponding to attribute
+ * @attr: name of the sysfs node to read
+ *
+ * Convenience wrapper to read a signed 32bit integer from a sysfs file.
+ * It asserts on failure.
+ *
+ * Returns:
+ * Read value.
+ */
+int32_t igt_sysfs_get_s32(int dir, const char *attr)
+{
+ int32_t value;
+
+ igt_assert_f(__igt_sysfs_get_s32(dir, attr, &value),
+ "Failed to read %s attribute (%s)\n", attr, strerror(errno));
+
+ return value;
+}
+
+/**
+ * __igt_sysfs_set_s32:
+ * @dir: directory corresponding to attribute
+ * @attr: name of the sysfs node to write
+ * @value: value to set
+ *
+ * Convenience wrapper to write a signed 32bit integer to a sysfs file.
+ *
+ * Returns:
+ * True if successfully written, false otherwise.
+ */
+bool __igt_sysfs_set_s32(int dir, const char *attr, int32_t value)
+{
+ return igt_sysfs_printf(dir, attr, "%d", value) > 0;
+}
+
+/**
+ * igt_sysfs_set_s32:
+ * @dir: directory corresponding to attribute
+ * @attr: name of the sysfs node to write
+ * @value: value to set
+ *
+ * Convenience wrapper to write a signed 32bit integer to a sysfs file.
+ * It asserts on failure.
+ */
+void igt_sysfs_set_s32(int dir, const char *attr, int32_t value)
+{
+ igt_assert_f(__igt_sysfs_set_s32(dir, attr, value),
+ "Failed to write %d to %s attribute (%s)\n", value, attr, strerror(errno));
+}
+
/**
* __igt_sysfs_get_u64:
* @dir: directory corresponding to attribute
diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h
index 852b95053..54a408791 100644
--- a/lib/igt_sysfs.h
+++ b/lib/igt_sysfs.h
@@ -125,6 +125,11 @@ uint32_t igt_sysfs_get_u32(int dir, const char *attr);
bool __igt_sysfs_set_u32(int dir, const char *attr, uint32_t value);
void igt_sysfs_set_u32(int dir, const char *attr, uint32_t value);
+bool __igt_sysfs_get_s32(int dir, const char *attr, int32_t *value);
+int32_t igt_sysfs_get_s32(int dir, const char *attr);
+bool __igt_sysfs_set_s32(int dir, const char *attr, int32_t value);
+void igt_sysfs_set_s32(int dir, const char *attr, int32_t value);
+
bool __igt_sysfs_get_u64(int dir, const char *attr, uint64_t *value);
uint64_t igt_sysfs_get_u64(int dir, const char *attr);
bool __igt_sysfs_set_u64(int dir, const char *attr, uint64_t value);
--
2.43.0
More information about the igt-dev
mailing list