adding hexdump-like dump utility function for debugging purpose
authorNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 11 Apr 2012 21:35:12 +0000 (23:35 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 11 Apr 2012 21:35:12 +0000 (23:35 +0200)
Makefile
isp_main.c
isp_utils.c
isp_utils.h [new file with mode: 0644]

index 502ee78..b04ff17 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,5 +7,5 @@ CFLAGS += -Wall -Wextra -O2
 all: isp
 
 isp: isp_main.c isp_utils.c isp_commands.c
-       $(CC) $(CFLAGS) $< -o $@
+       $(CC) $(CFLAGS) $^ -o $@
 
index b4c3d7c..5a02f2f 100644 (file)
 #include <sys/stat.h>
 
 #include <string.h>
+#include <errno.h>
+#include <getopt.h>
 
-#include <termios.h>
+#include <termios.h> /* for serial config */
 #include <ctype.h>
-#include <errno.h>
 
-#include <getopt.h> /* for getopt */
+#include "isp_utils.h"
 
 #define PROG_NAME "LPC11xx ISP"
 #define VERSION   "0.01"
@@ -66,7 +67,7 @@ void help(char *prog_name)
 
 #define SERIAL_BUFSIZE  128
 
-#define SYNCHRO_START '?'
+#define SYNCHRO_START "?"
 #define SYNCHRO  "Synchronized"
 
 #define SERIAL_BAUD  B115200
@@ -104,13 +105,13 @@ int serial_open(int baudrate)
        return 0;
 }
 
-int serial_write(const char* buf, int buf_size)
+int serial_write(const char* buf, unsigned int buf_size)
 {
        int nb = 0;
 
        if (trace_on) {
                printf("Sending %d :\n", buf_size);
-               /* FIXME : display data as in hexdump -C */
+               isp_dump((unsigned char*)buf, buf_size);
        }
        nb = write(serial_fd, buf, buf_size);
        if (nb <= 0) {
@@ -129,7 +130,7 @@ int isp_ret_code(char* buf)
        return 0;
 }
 
-int serial_read(char* buf, int buf_size)
+int serial_read(char* buf, unsigned int buf_size)
 {
        int nb = 0;
 
@@ -143,7 +144,7 @@ int serial_read(char* buf, int buf_size)
        }
        if (trace_on) {
                printf("Received : %d octets\n", nb);
-               /* FIXME : display data as in hexdump -C */
+               isp_dump((unsigned char*)buf, nb);
        }
        return nb;
 }
@@ -155,7 +156,7 @@ int connect()
        char buf[SERIAL_BUFSIZE];
 
        /* Send synchronize request */
-       serial_write("?", 1);
+       serial_write(SYNCHRO_START, 1);
 
        /* Wait for answer */
        serial_read(buf, SERIAL_BUFSIZE);
index 33890b1..ed4ed91 100644 (file)
 #include <errno.h>
 
 
+/* display data as in hexdump -C :
+   00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
+ */
+void isp_dump(const unsigned char* buf, unsigned int buf_size)
+{
+       unsigned int count = 0;
+       char pass = 0;
+       while ((count < buf_size) || (pass == 0)) {
+               if (count == buf_size) {
+                       while (count & 0x0F) {
+                               printf("   ");
+                               count++;
+                       }
+                       pass = 1;
+                       count -= 0x10;
+               }
+               if ((count % 0x10) == 0) {
+                       if (pass == 0) {
+                               printf(" %08x  ", count);
+                       } else {
+                               printf(" |");
+                       }
+               }
+               if (pass == 0) {
+                       printf("%02x ", buf[count]);
+               } else {
+                       if (isgraph((int)buf[count])) {
+                               printf("%c", buf[count]);
+                       } else {
+                               printf(".");
+                       }
+               }
+               count++;
+               if ((count % 0x10) == 0) {
+                       if (pass == 0) {
+                               count -= 0x10;
+                       } else {
+                               printf("|\n");
+                       }
+                       pass = !pass;
+               }
+       }
+       printf("|\n");
+}
+
+
 /* FIXME : This is a place-holder forr uuencode and uudecode functions !!! */
 int uu_encode(unsigned char* dest, unsigned char* src, int orig_size)
 {
diff --git a/isp_utils.h b/isp_utils.h
new file mode 100644 (file)
index 0000000..bf3ea17
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef ISP_UTILS_H
+#define ISP_UTILS_H
+
+void isp_dump(const unsigned char* buf, unsigned int buf_size);
+
+#endif /* ISP_UTILS_H */