]> sigrok.org Git - libsigrok.git/blobdiff - hardware/gmc-mh-1x-2x/api.c
ikalogic-scanalogic2: Publish SR_CONF_MAX_UNCOMPRESSED_SAMPLES.
[libsigrok.git] / hardware / gmc-mh-1x-2x / api.c
index 42969412025c7ebc7d1e8cdfe0f7d9c1142eff6d..197522a2cf3baccd7bfc103bb0f6b0affb63e062 100644 (file)
@@ -18,7 +18,6 @@
  */
 
 #include <string.h>
-
 #include "protocol.h"
 
 /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
@@ -27,6 +26,7 @@
 #define VENDOR_GMC "Gossen Metrawatt"
 
 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
+static struct sr_dev_driver *di = &gmc_mh_1x_2x_rs232_driver_info;
 
 static const int32_t hwopts[] = {
        SR_CONF_CONN,
@@ -40,20 +40,20 @@ static const int32_t hwcaps[] = {
        SR_CONF_CONTINUOUS,
 };
 
-/** Driver init function */
-static int init_1x_2x_rs232(struct sr_context *sr_ctx)
+static int init(struct sr_context *sr_ctx)
 {
-       return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX);
+       return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-
-/** Read single byte from serial port.
- *  \retval -1 Timeout or error.
- *  \retval other Byte.
+/**
+ * Read single byte from serial port.
+ *
+ * @retval -1 Timeout or error.
+ * @retval other Byte.
  */
 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
 {
-       guint8 result = 0;
+       uint8_t result = 0;
        int rc = 0;
 
        for (;;) {
@@ -68,11 +68,13 @@ static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
        }
 }
 
-/** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
- *  \param serial Configured, open serial port.
+/**
+ * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
+ *
+ * @param serial Configured, open serial port.
  *
- *  \retval NULL Detection failed.
- *  \retval other Model code.
+ * @retval NULL Detection failed.
+ * @retval other Model code.
  */
 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
 {
@@ -80,25 +82,27 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
        enum model model;
        gint64 timeout_us;
 
-       model = SR_METRAHIT_NONE;
-       timeout_us = g_get_monotonic_time() + 1*1000*1000;
+       model = METRAHIT_NONE;
+       timeout_us = g_get_monotonic_time() + 1 * 1000 * 1000;
 
-       /* Try to find message consisting of device code and several
-        * (at least 4) data bytes. */
+       /*
+        * Try to find message consisting of device code and several
+        * (at least 4) data bytes.
+        */
        for (bytecnt = 0; bytecnt < 100; bytecnt++) {
                byte = read_byte(serial, timeout_us);
                if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
                        break;
                if ((byte & MSGID_MASK) == MSGID_INF) {
-                       if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK)))
+                       if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
                                break;
-                       /* Now expect (at least) 4 data bytes */
+                       /* Now expect (at least) 4 data bytes. */
                        for (cnt = 0; cnt < 4; cnt++) {
                                byte = read_byte(serial, timeout_us);
                                if ((byte == -1) ||
                                        ((byte & MSGID_MASK) != MSGID_DATA))
                                {
-                                       model = SR_METRAHIT_NONE;
+                                       model = METRAHIT_NONE;
                                        bytecnt = 100;
                                        break;
                                }
@@ -110,13 +114,16 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
        return model;
 }
 
-/** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
- *  'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud.
- *  The DMM usually sends up to about 20 messages per second. However, depending
- *  on configuration and measurement mode the intervals can be much larger and
- *  then the detection might not work.
+/**
+ * Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
+ * 'RS232' interface.
+ *
+ * The older 1x models use 8192 baud and the newer 2x 9600 baud.
+ * The DMM usually sends up to about 20 messages per second. However, depending
+ * on configuration and measurement mode the intervals can be much larger and
+ * then the detection might not work.
  */
-static GSList *scan_1x_2x_rs232(GSList *options)
+static GSList *scan(GSList *options)
 {
        struct sr_dev_inst *sdi;
        struct drv_context *drvc;
@@ -128,16 +135,15 @@ static GSList *scan_1x_2x_rs232(GSList *options)
        const char *conn, *serialcomm;
        enum model model;
        gboolean serialcomm_given;
-       int cnt;
 
        devices = NULL;
-       drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv;
+       drvc = di->priv;
        drvc->instances = NULL;
        conn = serialcomm = NULL;
-       model = SR_METRAHIT_NONE;
+       model = METRAHIT_NONE;
        serialcomm_given = FALSE;
 
-       sr_spew("scan_1x_2x_rs232() called!");
+       sr_spew("scan() called!");
 
        for (l = options; l; l = l->next) {
                src = l->data;
@@ -168,9 +174,11 @@ static GSList *scan_1x_2x_rs232(GSList *options)
 
        model = scan_model_sm(serial);
 
-       /* If detection failed and no user-supplied parameters,
-        * try second baud rate. */
-       if ((model == SR_METRAHIT_NONE) && !serialcomm_given) {
+       /*
+        * If detection failed and no user-supplied parameters,
+        * try second baud rate.
+        */
+       if ((model == METRAHIT_NONE) && !serialcomm_given) {
                serialcomm = SERIALCOMM_1X_RS232;
                g_free(serial->serialcomm);
                serial->serialcomm = g_strdup(serialcomm);
@@ -180,10 +188,10 @@ static GSList *scan_1x_2x_rs232(GSList *options)
                }
        }
 
-       if (model != SR_METRAHIT_NONE) {
-               sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model));
+       if (model != METRAHIT_NONE) {
+               sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
                if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC,
-                               sr_gmc_model_str(model), "")))
+                               gmc_model_str(model), "")))
                        return NULL;
                if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
                        sr_err("Device context malloc failed.");
@@ -198,7 +206,7 @@ static GSList *scan_1x_2x_rs232(GSList *options)
 
                sdi->conn = serial;
                sdi->priv = devc;
-               sdi->driver = &gmc_mh_1x_2x_rs232_driver_info;
+               sdi->driver = di;
                if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
                        return NULL;
                sdi->probes = g_slist_append(sdi->probes, probe);
@@ -209,39 +217,21 @@ static GSList *scan_1x_2x_rs232(GSList *options)
        return devices;
 }
 
-/** Driver device list function */
-static GSList *dev_list_1x_2x_rs232(void)
+static GSList *dev_list(void)
 {
-       return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))
-                       ->instances;
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
-static int dev_clear_1x_2x_rs232(void)
+static int dev_clear(void)
 {
-       return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL);
-}
-
-static int dev_open(struct sr_dev_inst *sdi)
-{
-       struct sr_serial_dev_inst *serial = sdi->conn;
-       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
-               return SR_ERR;
-
-       sdi->status = SR_ST_ACTIVE;
-
-       return SR_OK;
+       return std_dev_clear(di, NULL);
 }
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_serial_dev_inst *serial;
        struct dev_context *devc;
 
-       serial = sdi->conn;
-       if (serial && serial->fd != -1) {
-               serial_close(serial);
-               sdi->status = SR_ST_INACTIVE;
-       }
+       std_serial_dev_close(sdi);
 
        sdi->status = SR_ST_INACTIVE;
 
@@ -249,15 +239,15 @@ static int dev_close(struct sr_dev_inst *sdi)
        if ((devc = sdi->priv) && devc->elapsed_msec) {
                g_timer_destroy(devc->elapsed_msec);
                devc->elapsed_msec = NULL;
-               devc->model = SR_METRAHIT_NONE;
+               devc->model = METRAHIT_NONE;
        }
 
        return SR_OK;
 }
 
-static int cleanup_sm_rs232(void)
+static int cleanup(void)
 {
-       return dev_clear_1x_2x_rs232();
+       return dev_clear();
 }
 
 /** TODO */
@@ -321,6 +311,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_probe_group *probe_group)
 {
        (void)sdi;
+       (void)probe_group;
 
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
@@ -338,8 +329,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
@@ -363,13 +353,13 @@ static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
 
        /* Poll every 40ms, or whenever some data comes in. */
        serial = sdi->conn;
-       sr_source_add(serial->fd, G_IO_IN, 40, gmc_mh_1x_2x_receive_data,
+       serial_source_add(serial, G_IO_IN, 40, gmc_mh_1x_2x_receive_data,
                (void *)sdi);
 
        return SR_OK;
 }
 
-static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
 
@@ -377,26 +367,25 @@ static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data)
        if (sdi && (devc = sdi->priv) && devc->limit_msec)
                g_timer_stop(devc->elapsed_msec);
 
-       return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
+       return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
                        sdi->conn, LOG_PREFIX);
 }
 
 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
        .name = "gmc-mh-1x-2x-rs232",
-       .longname =
-               "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface",
+       .longname = "Gossen Metrawatt Metrahit 1x/2x, 'RS232' interface",
        .api_version = 1,
-       .init = init_1x_2x_rs232,
-       .cleanup = cleanup_sm_rs232,
-       .scan = scan_1x_2x_rs232,
-       .dev_list = dev_list_1x_2x_rs232,
-       .dev_clear = dev_clear_1x_2x_rs232,
+       .init = init,
+       .cleanup = cleanup,
+       .scan = scan,
+       .dev_list = dev_list,
+       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
-       .dev_open = dev_open,
+       .dev_open = std_serial_dev_open,
        .dev_close = dev_close,
-       .dev_acquisition_start = dev_acq_start_1x_2x_rs232,
-       .dev_acquisition_stop = dev_acq_stop,
+       .dev_acquisition_start = dev_acquisition_start,
+       .dev_acquisition_stop = dev_acquisition_stop,
        .priv = NULL,
 };