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_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
49 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
50 static struct sr_dev_driver *di = &flukedmm_driver_info;
52 static const struct flukedmm_profile supported_flukedmm[] = {
53 { FLUKE_187, "187", 100 },
54 { FLUKE_287, "287", 100 },
58 /* Properly close and free all devices. */
59 static int clear_instances(void)
61 struct sr_dev_inst *sdi;
62 struct drv_context *drvc;
63 struct dev_context *devc;
66 if (!(drvc = di->priv))
70 for (l = drvc->instances; l; l = l->next) {
73 if (!(devc = sdi->priv))
75 sr_serial_dev_inst_free(devc->serial);
76 sr_dev_inst_free(sdi);
78 g_slist_free(drvc->instances);
79 drvc->instances = NULL;
84 static int hw_init(void)
86 struct drv_context *drvc;
88 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
89 sr_err("Driver context malloc failed.");
98 static GSList *fluke_scan(const char *conn, const char *serialcomm)
100 struct sr_dev_inst *sdi;
101 struct drv_context *drvc;
102 struct dev_context *devc;
103 struct sr_probe *probe;
105 int fd, retry, len, i, s;
106 char buf[128], *b, **tokens;
108 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
109 sr_err("Unable to open %s: %s.", conn, strerror(errno));
112 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
113 sr_err("Unable to set serial parameters.");
121 /* We'll try the discovery sequence three times in case the device
122 * is not in an idle state when we send ID. */
123 while (!devices && retry < 3) {
126 if (serial_write(fd, "ID\r", 3) == -1) {
127 sr_err("Unable to send ID string: %s.",
132 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
133 * or '1' to signify an error. */
135 serial_readline(fd, &b, &len, 150);
141 /* If CMD_ACK was OK, ID string follows. */
143 serial_readline(fd, &b, &len, 150);
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))
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)))
157 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
158 sr_err("Device context malloc failed.");
161 devc->profile = &supported_flukedmm[i];
162 devc->serial = sr_serial_dev_inst_new(conn, -1);
163 devc->serialcomm = g_strdup(serialcomm);
166 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
168 sdi->probes = g_slist_append(sdi->probes, probe);
169 drvc->instances = g_slist_append(drvc->instances, sdi);
170 devices = g_slist_append(devices, sdi);
181 static GSList *hw_scan(GSList *options)
183 struct sr_hwopt *opt;
185 const char *conn, *serialcomm;
187 conn = serialcomm = NULL;
188 for (l = options; l; l = l->next) {
190 switch (opt->hwopt) {
194 case SR_HWOPT_SERIALCOMM:
195 serialcomm = opt->value;
203 /* Use the provided comm specs. */
204 devices = fluke_scan(conn, serialcomm);
206 /* Try 115200, as used on 287/289. */
207 devices = fluke_scan(conn, "115200/8n1");
209 /* Fall back to 9600 for 187/189. */
210 devices = fluke_scan(conn, "9600/8n1");
216 static GSList *hw_dev_list(void)
218 struct drv_context *drvc;
222 return drvc->instances;
225 static int hw_dev_open(struct sr_dev_inst *sdi)
227 struct dev_context *devc;
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
234 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
235 if (devc->serial->fd == -1) {
236 sr_err("Couldn't open serial port '%s'.", devc->serial->port);
239 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
240 sr_err("Unable to set serial parameters.");
243 sdi->status = SR_ST_ACTIVE;
248 static int hw_dev_close(struct sr_dev_inst *sdi)
250 struct dev_context *devc;
252 if (!(devc = sdi->priv)) {
253 sr_err("sdi->priv was NULL.");
257 if (devc->serial && devc->serial->fd != -1) {
258 serial_close(devc->serial->fd);
259 devc->serial->fd = -1;
260 sdi->status = SR_ST_INACTIVE;
266 static int hw_cleanup(void)
274 static int hw_info_get(int info_id, const void **data,
275 const struct sr_dev_inst *sdi)
287 case SR_DI_NUM_PROBES:
288 *data = GINT_TO_POINTER(1);
290 case SR_DI_PROBE_NAMES:
300 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
303 struct dev_context *devc;
305 if (sdi->status != SR_ST_ACTIVE)
308 if (!(devc = sdi->priv)) {
309 sr_err("sdi->priv was NULL.");
314 case SR_HWCAP_LIMIT_MSEC:
315 /* TODO: not yet implemented */
316 if (*(const uint64_t *)value == 0) {
317 sr_err("LIMIT_MSEC can't be 0.");
320 devc->limit_msec = *(const uint64_t *)value;
321 sr_dbg("Setting time limit to %" PRIu64 "ms.",
324 case SR_HWCAP_LIMIT_SAMPLES:
325 devc->limit_samples = *(const uint64_t *)value;
326 sr_dbg("Setting sample limit to %" PRIu64 ".",
327 devc->limit_samples);
330 sr_err("Unknown capability: %d.", hwcap);
338 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
341 struct sr_datafeed_packet packet;
342 struct sr_datafeed_header header;
343 struct sr_datafeed_meta_analog meta;
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 /* Send metadata about the SR_DF_ANALOG packets to come. */
364 sr_dbg("Sending SR_DF_META_ANALOG.");
365 packet.type = SR_DF_META_ANALOG;
366 packet.payload = &meta;
368 sr_session_send(devc->cb_data, &packet);
370 /* Poll every 100ms, or whenever some data comes in. */
371 sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
373 if (serial_write(devc->serial->fd, "QM\r", 3) == -1) {
374 sr_err("Unable to send QM: %s.", strerror(errno));
377 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
378 devc->expect_response = TRUE;
383 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
385 struct sr_datafeed_packet packet;
386 struct dev_context *devc;
388 if (sdi->status != SR_ST_ACTIVE)
391 if (!(devc = sdi->priv)) {
392 sr_err("sdi->priv was NULL.");
396 sr_dbg("Stopping acquisition.");
398 sr_source_remove(devc->serial->fd);
399 hw_dev_close((struct sr_dev_inst *)sdi);
401 /* Send end packet to the session bus. */
402 sr_dbg("Sending SR_DF_END.");
403 packet.type = SR_DF_END;
404 sr_session_send(cb_data, &packet);
409 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
411 .longname = "Fluke 18x/28x series DMMs",
414 .cleanup = hw_cleanup,
416 .dev_list = hw_dev_list,
417 .dev_clear = clear_instances,
418 .dev_open = hw_dev_open,
419 .dev_close = hw_dev_close,
420 .info_get = hw_info_get,
421 .dev_config_set = hw_dev_config_set,
422 .dev_acquisition_start = hw_dev_acquisition_start,
423 .dev_acquisition_stop = hw_dev_acquisition_stop,