]> sigrok.org Git - libsigrok.git/blob - src/hardware/agilent-dmm/protocol.c
agilent-dmm: Fix handling of the 2nd channel of 2 channels models.
[libsigrok.git] / src / hardware / agilent-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 <config.h>
21 #include <glib.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <math.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 #define JOB_TIMEOUT 300
32
33 #define INFINITE_INTERVAL   INT_MAX
34 #define SAMPLERATE_INTERVAL -1
35
36 static const struct agdmm_job *job_current(const struct dev_context *devc)
37 {
38         return &devc->jobs[devc->current_job];
39 }
40
41 static void job_done(struct dev_context *devc)
42 {
43         devc->job_running = FALSE;
44 }
45
46 static void job_again(struct dev_context *devc)
47 {
48         devc->job_again = TRUE;
49 }
50
51 static gboolean job_is_running(const struct dev_context *devc)
52 {
53         return devc->job_running;
54 }
55
56 static gboolean job_in_interval(const struct dev_context *devc)
57 {
58         int64_t job_start = devc->jobs_start[devc->current_job];
59         int64_t now = g_get_monotonic_time() / 1000;
60         int interval = job_current(devc)->interval;
61         if (interval == SAMPLERATE_INTERVAL)
62                 interval = 1000 / devc->cur_samplerate;
63         return (now - job_start) < interval || interval == INFINITE_INTERVAL;
64 }
65
66 static gboolean job_has_timeout(const struct dev_context *devc)
67 {
68         int64_t job_start = devc->jobs_start[devc->current_job];
69         int64_t now = g_get_monotonic_time() / 1000;
70         return job_is_running(devc) && (now - job_start) > JOB_TIMEOUT;
71 }
72
73 static const struct agdmm_job *job_next(struct dev_context *devc)
74 {
75         int current_job = devc->current_job;
76         do {
77                 devc->current_job++;
78                 if (!job_current(devc)->send)
79                         devc->current_job = 0;
80         } while(job_in_interval(devc) && devc->current_job != current_job);
81         return job_current(devc);
82 }
83
84 static void job_run_again(const struct sr_dev_inst *sdi)
85 {
86         struct dev_context *devc = sdi->priv;
87         devc->job_again = FALSE;
88         devc->job_running = TRUE;
89         if (job_current(devc)->send(sdi) == SR_ERR_NA)
90                 job_done(devc);
91 }
92
93 static void job_run(const struct sr_dev_inst *sdi)
94 {
95         struct dev_context *devc = sdi->priv;
96         int64_t now = g_get_monotonic_time() / 1000;
97         devc->jobs_start[devc->current_job] = now;
98         job_run_again(sdi);
99 }
100
101 static void dispatch(const struct sr_dev_inst *sdi)
102 {
103         struct dev_context *devc = sdi->priv;
104
105         if (devc->job_again) {
106                 job_run_again(sdi);
107                 return;
108         }
109
110         if (!job_is_running(devc))
111                 job_next(devc);
112         else if (job_has_timeout(devc))
113                 job_done(devc);
114
115         if (!job_is_running(devc) && !job_in_interval(devc))
116                 job_run(sdi);
117 }
118
119 static gboolean receive_line(const struct sr_dev_inst *sdi)
120 {
121         struct dev_context *devc;
122         const struct agdmm_recv *recvs, *recv;
123         GRegex *reg;
124         GMatchInfo *match;
125         gboolean stop = FALSE;
126         int i;
127
128         devc = sdi->priv;
129
130         /* Strip CRLF */
131         while (devc->buflen) {
132                 if (*(devc->buf + devc->buflen - 1) == '\r'
133                                 || *(devc->buf + devc->buflen - 1) == '\n')
134                         *(devc->buf + --devc->buflen) = '\0';
135                 else
136                         break;
137         }
138         sr_spew("Received '%s'.", devc->buf);
139
140         recv = NULL;
141         recvs = devc->profile->recvs;
142         for (i = 0; (&recvs[i])->recv_regex; i++) {
143                 reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
144                 if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
145                         recv = &recvs[i];
146                         break;
147                 }
148                 g_match_info_unref(match);
149                 g_regex_unref(reg);
150         }
151         if (recv) {
152                 enum job_type type = recv->recv(sdi, match);
153                 if (type == job_current(devc)->type)
154                         job_done(devc);
155                 else if (type == JOB_AGAIN)
156                         job_again(devc);
157                 else if (type == JOB_STOP)
158                         stop = TRUE;
159                 g_match_info_unref(match);
160                 g_regex_unref(reg);
161         } else
162                 sr_dbg("Unknown line '%s'.", devc->buf);
163
164         /* Done with this. */
165         devc->buflen = 0;
166         return stop;
167 }
168
169 SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
170 {
171         struct sr_dev_inst *sdi;
172         struct dev_context *devc;
173         struct sr_serial_dev_inst *serial;
174         gboolean stop = FALSE;
175         int len;
176
177         (void)fd;
178
179         if (!(sdi = cb_data))
180                 return TRUE;
181
182         if (!(devc = sdi->priv))
183                 return TRUE;
184
185         serial = sdi->conn;
186         if (revents == G_IO_IN) {
187                 /* Serial data arrived. */
188                 while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) {
189                         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
190                         if (len < 1)
191                                 break;
192                         devc->buflen += len;
193                         *(devc->buf + devc->buflen) = '\0';
194                         if (*(devc->buf + devc->buflen - 1) == '\n') {
195                                 /* End of line */
196                                 stop = receive_line(sdi);
197                                 break;
198                         }
199                 }
200         }
201
202         if (sr_sw_limits_check(&devc->limits) || stop)
203                 sdi->driver->dev_acquisition_stop(sdi);
204         else
205                 dispatch(sdi);
206
207         return TRUE;
208 }
209
210 static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd, ...)
211 {
212         struct sr_serial_dev_inst *serial;
213         va_list args;
214         char buf[32];
215
216         serial = sdi->conn;
217
218         va_start(args, cmd);
219         vsnprintf(buf, sizeof(buf) - 3, cmd, args);
220         va_end(args);
221         sr_spew("Sending '%s'.", buf);
222         if (!strncmp(buf, "*IDN?", 5))
223                 strcat(buf, "\r\n");
224         else
225                 strcat(buf, "\n\r\n");
226         if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) {
227                 sr_err("Failed to send.");
228                 return SR_ERR;
229         }
230
231         return SR_OK;
232 }
233
234 static int send_stat(const struct sr_dev_inst *sdi)
235 {
236         return agdmm_send(sdi, "STAT?");
237 }
238
239 static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
240 {
241         struct dev_context *devc;
242         char *s;
243
244         devc = sdi->priv;
245         s = g_match_info_fetch(match, 1);
246         sr_spew("STAT response '%s'.", s);
247
248         /* Max, Min or Avg mode -- no way to tell which, so we'll
249          * set both flags to denote it's not a normal measurement. */
250         if (s[0] == '1')
251                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
252         else
253                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
254
255         if (s[1] == '1')
256                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
257         else
258                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
259
260         /* Triggered or auto hold modes. */
261         if (s[2] == '1' || s[3] == '1')
262                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
263         else
264                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
265
266         /* Temp/aux mode. */
267         if (s[7] == '1')
268                 devc->mode_tempaux = TRUE;
269         else
270                 devc->mode_tempaux = FALSE;
271
272         /* Continuity mode. */
273         if (s[16] == '1')
274                 devc->mode_continuity = TRUE;
275         else
276                 devc->mode_continuity = FALSE;
277
278         g_free(s);
279
280         return JOB_STAT;
281 }
282
283 static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match)
284 {
285         struct dev_context *devc;
286         char *s;
287
288         devc = sdi->priv;
289         s = g_match_info_fetch(match, 1);
290         sr_spew("STAT response '%s'.", s);
291
292         /* Max, Min or Avg mode -- no way to tell which, so we'll
293          * set both flags to denote it's not a normal measurement. */
294         if (s[0] == '1')
295                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
296         else
297                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
298
299         if (s[1] == '1')
300                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
301         else
302                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
303
304         /* Hold mode. */
305         if (s[7] == '1')
306                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
307         else
308                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
309
310         g_free(s);
311
312         return JOB_STAT;
313 }
314
315 static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
316 {
317         struct dev_context *devc;
318         char *s;
319
320         devc = sdi->priv;
321         s = g_match_info_fetch(match, 1);
322         sr_spew("STAT response '%s'.", s);
323
324         /* dBm/dBV modes. */
325         if ((s[2] & ~0x20) == 'M')
326                 devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW;
327         else if ((s[2] & ~0x20) == 'V')
328                 devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT;
329         else
330                 devc->mode_dbm_dbv = 0;
331
332         /* Peak hold mode. */
333         if (s[4] == '1')
334                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX;
335         else
336                 devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX;
337
338         /* Triggered hold mode. */
339         if (s[7] == '1')
340                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
341         else
342                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
343
344         g_free(s);
345
346         return JOB_STAT;
347 }
348
349 static int recv_stat_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match)
350 {
351         struct dev_context *devc;
352         char *s;
353
354         devc = sdi->priv;
355         s = g_match_info_fetch(match, 1);
356         sr_spew("STAT response '%s'.", s);
357
358         /* Max, Min or Avg mode -- no way to tell which, so we'll
359          * set both flags to denote it's not a normal measurement. */
360         if (s[0] == '1')
361                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG;
362         else
363                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG);
364
365         /* dBm/dBV modes. */
366         if ((s[2] & ~0x20) == 'M')
367                 devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW;
368         else if ((s[2] & ~0x20) == 'V')
369                 devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT;
370         else
371                 devc->mode_dbm_dbv = 0;
372
373         /* Peak hold mode. */
374         if (s[4] == '4')
375                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX;
376         else
377                 devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX;
378
379         /* Null function. */
380         if (s[1] == '1')
381                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
382         else
383                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
384
385         /* Triggered or auto hold modes. */
386         if (s[7] == '1' || s[11] == '1')
387                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
388         else
389                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
390
391         g_free(s);
392
393         return JOB_STAT;
394 }
395
396 static int send_fetc(const struct sr_dev_inst *sdi)
397 {
398         struct dev_context *devc = sdi->priv;
399
400         if (devc->mode_squarewave)
401                 return SR_ERR_NA;
402
403         if (devc->cur_channel->index > 0)
404                 return agdmm_send(sdi, "FETC? @%d", devc->cur_channel->index + 1);
405         else
406                 return agdmm_send(sdi, "FETC?");
407 }
408
409 static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
410 {
411         struct dev_context *devc;
412         struct sr_datafeed_packet packet;
413         struct sr_datafeed_analog analog;
414         struct sr_analog_encoding encoding;
415         struct sr_analog_meaning meaning;
416         struct sr_analog_spec spec;
417         struct sr_channel *prev_chan;
418         float fvalue;
419         const char *s;
420         char *mstr;
421         int i, exp;
422
423         sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
424         devc = sdi->priv;
425         i = devc->cur_channel->index;
426
427         if (devc->cur_mq[i] == -1)
428                 /* This detects when channel P2 is reporting TEMP as an identical
429                  * copy of channel P3. In this case, we just skip P2. */
430                 goto skip_value;
431
432         s = g_match_info_get_string(match);
433         if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) {
434                 /* An invalid measurement shows up on the display as "O.L", but
435                  * comes through like this. Since comparing 38-digit floats
436                  * is rather problematic, we'll cut through this here. */
437                 fvalue = NAN;
438         } else {
439                 mstr = g_match_info_fetch(match, 1);
440                 if (sr_atof_ascii(mstr, &fvalue) != SR_OK) {
441                         g_free(mstr);
442                         sr_dbg("Invalid float.");
443                         return SR_ERR;
444                 }
445                 g_free(mstr);
446                 if (devc->cur_exponent[i] != 0)
447                         fvalue *= powf(10, devc->cur_exponent[i]);
448         }
449
450         if (devc->cur_unit[i] == SR_UNIT_DECIBEL_MW ||
451             devc->cur_unit[i] == SR_UNIT_DECIBEL_VOLT ||
452             devc->cur_unit[i] == SR_UNIT_PERCENTAGE) {
453                 mstr = g_match_info_fetch(match, 2);
454                 if (mstr && sr_atoi(mstr, &exp) == SR_OK) {
455                         devc->cur_digits[i] = MIN(4 - exp, devc->cur_digits[i]);
456                         devc->cur_encoding[i] = MIN(5 - exp, devc->cur_encoding[i]);
457                 }
458                 g_free(mstr);
459         }
460
461         sr_analog_init(&analog, &encoding, &meaning, &spec,
462                        devc->cur_digits[i] - devc->cur_exponent[i]);
463         analog.meaning->mq = devc->cur_mq[i];
464         analog.meaning->unit = devc->cur_unit[i];
465         analog.meaning->mqflags = devc->cur_mqflags[i];
466         analog.meaning->channels = g_slist_append(NULL, devc->cur_channel);
467         analog.num_samples = 1;
468         analog.data = &fvalue;
469         encoding.digits = devc->cur_encoding[i] - devc->cur_exponent[i];
470         packet.type = SR_DF_ANALOG;
471         packet.payload = &analog;
472         sr_session_send(sdi, &packet);
473         g_slist_free(analog.meaning->channels);
474
475         sr_sw_limits_update_samples_read(&devc->limits, 1);
476
477 skip_value:
478         prev_chan = devc->cur_channel;
479         devc->cur_channel = sr_next_enabled_channel(sdi, devc->cur_channel);
480         if (devc->cur_channel->index > prev_chan->index)
481                 return JOB_AGAIN;
482         else
483                 return JOB_FETC;
484 }
485
486 static int send_conf(const struct sr_dev_inst *sdi)
487 {
488         struct dev_context *devc = sdi->priv;
489
490         /* Do not try to send CONF? for internal temperature channel. */
491         if (devc->cur_conf->index >= MIN(devc->profile->nb_channels, 2))
492                 return SR_ERR_NA;
493
494         if (devc->cur_conf->index > 0)
495                 return agdmm_send(sdi, "CONF? @%d", devc->cur_conf->index + 1);
496         else
497                 return agdmm_send(sdi, "CONF?");
498 }
499
500 static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
501 {
502         struct dev_context *devc;
503         char *mstr, *rstr;
504         int i, resolution;
505
506         sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
507         devc = sdi->priv;
508         i = devc->cur_conf->index;
509
510         rstr = g_match_info_fetch(match, 2);
511         if (rstr)
512                 sr_atoi(rstr, &resolution);
513         g_free(rstr);
514
515         mstr = g_match_info_fetch(match, 1);
516         if (!strcmp(mstr, "V")) {
517                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
518                 devc->cur_unit[i] = SR_UNIT_VOLT;
519                 devc->cur_mqflags[i] = 0;
520                 devc->cur_exponent[i] = 0;
521                 devc->cur_digits[i] = 4 - resolution;
522         } else if (!strcmp(mstr, "MV")) {
523                 if (devc->mode_tempaux) {
524                         devc->cur_mq[i] = SR_MQ_TEMPERATURE;
525                         /* No way to detect whether Fahrenheit or Celsius
526                          * is used, so we'll just default to Celsius. */
527                         devc->cur_unit[i] = SR_UNIT_CELSIUS;
528                         devc->cur_mqflags[i] = 0;
529                         devc->cur_exponent[i] = 0;
530                         devc->cur_digits[i] = 1;
531                 } else {
532                         devc->cur_mq[i] = SR_MQ_VOLTAGE;
533                         devc->cur_unit[i] = SR_UNIT_VOLT;
534                         devc->cur_mqflags[i] = 0;
535                         devc->cur_exponent[i] = -3;
536                         devc->cur_digits[i] = 5 - resolution;
537                 }
538         } else if (!strcmp(mstr, "A")) {
539                 devc->cur_mq[i] = SR_MQ_CURRENT;
540                 devc->cur_unit[i] = SR_UNIT_AMPERE;
541                 devc->cur_mqflags[i] = 0;
542                 devc->cur_exponent[i] = 0;
543                 devc->cur_digits[i] = 3 - resolution;
544         } else if (!strcmp(mstr, "UA")) {
545                 devc->cur_mq[i] = SR_MQ_CURRENT;
546                 devc->cur_unit[i] = SR_UNIT_AMPERE;
547                 devc->cur_mqflags[i] = 0;
548                 devc->cur_exponent[i] = -6;
549                 devc->cur_digits[i] = 8 - resolution;
550         } else if (!strcmp(mstr, "FREQ")) {
551                 devc->cur_mq[i] = SR_MQ_FREQUENCY;
552                 devc->cur_unit[i] = SR_UNIT_HERTZ;
553                 devc->cur_mqflags[i] = 0;
554                 devc->cur_exponent[i] = 0;
555                 devc->cur_digits[i] = 2 - resolution;
556         } else if (!strcmp(mstr, "RES")) {
557                 if (devc->mode_continuity) {
558                         devc->cur_mq[i] = SR_MQ_CONTINUITY;
559                         devc->cur_unit[i] = SR_UNIT_BOOLEAN;
560                 } else {
561                         devc->cur_mq[i] = SR_MQ_RESISTANCE;
562                         devc->cur_unit[i] = SR_UNIT_OHM;
563                 }
564                 devc->cur_mqflags[i] = 0;
565                 devc->cur_exponent[i] = 0;
566                 devc->cur_digits[i] = 1 - resolution;
567         } else if (!strcmp(mstr, "DIOD")) {
568                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
569                 devc->cur_unit[i] = SR_UNIT_VOLT;
570                 devc->cur_mqflags[i] = SR_MQFLAG_DIODE;
571                 devc->cur_exponent[i] = 0;
572                 devc->cur_digits[i] = 3;
573         } else if (!strcmp(mstr, "CAP")) {
574                 devc->cur_mq[i] = SR_MQ_CAPACITANCE;
575                 devc->cur_unit[i] = SR_UNIT_FARAD;
576                 devc->cur_mqflags[i] = 0;
577                 devc->cur_exponent[i] = 0;
578                 devc->cur_digits[i] = 9 - resolution;
579         } else
580                 sr_dbg("Unknown first argument.");
581         g_free(mstr);
582
583         /* This is based on guess, supposing similarity with other models. */
584         devc->cur_encoding[i] = devc->cur_digits[i] + 1;
585
586         if (g_match_info_get_match_count(match) == 4) {
587                 mstr = g_match_info_fetch(match, 3);
588                 /* Third value, if present, is always AC or DC. */
589                 if (!strcmp(mstr, "AC")) {
590                         devc->cur_mqflags[i] |= SR_MQFLAG_AC;
591                         if (devc->cur_mq[i] == SR_MQ_VOLTAGE)
592                                 devc->cur_mqflags[i] |= SR_MQFLAG_RMS;
593                 } else if (!strcmp(mstr, "DC")) {
594                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
595                 } else {
596                 sr_dbg("Unknown first argument '%s'.", mstr);
597                 }
598                 g_free(mstr);
599         } else
600                 devc->cur_mqflags[i] &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
601
602         return JOB_CONF;
603 }
604
605 static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match)
606 {
607         struct dev_context *devc;
608         char *mstr, *rstr, *m2;
609         int i, resolution;
610
611         sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
612         devc = sdi->priv;
613         i = devc->cur_conf->index;
614
615         devc->mode_squarewave = 0;
616
617         rstr = g_match_info_fetch(match, 4);
618         if (rstr && sr_atoi(rstr, &resolution) == SR_OK) {
619                 devc->cur_digits[i] = -resolution;
620                 devc->cur_encoding[i] = -resolution + 1;
621         }
622         g_free(rstr);
623
624         mstr = g_match_info_fetch(match, 1);
625         if (!strncmp(mstr, "VOLT", 4)) {
626                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
627                 devc->cur_unit[i] = SR_UNIT_VOLT;
628                 devc->cur_mqflags[i] = 0;
629                 devc->cur_exponent[i] = 0;
630                 if (i == 0 && devc->mode_dbm_dbv) {
631                         devc->cur_unit[i] = devc->mode_dbm_dbv;
632                         devc->cur_digits[i] = 3;
633                         devc->cur_encoding[i] = 4;
634                 }
635                 if (mstr[4] == ':') {
636                         if (!strncmp(mstr + 5, "ACDC", 4)) {
637                                 /* AC + DC offset */
638                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
639                         } else if (!strncmp(mstr + 5, "AC", 2)) {
640                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
641                         } else if (!strncmp(mstr + 5, "DC", 2)) {
642                                 devc->cur_mqflags[i] |= SR_MQFLAG_DC;
643                         }
644                 } else
645                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
646         } else if (!strncmp(mstr, "CURR", 4)) {
647                 devc->cur_mq[i] = SR_MQ_CURRENT;
648                 devc->cur_unit[i] = SR_UNIT_AMPERE;
649                 devc->cur_mqflags[i] = 0;
650                 devc->cur_exponent[i] = 0;
651                 if (mstr[4] == ':') {
652                         if (!strncmp(mstr + 5, "ACDC", 4)) {
653                                 /* AC + DC offset */
654                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
655                         } else if (!strncmp(mstr + 5, "AC", 2)) {
656                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
657                         } else if (!strncmp(mstr + 5, "DC", 2)) {
658                                 devc->cur_mqflags[i] |= SR_MQFLAG_DC;
659                         }
660                 } else
661                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
662         } else if (!strcmp(mstr, "RES")) {
663                 devc->cur_mq[i] = SR_MQ_RESISTANCE;
664                 devc->cur_unit[i] = SR_UNIT_OHM;
665                 devc->cur_mqflags[i] = 0;
666                 devc->cur_exponent[i] = 0;
667         } else if (!strcmp(mstr, "COND")) {
668                 devc->cur_mq[i] = SR_MQ_CONDUCTANCE;
669                 devc->cur_unit[i] = SR_UNIT_SIEMENS;
670                 devc->cur_mqflags[i] = 0;
671                 devc->cur_exponent[i] = 0;
672         } else if (!strcmp(mstr, "CAP")) {
673                 devc->cur_mq[i] = SR_MQ_CAPACITANCE;
674                 devc->cur_unit[i] = SR_UNIT_FARAD;
675                 devc->cur_mqflags[i] = 0;
676                 devc->cur_exponent[i] = 0;
677         } else if (!strncmp(mstr, "FREQ", 4) || !strncmp(mstr, "FC1", 3)) {
678                 devc->cur_mq[i] = SR_MQ_FREQUENCY;
679                 devc->cur_unit[i] = SR_UNIT_HERTZ;
680                 devc->cur_mqflags[i] = 0;
681                 devc->cur_exponent[i] = 0;
682         } else if (!strncmp(mstr, "PULS:PWID", 9)) {
683                 devc->cur_mq[i] = SR_MQ_PULSE_WIDTH;
684                 devc->cur_unit[i] = SR_UNIT_SECOND;
685                 devc->cur_mqflags[i] = 0;
686                 devc->cur_exponent[i] = 0;
687                 devc->cur_encoding[i] = MIN(devc->cur_encoding[i], 6);
688         } else if (!strncmp(mstr, "PULS:PDUT", 9)) {
689                 devc->cur_mq[i] = SR_MQ_DUTY_CYCLE;
690                 devc->cur_unit[i] = SR_UNIT_PERCENTAGE;
691                 devc->cur_mqflags[i] = 0;
692                 devc->cur_exponent[i] = 0;
693                 devc->cur_digits[i] = 3;
694                 devc->cur_encoding[i] = 4;
695         } else if (!strcmp(mstr, "CONT")) {
696                 devc->cur_mq[i] = SR_MQ_CONTINUITY;
697                 devc->cur_unit[i] = SR_UNIT_OHM;
698                 devc->cur_mqflags[i] = 0;
699                 devc->cur_exponent[i] = 0;
700         } else if (!strcmp(mstr, "DIOD")) {
701                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
702                 devc->cur_unit[i] = SR_UNIT_VOLT;
703                 devc->cur_mqflags[i] = SR_MQFLAG_DIODE;
704                 devc->cur_exponent[i] = 0;
705                 devc->cur_digits[i] = 4;
706                 devc->cur_encoding[i] = 5;
707         } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2) ||
708                    !strncmp(mstr, "TEMP", 4)) {
709                 devc->cur_mq[i] = SR_MQ_TEMPERATURE;
710                 m2 = g_match_info_fetch(match, 2);
711                 if (!m2 && devc->profile->nb_channels == 3)
712                         /*
713                          * TEMP without param is for secondary display (channel P2)
714                          * and is identical to channel P3, so discard it.
715                          */
716                         devc->cur_mq[i] = -1;
717                 else if (m2 && !strcmp(m2, "FAR"))
718                         devc->cur_unit[i] = SR_UNIT_FAHRENHEIT;
719                 else
720                         devc->cur_unit[i] = SR_UNIT_CELSIUS;
721                 g_free(m2);
722                 devc->cur_mqflags[i] = 0;
723                 devc->cur_exponent[i] = 0;
724                 devc->cur_digits[i] = 1;
725                 devc->cur_encoding[i] = 2;
726         } else if (!strcmp(mstr, "SCOU")) {
727                 /*
728                  * Switch counter, not supported. Not sure what values
729                  * come from FETC in this mode, or how they would map
730                  * into libsigrok.
731                  */
732         } else if (!strncmp(mstr, "CPER:", 5)) {
733                 devc->cur_mq[i] = SR_MQ_CURRENT;
734                 devc->cur_unit[i] = SR_UNIT_PERCENTAGE;
735                 devc->cur_mqflags[i] = 0;
736                 devc->cur_exponent[i] = 0;
737                 devc->cur_digits[i] = 2;
738                 devc->cur_encoding[i] = 3;
739         } else if (!strcmp(mstr, "SQU")) {
740                 /*
741                  * Square wave output, not supported. FETC just return
742                  * an error in this mode, so don't even call it.
743                  */
744                 devc->mode_squarewave = 1;
745         } else {
746                 sr_dbg("Unknown first argument '%s'.", mstr);
747         }
748         g_free(mstr);
749
750         struct sr_channel *prev_conf = devc->cur_conf;
751         devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf);
752         if (devc->cur_conf->index >= MIN(devc->profile->nb_channels, 2))
753                 devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf);
754         if (devc->cur_conf->index > prev_conf->index)
755                 return JOB_AGAIN;
756         else
757                 return JOB_CONF;
758 }
759
760 static int send_log(const struct sr_dev_inst *sdi)
761 {
762         const char *source[] = { "LOG:HAND", "LOG:TRIG", "LOG:AUTO", "LOG:EXPO" };
763         struct dev_context *devc = sdi->priv;
764         return agdmm_send(sdi, "%s %d",
765                           source[devc->data_source - 1], devc->cur_sample);
766 }
767
768 static int recv_log_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match)
769 {
770         static const int mqs[] = { SR_MQ_VOLTAGE, SR_MQ_VOLTAGE, SR_MQ_CURRENT, SR_MQ_CURRENT, SR_MQ_RESISTANCE, SR_MQ_VOLTAGE, SR_MQ_TEMPERATURE, SR_MQ_CAPACITANCE, SR_MQ_FREQUENCY, SR_MQ_DUTY_CYCLE, SR_MQ_PULSE_WIDTH, SR_MQ_VOLTAGE, SR_MQ_CURRENT, SR_MQ_CONDUCTANCE };
771         static const int units[] = { SR_UNIT_VOLT, SR_UNIT_VOLT, SR_UNIT_AMPERE, SR_UNIT_AMPERE, SR_UNIT_OHM, SR_UNIT_VOLT, SR_UNIT_CELSIUS, SR_UNIT_FARAD, SR_UNIT_HERTZ, SR_UNIT_PERCENTAGE, SR_UNIT_SECOND, SR_UNIT_DECIBEL_MW, SR_UNIT_PERCENTAGE, SR_UNIT_SIEMENS };
772         static const int exponents[] = { -6, -4, -9, -4, -3, -4, -1, -12, -3, -3, -6, -3, -2, -11 };
773         struct dev_context *devc;
774         struct sr_datafeed_packet packet;
775         struct sr_datafeed_analog analog;
776         struct sr_analog_encoding encoding;
777         struct sr_analog_meaning meaning;
778         struct sr_analog_spec spec;
779         char *mstr;
780         unsigned function;
781         int value, negative, overload, exponent, alternate_unit, mq, unit;
782         int mqflags = 0;
783         float fvalue;
784
785         sr_spew("LOG response '%s'.", g_match_info_get_string(match));
786
787         devc = sdi->priv;
788
789         mstr = g_match_info_fetch(match, 2);
790         if (sr_atoi(mstr, (int*)&function) != SR_OK || function >= ARRAY_SIZE(mqs)) {
791                 g_free(mstr);
792                 sr_dbg("Invalid function.");
793                 return SR_ERR;
794         }
795         g_free(mstr);
796
797         mstr = g_match_info_fetch(match, 3);
798         if (sr_atoi(mstr, &value) != SR_OK) {
799                 g_free(mstr);
800                 sr_dbg("Invalid value.");
801                 return SR_ERR;
802         }
803         g_free(mstr);
804
805         mstr = g_match_info_fetch(match, 1);
806         negative = mstr[7] & 2 ? -1 : 1;
807         overload = mstr[8] & 4;
808         exponent = (mstr[9] & 0xF) + exponents[function];
809         alternate_unit = mstr[10] & 1;
810
811         if (mstr[ 8] & 1)  mqflags |= SR_MQFLAG_DC;
812         if (mstr[ 8] & 2)  mqflags |= SR_MQFLAG_AC;
813         if (mstr[11] & 4)  mqflags |= SR_MQFLAG_RELATIVE;
814         if (mstr[12] & 1)  mqflags |= SR_MQFLAG_AVG;
815         if (mstr[12] & 2)  mqflags |= SR_MQFLAG_MIN;
816         if (mstr[12] & 4)  mqflags |= SR_MQFLAG_MAX;
817         if (function == 5) mqflags |= SR_MQFLAG_DIODE;
818         g_free(mstr);
819
820         mq = mqs[function];
821         unit = units[function];
822         if (alternate_unit) {
823                 if (mq == SR_MQ_RESISTANCE)
824                         mq = SR_MQ_CONTINUITY;
825                 if (unit == SR_UNIT_DECIBEL_MW)
826                         unit = SR_UNIT_DECIBEL_VOLT;
827                 if (unit == SR_UNIT_CELSIUS) {
828                         unit = SR_UNIT_FAHRENHEIT;
829                         exponent--;
830                 }
831         }
832
833         if (overload)
834                 fvalue = NAN;
835         else
836                 fvalue = negative * value * powf(10, exponent);
837
838         sr_analog_init(&analog, &encoding, &meaning, &spec, -exponent);
839         analog.meaning->mq = mq;
840         analog.meaning->unit = unit;
841         analog.meaning->mqflags = mqflags;
842         analog.meaning->channels = g_slist_append(NULL, devc->cur_channel);
843         analog.num_samples = 1;
844         analog.data = &fvalue;
845         packet.type = SR_DF_ANALOG;
846         packet.payload = &analog;
847         sr_session_send(sdi, &packet);
848         g_slist_free(analog.meaning->channels);
849
850         sr_sw_limits_update_samples_read(&devc->limits, 1);
851         devc->cur_sample++;
852
853         return JOB_LOG;
854 }
855
856 /* This comes in whenever the rotary switch is changed to a new position.
857  * We could use it to determine the major measurement mode, but we already
858  * have the output of CONF? for that, which is more detailed. However
859  * we do need to catch this here, or it'll show up in some other output. */
860 static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
861 {
862         struct dev_context *devc = sdi->priv;
863
864         sr_spew("Switch '%s'.", g_match_info_get_string(match));
865
866         devc->current_job = 0;
867         devc->job_running = FALSE;
868         memset(devc->jobs_start, 0, sizeof(devc->jobs_start));
869         devc->cur_mq[0] = -1;
870         if (devc->profile->nb_channels > 2)
871                 devc->cur_mq[1] = -1;
872
873         return SR_OK;
874 }
875
876 static int recv_err(const struct sr_dev_inst *sdi, GMatchInfo *match)
877 {
878         struct dev_context *devc = sdi->priv;
879
880         (void) match;
881
882         if (devc->data_source != DATA_SOURCE_LIVE)
883                 return JOB_STOP; /* In log mode, stop acquisition after receiving *E. */
884         else
885                 return JOB_AGAIN;
886 }
887
888 /* Poll CONF/STAT at 1Hz and values at samplerate. */
889 SR_PRIV const struct agdmm_job agdmm_jobs_live[] = {
890         { JOB_FETC, SAMPLERATE_INTERVAL, send_fetc },
891         { JOB_CONF,                1000, send_conf },
892         { JOB_STAT,                1000, send_stat },
893         ALL_ZERO
894 };
895
896 /* Poll LOG as fast as possible. */
897 SR_PRIV const struct agdmm_job agdmm_jobs_log[] = {
898         { JOB_LOG,                    0, send_log  },
899         ALL_ZERO
900 };
901
902 SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
903         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
904         { "^\\*([0-9])$", recv_switch },
905         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
906         { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
907         { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
908         { "^\"(DIOD)\"$", recv_conf_u123x },
909         ALL_ZERO
910 };
911
912 SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = {
913         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x },
914         { "^\\*([0-9])$", recv_switch },
915         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
916         { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
917         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
918         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
919         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
920         { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
921         { "^\"(DIOD)\"$", recv_conf_u124x_5x },
922         ALL_ZERO
923 };
924
925 SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
926         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
927         { "^\\*([0-9])$", recv_switch },
928         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
929         { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
930         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
931         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
932         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
933         { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
934         { "^\"(DIOD)\"$", recv_conf_u124x_5x },
935         ALL_ZERO
936 };
937
938 SR_PRIV const struct agdmm_recv agdmm_recvs_u128x[] = {
939         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u128x },
940         { "^\\*([0-9])$", recv_switch },
941         { "^([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))$", recv_fetc },
942         { "^\"(VOLT|CURR|RES|CONT|COND|CAP|FREQ|FC1|FC100) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
943         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
944         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
945         { "^\"(FREQ:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
946         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
947         { "^\"(PULS:PWID|PULS:PWID:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
948         { "^\"(TEMP:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
949         { "^\"(DIOD|SQU|PULS:PDUT|TEMP)\"$", recv_conf_u124x_5x },
950         { "^\"((\\d{2})(\\d{5})\\d{7})\"$", recv_log_u128x },
951         { "^\\*E$", recv_err },
952         ALL_ZERO
953 };