moving serial utility functions to isp_utils.c
authorNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 12 Apr 2012 13:40:28 +0000 (15:40 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 12 Apr 2012 13:40:28 +0000 (15:40 +0200)
isp_main.c
isp_utils.c
isp_utils.h

index c3f30f1..8480f34 100644 (file)
@@ -72,61 +72,9 @@ void help(char *prog_name)
 
 #define SERIAL_BAUD  B115200
 
-int serial_fd = -1;
 int trace_on = 0;
 
 
-/* Open the serial device and set it up.
- * Returns 0 on success, negativ value on error.
- * Actal setup is done according to LPC11xx user's manual.
- * Only baudrate can be changed using command line option.
- */
-int serial_open(int baudrate, char* serial_device)
-{
-       struct termios tio;
-
-       if (serial_device == NULL) {
-               printf("No serial device given on command line\n");
-               return -2;
-       }
-
-       /* Open serial port */
-       serial_fd = open(serial_device, O_RDWR | O_NONBLOCK);
-       if (serial_fd < 0) {
-               perror("Unable to open serial_device");
-               printf("Tried to open \"%s\".\n", serial_device);
-               return -1;
-       }
-       /* Setup serial port */
-       memset(&tio, 0, sizeof(tio));
-       tio.c_iflag = IXON | IXOFF;  /* See section 21.4.4 of LPC11xx user's manual (UM10398) */
-       tio.c_cflag = CS8 | CREAD | CLOCAL;  /* 8n1, see termios.h for more information */
-       tio.c_cc[VMIN] = 1;
-       tio.c_cc[VTIME] = 5;
-       cfsetospeed(&tio, baudrate);
-       cfsetispeed(&tio, baudrate);
-       tcsetattr(serial_fd, TCSANOW, &tio);
-       
-       return 0;
-}
-
-/* Simple write() wrapper, with trace if enabled */
-int serial_write(const char* buf, unsigned int buf_size)
-{
-       int nb = 0;
-
-       if (trace_on) {
-               printf("Sending %d octet(s) :\n", buf_size);
-               isp_dump((unsigned char*)buf, buf_size);
-       }
-       nb = write(serial_fd, buf, buf_size);
-       if (nb <= 0) {
-               perror("Serial write error");
-               return -1;
-       }
-       return nb;
-}
-
 int isp_ret_code(char* buf)
 {
        if (trace_on) {
@@ -136,67 +84,29 @@ int isp_ret_code(char* buf)
        return 0;
 }
 
-/* Try to read at least "min_read" characters from the serial line.
- * Returns -1 on error, 0 on end of file, or read count otherwise.
- */
-int serial_read(char* buf, unsigned int buf_size, unsigned int min_read)
-{
-       int nb = 0;
-       unsigned int count = 0;
-       
-       if (min_read > buf_size) {
-               printf("serial_read: buffer too small for min read value.\n");
-               return -3;
-       }
-
-       do {
-               nb = read(serial_fd, &buf[count], (buf_size - count));
-               if (nb < 0) {
-                       if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
-                               usleep( 5000 );
-                               continue;
-                       }
-                       perror("Serial read error");
-                       return -1;
-               } else if (nb == 0) {
-                       printf("serial_read: end of file !!!!\n");
-                       return 0;
-               }
-               if (trace_on == 2) {
-                       isp_dump((unsigned char*)(&buf[count]), nb);
-               }       
-               count += nb;
-       } while (count < min_read);
-
-       if (trace_on) {
-               printf("Received %d octet(s) :\n", count);
-               isp_dump((unsigned char*)buf, count);
-       }
-       return count;
-}
 
 /* Connect or reconnect to the target.
  * Return positive value when connection is OK, or negative value otherwise.
  */
-int connect()
+int isp_connect()
 {
        char buf[SERIAL_BUFSIZE];
 
        /* Send synchronize request */
-       if (serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) {
+       if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) {
                printf("Unable to send synchronize request.\n");
                return -5;
        }
 
        /* Wait for answer */
-       if (serial_read(buf, SERIAL_BUFSIZE, strlen(SYNCHRO)) < 0) {
+       if (isp_serial_read(buf, SERIAL_BUFSIZE, strlen(SYNCHRO)) < 0) {
                printf("Error reading synchronize answer.\n");
                return -4;
        }
 
        /* Check answer, and acknoledge if OK */
        if (strncmp(SYNCHRO, buf, strlen(SYNCHRO)) == 0) {
-               serial_write(SYNCHRO, strlen(SYNCHRO));
+               isp_serial_write(SYNCHRO, strlen(SYNCHRO));
        } else {
                printf("Unable to synchronize.\n");
                return -1;
@@ -204,11 +114,11 @@ int connect()
 
        /* Empty read buffer (maybe echo is on ?) */
        usleep( 5000 );
-       serial_read(buf, SERIAL_BUFSIZE, 0);
+       isp_serial_read(buf, SERIAL_BUFSIZE, 0);
 
        /* FIXME : Do we always turn off echo ? */
        /* and turn off echo */
-       serial_write("A 0\r\n", 5);
+       isp_serial_write("A 0\r\n", 5);
 
        return 1;
 }
@@ -218,7 +128,7 @@ int main(int argc, char** argv)
 {
        int baudrate = SERIAL_BAUD;
        int dont_synchronize = 0;
-       char* serial_device = NULL;
+       char* isp_serial_device = NULL;
 
        /* For "command" handling */
        char* command = NULL;
@@ -280,17 +190,17 @@ int main(int argc, char** argv)
 
        /* First one should (must) be serial device */
        if (optind < argc) {
-               serial_device = argv[optind++];
+               isp_serial_device = argv[optind++];
                if (trace_on) {
-                       printf("Serial device : %s\n", serial_device);
+                       printf("Serial device : %s\n", isp_serial_device);
                }
        }
-       if (serial_device == NULL) {
+       if (isp_serial_device == NULL) {
                printf("No serial device given, exiting\n");
                help(argv[0]);
                return 0;
        }
-       if (serial_open(baudrate, serial_device) != 0) {
+       if (isp_serial_open(baudrate, isp_serial_device) != 0) {
                printf("Serial open failed, unable to initiate serial communication with target.\n");
                return -1;
        }
@@ -319,7 +229,7 @@ int main(int argc, char** argv)
        }
 
        if (! dont_synchronize) {
-               connect();
+               isp_connect();
        }
 
        /* FIXME : call command handler */
@@ -328,7 +238,7 @@ int main(int argc, char** argv)
        if (cmd_args != NULL) {
                free(cmd_args);
        }
-       close(serial_fd);
+       isp_serial_close();
        return 0;
 }
 
index ed4ed91..c1b0707 100644 (file)
@@ -21,6 +21,8 @@
 #include <errno.h>
 
 
+extern int trace_on;
+
 /* display data as in hexdump -C :
    00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
  */
@@ -67,8 +69,110 @@ void isp_dump(const unsigned char* buf, unsigned int buf_size)
 }
 
 
+/* ---- Serial utility functions ---------------------------------------------------*/
+
+static int serial_fd = -1;
+
+/* Open the serial device and set it up.
+ * Returns 0 on success, negativ value on error.
+ * Actal setup is done according to LPC11xx user's manual.
+ * Only baudrate can be changed using command line option.
+ */
+int isp_serial_open(int baudrate, char* serial_device)
+{
+       struct termios tio;
+
+       if (serial_device == NULL) {
+               printf("No serial device given on command line\n");
+               return -2;
+       }
+
+       /* Open serial port */
+       serial_fd = open(serial_device, O_RDWR | O_NONBLOCK);
+       if (serial_fd < 0) {
+               perror("Unable to open serial_device");
+               printf("Tried to open \"%s\".\n", serial_device);
+               return -1;
+       }
+       /* Setup serial port */
+       memset(&tio, 0, sizeof(tio));
+       tio.c_iflag = IXON | IXOFF;  /* See section 21.4.4 of LPC11xx user's manual (UM10398) */
+       tio.c_cflag = CS8 | CREAD | CLOCAL;  /* 8n1, see termios.h for more information */
+       tio.c_cc[VMIN] = 1;
+       tio.c_cc[VTIME] = 5;
+       cfsetospeed(&tio, baudrate);
+       cfsetispeed(&tio, baudrate);
+       tcsetattr(serial_fd, TCSANOW, &tio);
+       
+       return 0;
+}
+
+void isp_serial_close(void)
+{
+       close(serial_fd);
+}
+
+/* Simple write() wrapper, with trace if enabled */
+int isp_serial_write(const char* buf, unsigned int buf_size)
+{
+       int nb = 0;
+
+       if (trace_on) {
+               printf("Sending %d octet(s) :\n", buf_size);
+               isp_dump((unsigned char*)buf, buf_size);
+       }
+       nb = write(serial_fd, buf, buf_size);
+       if (nb <= 0) {
+               perror("Serial write error");
+               return -1;
+       }
+       return nb;
+}
+
+/* Try to read at least "min_read" characters from the serial line.
+ * Returns -1 on error, 0 on end of file, or read count otherwise.
+ */
+int isp_serial_read(char* buf, unsigned int buf_size, unsigned int min_read)
+{
+       int nb = 0;
+       unsigned int count = 0;
+       
+       if (min_read > buf_size) {
+               printf("serial_read: buffer too small for min read value.\n");
+               return -3;
+       }
+
+       do {
+               nb = read(serial_fd, &buf[count], (buf_size - count));
+               if (nb < 0) {
+                       if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
+                               usleep( 5000 );
+                               continue;
+                       }
+                       perror("Serial read error");
+                       return -1;
+               } else if (nb == 0) {
+                       printf("serial_read: end of file !!!!\n");
+                       return 0;
+               }
+               if (trace_on == 2) {
+                       isp_dump((unsigned char*)(&buf[count]), nb);
+               }       
+               count += nb;
+       } while (count < min_read);
+
+       if (trace_on) {
+               printf("Received %d octet(s) :\n", count);
+               isp_dump((unsigned char*)buf, count);
+       }
+       return count;
+}
+
+
+/* ---- UU_Encoding utility functions ----------------------------------------------*/
+
 /* FIXME : This is a place-holder forr uuencode and uudecode functions !!! */
-int uu_encode(unsigned char* dest, unsigned char* src, int orig_size)
+int isp_uu_encode(char* dest, char* src, unsigned int orig_size)
 {
        int new_size = 0;
 
@@ -81,7 +185,8 @@ int uu_encode(unsigned char* dest, unsigned char* src, int orig_size)
        }
        return new_size;
 }
-int uu_decode(unsigned char* dest, unsigned char* src, int orig_size)
+
+int isp_uu_decode(char* dest, char* src, unsigned int orig_size)
 {
        int new_size = 0;
        while (orig_size--) {
index bf3ea17..5bf4a95 100644 (file)
@@ -3,4 +3,31 @@
 
 void isp_dump(const unsigned char* buf, unsigned int buf_size);
 
+
+/* ---- Serial utility functions ---------------------------------------------------*/
+
+/* Open the serial device and set it up.
+ * Returns 0 on success, negativ value on error.
+ * Actal setup is done according to LPC11xx user's manual.
+ * Only baudrate can be changed using command line option.
+ */
+int isp_serial_open(int baudrate, char* serial_device);
+void isp_serial_close(void);
+
+/* Simple write() wrapper, with trace if enabled */
+int isp_serial_write(const char* buf, unsigned int buf_size);
+
+/* Try to read at least "min_read" characters from the serial line.
+ * Returns -1 on error, 0 on end of file, or read count otherwise.
+ */
+int isp_serial_read(char* buf, unsigned int buf_size, unsigned int min_read);
+
+
+/* ---- UU_Encoding utility functions ----------------------------------------------*/
+
+/* FIXME : This is a place-holder forr uuencode and uudecode functions !!! */
+int isp_uu_encode(char* dest, char* src, unsigned int orig_size);
+
+int isp_uu_decode(char* dest, char* src, unsigned int orig_size);
+
 #endif /* ISP_UTILS_H */