From 6f22a8ef2ccf7091324b41b553632695507215a7 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Tue, 30 Oct 2012 19:59:21 +0100 Subject: [PATCH] Factor out serial_readline() to serial.c. Only one (slightly different) variant remains in agilent-dmm, this will be merged soon too, though. --- hardware/agilent-dmm/api.c | 11 +++++----- hardware/common/serial.c | 34 +++++++++++++++++++++++++++++ hardware/fluke-dmm/api.c | 40 +++-------------------------------- hardware/radioshack-dmm/api.c | 40 +++-------------------------------- hardware/tekpower-dmm/api.c | 34 ----------------------------- libsigrok-internal.h | 2 ++ 6 files changed, 48 insertions(+), 113 deletions(-) diff --git a/hardware/agilent-dmm/api.c b/hardware/agilent-dmm/api.c index b6af3bbd..7dfb2c65 100644 --- a/hardware/agilent-dmm/api.c +++ b/hardware/agilent-dmm/api.c @@ -18,14 +18,14 @@ */ #include -#include "libsigrok.h" -#include "libsigrok-internal.h" -#include "agilent-dmm.h" #include #include #include #include #include +#include "libsigrok.h" +#include "libsigrok-internal.h" +#include "agilent-dmm.h" static const int hwopts[] = { SR_HWOPT_CONN, @@ -104,7 +104,8 @@ static int hw_init(void) return SR_OK; } -static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms) +/* TODO: Merge into serial_readline() from serial.c. */ +static int serial_readline2(int fd, char **buf, int *buflen, uint64_t timeout_ms) { uint64_t start; int maxlen, len; @@ -196,7 +197,7 @@ static GSList *hw_scan(GSList *options) len = 128; buf = g_try_malloc(len); - serial_readline(fd, &buf, &len, 150); + serial_readline2(fd, &buf, &len, 150); if (!len) return NULL; diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 651b5826..4a92a668 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -383,3 +383,37 @@ SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) return SR_ERR_ARG; } +SR_PRIV int serial_readline(int fd, char **buf, int *buflen, + uint64_t timeout_ms) +{ + uint64_t start; + int maxlen, len; + + timeout_ms *= 1000; + start = g_get_monotonic_time(); + + maxlen = *buflen; + *buflen = len = 0; + while(1) { + len = maxlen - *buflen - 1; + if (len < 1) + break; + len = serial_read(fd, *buf + *buflen, 1); + if (len > 0) { + *buflen += len; + *(*buf + *buflen) = '\0'; + if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { + /* Strip LF and terminate. */ + *(*buf + --*buflen) = '\0'; + break; + } + } + if (g_get_monotonic_time() - start > timeout_ms) + /* Timeout */ + break; + g_usleep(2000); + } + sr_dbg("Received %d: '%s'.", *buflen, *buf); + + return SR_OK; +} diff --git a/hardware/fluke-dmm/api.c b/hardware/fluke-dmm/api.c index 69ce1724..5786438d 100644 --- a/hardware/fluke-dmm/api.c +++ b/hardware/fluke-dmm/api.c @@ -18,14 +18,14 @@ */ #include -#include "libsigrok.h" -#include "libsigrok-internal.h" -#include "fluke-dmm.h" #include #include #include #include #include +#include "libsigrok.h" +#include "libsigrok-internal.h" +#include "fluke-dmm.h" static const int hwopts[] = { SR_HWOPT_CONN, @@ -95,40 +95,6 @@ static int hw_init(void) return SR_OK; } -static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms) -{ - uint64_t start; - int maxlen, len; - - timeout_ms *= 1000; - start = g_get_monotonic_time(); - - maxlen = *buflen; - *buflen = len = 0; - while(1) { - len = maxlen - *buflen - 1; - if (len < 1) - break; - len = serial_read(fd, *buf + *buflen, 1); - if (len > 0) { - *buflen += len; - *(*buf + *buflen) = '\0'; - if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { - /* Strip LF and terminate. */ - *(*buf + --*buflen) = '\0'; - break; - } - } - if (g_get_monotonic_time() - start > timeout_ms) - /* Timeout */ - break; - g_usleep(2000); - } - sr_dbg("Received %d: '%s'.", *buflen, *buf); - - return SR_OK; -} - static GSList *fluke_scan(const char *conn, const char *serialcomm) { struct sr_dev_inst *sdi; diff --git a/hardware/radioshack-dmm/api.c b/hardware/radioshack-dmm/api.c index abfe2072..e9abf830 100644 --- a/hardware/radioshack-dmm/api.c +++ b/hardware/radioshack-dmm/api.c @@ -19,14 +19,14 @@ */ #include -#include "libsigrok.h" -#include "libsigrok-internal.h" -#include "radioshack-dmm.h" #include #include #include #include #include +#include "libsigrok.h" +#include "libsigrok-internal.h" +#include "radioshack-dmm.h" static const int hwopts[] = { SR_HWOPT_CONN, @@ -93,40 +93,6 @@ static int hw_init(void) return SR_OK; } -static int serial_readline(int fd, char **buf, size_t *buflen, - uint64_t timeout_ms) -{ - uint64_t start; - int maxlen, len; - - timeout_ms *= 1000; - start = g_get_monotonic_time(); - - maxlen = *buflen; - *buflen = len = 0; - while(1) { - len = maxlen - *buflen - 1; - if (len < 1) - break; - len = serial_read(fd, *buf + *buflen, 1); - if (len > 0) { - *buflen += len; - *(*buf + *buflen) = '\0'; - if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { - /* Strip LF and terminate. */ - *(*buf + --*buflen) = '\0'; - break; - } - } - if (g_get_monotonic_time() - start > timeout_ms) - /* Timeout */ - break; - g_usleep(2000); - } - - return SR_OK; -} - static GSList *rs_22_812_scan(const char *conn, const char *serialcomm) { struct sr_dev_inst *sdi; diff --git a/hardware/tekpower-dmm/api.c b/hardware/tekpower-dmm/api.c index 74f190b2..2e449166 100644 --- a/hardware/tekpower-dmm/api.c +++ b/hardware/tekpower-dmm/api.c @@ -89,40 +89,6 @@ static int hw_init(void) return SR_OK; } -static int serial_readline(int fd, char **buf, int *buflen, - uint64_t timeout_ms) -{ - uint64_t start; - int maxlen, len; - - timeout_ms *= 1000; - start = g_get_monotonic_time(); - - maxlen = *buflen; - *buflen = len = 0; - while (1) { - len = maxlen - *buflen - 1; - if (len < 1) - break; - len = serial_read(fd, *buf + *buflen, 1); - if (len > 0) { - *buflen += len; - *(*buf + *buflen) = '\0'; - if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { - /* Strip LF and terminate. */ - *(*buf + --*buflen) = '\0'; - break; - } - } - if (g_get_monotonic_time() - start > timeout_ms) - /* Timeout */ - break; - g_usleep(2000); - } - - return SR_OK; -} - static GSList *lcd14_scan(const char *conn, const char *serialcomm) { struct sr_dev_inst *sdi; diff --git a/libsigrok-internal.h b/libsigrok-internal.h index f491a3d1..b99ae6ab 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -135,6 +135,8 @@ SR_PRIV void serial_restore_params(int fd, void *backup); SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, int stopbits, int flowcontrol); SR_PRIV int serial_set_paramstr(int fd, const char *paramstr); +SR_PRIV int serial_readline(int fd, char **buf, int *buflen, + uint64_t timeout_ms); /*--- hardware/common/ezusb.c -----------------------------------------------*/