]> sigrok.org Git - libsigrok.git/commitdiff
Factor out serial_readline() to serial.c.
authorUwe Hermann <redacted>
Tue, 30 Oct 2012 18:59:21 +0000 (19:59 +0100)
committerUwe Hermann <redacted>
Tue, 30 Oct 2012 18:59:21 +0000 (19:59 +0100)
Only one (slightly different) variant remains in agilent-dmm, this will
be merged soon too, though.

hardware/agilent-dmm/api.c
hardware/common/serial.c
hardware/fluke-dmm/api.c
hardware/radioshack-dmm/api.c
hardware/tekpower-dmm/api.c
libsigrok-internal.h

index b6af3bbdfb474a2ceb468eb86d7a4993a30f68d1..7dfb2c65dfe7dc2f5a6aca14ed9a3ab345f46740 100644 (file)
  */
 
 #include <glib.h>
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
-#include "agilent-dmm.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
+#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;
 
index 651b58269c74cf6ccee09484afe83f714ec426c4..4a92a6684eba4edac865ab4c6ccd6658e1989877 100644 (file)
@@ -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;
+}
index 69ce1724c7be2c3b063d3727dfa56cb51a7300db..5786438d9188e1b2c6f37a5fd776edb1725576f4 100644 (file)
  */
 
 #include <glib.h>
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
-#include "fluke-dmm.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
+#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;
index abfe20728208b16871836b82c93ef849656ee5d7..e9abf83068efc803e3e957ec28cd6aebf269e624 100644 (file)
  */
 
 #include <glib.h>
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
-#include "radioshack-dmm.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
+#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;
index 74f190b2dfc5d78686c2f905c6eb06b42e42872e..2e4491661d0829f8fac0a96cb81c75d4942faf3a 100644 (file)
@@ -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;
index f491a3d1e9bf01a65126e3141512f165bc5c286a..b99ae6abafcadd586b03cdb20c260b24813380d5 100644 (file)
@@ -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 -----------------------------------------------*/