scpi-dmm: Add support for Owon XDM2041
[libsigrok.git] / src / hardware / scpi-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <string.h>
21 #include "protocol.h"
22
23 static struct sr_dev_driver scpi_dmm_driver_info;
24
25 static const uint32_t scanopts[] = {
26         SR_CONF_CONN,
27         SR_CONF_SERIALCOMM,
28 };
29
30 static const uint32_t drvopts[] = {
31         SR_CONF_MULTIMETER,
32 };
33
34 static const uint32_t devopts_generic[] = {
35         SR_CONF_CONTINUOUS,
36         SR_CONF_CONN | SR_CONF_GET,
37         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39         SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 };
41
42 static const struct scpi_command cmdset_agilent[] = {
43         { DMM_CMD_SETUP_REMOTE, "\n", },
44         { DMM_CMD_SETUP_FUNC, "CONF:%s", },
45         { DMM_CMD_QUERY_FUNC, "CONF?", },
46         { DMM_CMD_START_ACQ, "MEAS", },
47         { DMM_CMD_STOP_ACQ, "ABORT", },
48         { DMM_CMD_QUERY_VALUE, "READ?", },
49         { DMM_CMD_QUERY_PREC, "CONF?", },
50         ALL_ZERO,
51 };
52
53 /*
54  * cmdset_hp is used for the 34401A, which was added to this code after the
55  * 34405A and 34465A. It differs in starting the measurement with INIT: using
56  * MEAS without a trailing '?' (as used for the 34405A) is not valid for the
57  * 34401A and gives an error.
58  * I'm surprised the same instruction sequence doesn't work and INIT may
59  * work for both, but I don't have the others to re-test.
60  *
61  * cmdset_hp also works well for the 34410A, using cmdset_agilent throws an
62  * error on 'MEAS' without a '?'.
63  *
64  * On the 34401A,
65  *  MEAS <optional parameters> ? configures, arms, triggers and waits
66  *       for a reading
67  *  CONF <parameters> configures
68  *  INIT prepares for triggering (trigger mode is not set, assumed
69  *       internal - external might time out)
70  *  *OPC waits for completion, and
71  *  READ? retrieves the result
72  */
73 static const struct scpi_command cmdset_hp[] = {
74         { DMM_CMD_SETUP_REMOTE, "\n", },
75         { DMM_CMD_SETUP_FUNC, "CONF:%s", },
76         { DMM_CMD_QUERY_FUNC, "CONF?", },
77         { DMM_CMD_START_ACQ, "INIT", },
78         { DMM_CMD_STOP_ACQ, "ABORT", },
79         { DMM_CMD_QUERY_VALUE, "READ?", },
80         { DMM_CMD_QUERY_PREC, "CONF?", },
81         ALL_ZERO,
82 };
83
84 static const struct scpi_command cmdset_gwinstek[] = {
85         { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
86         { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
87         { DMM_CMD_SETUP_FUNC, "CONF:%s", },
88         { DMM_CMD_QUERY_FUNC, "CONF:STAT:FUNC?", },
89         { DMM_CMD_START_ACQ, "*CLS;SYST:REM", },
90         { DMM_CMD_QUERY_VALUE, "VAL1?", },
91         { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
92         ALL_ZERO,
93 };
94
95 static const struct scpi_command cmdset_gwinstek_906x[] = {
96         { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
97         { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
98         { DMM_CMD_SETUP_FUNC, "CONF:%s", },
99         { DMM_CMD_QUERY_FUNC, "CONF?", },
100         { DMM_CMD_START_ACQ, "INIT", },
101         { DMM_CMD_STOP_ACQ, "ABORT", },
102         { DMM_CMD_QUERY_VALUE, "VAL1?", },
103         { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
104         ALL_ZERO,
105 };
106
107 static const struct scpi_command cmdset_owon[] = {
108         { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
109         { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
110         { DMM_CMD_SETUP_FUNC, "CONF:%s", },
111         { DMM_CMD_QUERY_FUNC, "FUNC?", },
112         { DMM_CMD_QUERY_VALUE, "MEAS1?", },
113         ALL_ZERO,
114 };
115
116 static const struct mqopt_item mqopts_agilent_34405a[] = {
117         { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
118         { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
119         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
120         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
121         { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
122         { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
123         { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, },
124         { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
125         { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, },
126         { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
127 };
128
129 static const struct mqopt_item mqopts_agilent_34401a[] = {
130         { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
131         { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
132         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
133         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
134         { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
135         { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES ", NO_DFLT_PREC, },
136         { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
137         { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
138         { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
139         { SR_MQ_TIME, 0, "PER", "PER ", NO_DFLT_PREC, },
140 };
141
142 static const struct mqopt_item mqopts_gwinstek_gdm8200a[] = {
143         { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "01", NO_DFLT_PREC, },
144         { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "02", NO_DFLT_PREC, },
145         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "03", NO_DFLT_PREC, },
146         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "04", NO_DFLT_PREC, },
147         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "05", NO_DFLT_PREC, }, /* mA */
148         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "06", NO_DFLT_PREC, }, /* mA */
149         { SR_MQ_RESISTANCE, 0, "RES", "07", NO_DFLT_PREC, },
150         { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "16", NO_DFLT_PREC, },
151         { SR_MQ_CONTINUITY, 0, "CONT", "13", -1, },
152         { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "17", -4, },
153         { SR_MQ_TEMPERATURE, 0, "TEMP", "09", NO_DFLT_PREC, }, /* Celsius */
154         { SR_MQ_TEMPERATURE, 0, "TEMP", "15", NO_DFLT_PREC, }, /* Fahrenheit */
155         { SR_MQ_FREQUENCY, 0, "FREQ", "08", NO_DFLT_PREC, },
156         { SR_MQ_TIME, 0, "PER", "14", NO_DFLT_PREC, },
157 };
158
159 static const struct mqopt_item mqopts_gwinstek_gdm906x[] = {
160         { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
161         { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC", NO_DFLT_PREC, },
162         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
163         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC", NO_DFLT_PREC, },
164         { SR_MQ_RESISTANCE, 0, "RES", "RES", NO_DFLT_PREC, },
165         { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES", NO_DFLT_PREC, },
166         { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
167         { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
168         { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP", NO_DFLT_PREC, },
169         { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ", NO_DFLT_PREC, },
170         { SR_MQ_TIME, 0, "PER", "PER", NO_DFLT_PREC, },
171         { SR_MQ_CAPACITANCE, 0, "CAP", "CAP", NO_DFLT_PREC, },
172 };
173
174 static const struct mqopt_item mqopts_owon_xdm2041[] = {
175         { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT AC", NO_DFLT_PREC, },
176         { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT", NO_DFLT_PREC, },
177         { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR AC", NO_DFLT_PREC, },
178         { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR", NO_DFLT_PREC, },
179         { SR_MQ_RESISTANCE, 0, "RES", "RES", NO_DFLT_PREC, },
180         { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES", NO_DFLT_PREC, },
181         { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
182         { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
183         { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP", NO_DFLT_PREC, },
184         { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ", NO_DFLT_PREC, },
185         { SR_MQ_CAPACITANCE, 0, "CAP", "CAP", NO_DFLT_PREC, },
186 };
187
188 SR_PRIV const struct scpi_dmm_model models[] = {
189         {
190                 "Agilent", "34405A",
191                 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
192                 scpi_dmm_get_meas_agilent,
193                 ARRAY_AND_SIZE(devopts_generic),
194                 0, 0,
195         },
196         {
197                 "Agilent", "34410A",
198                 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34405a),
199                 scpi_dmm_get_meas_agilent,
200                 ARRAY_AND_SIZE(devopts_generic),
201                 0, 0,
202         },
203         {
204                 "GW", "GDM8251A",
205                 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
206                 scpi_dmm_get_meas_gwinstek,
207                 ARRAY_AND_SIZE(devopts_generic),
208                 1000 * 2500, 0,
209         },
210         {
211                 "GW", "GDM8255A",
212                 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
213                 scpi_dmm_get_meas_gwinstek,
214                 ARRAY_AND_SIZE(devopts_generic),
215                 1000 * 2500, 0,
216         },
217         {
218                 "GWInstek", "GDM9060",
219                 1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
220                 scpi_dmm_get_meas_agilent,
221                 ARRAY_AND_SIZE(devopts_generic),
222                 0, 0,
223         },
224         {
225                 "GWInstek", "GDM9061",
226                 1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
227                 scpi_dmm_get_meas_agilent,
228                 ARRAY_AND_SIZE(devopts_generic),
229                 0, 0,
230         },
231         {
232                 "HP", "34401A",
233                 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a),
234                 scpi_dmm_get_meas_agilent,
235                 ARRAY_AND_SIZE(devopts_generic),
236                 /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */
237                 1000 * 1500, 0,
238         },
239         {
240                 "Keysight", "34465A",
241                 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
242                 scpi_dmm_get_meas_agilent,
243                 ARRAY_AND_SIZE(devopts_generic),
244                 0, 0,
245         },
246         {
247                 "OWON", "XDM2041",
248                 1, 5, cmdset_owon, ARRAY_AND_SIZE(mqopts_owon_xdm2041),
249                 scpi_dmm_get_meas_gwinstek,
250                 ARRAY_AND_SIZE(devopts_generic),
251                 0, 1e9,
252         },
253 };
254
255 static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model)
256 {
257         size_t i;
258         const struct scpi_dmm_model *entry;
259
260         for (i = 0; i < ARRAY_SIZE(models); i++) {
261                 entry = &models[i];
262                 if (!entry->vendor || !entry->model)
263                         continue;
264                 if (strcmp(vendor, entry->vendor) != 0)
265                         continue;
266                 if (strcmp(model, entry->model) != 0)
267                         continue;
268                 return entry;
269         }
270
271         return NULL;
272 }
273
274 /*
275  * Some devices (such as Owon XDM2041) do not support the standard
276  * OPeration Complete? command. This function tests the command with
277  * a short timeout, and returns TRUE if any reply (busy or not) is received.
278  */
279 static gboolean probe_opc_support(struct sr_scpi_dev_inst *scpi)
280 {
281         gboolean result;
282         GString *response;
283
284         response = g_string_sized_new(128);
285         result = TRUE;
286         if (sr_scpi_get_data(scpi, SCPI_CMD_OPC, &response) != SR_OK)
287                 result = FALSE;
288         g_string_free(response, TRUE);
289
290         return result;
291 }
292
293 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
294 {
295         struct sr_scpi_hw_info *hw_info;
296         int ret;
297         const char *vendor;
298         const struct scpi_dmm_model *model;
299         struct sr_dev_inst *sdi;
300         struct dev_context *devc;
301         size_t i;
302         gchar *channel_name;
303         const char *command;
304
305         if (!probe_opc_support(scpi))
306                 scpi->no_opc_command = TRUE;
307
308         scpi_dmm_cmd_delay(scpi);
309         ret = sr_scpi_get_hw_id(scpi, &hw_info);
310         if (ret != SR_OK) {
311                 sr_info("Could not get IDN response.");
312                 return NULL;
313         }
314         vendor = sr_vendor_alias(hw_info->manufacturer);
315         model = is_compatible(vendor, hw_info->model);
316         if (!model) {
317                 sr_scpi_hw_info_free(hw_info);
318                 return NULL;
319         }
320
321         sdi = g_malloc0(sizeof(*sdi));
322         sdi->vendor = g_strdup(hw_info->manufacturer);
323         sdi->model = g_strdup(hw_info->model);
324         sdi->version = g_strdup(hw_info->firmware_version);
325         sdi->serial_num = g_strdup(hw_info->serial_number);
326         sdi->conn = scpi;
327         sdi->driver = &scpi_dmm_driver_info;
328         sdi->inst_type = SR_INST_SCPI;
329         ret = sr_scpi_connection_id(scpi, &sdi->connection_id);
330         if (ret != SR_OK) {
331                 g_free(sdi->connection_id);
332                 sdi->connection_id = NULL;
333         }
334         sr_scpi_hw_info_free(hw_info);
335         if (model->read_timeout_us)  /* non-default read timeout */
336                 scpi->read_timeout_us = model->read_timeout_us;
337         devc = g_malloc0(sizeof(*devc));
338         sdi->priv = devc;
339         devc->num_channels = model->num_channels;
340         devc->cmdset = model->cmdset;
341         devc->model = model;
342
343         for (i = 0; i < devc->num_channels; i++) {
344                 channel_name = g_strdup_printf("P%zu", i + 1);
345                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
346         }
347
348         /*
349          * If device has DMM_CMD_SETUP_LOCAL command, send it now. To avoid
350          * leaving device in remote mode (if only a "scan" is run).
351          */
352         command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
353         if (command && *command) {
354                 scpi_dmm_cmd_delay(scpi);
355                 sr_scpi_send(scpi, command);
356         }
357
358         return sdi;
359 }
360
361 static GSList *scan(struct sr_dev_driver *di, GSList *options)
362 {
363         return sr_scpi_scan(di->context, options, probe_device);
364 }
365
366 static int dev_open(struct sr_dev_inst *sdi)
367 {
368         struct sr_scpi_dev_inst *scpi;
369         int ret;
370
371         scpi = sdi->conn;
372         ret = sr_scpi_open(scpi);
373         if (ret < 0) {
374                 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
375                 return SR_ERR;
376         }
377
378         return SR_OK;
379 }
380
381 static int dev_close(struct sr_dev_inst *sdi)
382 {
383         struct dev_context *devc;
384         struct sr_scpi_dev_inst *scpi;
385         const char *command;
386
387         devc = sdi->priv;
388         scpi = sdi->conn;
389         if (!scpi)
390                 return SR_ERR_BUG;
391
392         sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND);
393         if (sdi->status <= SR_ST_INACTIVE)
394                 return SR_OK;
395
396         /*
397          * If device has DMM_CMD_SETUP_LOCAL command, send it now
398          * to avoid leaving device in remote mode.
399          */
400         command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
401         if (command && *command) {
402                 scpi_dmm_cmd_delay(scpi);
403                 sr_scpi_send(scpi, command);
404         }
405
406         return sr_scpi_close(scpi);
407 }
408
409 static int config_get(uint32_t key, GVariant **data,
410         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
411 {
412         struct dev_context *devc;
413         enum sr_mq mq;
414         enum sr_mqflag mqflag;
415         GVariant *arr[2];
416         int ret;
417
418         (void)cg;
419
420         devc = sdi->priv;
421
422         switch (key) {
423         case SR_CONF_CONN:
424                 if (!sdi || !sdi->connection_id)
425                         return SR_ERR_NA;
426                 *data = g_variant_new_string(sdi->connection_id);
427                 return SR_OK;
428         case SR_CONF_LIMIT_SAMPLES:
429         case SR_CONF_LIMIT_MSEC:
430                 return sr_sw_limits_config_get(&devc->limits, key, data);
431         case SR_CONF_MEASURED_QUANTITY:
432                 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL, NULL);
433                 if (ret != SR_OK)
434                         return ret;
435                 arr[0] = g_variant_new_uint32(mq);
436                 arr[1] = g_variant_new_uint64(mqflag);
437                 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
438                 return SR_OK;
439         default:
440                 return SR_ERR_NA;
441         }
442 }
443
444 static int config_set(uint32_t key, GVariant *data,
445         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
446 {
447         struct dev_context *devc;
448         enum sr_mq mq;
449         enum sr_mqflag mqflag;
450         GVariant *tuple_child;
451
452         (void)cg;
453
454         devc = sdi->priv;
455
456         switch (key) {
457         case SR_CONF_LIMIT_SAMPLES:
458         case SR_CONF_LIMIT_MSEC:
459                 return sr_sw_limits_config_set(&devc->limits, key, data);
460         case SR_CONF_MEASURED_QUANTITY:
461                 tuple_child = g_variant_get_child_value(data, 0);
462                 mq = g_variant_get_uint32(tuple_child);
463                 g_variant_unref(tuple_child);
464                 tuple_child = g_variant_get_child_value(data, 1);
465                 mqflag = g_variant_get_uint64(tuple_child);
466                 g_variant_unref(tuple_child);
467                 return scpi_dmm_set_mq(sdi, mq, mqflag);
468         default:
469                 return SR_ERR_NA;
470         }
471 }
472
473 static int config_list(uint32_t key, GVariant **data,
474         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
475 {
476         struct dev_context *devc;
477         GVariant *gvar, *arr[2];
478         GVariantBuilder gvb;
479         size_t i;
480
481         (void)cg;
482
483         devc = sdi ? sdi->priv : NULL;
484
485         switch (key) {
486         case SR_CONF_SCAN_OPTIONS:
487         case SR_CONF_DEVICE_OPTIONS:
488                 if (!devc)
489                         return STD_CONFIG_LIST(key, data, sdi, cg,
490                                 scanopts, drvopts, devopts_generic);
491                 return std_opts_config_list(key, data, sdi, cg,
492                         ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
493                         devc->model->devopts, devc->model->devopts_size);
494         case SR_CONF_MEASURED_QUANTITY:
495                 /* TODO Use std_gvar_measured_quantities() when available. */
496                 if (!devc)
497                         return SR_ERR_ARG;
498                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
499                 for (i = 0; i < devc->model->mqopt_size; i++) {
500                         arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq);
501                         arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag);
502                         gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
503                         g_variant_builder_add_value(&gvb, gvar);
504                 }
505                 *data = g_variant_builder_end(&gvb);
506                 return SR_OK;
507         default:
508                 (void)devc;
509                 return SR_ERR_NA;
510         }
511 }
512
513 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
514 {
515         struct sr_scpi_dev_inst *scpi;
516         struct dev_context *devc;
517         int ret;
518         const struct mqopt_item *item;
519         const char *command;
520         char *response;
521
522         scpi = sdi->conn;
523         devc = sdi->priv;
524
525         ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq,
526                 &devc->start_acq_mq.curr_mqflag, NULL, &item);
527         if (ret != SR_OK)
528                 return ret;
529
530         /*
531          * Query for current precision if DMM supports the command
532          */
533         command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_PREC);
534         if (command && *command) {
535                 scpi_dmm_cmd_delay(scpi);
536                 ret = sr_scpi_get_string(scpi, command, &response);
537                 if (ret == SR_OK) {
538                         g_strstrip(response);
539                         g_free(devc->precision);
540                         devc->precision = g_strdup(response);
541                         g_free(response);
542                         sr_dbg("%s: Precision: '%s'", __func__, devc->precision);
543                 } else {
544                         sr_info("Precision query ('%s') failed: %d",
545                                 command, ret);
546                 }
547         }
548
549         command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ);
550         if (command && *command) {
551                 scpi_dmm_cmd_delay(scpi);
552                 ret = sr_scpi_send(scpi, command);
553                 if (ret != SR_OK)
554                         return ret;
555         }
556
557         sr_sw_limits_acquisition_start(&devc->limits);
558         ret = std_session_send_df_header(sdi);
559         if (ret != SR_OK)
560                 return ret;
561
562         ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
563                 scpi_dmm_receive_data, (void *)sdi);
564         if (ret != SR_OK)
565                 return ret;
566
567         return SR_OK;
568 }
569
570 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
571 {
572         struct sr_scpi_dev_inst *scpi;
573         struct dev_context *devc;
574         const char *command;
575
576         scpi = sdi->conn;
577         devc = sdi->priv;
578
579         command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ);
580         if (command && *command) {
581                 scpi_dmm_cmd_delay(scpi);
582                 (void)sr_scpi_send(scpi, command);
583         }
584         sr_scpi_source_remove(sdi->session, scpi);
585
586         std_session_send_df_end(sdi);
587
588         g_free(devc->precision);
589         devc->precision = NULL;
590
591         return SR_OK;
592 }
593
594 static struct sr_dev_driver scpi_dmm_driver_info = {
595         .name = "scpi-dmm",
596         .longname = "SCPI DMM",
597         .api_version = 1,
598         .init = std_init,
599         .cleanup = std_cleanup,
600         .scan = scan,
601         .dev_list = std_dev_list,
602         .dev_clear = std_dev_clear,
603         .config_get = config_get,
604         .config_set = config_set,
605         .config_list = config_list,
606         .dev_open = dev_open,
607         .dev_close = dev_close,
608         .dev_acquisition_start = dev_acquisition_start,
609         .dev_acquisition_stop = dev_acquisition_stop,
610         .context = NULL,
611 };
612 SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info);