]> sigrok.org Git - libsigrok.git/blob - hardware/fluke-dmm/api.c
Factor out common hw_init() driver code.
[libsigrok.git] / hardware / fluke-dmm / api.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 <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "fluke-dmm.h"
29
30 static const int hwopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33         0,
34 };
35
36 static const int hwcaps[] = {
37         SR_CONF_MULTIMETER,
38         SR_CONF_LIMIT_SAMPLES,
39         SR_CONF_LIMIT_MSEC,
40         SR_CONF_CONTINUOUS,
41         0,
42 };
43
44 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
45 static struct sr_dev_driver *di = &flukedmm_driver_info;
46
47 static char *scan_conn[] = {
48         /* 287/289 */
49         "115200/8n1",
50         /* 187/189 */
51         "9600/8n1",
52         /* Scopemeter 190 series */
53         "1200/8n1",
54         NULL
55 };
56
57 static const struct flukedmm_profile supported_flukedmm[] = {
58         { FLUKE_187, "187", 100, 1000 },
59         { FLUKE_287, "287", 100, 1000 },
60         { FLUKE_190, "199B", 1000, 3500 },
61 };
62
63
64 /* Properly close and free all devices. */
65 static int clear_instances(void)
66 {
67         struct sr_dev_inst *sdi;
68         struct drv_context *drvc;
69         struct dev_context *devc;
70         GSList *l;
71
72         if (!(drvc = di->priv))
73                 return SR_OK;
74
75         drvc = di->priv;
76         for (l = drvc->instances; l; l = l->next) {
77                 if (!(sdi = l->data))
78                         continue;
79                 if (!(devc = sdi->priv))
80                         continue;
81                 sr_serial_dev_inst_free(devc->serial);
82                 sr_dev_inst_free(sdi);
83         }
84         g_slist_free(drvc->instances);
85         drvc->instances = NULL;
86
87         return SR_OK;
88 }
89
90 static int hw_init(struct sr_context *sr_ctx)
91 {
92         return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
93 }
94
95 static GSList *fluke_scan(const char *conn, const char *serialcomm)
96 {
97         struct sr_dev_inst *sdi;
98         struct drv_context *drvc;
99         struct dev_context *devc;
100         struct sr_probe *probe;
101         struct sr_serial_dev_inst *serial;
102         GSList *devices;
103         int retry, len, i, s;
104         char buf[128], *b, **tokens;
105
106         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
107                 return NULL;
108
109         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
110                 return NULL;
111
112         drvc = di->priv;
113         b = buf;
114         retry = 0;
115         devices = NULL;
116         /* We'll try the discovery sequence three times in case the device
117          * is not in an idle state when we send ID. */
118         while (!devices && retry < 3) {
119                 retry++;
120                 serial_flush(serial);
121                 if (serial_write(serial, "ID\r", 3) == -1) {
122                         sr_err("Unable to send ID string: %s.",
123                                strerror(errno));
124                         continue;
125                 }
126
127                 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
128                  * or '1' to signify an error. */
129                 len = 128;
130                 serial_readline(serial, &b, &len, 150);
131                 if (len != 1)
132                         continue;
133                 if (buf[0] != '0')
134                         continue;
135
136                 /* If CMD_ACK was OK, ID string follows. */
137                 len = 128;
138                 serial_readline(serial, &b, &len, 850);
139                 if (len < 10)
140                         continue;
141                 if (strcspn(buf, ",") < 15)
142                         /* Looks like it's comma-separated. */
143                         tokens = g_strsplit(buf, ",", 3);
144                 else
145                         /* Fluke 199B, at least, uses semicolon. */
146                         tokens = g_strsplit(buf, ";", 3);
147                 if (!strncmp("FLUKE", tokens[0], 5)
148                                 && tokens[1] && tokens[2]) {
149                         for (i = 0; supported_flukedmm[i].model; i++) {
150                                 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
151                                         continue;
152                                 /* Skip leading spaces in version number. */
153                                 for (s = 0; tokens[1][s] == ' '; s++);
154                                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
155                                                 tokens[0] + 6, tokens[1] + s)))
156                                         return NULL;
157                                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
158                                         sr_err("Device context malloc failed.");
159                                         return NULL;
160                                 }
161                                 devc->profile = &supported_flukedmm[i];
162                                 devc->serial = serial;
163                                 sdi->priv = devc;
164                                 sdi->driver = di;
165                                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
166                                         return NULL;
167                                 sdi->probes = g_slist_append(sdi->probes, probe);
168                                 drvc->instances = g_slist_append(drvc->instances, sdi);
169                                 devices = g_slist_append(devices, sdi);
170                                 break;
171                         }
172                 }
173                 g_strfreev(tokens);
174                 if (devices)
175                         /* Found one. */
176                         break;
177         }
178         serial_close(serial);
179         if (!devices)
180                 sr_serial_dev_inst_free(serial);
181
182         return devices;
183 }
184
185 static GSList *hw_scan(GSList *options)
186 {
187         struct sr_config *src;
188         GSList *l, *devices;
189         int i;
190         const char *conn, *serialcomm;
191
192         conn = serialcomm = NULL;
193         for (l = options; l; l = l->next) {
194                 src = l->data;
195                 switch (src->key) {
196                 case SR_CONF_CONN:
197                         conn = src->value;
198                         break;
199                 case SR_CONF_SERIALCOMM:
200                         serialcomm = src->value;
201                         break;
202                 }
203         }
204         if (!conn)
205                 return NULL;
206
207         if (serialcomm) {
208                 /* Use the provided comm specs. */
209                 devices = fluke_scan(conn, serialcomm);
210         } else {
211                 for (i = 0; scan_conn[i]; i++) {
212                         if ((devices = fluke_scan(conn, scan_conn[i])))
213                                 break;
214                         /* The Scopemeter 199B, at least, requires this
215                          * after all the 115k/9.6k confusion. */
216                         g_usleep(5000);
217                 }
218         }
219
220         return devices;
221 }
222
223 static GSList *hw_dev_list(void)
224 {
225         struct drv_context *drvc;
226
227         drvc = di->priv;
228
229         return drvc->instances;
230 }
231
232 static int hw_dev_open(struct sr_dev_inst *sdi)
233 {
234         struct dev_context *devc;
235
236         if (!(devc = sdi->priv)) {
237                 sr_err("sdi->priv was NULL.");
238                 return SR_ERR_BUG;
239         }
240
241         if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
242                 return SR_ERR;
243
244         sdi->status = SR_ST_ACTIVE;
245
246         return SR_OK;
247 }
248
249 static int hw_dev_close(struct sr_dev_inst *sdi)
250 {
251         struct dev_context *devc;
252
253         if (!(devc = sdi->priv)) {
254                 sr_err("sdi->priv was NULL.");
255                 return SR_ERR_BUG;
256         }
257
258         if (devc->serial && devc->serial->fd != -1) {
259                 serial_close(devc->serial);
260                 sdi->status = SR_ST_INACTIVE;
261         }
262
263         return SR_OK;
264 }
265
266 static int hw_cleanup(void)
267 {
268
269         clear_instances();
270
271         return SR_OK;
272 }
273
274 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
275 {
276         struct dev_context *devc;
277
278         if (sdi->status != SR_ST_ACTIVE)
279                 return SR_ERR;
280
281         if (!(devc = sdi->priv)) {
282                 sr_err("sdi->priv was NULL.");
283                 return SR_ERR_BUG;
284         }
285
286         switch (id) {
287         case SR_CONF_LIMIT_MSEC:
288                 /* TODO: not yet implemented */
289                 if (*(const uint64_t *)value == 0) {
290                         sr_err("LIMIT_MSEC can't be 0.");
291                         return SR_ERR;
292                 }
293                 devc->limit_msec = *(const uint64_t *)value;
294                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
295                        devc->limit_msec);
296                 break;
297         case SR_CONF_LIMIT_SAMPLES:
298                 devc->limit_samples = *(const uint64_t *)value;
299                 sr_dbg("Setting sample limit to %" PRIu64 ".",
300                        devc->limit_samples);
301                 break;
302         default:
303                 sr_err("Unknown capability: %d.", id);
304                 return SR_ERR;
305                 break;
306         }
307
308         return SR_OK;
309 }
310
311 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
312 {
313
314         (void)sdi;
315
316         switch (key) {
317         case SR_CONF_SCAN_OPTIONS:
318                 *data = hwopts;
319                 break;
320         case SR_CONF_DEVICE_OPTIONS:
321                 *data = hwcaps;
322                 break;
323         default:
324                 return SR_ERR_ARG;
325         }
326
327         return SR_OK;
328 }
329
330 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
331                 void *cb_data)
332 {
333         struct sr_datafeed_packet packet;
334         struct sr_datafeed_header header;
335         struct dev_context *devc;
336
337         if (!(devc = sdi->priv)) {
338                 sr_err("sdi->priv was NULL.");
339                 return SR_ERR_BUG;
340         }
341
342         sr_dbg("Starting acquisition.");
343
344         devc->cb_data = cb_data;
345
346         /* Send header packet to the session bus. */
347         sr_dbg("Sending SR_DF_HEADER.");
348         packet.type = SR_DF_HEADER;
349         packet.payload = (uint8_t *)&header;
350         header.feed_version = 1;
351         gettimeofday(&header.starttime, NULL);
352         sr_session_send(devc->cb_data, &packet);
353
354         /* Poll every 100ms, or whenever some data comes in. */
355         sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
356
357         if (serial_write(devc->serial, "QM\r", 3) == -1) {
358                 sr_err("Unable to send QM: %s.", strerror(errno));
359                 return SR_ERR;
360         }
361         devc->cmd_sent_at = g_get_monotonic_time() / 1000;
362         devc->expect_response = TRUE;
363
364         return SR_OK;
365 }
366
367 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
368 {
369         struct sr_datafeed_packet packet;
370         struct dev_context *devc;
371
372         if (sdi->status != SR_ST_ACTIVE)
373                 return SR_ERR;
374
375         if (!(devc = sdi->priv)) {
376                 sr_err("sdi->priv was NULL.");
377                 return SR_ERR_BUG;
378         }
379
380         sr_dbg("Stopping acquisition.");
381
382         sr_source_remove(devc->serial->fd);
383         hw_dev_close((struct sr_dev_inst *)sdi);
384
385         /* Send end packet to the session bus. */
386         sr_dbg("Sending SR_DF_END.");
387         packet.type = SR_DF_END;
388         sr_session_send(cb_data, &packet);
389
390         return SR_OK;
391 }
392
393 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
394         .name = "fluke-dmm",
395         .longname = "Fluke 18x/28x series DMMs",
396         .api_version = 1,
397         .init = hw_init,
398         .cleanup = hw_cleanup,
399         .scan = hw_scan,
400         .dev_list = hw_dev_list,
401         .dev_clear = clear_instances,
402         .config_set = config_set,
403         .config_list = config_list,
404         .dev_open = hw_dev_open,
405         .dev_close = hw_dev_close,
406         .dev_acquisition_start = hw_dev_acquisition_start,
407         .dev_acquisition_stop = hw_dev_acquisition_stop,
408         .priv = NULL,
409 };