]> sigrok.org Git - libsigrok.git/blobdiff - src/lcr/es51919.c
drivers: Provide proper drvopts.
[libsigrok.git] / src / lcr / es51919.c
index 862374129e3f836a226333cb413c86299dafbcb2..eeb724ec20cef49de7992374d84c0860cafa33f6 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdint.h>
 #include <string.h>
 #include <math.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "es51919"
@@ -250,32 +251,6 @@ static int serial_stream_check(struct sr_serial_dev_inst *serial,
                                       is_valid, timeout_ms, baudrate);
 }
 
-struct std_opt_desc {
-       const uint32_t *scanopts;
-       const int num_scanopts;
-       const uint32_t *devopts;
-       const int num_devopts;
-};
-
-static int std_config_list(uint32_t key, GVariant **data,
-                          const struct std_opt_desc *d)
-{
-       switch (key) {
-       case SR_CONF_SCAN_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       d->scanopts, d->num_scanopts, sizeof(uint32_t));
-               break;
-       case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       d->devopts, d->num_devopts, sizeof(uint32_t));
-               break;
-       default:
-               return SR_ERR_NA;
-       }
-
-       return SR_OK;
-}
-
 static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
 {
        struct sr_datafeed_packet packet;
@@ -305,7 +280,6 @@ static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key,
        sr_config_free(cfg);
 
        return ret;
-
 }
 
 /*
@@ -403,24 +377,10 @@ static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key,
 
 #define PACKET_SIZE 17
 
-static const uint64_t frequencies[] = {
+static const double frequencies[] = {
        100, 120, 1000, 10000, 100000, 0,
 };
 
-enum { QUANT_AUTO = 5, };
-
-static const char *const quantities1[] = {
-       "NONE", "INDUCTANCE", "CAPACITANCE", "RESISTANCE", "RESISTANCE", "AUTO",
-};
-
-static const char *const list_quantities1[] = {
-       "NONE", "INDUCTANCE", "CAPACITANCE", "RESISTANCE", "AUTO",
-};
-
-static const char *const quantities2[] = {
-       "NONE", "DISSIPATION", "QUALITY", "RESISTANCE", "ANGLE", "AUTO",
-};
-
 enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, };
 
 static const char *const models[] = {
@@ -429,9 +389,6 @@ static const char *const models[] = {
 
 /** Private, per-device-instance driver context. */
 struct dev_context {
-       /** Opaque pointer passed in by the frontend. */
-       void *cb_data;
-
        /** The number of frames. */
        struct dev_limit_counter frame_count;
 
@@ -444,12 +401,6 @@ struct dev_context {
        /** The frequency of the test signal (index to frequencies[]). */
        unsigned int freq;
 
-       /** Measured primary quantity (index to quantities1[]). */
-       unsigned int quant1;
-
-       /** Measured secondary quantity (index to quantities2[]). */
-       unsigned int quant2;
-
        /** Equivalent circuit model (index to models[]). */
        unsigned int model;
 };
@@ -467,7 +418,7 @@ static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
 
        switch (is_secondary << 8 | buf[0]) {
        case 0x001:
-               return is_parallel ?
+               return is_parallel ?
                        SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE;
        case 0x002:
                return is_parallel ?
@@ -490,18 +441,19 @@ static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
 
        sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]);
 
-       return -1;
+       return 0;
 }
 
-static float parse_value(const uint8_t *buf)
+static float parse_value(const uint8_t *buf, int *digits)
 {
-       static const float decimals[] = {
-               1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7
-       };
+       static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7};
+       int exponent;
        int16_t val;
 
+       exponent = exponents[buf[3] & 7];
+       *digits = -exponent;
        val = (buf[1] << 8) | buf[2];
-       return (float)val * decimals[buf[3] & 7];
+       return (float)val * powf(10, exponent);
 }
 
 static void parse_measurement(const uint8_t *pkt, float *floatval,
@@ -510,31 +462,32 @@ static void parse_measurement(const uint8_t *pkt, float *floatval,
 {
        static const struct {
                int unit;
-               float mult;
+               int exponent;
        } units[] = {
-               { SR_UNIT_UNITLESS, 1 },        /* no unit */
-               { SR_UNIT_OHM, 1 },             /* Ohm     */
-               { SR_UNIT_OHM, 1e3 },           /* kOhm    */
-               { SR_UNIT_OHM, 1e6 },           /* MOhm    */
-               { -1, 0 },                      /* ???     */
-               { SR_UNIT_HENRY, 1e-6 },        /* uH      */
-               { SR_UNIT_HENRY, 1e-3 },        /* mH      */
-               { SR_UNIT_HENRY, 1 },           /* H       */
-               { SR_UNIT_HENRY, 1e3 },         /* kH      */
-               { SR_UNIT_FARAD, 1e-12 },       /* pF      */
-               { SR_UNIT_FARAD, 1e-9 },        /* nF      */
-               { SR_UNIT_FARAD, 1e-6 },        /* uF      */
-               { SR_UNIT_FARAD, 1e-3 },        /* mF      */
-               { SR_UNIT_PERCENTAGE, 1 },      /* %       */
-               { SR_UNIT_DEGREE, 1 }           /* degree  */
+               { SR_UNIT_UNITLESS,   0 }, /* no unit */
+               { SR_UNIT_OHM,        0 }, /* Ohm */
+               { SR_UNIT_OHM,        3 }, /* kOhm */
+               { SR_UNIT_OHM,        6 }, /* MOhm */
+               { -1,                 0 }, /* ??? */
+               { SR_UNIT_HENRY,     -6 }, /* uH */
+               { SR_UNIT_HENRY,     -3 }, /* mH */
+               { SR_UNIT_HENRY,      0 }, /* H */
+               { SR_UNIT_HENRY,      3 }, /* kH */
+               { SR_UNIT_FARAD,    -12 }, /* pF */
+               { SR_UNIT_FARAD,     -9 }, /* nF */
+               { SR_UNIT_FARAD,     -6 }, /* uF */
+               { SR_UNIT_FARAD,     -3 }, /* mF */
+               { SR_UNIT_PERCENTAGE, 0 }, /* % */
+               { SR_UNIT_DEGREE,     0 }, /* degree */
        };
        const uint8_t *buf;
+       int digits, exponent;
        int state;
 
        buf = pkt_to_buf(pkt, is_secondary);
 
-       analog->mq = -1;
-       analog->mqflags = 0;
+       analog->meaning->mq = 0;
+       analog->meaning->mqflags = 0;
 
        state = buf[4] & 0xf;
 
@@ -548,27 +501,30 @@ static void parse_measurement(const uint8_t *pkt, float *floatval,
 
        if (!is_secondary) {
                if (pkt[2] & 0x01)
-                       analog->mqflags |= SR_MQFLAG_HOLD;
+                       analog->meaning->mqflags |= SR_MQFLAG_HOLD;
                if (pkt[2] & 0x02)
-                       analog->mqflags |= SR_MQFLAG_REFERENCE;
+                       analog->meaning->mqflags |= SR_MQFLAG_REFERENCE;
        } else {
                if (pkt[2] & 0x04)
-                       analog->mqflags |= SR_MQFLAG_RELATIVE;
+                       analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
        }
 
-       if ((analog->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) < 0)
+       if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0)
                return;
 
        if ((buf[3] >> 3) >= ARRAY_SIZE(units)) {
                sr_err("Unknown unit %u.", buf[3] >> 3);
-               analog->mq = -1;
+               analog->meaning->mq = 0;
                return;
        }
 
-       analog->unit = units[buf[3] >> 3].unit;
+       analog->meaning->unit = units[buf[3] >> 3].unit;
 
-       *floatval = parse_value(buf);
-       *floatval *= (state == 0) ? units[buf[3] >> 3].mult : INFINITY;
+       exponent = units[buf[3] >> 3].exponent;
+       *floatval = parse_value(buf, &digits);
+       *floatval *= (state == 0) ? powf(10, exponent) : INFINITY;
+       analog->encoding->digits = digits - exponent;
+       analog->spec->spec_digits = digits - exponent;
 }
 
 static unsigned int parse_freq(const uint8_t *pkt)
@@ -585,18 +541,6 @@ static unsigned int parse_freq(const uint8_t *pkt)
        return freq;
 }
 
-static unsigned int parse_quant(const uint8_t *pkt, int is_secondary)
-{
-       const uint8_t *buf;
-
-       if (pkt[2] & 0x20)
-               return QUANT_AUTO;
-
-       buf = pkt_to_buf(pkt, is_secondary);
-
-       return buf[0];
-}
-
 static unsigned int parse_model(const uint8_t *pkt)
 {
        if (pkt[2] & 0x40)
@@ -607,7 +551,6 @@ static unsigned int parse_model(const uint8_t *pkt)
                return MODEL_PAR;
        else
                return MODEL_SER;
-
 }
 
 static gboolean packet_valid(const uint8_t *pkt)
@@ -628,29 +571,13 @@ static gboolean packet_valid(const uint8_t *pkt)
 static int do_config_update(struct sr_dev_inst *sdi, uint32_t key,
                            GVariant *var)
 {
-       struct dev_context *devc;
-
-       devc = sdi->priv;
-
-       return send_config_update_key(devc->cb_data, key, var);
+       return send_config_update_key(sdi, key, var);
 }
 
 static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq)
 {
        return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY,
-                               g_variant_new_uint64(frequencies[freq]));
-}
-
-static int send_quant1_update(struct sr_dev_inst *sdi, unsigned int quant)
-{
-       return do_config_update(sdi, SR_CONF_MEASURED_QUANTITY,
-                               g_variant_new_string(quantities1[quant]));
-}
-
-static int send_quant2_update(struct sr_dev_inst *sdi, unsigned int quant)
-{
-       return do_config_update(sdi, SR_CONF_MEASURED_2ND_QUANTITY,
-                               g_variant_new_string(quantities2[quant]));
+                               g_variant_new_double(frequencies[freq]));
 }
 
 static int send_model_update(struct sr_dev_inst *sdi, unsigned int model)
@@ -663,6 +590,9 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
        struct dev_context *devc;
        unsigned int val;
        float floatval;
@@ -678,22 +608,6 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
                        return;
        }
 
-       val = parse_quant(pkt, 0);
-       if (val != devc->quant1) {
-               if (send_quant1_update(sdi, val) == SR_OK)
-                       devc->quant1 = val;
-               else
-                       return;
-       }
-
-       val = parse_quant(pkt, 1);
-       if (val != devc->quant2) {
-               if (send_quant2_update(sdi, val) == SR_OK)
-                       devc->quant2 = val;
-               else
-                       return;
-       }
-
        val = parse_model(pkt);
        if (val != devc->model) {
                if (send_model_update(sdi, val) == SR_OK)
@@ -704,46 +618,50 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
 
        frame = FALSE;
 
-       memset(&analog, 0, sizeof(analog));
+       /* Note: digits/spec_digits will be overridden later. */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
 
        analog.num_samples = 1;
        analog.data = &floatval;
 
-       analog.channels = g_slist_append(NULL, sdi->channels->data);
+       analog.meaning->channels = g_slist_append(NULL, sdi->channels->data);
 
        parse_measurement(pkt, &floatval, &analog, 0);
-       if (analog.mq >= 0) {
+       if (analog.meaning->mq != 0) {
                if (!frame) {
                        packet.type = SR_DF_FRAME_BEGIN;
-                       sr_session_send(devc->cb_data, &packet);
+                       sr_session_send(sdi, &packet);
                        frame = TRUE;
                }
 
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
 
-               sr_session_send(devc->cb_data, &packet);
+               sr_session_send(sdi, &packet);
        }
 
-       analog.channels = g_slist_append(NULL, sdi->channels->next->data);
+       g_slist_free(analog.meaning->channels);
+       analog.meaning->channels = g_slist_append(NULL, sdi->channels->next->data);
 
        parse_measurement(pkt, &floatval, &analog, 1);
-       if (analog.mq >= 0) {
+       if (analog.meaning->mq != 0) {
                if (!frame) {
                        packet.type = SR_DF_FRAME_BEGIN;
-                       sr_session_send(devc->cb_data, &packet);
+                       sr_session_send(sdi, &packet);
                        frame = TRUE;
                }
 
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
 
-               sr_session_send(devc->cb_data, &packet);
+               sr_session_send(sdi, &packet);
        }
 
+       g_slist_free(analog.meaning->channels);
+
        if (frame) {
                packet.type = SR_DF_FRAME_END;
-               sr_session_send(devc->cb_data, &packet);
+               sr_session_send(sdi, &packet);
                dev_limit_counter_inc(&devc->frame_count);
        }
 }
@@ -787,37 +705,21 @@ static int receive_data(int fd, int revents, void *cb_data)
 
        if (dev_limit_counter_limit_reached(&devc->frame_count) ||
            dev_time_limit_reached(&devc->time_count))
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
+               sr_dev_acquisition_stop(sdi);
 
        return TRUE;
 }
 
-static int add_channel(struct sr_dev_inst *sdi, int idx, const char *name)
-{
-       struct sr_channel *ch;
-
-       ch = sr_channel_new(idx, SR_CHANNEL_ANALOG, TRUE, name);
-       sdi->channels = g_slist_append(sdi->channels, ch);
-
-       return SR_OK;
-}
-
 static const char *const channel_names[] = { "P1", "P2" };
 
 static int setup_channels(struct sr_dev_inst *sdi)
 {
        unsigned int i;
-       int ret;
 
-       ret = SR_ERR_BUG;
+       for (i = 0; i < ARRAY_SIZE(channel_names); i++)
+               sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
 
-       for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
-               ret = add_channel(sdi, i, channel_names[i]);
-               if (ret != SR_OK)
-                       break;
-       }
-
-       return ret;
+       return SR_OK;
 }
 
 SR_PRIV void es51919_serial_clean(void *priv)
@@ -871,10 +773,8 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
 
 scan_cleanup:
        es51919_serial_clean(devc);
-       if (sdi)
-               sr_dev_inst_free(sdi);
-       if (serial)
-               sr_serial_dev_inst_free(serial);
+       sr_dev_inst_free(sdi);
+       sr_serial_dev_inst_free(serial);
 
        return NULL;
 }
@@ -887,24 +787,16 @@ SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data,
 
        (void)cg;
 
-       if (!(devc = sdi->priv))
-               return SR_ERR_BUG;
+       devc = sdi->priv;
 
        switch (key) {
        case SR_CONF_OUTPUT_FREQUENCY:
-               *data = g_variant_new_uint64(frequencies[devc->freq]);
-               break;
-       case SR_CONF_MEASURED_QUANTITY:
-               *data = g_variant_new_string(quantities1[devc->quant1]);
-               break;
-       case SR_CONF_MEASURED_2ND_QUANTITY:
-               *data = g_variant_new_string(quantities2[devc->quant2]);
+               *data = g_variant_new_double(frequencies[devc->freq]);
                break;
        case SR_CONF_EQUIV_CIRCUIT_MODEL:
                *data = g_variant_new_string(models[devc->model]);
                break;
        default:
-               sr_spew("%s: Unsupported key %u", __func__, key);
                return SR_ERR_NA;
        }
 
@@ -947,75 +839,52 @@ static const uint32_t scanopts[] = {
        SR_CONF_SERIALCOMM,
 };
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_LCRMETER,
+};
+
+static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
        SR_CONF_LIMIT_MSEC | SR_CONF_SET,
        SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST,
-       SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_LIST,
-       SR_CONF_MEASURED_2ND_QUANTITY | SR_CONF_GET | SR_CONF_LIST,
        SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST,
 };
 
-static const struct std_opt_desc opts = {
-       scanopts, ARRAY_SIZE(scanopts),
-       devopts, ARRAY_SIZE(devopts),
-};
-
 SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
                                       const struct sr_dev_inst *sdi,
                                       const struct sr_channel_group *cg)
 {
-       (void)sdi;
-       (void)cg;
-
-       if (std_config_list(key, data, &opts) == SR_OK)
-               return SR_OK;
-
        switch (key) {
+       case SR_CONF_SCAN_OPTIONS:
+       case SR_CONF_DEVICE_OPTIONS:
+               return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
        case SR_CONF_OUTPUT_FREQUENCY:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
-                       frequencies, ARRAY_SIZE(frequencies), sizeof(uint64_t));
-               break;
-       case SR_CONF_MEASURED_QUANTITY:
-               *data = g_variant_new_strv(list_quantities1,
-                                          ARRAY_SIZE(list_quantities1));
-               break;
-       case SR_CONF_MEASURED_2ND_QUANTITY:
-               *data = g_variant_new_strv(quantities2,
-                                          ARRAY_SIZE(quantities2));
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE,
+                       frequencies, ARRAY_SIZE(frequencies), sizeof(double));
                break;
        case SR_CONF_EQUIV_CIRCUIT_MODEL:
                *data = g_variant_new_strv(models, ARRAY_SIZE(models));
                break;
        default:
-               sr_spew("%s: Unsupported key %u", __func__, key);
                return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
-                                            void *cb_data)
+SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        if (!(devc = sdi->priv))
                return SR_ERR_BUG;
 
-       devc->cb_data = cb_data;
-
        dev_limit_counter_start(&devc->frame_count);
        dev_time_counter_start(&devc->time_count);
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi);
 
        /* Poll every 50ms, or whenever some data comes in. */
        serial = sdi->conn;
@@ -1024,10 +893,3 @@ SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
 
        return SR_OK;
 }
-
-SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi,
-                                           void *cb_data)
-{
-       return std_serial_dev_acquisition_stop(sdi, cb_data,
-                       std_serial_dev_close, sdi->conn, LOG_PREFIX);
-}