2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "fluke-dmm.h"
30 static const int hwopts[] = {
36 static const int hwcaps[] = {
38 SR_CONF_LIMIT_SAMPLES,
44 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
45 static struct sr_dev_driver *di = &flukedmm_driver_info;
47 static char *scan_conn[] = {
52 /* Scopemeter 190 series */
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 },
64 /* Properly close and free all devices. */
65 static int clear_instances(void)
67 struct sr_dev_inst *sdi;
68 struct drv_context *drvc;
69 struct dev_context *devc;
72 if (!(drvc = di->priv))
76 for (l = drvc->instances; l; l = l->next) {
79 if (!(devc = sdi->priv))
81 sr_serial_dev_inst_free(devc->serial);
82 sr_dev_inst_free(sdi);
84 g_slist_free(drvc->instances);
85 drvc->instances = NULL;
90 static int hw_init(struct sr_context *sr_ctx)
92 struct drv_context *drvc;
94 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
95 sr_err("Driver context malloc failed.");
99 drvc->sr_ctx = sr_ctx;
105 static GSList *fluke_scan(const char *conn, const char *serialcomm)
107 struct sr_dev_inst *sdi;
108 struct drv_context *drvc;
109 struct dev_context *devc;
110 struct sr_probe *probe;
111 struct sr_serial_dev_inst *serial;
113 int retry, len, i, s;
114 char buf[128], *b, **tokens;
116 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
119 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
126 /* We'll try the discovery sequence three times in case the device
127 * is not in an idle state when we send ID. */
128 while (!devices && retry < 3) {
130 serial_flush(serial);
131 if (serial_write(serial, "ID\r", 3) == -1) {
132 sr_err("Unable to send ID string: %s.",
137 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
138 * or '1' to signify an error. */
140 serial_readline(serial, &b, &len, 150);
146 /* If CMD_ACK was OK, ID string follows. */
148 serial_readline(serial, &b, &len, 850);
151 if (strcspn(buf, ",") < 15)
152 /* Looks like it's comma-separated. */
153 tokens = g_strsplit(buf, ",", 3);
155 /* Fluke 199B, at least, uses semicolon. */
156 tokens = g_strsplit(buf, ";", 3);
157 if (!strncmp("FLUKE", tokens[0], 5)
158 && tokens[1] && tokens[2]) {
159 for (i = 0; supported_flukedmm[i].model; i++) {
160 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
162 /* Skip leading spaces in version number. */
163 for (s = 0; tokens[1][s] == ' '; s++);
164 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
165 tokens[0] + 6, tokens[1] + s)))
167 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
168 sr_err("Device context malloc failed.");
171 devc->profile = &supported_flukedmm[i];
172 devc->serial = serial;
175 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
177 sdi->probes = g_slist_append(sdi->probes, probe);
178 drvc->instances = g_slist_append(drvc->instances, sdi);
179 devices = g_slist_append(devices, sdi);
188 serial_close(serial);
190 sr_serial_dev_inst_free(serial);
195 static GSList *hw_scan(GSList *options)
197 struct sr_config *src;
200 const char *conn, *serialcomm;
202 conn = serialcomm = NULL;
203 for (l = options; l; l = l->next) {
209 case SR_CONF_SERIALCOMM:
210 serialcomm = src->value;
218 /* Use the provided comm specs. */
219 devices = fluke_scan(conn, serialcomm);
221 for (i = 0; scan_conn[i]; i++) {
222 if ((devices = fluke_scan(conn, scan_conn[i])))
224 /* The Scopemeter 199B, at least, requires this
225 * after all the 115k/9.6k confusion. */
233 static GSList *hw_dev_list(void)
235 struct drv_context *drvc;
239 return drvc->instances;
242 static int hw_dev_open(struct sr_dev_inst *sdi)
244 struct dev_context *devc;
246 if (!(devc = sdi->priv)) {
247 sr_err("sdi->priv was NULL.");
251 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
254 sdi->status = SR_ST_ACTIVE;
259 static int hw_dev_close(struct sr_dev_inst *sdi)
261 struct dev_context *devc;
263 if (!(devc = sdi->priv)) {
264 sr_err("sdi->priv was NULL.");
268 if (devc->serial && devc->serial->fd != -1) {
269 serial_close(devc->serial);
270 sdi->status = SR_ST_INACTIVE;
276 static int hw_cleanup(void)
284 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
302 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
304 struct dev_context *devc;
306 if (sdi->status != SR_ST_ACTIVE)
309 if (!(devc = sdi->priv)) {
310 sr_err("sdi->priv was NULL.");
315 case SR_CONF_LIMIT_MSEC:
316 /* TODO: not yet implemented */
317 if (*(const uint64_t *)value == 0) {
318 sr_err("LIMIT_MSEC can't be 0.");
321 devc->limit_msec = *(const uint64_t *)value;
322 sr_dbg("Setting time limit to %" PRIu64 "ms.",
325 case SR_CONF_LIMIT_SAMPLES:
326 devc->limit_samples = *(const uint64_t *)value;
327 sr_dbg("Setting sample limit to %" PRIu64 ".",
328 devc->limit_samples);
331 sr_err("Unknown capability: %d.", id);
339 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
342 struct sr_datafeed_packet packet;
343 struct sr_datafeed_header header;
344 struct dev_context *devc;
346 if (!(devc = sdi->priv)) {
347 sr_err("sdi->priv was NULL.");
351 sr_dbg("Starting acquisition.");
353 devc->cb_data = cb_data;
355 /* Send header packet to the session bus. */
356 sr_dbg("Sending SR_DF_HEADER.");
357 packet.type = SR_DF_HEADER;
358 packet.payload = (uint8_t *)&header;
359 header.feed_version = 1;
360 gettimeofday(&header.starttime, NULL);
361 sr_session_send(devc->cb_data, &packet);
363 /* Poll every 100ms, or whenever some data comes in. */
364 sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
366 if (serial_write(devc->serial, "QM\r", 3) == -1) {
367 sr_err("Unable to send QM: %s.", strerror(errno));
370 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
371 devc->expect_response = TRUE;
376 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
378 struct sr_datafeed_packet packet;
379 struct dev_context *devc;
381 if (sdi->status != SR_ST_ACTIVE)
384 if (!(devc = sdi->priv)) {
385 sr_err("sdi->priv was NULL.");
389 sr_dbg("Stopping acquisition.");
391 sr_source_remove(devc->serial->fd);
392 hw_dev_close((struct sr_dev_inst *)sdi);
394 /* Send end packet to the session bus. */
395 sr_dbg("Sending SR_DF_END.");
396 packet.type = SR_DF_END;
397 sr_session_send(cb_data, &packet);
402 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
404 .longname = "Fluke 18x/28x series DMMs",
407 .cleanup = hw_cleanup,
409 .dev_list = hw_dev_list,
410 .dev_clear = clear_instances,
411 .config_get = config_get,
412 .config_set = config_set,
413 .dev_open = hw_dev_open,
414 .dev_close = hw_dev_close,
415 .dev_acquisition_start = hw_dev_acquisition_start,
416 .dev_acquisition_stop = hw_dev_acquisition_stop,