From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
To: Linus Walleij <linus.walleij@linaro.org>,
Kent Gibson <warthog618@gmail.com>,
Vincent Fazio <vfazio@xes-inc.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: linux-gpio@vger.kernel.org, brgl@kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH RFC libgpiod 2/3] tools: factor out line event formatting into tools-common
Date: Fri, 16 Oct 2026 16:18:09 +0200 [thread overview]
Message-ID: <20261016-gpioctl-v1-2-8770622679b8@oss.qualcomm.com> (raw)
In-Reply-To: <20261016-gpioctl-v1-0-8770622679b8@oss.qualcomm.com>
Move helpers used by gpiomon for formatting of line events and printing
of the help text for format specifiers to tools-common so that both
gpiomon and the upcoming gpioctl can reuse the same code.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
tools/gpiomon.c | 128 +++++++--------------------------------------------
tools/tools-common.c | 100 ++++++++++++++++++++++++++++++++++++++++
tools/tools-common.h | 4 ++
3 files changed, 120 insertions(+), 112 deletions(-)
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index 2b9a69b8aea60d62d42b34dae510ac0a83e0f013..79006ed06e64f1a7fa0afc8e14904b915cf9f695 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -79,16 +79,7 @@ static void print_help(void)
printf(" -v, --version\t\toutput version information and exit\n");
print_chip_help();
print_period_help();
- printf("\n");
- printf("Format specifiers:\n");
- printf(" %%o GPIO line offset\n");
- printf(" %%l GPIO line name\n");
- printf(" %%c GPIO chip name\n");
- printf(" %%e numeric edge event type ('1' - rising or '2' - falling)\n");
- printf(" %%E edge event type ('rising' or 'falling')\n");
- printf(" %%S event timestamp as seconds\n");
- printf(" %%U event timestamp as UTC\n");
- printf(" %%L event timestamp as local time\n");
+ print_event_format_help(true);
}
static int parse_edges_or_die(const char *option)
@@ -254,115 +245,28 @@ static void print_banner(int num_lines, char **lines)
}
}
-static void event_print_formatted(struct gpiod_edge_event *event,
- struct line_resolver *resolver, int chip_num,
- struct config *cfg)
-{
- const char *lname, *prev, *curr;
- unsigned int offset;
- uint64_t evtime;
- int evtype;
- char fmt;
-
- offset = gpiod_edge_event_get_line_offset(event);
- evtime = gpiod_edge_event_get_timestamp_ns(event);
- evtype = gpiod_edge_event_get_event_type(event);
-
- for (prev = curr = cfg->fmt;;) {
- curr = strchr(curr, '%');
- if (!curr) {
- fputs(prev, stdout);
- break;
- }
-
- if (prev != curr)
- fwrite(prev, curr - prev, 1, stdout);
-
- fmt = *(curr + 1);
-
- switch (fmt) {
- case 'c':
- fputs(get_chip_name(resolver, chip_num), stdout);
- break;
- case 'e':
- printf("%d", evtype);
- break;
- case 'E':
- if (evtype == GPIOD_EDGE_EVENT_RISING_EDGE)
- fputs("rising", stdout);
- else
- fputs("falling", stdout);
- break;
- case 'l':
- lname = get_line_name(resolver, chip_num, offset);
- if (!lname)
- lname = "unnamed";
- fputs(lname, stdout);
- break;
- case 'L':
- print_event_time(evtime, 2);
- break;
- case 'o':
- printf("%u", offset);
- break;
- case 'S':
- print_event_time(evtime, 0);
- break;
- case 'U':
- print_event_time(evtime, 1);
- break;
- case '%':
- fputc('%', stdout);
- break;
- case '\0':
- fputc('%', stdout);
- goto end;
- default:
- fwrite(curr, 2, 1, stdout);
- break;
- }
-
- curr += 2;
- prev = curr;
- }
-
-end:
- fputc('\n', stdout);
-}
-
-static void event_print_human_readable(struct gpiod_edge_event *event,
- struct line_resolver *resolver,
- int chip_num, struct config *cfg)
-{
- unsigned int offset;
- uint64_t evtime;
-
- offset = gpiod_edge_event_get_line_offset(event);
- evtime = gpiod_edge_event_get_timestamp_ns(event);
-
- print_event_time(evtime, cfg->timestamp_fmt);
-
- if (gpiod_edge_event_get_event_type(event) ==
- GPIOD_EDGE_EVENT_RISING_EDGE)
- fputs("\trising\t", stdout);
- else
- fputs("\tfalling\t", stdout);
-
- print_line_id(resolver, chip_num, offset, cfg->chip_id, cfg->unquoted);
- fputc('\n', stdout);
-}
-
static void event_print(struct gpiod_edge_event *event,
struct line_resolver *resolver, int chip_num,
struct config *cfg)
{
+ const char *line_name, *chip_name;
+ unsigned int offset;
+
if (cfg->quiet)
return;
- if (cfg->fmt)
- event_print_formatted(event, resolver, chip_num, cfg);
- else
- event_print_human_readable(event, resolver, chip_num, cfg);
+ offset = gpiod_edge_event_get_line_offset(event);
+ line_name = get_line_name(resolver, chip_num, offset);
+ chip_name = cfg->chip_id ? get_chip_name(resolver, chip_num) : NULL;
+
+ print_event(gpiod_edge_event_get_timestamp_ns(event),
+ offset,
+ gpiod_edge_event_get_event_type(event),
+ cfg->timestamp_fmt,
+ cfg->fmt,
+ line_name,
+ chip_name,
+ cfg->unquoted);
}
int main(int argc, char **argv)
diff --git a/tools/tools-common.c b/tools/tools-common.c
index 6ac9517821f81ee8592493ed37237ef795c8f1a6..d6cfcb4fd6b1aa24b201556dbeab9f095b1bf5f0 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -305,6 +305,22 @@ void print_period_help(void)
printf(" microseconds respectively.\n");
}
+void print_event_format_help(bool include_line_chip)
+{
+ printf("\n");
+ printf("Format specifiers:\n");
+ printf(" %%o GPIO line offset\n");
+ if (include_line_chip) {
+ printf(" %%l GPIO line name\n");
+ printf(" %%c GPIO chip name\n");
+ }
+ printf(" %%e numeric edge event type ('1' - rising or '2' - falling)\n");
+ printf(" %%E edge event type ('rising' or 'falling')\n");
+ printf(" %%S event timestamp as seconds\n");
+ printf(" %%U event timestamp as UTC\n");
+ printf(" %%L event timestamp as local time\n");
+}
+
#define TIME_BUFFER_SIZE 20
/*
@@ -337,6 +353,90 @@ void print_event_time(uint64_t evtime, int format)
}
}
+void print_event(uint64_t timestamp_ns, unsigned int offset, int event_type,
+ int timestamp_fmt, const char *fmt,
+ const char *line_name, const char *chip_name, bool unquoted)
+{
+ const char *prev, *curr;
+ char spec;
+
+ if (!fmt) {
+ print_event_time(timestamp_ns, timestamp_fmt);
+ if (event_type == GPIOD_EDGE_EVENT_RISING_EDGE)
+ fputs("\trising\t", stdout);
+ else
+ fputs("\tfalling\t", stdout);
+ if (!line_name) {
+ if (chip_name)
+ printf("%s %u", chip_name, offset);
+ else
+ printf("%u", offset);
+ } else {
+ if (chip_name)
+ printf("%s %u ", chip_name, offset);
+ printf(unquoted ? "%s" : "\"%s\"", line_name);
+ }
+ fputc('\n', stdout);
+ return;
+ }
+
+ for (prev = curr = fmt;;) {
+ curr = strchr(curr, '%');
+ if (!curr) {
+ fputs(prev, stdout);
+ break;
+ }
+
+ if (prev != curr)
+ fwrite(prev, curr - prev, 1, stdout);
+
+ spec = *(curr + 1);
+
+ switch (spec) {
+ case 'c':
+ fputs(chip_name ? chip_name : "unknown", stdout);
+ break;
+ case 'e':
+ printf("%d", event_type);
+ break;
+ case 'E':
+ fputs(event_type == GPIOD_EDGE_EVENT_RISING_EDGE ?
+ "rising" : "falling", stdout);
+ break;
+ case 'l':
+ fputs(line_name ? line_name : "unnamed", stdout);
+ break;
+ case 'L':
+ print_event_time(timestamp_ns, 2);
+ break;
+ case 'o':
+ printf("%u", offset);
+ break;
+ case 'S':
+ print_event_time(timestamp_ns, 0);
+ break;
+ case 'U':
+ print_event_time(timestamp_ns, 1);
+ break;
+ case '%':
+ fputc('%', stdout);
+ break;
+ case '\0':
+ fputc('%', stdout);
+ goto end;
+ default:
+ fwrite(curr, 2, 1, stdout);
+ break;
+ }
+
+ curr += 2;
+ prev = curr;
+ }
+
+end:
+ fputc('\n', stdout);
+}
+
static void print_bias(struct gpiod_line_info *info)
{
const char *name;
diff --git a/tools/tools-common.h b/tools/tools-common.h
index 5083b0908584f98a00321ca3fadf46f55c0a3066..95517fcd016de89e0b01c3ffc6630a30c67ba933 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -48,7 +48,11 @@ unsigned int parse_uint_or_die(const char *option);
void print_bias_help(void);
void print_chip_help(void);
void print_period_help(void);
+void print_event_format_help(bool include_line_chip);
void print_event_time(uint64_t evtime, int format);
+void print_event(uint64_t timestamp_ns, unsigned int offset, int event_type,
+ int timestamp_fmt, const char *fmt,
+ const char *line_name, const char *chip_name, bool unquoted);
void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings);
void print_line_id(struct line_resolver *resolver, int chip_num,
unsigned int offset, const char *chip_id, bool unquoted);
--
2.47.3
next prev parent reply other threads:[~2026-10-16 14:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 14:18 [PATCH RFC libgpiod 0/3] tools: new tool - gpioctl Bartosz Golaszewski
2026-10-16 14:18 ` [PATCH RFC libgpiod 1/3] tools: factor out line value parsing into tools-common Bartosz Golaszewski
2026-10-16 14:18 ` Bartosz Golaszewski [this message]
2026-10-19 11:13 ` [PATCH RFC libgpiod 2/3] tools: factor out line event formatting " Andy Shevchenko
2026-10-21 10:35 ` Bartosz Golaszewski
2026-10-16 14:18 ` [PATCH RFC libgpiod 3/3] tools: implement gpioctl Bartosz Golaszewski
2026-10-16 18:00 ` Linus Walleij
2026-10-19 7:50 ` Bartosz Golaszewski
2026-10-19 11:15 ` Andy Shevchenko
2026-10-19 13:37 ` Vincent Fazio
2026-10-19 13:58 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261016-gpioctl-v1-2-8770622679b8@oss.qualcomm.com \
--to=bartosz.golaszewski@oss.qualcomm.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=brgl@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=vfazio@xes-inc.com \
--cc=warthog618@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox