]> sigrok.org Git - libsigrok.git/blob - hardware/fluke-dmm/fluke.c
Return SR_ERR_MALLOC upon allocation errors.
[libsigrok.git] / hardware / fluke-dmm / fluke.c
1 /*
2  * This file is part of the sigrok 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 <glib.h>
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #include "fluke-dmm.h"
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <errno.h>
28
29
30 static struct sr_datafeed_analog *handle_qm_v1(const struct sr_dev_inst *sdi,
31                 char **tokens)
32 {
33         struct sr_datafeed_analog *analog;
34         float fvalue;
35         char *e, *u;
36         gboolean is_oor;
37
38         (void)sdi;
39         if (strcmp(tokens[0], "QM"))
40                 return NULL;
41
42         if ((e = strstr(tokens[1], "Out of range"))) {
43                 is_oor = TRUE;
44                 fvalue = -1;
45         } else {
46                 is_oor = FALSE;
47                 fvalue = strtof(tokens[1], &e);
48                 if (fvalue == 0.0 && e == tokens[1]) {
49                         /* Happens all the time, when switching modes. */
50                         sr_dbg("Invalid float.");
51                         return NULL;
52                 }
53         }
54         while(*e && *e == ' ')
55                 e++;
56
57         /* TODO: Check malloc return value. */
58         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
59         analog->num_samples = 1;
60         /* TODO: Check malloc return value. */
61         analog->data = g_try_malloc(sizeof(float));
62         if (is_oor)
63                 *analog->data = NAN;
64         else
65                 *analog->data = fvalue;
66         analog->mq = -1;
67
68         if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
69                 analog->mq = SR_MQ_VOLTAGE;
70                 analog->unit = SR_UNIT_VOLT;
71                 if (!is_oor && e[0] == 'm')
72                         *analog->data /= 1000;
73                 /* This catches "V AC", "V DC" and "V AC+DC". */
74                 if (strstr(u, "AC"))
75                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
76                 if (strstr(u, "DC"))
77                         analog->mqflags |= SR_MQFLAG_DC;
78         } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
79                 analog->mq = SR_MQ_VOLTAGE;
80                 if (u[2] == 'm')
81                         analog->unit = SR_UNIT_DECIBEL_MW;
82                 else
83                         analog->unit = SR_UNIT_DECIBEL_VOLT;
84                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
85         } else if ((u = strstr(e, "Ohms"))) {
86                 analog->mq = SR_MQ_RESISTANCE;
87                 analog->unit = SR_UNIT_OHM;
88                 if (is_oor)
89                         *analog->data = INFINITY;
90                 else if (e[0] == 'k')
91                         *analog->data *= 1000;
92                 else if (e[0] == 'M')
93                         *analog->data *= 1000000;
94         } else if (!strcmp(e, "nS")) {
95                 analog->mq = SR_MQ_CONDUCTANCE;
96                 analog->unit = SR_UNIT_SIEMENS;
97                 *analog->data /= 1e+9;
98         } else if ((u = strstr(e, "Farads"))) {
99                 analog->mq = SR_MQ_CAPACITANCE;
100                 analog->unit = SR_UNIT_FARAD;
101                 if (!is_oor) {
102                         if (e[0] == 'm')
103                                 *analog->data /= 1e+3;
104                         else if (e[0] == 'u')
105                                 *analog->data /= 1e+6;
106                         else if (e[0] == 'n')
107                                 *analog->data /= 1e+9;
108                 }
109         } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
110                 analog->mq = SR_MQ_TEMPERATURE;
111                 if (u[4] == 'C')
112                         analog->unit = SR_UNIT_CELSIUS;
113                 else
114                         analog->unit = SR_UNIT_FAHRENHEIT;
115         } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
116                 analog->mq = SR_MQ_CURRENT;
117                 analog->unit = SR_UNIT_AMPERE;
118                 /* This catches "A AC", "A DC" and "A AC+DC". */
119                 if (strstr(u, "AC"))
120                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
121                 if (strstr(u, "DC"))
122                         analog->mqflags |= SR_MQFLAG_DC;
123                 if (!is_oor) {
124                         if (e[0] == 'm')
125                                 *analog->data /= 1e+3;
126                         else if (e[0] == 'u')
127                                 *analog->data /= 1e+6;
128                 }
129         } else if ((u = strstr(e, "Hz"))) {
130                 analog->mq = SR_MQ_FREQUENCY;
131                 analog->unit = SR_UNIT_HERTZ;
132                 if (e[0] == 'k')
133                         *analog->data *= 1e+3;
134         } else if (!strcmp(e, "%")) {
135                 analog->mq = SR_MQ_DUTY_CYCLE;
136                 analog->unit = SR_UNIT_PERCENTAGE;
137         } else if ((u = strstr(e, "ms"))) {
138                 analog->mq = SR_MQ_PULSE_WIDTH;
139                 analog->unit = SR_UNIT_SECOND;
140                 *analog->data /= 1e+3;
141         }
142
143         if (analog->mq == -1) {
144                 /* Not a valid measurement. */
145                 g_free(analog->data);
146                 g_free(analog);
147                 analog = NULL;
148         }
149
150         return analog;
151 }
152
153 static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
154                 char **tokens)
155 {
156         struct sr_datafeed_analog *analog;
157         float fvalue;
158         char *eptr;
159
160         (void)sdi;
161         fvalue = strtof(tokens[0], &eptr);
162         if (fvalue == 0.0 && eptr == tokens[0]) {
163                 sr_err("Invalid float.");
164                 return NULL;
165         }
166
167         /* TODO: Check malloc return value. */
168         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
169         analog->num_samples = 1;
170         /* TODO: Check malloc return value. */
171         analog->data = g_try_malloc(sizeof(float));
172         *analog->data = fvalue;
173         analog->mq = -1;
174
175         if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
176                 analog->mq = SR_MQ_VOLTAGE;
177                 analog->unit = SR_UNIT_VOLT;
178                 if (!strcmp(tokens[2], "NORMAL")) {
179                         if (tokens[1][1] == 'A') {
180                                 analog->mqflags |= SR_MQFLAG_AC;
181                                 analog->mqflags |= SR_MQFLAG_RMS;
182                         } else
183                                 analog->mqflags |= SR_MQFLAG_DC;
184                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
185                         *analog->data = NAN;
186                 } else
187                         analog->mq = -1;
188         } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
189                 analog->mq = SR_MQ_VOLTAGE;
190                 if (tokens[1][2] == 'm')
191                         analog->unit = SR_UNIT_DECIBEL_MW;
192                 else
193                         analog->unit = SR_UNIT_DECIBEL_VOLT;
194                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
195         } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
196                 if (!strcmp(tokens[2], "NORMAL")) {
197                         analog->mq = SR_MQ_TEMPERATURE;
198                         if (tokens[1][0] == 'C')
199                                 analog->unit = SR_UNIT_CELSIUS;
200                         else
201                                 analog->unit = SR_UNIT_FAHRENHEIT;
202                 }
203         } else if (!strcmp(tokens[1], "OHM")) {
204                 if (!strcmp(tokens[3], "NONE")) {
205                         analog->mq = SR_MQ_RESISTANCE;
206                         analog->unit = SR_UNIT_OHM;
207                         if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
208                                 *analog->data = INFINITY;
209                         } else if (strcmp(tokens[2], "NORMAL"))
210                                 analog->mq = -1;
211                 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
212                         analog->mq = SR_MQ_CONTINUITY;
213                         analog->unit = SR_UNIT_BOOLEAN;
214                         *analog->data = 0.0;
215                 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
216                         analog->mq = SR_MQ_CONTINUITY;
217                         analog->unit = SR_UNIT_BOOLEAN;
218                         *analog->data = 1.0;
219                 }
220         } else if (!strcmp(tokens[1], "F")
221                         && !strcmp(tokens[2], "NORMAL")
222                         && !strcmp(tokens[3], "NONE")) {
223                 analog->mq = SR_MQ_CAPACITANCE;
224                 analog->unit = SR_UNIT_FARAD;
225         } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
226                 analog->mq = SR_MQ_CURRENT;
227                 analog->unit = SR_UNIT_AMPERE;
228                 if (!strcmp(tokens[2], "NORMAL")) {
229                         if (tokens[1][1] == 'A') {
230                                 analog->mqflags |= SR_MQFLAG_AC;
231                                 analog->mqflags |= SR_MQFLAG_RMS;
232                         } else
233                                 analog->mqflags |= SR_MQFLAG_DC;
234                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
235                         *analog->data = NAN;
236                 } else
237                         analog->mq = -1;
238         } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
239                 analog->mq = SR_MQ_FREQUENCY;
240                 analog->unit = SR_UNIT_HERTZ;
241         } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
242                 analog->mq = SR_MQ_DUTY_CYCLE;
243                 analog->unit = SR_UNIT_PERCENTAGE;
244         } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
245                 analog->mq = SR_MQ_PULSE_WIDTH;
246                 analog->unit = SR_UNIT_SECOND;
247         } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
248                 analog->mq = SR_MQ_CONDUCTANCE;
249                 analog->unit = SR_UNIT_SIEMENS;
250         }
251
252         if (analog->mq == -1) {
253                 /* Not a valid measurement. */
254                 g_free(analog->data);
255                 g_free(analog);
256                 analog = NULL;
257         }
258
259         return analog;
260 }
261
262 static void handle_line(const struct sr_dev_inst *sdi)
263 {
264         struct dev_context *devc;
265         struct sr_datafeed_packet packet;
266         struct sr_datafeed_analog *analog;
267         char **tokens;
268
269         devc = sdi->priv;
270         sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
271
272         if (devc->buflen == 1) {
273                 if (devc->buf[0] != '0') {
274                         /* Not just a CMD_ACK from the query command. */
275                         sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
276                         devc->expect_response = FALSE;
277                 }
278                 devc->buflen = 0;
279                 return;
280         }
281
282         analog = NULL;
283         tokens = g_strsplit(devc->buf, ",", 0);
284         if (tokens[0] && tokens[1]) {
285                 if (devc->profile->model == FLUKE_187) {
286                         devc->expect_response = FALSE;
287                         analog = handle_qm_v1(sdi, tokens);
288                 } else if (devc->profile->model == FLUKE_287) {
289                         devc->expect_response = FALSE;
290                         analog = handle_qm_v2(sdi, tokens);
291                 }
292         }
293         g_strfreev(tokens);
294         devc->buflen = 0;
295
296         if (analog) {
297                 /* Got a measurement. */
298                 packet.type = SR_DF_ANALOG;
299                 packet.payload = analog;
300                 sr_session_send(devc->cb_data, &packet);
301                 devc->num_samples++;
302                 g_free(analog->data);
303                 g_free(analog);
304         }
305
306 }
307
308 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
309 {
310         const struct sr_dev_inst *sdi;
311         struct dev_context *devc;
312         int len;
313         int64_t now, elapsed;
314
315         if (!(sdi = cb_data))
316                 return TRUE;
317
318         if (!(devc = sdi->priv))
319                 return TRUE;
320
321         if (revents == G_IO_IN) {
322                 /* Serial data arrived. */
323                 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
324                         len = serial_read(fd, devc->buf + devc->buflen, 1);
325                         if (len < 1)
326                                 break;
327                         devc->buflen++;
328                         *(devc->buf + devc->buflen) = '\0';
329                         if (*(devc->buf + devc->buflen - 1) == '\r') {
330                                 *(devc->buf + --devc->buflen) = '\0';
331                                 handle_line(sdi);
332                                 break;
333                         }
334                 }
335         }
336
337         if (devc->num_samples >= devc->limit_samples) {
338                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
339                 return TRUE;
340         }
341
342         now = g_get_monotonic_time() / 1000;
343         elapsed = now - devc->cmd_sent_at;
344         /* Send query command at poll_period interval, or after 1 second
345          * has elapsed. This will make it recover from any out-of-sync
346          * or temporary disconnect issues. */
347         if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
348                         || elapsed > 1000) {
349                 sr_spew("Sending QM.");
350                 if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
351                         sr_err("Unable to send QM: %s.", strerror(errno));
352                 devc->cmd_sent_at = now;
353                 devc->expect_response = TRUE;
354         }
355
356         return TRUE;
357 }
358
359