]> sigrok.org Git - libsigrok.git/blobdiff - hardware/fluke-dmm/api.c
hw_init(): Save struct sr_context * parameter in struct drv_context
[libsigrok.git] / hardware / fluke-dmm / api.c
index 69ce1724c7be2c3b063d3727dfa56cb51a7300db..154479ccc25c039c67154bf7883f31562b7c023a 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,
@@ -81,72 +81,37 @@ static int clear_instances(void)
        return SR_OK;
 }
 
-static int hw_init(void)
+static int hw_init(struct sr_context *sr_ctx)
 {
        struct drv_context *drvc;
 
        if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
                sr_err("Driver context malloc failed.");
-               return SR_ERR;
+               return SR_ERR_MALLOC;
        }
 
+       drvc->sr_ctx = sr_ctx;
        di->priv = drvc;
 
        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;
        struct drv_context *drvc;
        struct dev_context *devc;
        struct sr_probe *probe;
+       struct sr_serial_dev_inst *serial;
        GSList *devices;
-       int fd, retry, len, i, s;
+       int retry, len, i, s;
        char buf[128], *b, **tokens;
 
-       if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
-               sr_err("Unable to open %s: %s.", conn, strerror(errno));
+       if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
-       }
-       if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
-               sr_err("Unable to set serial parameters.");
+
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
-       }
 
        drvc = di->priv;
        b = buf;
@@ -156,8 +121,8 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
         * is not in an idle state when we send ID. */
        while (!devices && retry < 3) {
                retry++;
-               serial_flush(fd);
-               if (serial_write(fd, "ID\r", 3) == -1) {
+               serial_flush(serial);
+               if (serial_write(serial, "ID\r", 3) == -1) {
                        sr_err("Unable to send ID string: %s.",
                               strerror(errno));
                        continue;
@@ -166,7 +131,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
                /* Response is first a CMD_ACK byte (ASCII '0' for OK,
                 * or '1' to signify an error. */
                len = 128;
-               serial_readline(fd, &b, &len, 150);
+               serial_readline(serial, &b, &len, 150);
                if (len != 1)
                        continue;
                if (buf[0] != '0')
@@ -174,7 +139,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
 
                /* If CMD_ACK was OK, ID string follows. */
                len = 128;
-               serial_readline(fd, &b, &len, 150);
+               serial_readline(serial, &b, &len, 150);
                if (len < 10)
                        continue;
                tokens = g_strsplit(buf, ",", 3);
@@ -193,8 +158,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
                                        return NULL;
                                }
                                devc->profile = &supported_flukedmm[i];
-                               devc->serial = sr_serial_dev_inst_new(conn, -1);
-                               devc->serialcomm = g_strdup(serialcomm);
+                               devc->serial = serial;
                                sdi->priv = devc;
                                sdi->driver = di;
                                if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
@@ -207,7 +171,9 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
                }
                g_strfreev(tokens);
        }
-       serial_close(fd);
+       serial_close(serial);
+       if (!devices)
+               sr_serial_dev_inst_free(serial);
 
        return devices;
 }
@@ -265,15 +231,9 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
-       if (devc->serial->fd == -1) {
-               sr_err("Couldn't open serial port '%s'.", devc->serial->port);
-               return SR_ERR;
-       }
-       if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
-               sr_err("Unable to set serial parameters.");
+       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
-       }
+
        sdi->status = SR_ST_ACTIVE;
 
        return SR_OK;
@@ -289,8 +249,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        }
 
        if (devc->serial && devc->serial->fd != -1) {
-               serial_close(devc->serial->fd);
-               devc->serial->fd = -1;
+               serial_close(devc->serial);
                sdi->status = SR_ST_INACTIVE;
        }
 
@@ -404,7 +363,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        /* Poll every 100ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
 
-       if (serial_write(devc->serial->fd, "QM\r", 3) == -1) {
+       if (serial_write(devc->serial, "QM\r", 3) == -1) {
                sr_err("Unable to send QM: %s.", strerror(errno));
                return SR_ERR;
        }
@@ -414,8 +373,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
-               void *cb_data)
+static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct sr_datafeed_packet packet;
        struct dev_context *devc;