diff mbox

[1/4] Cleaned hexadecimal dump

Message ID 1425750995-18176-2-git-send-email-adrianremonda@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

adrianremonda@gmail.com March 7, 2015, 5:56 p.m. UTC
Signed-off-by: Adrian Remonda <adrianremonda@gmail.com>
---
 Documentation/spi/spidev_test.c |   35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

Comments

Joe Perches March 8, 2015, 2:22 a.m. UTC | #1
On Sat, 2015-03-07 at 12:56 -0500, Adrian Remonda wrote:
> Signed-off-by: Adrian Remonda <adrianremonda@gmail.com>
[]
> diff --git a/Documentation/spi/spidev_test.c b/Documentation/spi/spidev_test.c
[]
> +static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
> +{

Is there something necessary that print_hex_dump can't do?

> +	int i = 0;
> +	char *address = (char *)src;
> +	char *line = (char *)address;
> +	unsigned char c;
> +
> +	printf("%s | ", prefix);
> +	while (length-- > 0) {
> +		printf("%02X ", (unsigned char)*address++);
> +		if (!(++i % bLine) || (length == 0 && i % bLine)) {
> +			if (length == 0) {
> +				while (i++ % bLine)
> +					printf("__ ");
> +			}
> +			printf(" | ");  /* right close */
> +			while (line < address) {
> +				c = *line++;
> +				printf("%c", (c < 33 || c == 255) ? 0x2E : c);
> +			}
> +			printf("\n");
> +			if (length > 0)
> +				printf("%s | ", prefix);
> +		}
> +	}
> +}



--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andy Shevchenko March 8, 2015, 10:53 a.m. UTC | #2
On Sun, Mar 8, 2015 at 4:22 AM, Joe Perches <joe@perches.com> wrote:
> On Sat, 2015-03-07 at 12:56 -0500, Adrian Remonda wrote:

>> +static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
>> +{
>
> Is there something necessary that print_hex_dump can't do?

As far as I can see the tool is running in user space. Do we have
possibility to use print_hex_dump() in user space?
Joe Perches March 8, 2015, 4:20 p.m. UTC | #3
On Sun, 2015-03-08 at 12:53 +0200, Andy Shevchenko wrote:
> On Sun, Mar 8, 2015 at 4:22 AM, Joe Perches <joe@perches.com> wrote:
> > On Sat, 2015-03-07 at 12:56 -0500, Adrian Remonda wrote:
> 
> >> +static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
> > Is there something necessary that print_hex_dump can't do?
> As far as I can see the tool is running in user space.

OK, I skipped over that bit.  Nevermind.

> Do we have
> possibility to use print_hex_dump() in user space?

No.

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Geert Uytterhoeven March 9, 2015, 1:51 p.m. UTC | #4
On Sat, Mar 7, 2015 at 6:56 PM, Adrian Remonda <adrianremonda@gmail.com> wrote:
> diff --git a/Documentation/spi/spidev_test.c b/Documentation/spi/spidev_test.c
> index 3a2f9d59edab..533050ced9f1 100644
> --- a/Documentation/spi/spidev_test.c
> +++ b/Documentation/spi/spidev_test.c
> @@ -15,6 +15,7 @@
>  #include <unistd.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> +#include <string.h>
>  #include <getopt.h>
>  #include <fcntl.h>
>  #include <sys/ioctl.h>
> @@ -35,6 +36,33 @@ static uint8_t bits = 8;
>  static uint32_t speed = 500000;
>  static uint16_t delay;
>
> +static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
> +{
> +       int i = 0;
> +       char *address = (char *)src;

If you use "const char *address", you don't need the cast.
If you use "const unsigned char *address", one more cast below can be dropped.

> +       char *line = (char *)address;

"const unsigned char *line", and drop the cast.

> +       unsigned char c;
> +
> +       printf("%s | ", prefix);
> +       while (length-- > 0) {
> +               printf("%02X ", (unsigned char)*address++);

> @@ -76,12 +104,7 @@ static void transfer(int fd)
>         if (ret < 1)
>                 pabort("can't send spi message");
>
> -       for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
> -               if (!(ret % 6))
> -                       puts("");
> -               printf("%.2X ", rx[ret]);
> -       }
> -       puts("");
> +       hexDump(rx, ARRAY_SIZE(tx), 32, "RX");

I know rx and tx have the same size, but please switch to "ARRAY_SIZE(RX)"
while at it.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mark Brown March 9, 2015, 6:40 p.m. UTC | #5
On Sat, Mar 07, 2015 at 12:56:32PM -0500, Adrian Remonda wrote:

> +static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
> +{

Please call this hex_dump or something - that's more in line with the
kernel coding style.  Please also don't vUse aHungarian nNotation, it's
also not the kernel coding style.
diff mbox

Patch

diff --git a/Documentation/spi/spidev_test.c b/Documentation/spi/spidev_test.c
index 3a2f9d59edab..533050ced9f1 100644
--- a/Documentation/spi/spidev_test.c
+++ b/Documentation/spi/spidev_test.c
@@ -15,6 +15,7 @@ 
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <getopt.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -35,6 +36,33 @@  static uint8_t bits = 8;
 static uint32_t speed = 500000;
 static uint16_t delay;
 
+static void hexDump(const void *src, size_t length, size_t bLine, char *prefix)
+{
+	int i = 0;
+	char *address = (char *)src;
+	char *line = (char *)address;
+	unsigned char c;
+
+	printf("%s | ", prefix);
+	while (length-- > 0) {
+		printf("%02X ", (unsigned char)*address++);
+		if (!(++i % bLine) || (length == 0 && i % bLine)) {
+			if (length == 0) {
+				while (i++ % bLine)
+					printf("__ ");
+			}
+			printf(" | ");  /* right close */
+			while (line < address) {
+				c = *line++;
+				printf("%c", (c < 33 || c == 255) ? 0x2E : c);
+			}
+			printf("\n");
+			if (length > 0)
+				printf("%s | ", prefix);
+		}
+	}
+}
+
 static void transfer(int fd)
 {
 	int ret;
@@ -76,12 +104,7 @@  static void transfer(int fd)
 	if (ret < 1)
 		pabort("can't send spi message");
 
-	for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
-		if (!(ret % 6))
-			puts("");
-		printf("%.2X ", rx[ret]);
-	}
-	puts("");
+	hexDump(rx, ARRAY_SIZE(tx), 32, "RX");
 }
 
 static void print_usage(const char *prog)