diff mbox series

[libgpiod,3/7] tools: Add value support to line name lookup

Message ID 20220203042134.68425-4-joel@jms.id.au
State New
Headers show
Series tools: Add by-name support | expand

Commit Message

Joel Stanley Feb. 3, 2022, 4:21 a.m. UTC
Add support for pasring the values as well as the name in
line_names_to_offsets.

Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 tools/tools-common.c | 51 ++++++++++++++++++++++++++++++++++++++++++--
 tools/tools-common.h |  4 +++-
 2 files changed, 52 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/tools/tools-common.c b/tools/tools-common.c
index 958933ed6d51..586577566790 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -204,15 +204,57 @@  struct gpiod_chip *chip_by_line_name(const char *name)
 	return NULL;
 }
 
+char *split_line(const char *line_pair)
+{
+	char *name_end;
+	size_t name_len;
+	char *line_name;
+
+	name_end = strchr(line_pair, '=');
+	if (!name_end)
+		die("invalid name/value '%s'", line_pair);
+
+	name_len = name_end - line_pair;
+
+	if (name_len > 32)
+		die("line name exceeds maximum length");
+
+	line_name = calloc(1, name_len + 1);
+	strncpy(line_name, line_pair, name_len);
+
+	return line_name;
+}
+
 int line_names_to_offsets(struct gpiod_chip *chip, char **lines,
-			  unsigned int *offsets, int num_lines)
+			  unsigned int *offsets,
+			  int *values,
+			  int num_lines)
 {
 	int i;
 
 	for (i = 0; i < num_lines; i++) {
-		const char *line_name = lines[i];
+		char *line_name;
+		int value;
 		int offset;
 
+		if (values) {
+			const char *line_pair = lines[i];
+			char *name_end;
+			int rv;
+
+			line_name = split_line(line_pair);
+			name_end = strchr(line_pair, '=');
+
+			rv = sscanf(name_end, "=%d", &value);
+			if (rv != 1)
+				die("invalid offset<->value mapping: %s", line_pair);
+
+			if (value != 0 && value != 1)
+				die("value must be 0 or 1: %s", line_pair);
+		} else {
+			line_name = lines[i];
+		}
+
 		offset = gpiod_chip_find_line(chip, line_name);
 
 		if (offset < 0) {
@@ -222,6 +264,11 @@  int line_names_to_offsets(struct gpiod_chip *chip, char **lines,
 		}
 
 		offsets[i] = offset;
+
+		if (values) {
+			values[i] = value;
+			free(line_name);
+		}
 	}
 
 	return 0;
diff --git a/tools/tools-common.h b/tools/tools-common.h
index 7affea436a60..723999011733 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -33,6 +33,8 @@  struct gpiod_chip *chip_open_by_name(const char *name);
 struct gpiod_chip *chip_open_lookup(const char *device);
 struct gpiod_chip *chip_by_line_name(const char *name);
 int line_names_to_offsets(struct gpiod_chip *chip, char **lines,
-			  unsigned int *offsets, int num_lines);
+			  unsigned int *offsets, int *values,
+			  int num_lines);
+char *split_line(const char *line_pair);
 
 #endif /* __GPIOD_TOOLS_COMMON_H__ */