All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option
@ 2024-08-07 11:21 Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 1/3] i2ctransfer: sort command line options and add to help text Wolfram Sang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Wolfram Sang @ 2024-08-07 11:21 UTC (permalink / raw)
  To: linux-i2c; +Cc: linux-renesas-soc, Wolfram Sang

I wanted to print out read data as-is, so I added a binary printout
option (patch 2). The other two patches are cleanups found on the way.

Looking forward to comments!


Wolfram Sang (3):
  i2ctransfer: sort command line options and add to help text
  i2ctransfer: add option to print binary data
  i2ctransfer: don't use plain 'unsigned'

 tools/i2ctransfer.8 | 40 ++++++++++++++++++++--------------
 tools/i2ctransfer.c | 53 ++++++++++++++++++++++++++++++---------------
 2 files changed, 60 insertions(+), 33 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH i2c-tools 1/3] i2ctransfer: sort command line options and add to help text
  2024-08-07 11:21 [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
@ 2024-08-07 11:21 ` Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 2/3] i2ctransfer: add option to print binary data Wolfram Sang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2024-08-07 11:21 UTC (permalink / raw)
  To: linux-i2c; +Cc: linux-renesas-soc, Wolfram Sang

Sort the options in docs and code. Describe them in the help text.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2ctransfer.8 | 32 ++++++++++++++++----------------
 tools/i2ctransfer.c | 18 ++++++++++++------
 2 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/tools/i2ctransfer.8 b/tools/i2ctransfer.8
index c88ef64..88d5eb3 100644
--- a/tools/i2ctransfer.8
+++ b/tools/i2ctransfer.8
@@ -4,10 +4,10 @@ i2ctransfer \- send user-defined I2C messages in one transfer
 
 .SH SYNOPSIS
 .B i2ctransfer
+.RB [ -a ]
 .RB [ -f ]
-.RB [ -y ]
 .RB [ -v ]
-.RB [ -a ]
+.RB [ -y ]
 .I i2cbus desc
 .RI [ data ]
 .RI [ desc
@@ -15,10 +15,10 @@ i2ctransfer \- send user-defined I2C messages in one transfer
 .RI ...
 .br
 .B i2ctransfer
-.B -V
+.B -h
 .br
 .B i2ctransfer
-.B -h
+.B -V
 
 .SH DESCRIPTION
 .B i2ctransfer
@@ -40,6 +40,9 @@ This program helps you to create proper transfers for your needs.
 
 .SH OPTIONS
 .TP
+.B -a
+Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended.
+.TP
 .B -f
 Force access to the device even if it is already busy.
 By default,
@@ -51,13 +54,8 @@ It can also cause
 to silently write to the wrong register.
 So use at your own risk and only if you know what you're doing.
 .TP
-.B -y
-Disable interactive mode.
-By default,
-.B i2ctransfer
-will wait for a confirmation from the user before messing with the I2C bus.
-When this flag is used, it will perform the operation directly.
-This is mainly meant to be used in scripts.
+.B -h
+Display the help and exit.
 .TP
 .B -v
 Enable verbose output.
@@ -66,11 +64,13 @@ It will print infos about all messages sent, i.e. not only for read messages but
 .B -V
 Display the version and exit.
 .TP
-.B -h
-Display the help and exit.
-.TP
-.B -a
-Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended.
+.B -y
+Disable interactive mode.
+By default,
+.B i2ctransfer
+will wait for a confirmation from the user before messing with the I2C bus.
+When this flag is used, it will perform the operation directly.
+This is mainly meant to be used in scripts.
 
 .SH ARGUMENTS
 .PP
diff --git a/tools/i2ctransfer.c b/tools/i2ctransfer.c
index 85b70c3..15e774b 100644
--- a/tools/i2ctransfer.c
+++ b/tools/i2ctransfer.c
@@ -42,7 +42,13 @@ enum parse_state {
 static void help(void)
 {
 	fprintf(stderr,
-		"Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...\n"
+		"Usage: i2ctransfer [OPTIONS] I2CBUS DESC [DATA] [DESC [DATA]]...\n"
+		"  OPTIONS: -a allow even reserved addresses\n"
+		"           -f force access even if address is marked used\n"
+		"           -h this help text\n"
+		"           -v verbose mode\n"
+		"           -V version info\n"
+		"           -y yes to all confirmations\n"
 		"  I2CBUS is an integer or an I2C bus name\n"
 		"  DESC describes the transfer in the form: {r|w}LENGTH[@address]\n"
 		"    1) read/write-flag 2) LENGTH (range 0-65535, or '?')\n"
@@ -141,15 +147,15 @@ int main(int argc, char *argv[])
 		msgs[i].buf = NULL;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "Vafhvy")) != -1) {
+	while ((opt = getopt(argc, argv, "afhvVy")) != -1) {
 		switch (opt) {
-		case 'V': version = 1; break;
-		case 'v': verbose = 1; break;
+		case 'a': all_addrs = 1; break;
 		case 'f': force = 1; break;
+		case 'v': verbose = 1; break;
+		case 'V': version = 1; break;
 		case 'y': yes = 1; break;
-		case 'a': all_addrs = 1; break;
-		case 'h':
 		case '?':
+		case 'h':
 			help();
 			exit(opt == '?');
 		}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH i2c-tools 2/3] i2ctransfer: add option to print binary data
  2024-08-07 11:21 [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 1/3] i2ctransfer: sort command line options and add to help text Wolfram Sang
@ 2024-08-07 11:21 ` Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 3/3] i2ctransfer: don't use plain 'unsigned' Wolfram Sang
  2024-08-10 21:40 ` [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2024-08-07 11:21 UTC (permalink / raw)
  To: linux-i2c; +Cc: linux-renesas-soc, Wolfram Sang

Useful to pipe data to other commands for further inspection. Or to just
print out ASCII responses.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2ctransfer.8 |  8 ++++++++
 tools/i2ctransfer.c | 31 ++++++++++++++++++++++---------
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/tools/i2ctransfer.8 b/tools/i2ctransfer.8
index 88d5eb3..4bdf436 100644
--- a/tools/i2ctransfer.8
+++ b/tools/i2ctransfer.8
@@ -5,6 +5,7 @@ i2ctransfer \- send user-defined I2C messages in one transfer
 .SH SYNOPSIS
 .B i2ctransfer
 .RB [ -a ]
+.RB [ -b ]
 .RB [ -f ]
 .RB [ -v ]
 .RB [ -y ]
@@ -43,6 +44,13 @@ This program helps you to create proper transfers for your needs.
 .B -a
 Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended.
 .TP
+.B -b
+Print data from read messages as binary data. Disables
+.I -v
+(verbose). Useful for piping into a pretty printing tool like
+.B hexdump
+or for redirecting into a file to be analyzed later.
+.TP
 .B -f
 Force access to the device even if it is already busy.
 By default,
diff --git a/tools/i2ctransfer.c b/tools/i2ctransfer.c
index 15e774b..a7bbbaa 100644
--- a/tools/i2ctransfer.c
+++ b/tools/i2ctransfer.c
@@ -38,12 +38,14 @@ enum parse_state {
 #define PRINT_READ_BUF	(1 << 1)
 #define PRINT_WRITE_BUF	(1 << 2)
 #define PRINT_HEADER	(1 << 3)
+#define PRINT_BINARY	(1 << 4)
 
 static void help(void)
 {
 	fprintf(stderr,
 		"Usage: i2ctransfer [OPTIONS] I2CBUS DESC [DATA] [DESC [DATA]]...\n"
 		"  OPTIONS: -a allow even reserved addresses\n"
+		"           -b print read data as binary, disables -v\n"
 		"           -f force access even if address is marked used\n"
 		"           -h this help text\n"
 		"           -v verbose mode\n"
@@ -106,12 +108,16 @@ static void print_msgs(struct i2c_msg *msgs, __u32 nmsgs, unsigned flags)
 		}
 
 		if (len && print_buf) {
-			if (flags & PRINT_HEADER)
-				fprintf(output, ", buf ");
-			for (j = 0; j < len - 1; j++)
-				fprintf(output, "0x%02x ", msgs[i].buf[j]);
-			/* Print final byte with newline */
-			fprintf(output, "0x%02x\n", msgs[i].buf[j]);
+			if (flags & PRINT_BINARY) {
+				fwrite(msgs[i].buf, 1, len, output);
+			} else {
+				if (flags & PRINT_HEADER)
+					fprintf(output, ", buf ");
+				for (j = 0; j < len - 1; j++)
+					fprintf(output, "0x%02x ", msgs[i].buf[j]);
+				/* Print final byte with newline */
+				fprintf(output, "0x%02x\n", msgs[i].buf[j]);
+			}
 		} else if (flags & PRINT_HEADER) {
 			fprintf(output, "\n");
 		}
@@ -138,7 +144,7 @@ int main(int argc, char *argv[])
 {
 	char filename[20];
 	int i2cbus, address = -1, file, opt, arg_idx, nmsgs = 0, nmsgs_sent, i;
-	int force = 0, yes = 0, version = 0, verbose = 0, all_addrs = 0;
+	int force = 0, yes = 0, version = 0, verbose = 0, all_addrs = 0, binary = 0;
 	struct i2c_msg msgs[I2C_RDRW_IOCTL_MAX_MSGS];
 	enum parse_state state = PARSE_GET_DESC;
 	unsigned buf_idx = 0;
@@ -147,9 +153,10 @@ int main(int argc, char *argv[])
 		msgs[i].buf = NULL;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "afhvVy")) != -1) {
+	while ((opt = getopt(argc, argv, "abfhvVy")) != -1) {
 		switch (opt) {
 		case 'a': all_addrs = 1; break;
+		case 'b': binary = 1; break;
 		case 'f': force = 1; break;
 		case 'v': verbose = 1; break;
 		case 'V': version = 1; break;
@@ -326,6 +333,7 @@ int main(int argc, char *argv[])
 
 	if (yes || confirm(filename, msgs, nmsgs)) {
 		struct i2c_rdwr_ioctl_data rdwr;
+		unsigned print_flags = PRINT_READ_BUF;
 
 		rdwr.msgs = msgs;
 		rdwr.nmsgs = nmsgs;
@@ -337,7 +345,12 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Warning: only %d/%d messages were sent\n", nmsgs_sent, nmsgs);
 		}
 
-		print_msgs(msgs, nmsgs_sent, PRINT_READ_BUF | (verbose ? PRINT_HEADER | PRINT_WRITE_BUF : 0));
+		if (binary)
+			print_flags |= PRINT_BINARY;
+		else if (verbose)
+			print_flags |= PRINT_HEADER | PRINT_WRITE_BUF;
+
+		print_msgs(msgs, nmsgs_sent, print_flags);
 	}
 
 	close(file);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH i2c-tools 3/3] i2ctransfer: don't use plain 'unsigned'
  2024-08-07 11:21 [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 1/3] i2ctransfer: sort command line options and add to help text Wolfram Sang
  2024-08-07 11:21 ` [PATCH i2c-tools 2/3] i2ctransfer: add option to print binary data Wolfram Sang
@ 2024-08-07 11:21 ` Wolfram Sang
  2024-08-10 21:40 ` [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2024-08-07 11:21 UTC (permalink / raw)
  To: linux-i2c; +Cc: linux-renesas-soc, Wolfram Sang

Make those explicit 'unsigned int'. I got used to the explicit Kernel
coding style and prefer it meanwhile.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2ctransfer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/i2ctransfer.c b/tools/i2ctransfer.c
index a7bbbaa..c9a30cf 100644
--- a/tools/i2ctransfer.c
+++ b/tools/i2ctransfer.c
@@ -85,10 +85,10 @@ static int check_funcs(int file)
 	return 0;
 }
 
-static void print_msgs(struct i2c_msg *msgs, __u32 nmsgs, unsigned flags)
+static void print_msgs(struct i2c_msg *msgs, __u32 nmsgs, unsigned int flags)
 {
 	FILE *output = flags & PRINT_STDERR ? stderr : stdout;
-	unsigned i;
+	unsigned int i;
 	__u16 j;
 
 	for (i = 0; i < nmsgs; i++) {
@@ -147,7 +147,7 @@ int main(int argc, char *argv[])
 	int force = 0, yes = 0, version = 0, verbose = 0, all_addrs = 0, binary = 0;
 	struct i2c_msg msgs[I2C_RDRW_IOCTL_MAX_MSGS];
 	enum parse_state state = PARSE_GET_DESC;
-	unsigned buf_idx = 0;
+	unsigned int buf_idx = 0;
 
 	for (i = 0; i < I2C_RDRW_IOCTL_MAX_MSGS; i++)
 		msgs[i].buf = NULL;
@@ -333,7 +333,7 @@ int main(int argc, char *argv[])
 
 	if (yes || confirm(filename, msgs, nmsgs)) {
 		struct i2c_rdwr_ioctl_data rdwr;
-		unsigned print_flags = PRINT_READ_BUF;
+		unsigned int print_flags = PRINT_READ_BUF;
 
 		rdwr.msgs = msgs;
 		rdwr.nmsgs = nmsgs;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option
  2024-08-07 11:21 [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
                   ` (2 preceding siblings ...)
  2024-08-07 11:21 ` [PATCH i2c-tools 3/3] i2ctransfer: don't use plain 'unsigned' Wolfram Sang
@ 2024-08-10 21:40 ` Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2024-08-10 21:40 UTC (permalink / raw)
  To: linux-i2c; +Cc: linux-renesas-soc

[-- Attachment #1: Type: text/plain, Size: 461 bytes --]

On Wed, Aug 07, 2024 at 01:21:56PM +0200, Wolfram Sang wrote:
> I wanted to print out read data as-is, so I added a binary printout
> option (patch 2). The other two patches are cleanups found on the way.
> 
> Looking forward to comments!
> 
> 
> Wolfram Sang (3):
>   i2ctransfer: sort command line options and add to help text
>   i2ctransfer: add option to print binary data
>   i2ctransfer: don't use plain 'unsigned'
> 

Applied to master.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-08-10 21:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-07 11:21 [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang
2024-08-07 11:21 ` [PATCH i2c-tools 1/3] i2ctransfer: sort command line options and add to help text Wolfram Sang
2024-08-07 11:21 ` [PATCH i2c-tools 2/3] i2ctransfer: add option to print binary data Wolfram Sang
2024-08-07 11:21 ` [PATCH i2c-tools 3/3] i2ctransfer: don't use plain 'unsigned' Wolfram Sang
2024-08-10 21:40 ` [PATCH i2c-tools 0/3] i2ctransfer: cleanups & add binary data option Wolfram Sang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.