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