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 "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #include "fluke-dmm.h"
24 #include <sys/types.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 int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
104 start = g_get_monotonic_time();
109 len = maxlen - *buflen - 1;
112 len = serial_read(fd, *buf + *buflen, 1);
115 *(*buf + *buflen) = '\0';
116 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
117 /* Strip LF and terminate. */
118 *(*buf + --*buflen) = '\0';
122 if (g_get_monotonic_time() - start > timeout_ms)
127 sr_dbg("Received %d: '%s'.", *buflen, *buf);
132 static GSList *fluke_scan(const char *conn, const char *serialcomm)
134 struct sr_dev_inst *sdi;
135 struct drv_context *drvc;
136 struct dev_context *devc;
137 struct sr_probe *probe;
139 int fd, retry, len, i, s;
140 char buf[128], *b, **tokens;
142 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
143 sr_err("Unable to open %s: %s.", conn, strerror(errno));
146 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
147 sr_err("Unable to set serial parameters.");
155 /* We'll try the discovery sequence three times in case the device
156 * is not in an idle state when we send ID. */
157 while (!devices && retry < 3) {
160 if (serial_write(fd, "ID\r", 3) == -1) {
161 sr_err("Unable to send ID string: %s.",
166 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
167 * or '1' to signify an error. */
169 serial_readline(fd, &b, &len, 150);
175 /* If CMD_ACK was OK, ID string follows. */
177 serial_readline(fd, &b, &len, 150);
180 tokens = g_strsplit(buf, ",", 3);
181 if (!strncmp("FLUKE", tokens[0], 5)
182 && tokens[1] && tokens[2]) {
183 for (i = 0; supported_flukedmm[i].model; i++) {
184 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
186 /* Skip leading spaces in version number. */
187 for (s = 0; tokens[1][s] == ' '; s++);
188 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
189 tokens[0] + 6, tokens[1] + s)))
191 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
192 sr_err("Device context malloc failed.");
195 devc->profile = &supported_flukedmm[i];
196 devc->serial = sr_serial_dev_inst_new(conn, -1);
197 devc->serialcomm = g_strdup(serialcomm);
200 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
202 sdi->probes = g_slist_append(sdi->probes, probe);
203 drvc->instances = g_slist_append(drvc->instances, sdi);
204 devices = g_slist_append(devices, sdi);
215 static GSList *hw_scan(GSList *options)
217 struct sr_hwopt *opt;
219 const char *conn, *serialcomm;
221 conn = serialcomm = NULL;
222 for (l = options; l; l = l->next) {
224 switch (opt->hwopt) {
228 case SR_HWOPT_SERIALCOMM:
229 serialcomm = opt->value;
237 /* Use the provided comm specs. */
238 devices = fluke_scan(conn, serialcomm);
240 /* Try 115200, as used on 287/289. */
241 devices = fluke_scan(conn, "115200/8n1");
243 /* Fall back to 9600 for 187/189. */
244 devices = fluke_scan(conn, "9600/8n1");
250 static GSList *hw_dev_list(void)
252 struct drv_context *drvc;
256 return drvc->instances;
259 static int hw_dev_open(struct sr_dev_inst *sdi)
261 struct dev_context *devc;
263 if (!(devc = sdi->priv)) {
264 sr_err("sdi->priv was NULL.");
268 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
269 if (devc->serial->fd == -1) {
270 sr_err("Couldn't open serial port '%s'.", devc->serial->port);
273 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
274 sr_err("Unable to set serial parameters.");
277 sdi->status = SR_ST_ACTIVE;
282 static int hw_dev_close(struct sr_dev_inst *sdi)
284 struct dev_context *devc;
286 if (!(devc = sdi->priv)) {
287 sr_err("sdi->priv was NULL.");
291 if (devc->serial && devc->serial->fd != -1) {
292 serial_close(devc->serial->fd);
293 devc->serial->fd = -1;
294 sdi->status = SR_ST_INACTIVE;
300 static int hw_cleanup(void)
308 static int hw_info_get(int info_id, const void **data,
309 const struct sr_dev_inst *sdi)
321 case SR_DI_NUM_PROBES:
322 *data = GINT_TO_POINTER(1);
324 case SR_DI_PROBE_NAMES:
334 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
337 struct dev_context *devc;
339 if (sdi->status != SR_ST_ACTIVE)
342 if (!(devc = sdi->priv)) {
343 sr_err("sdi->priv was NULL.");
348 case SR_HWCAP_LIMIT_MSEC:
349 /* TODO: not yet implemented */
350 if (*(const uint64_t *)value == 0) {
351 sr_err("LIMIT_MSEC can't be 0.");
354 devc->limit_msec = *(const uint64_t *)value;
355 sr_dbg("Setting time limit to %" PRIu64 "ms.",
358 case SR_HWCAP_LIMIT_SAMPLES:
359 devc->limit_samples = *(const uint64_t *)value;
360 sr_dbg("Setting sample limit to %" PRIu64 ".",
361 devc->limit_samples);
364 sr_err("Unknown capability: %d.", hwcap);
372 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
375 struct sr_datafeed_packet packet;
376 struct sr_datafeed_header header;
377 struct sr_datafeed_meta_analog meta;
378 struct dev_context *devc;
380 if (!(devc = sdi->priv)) {
381 sr_err("sdi->priv was NULL.");
385 sr_dbg("Starting acquisition.");
387 devc->cb_data = cb_data;
389 /* Send header packet to the session bus. */
390 sr_dbg("Sending SR_DF_HEADER.");
391 packet.type = SR_DF_HEADER;
392 packet.payload = (uint8_t *)&header;
393 header.feed_version = 1;
394 gettimeofday(&header.starttime, NULL);
395 sr_session_send(devc->cb_data, &packet);
397 /* Send metadata about the SR_DF_ANALOG packets to come. */
398 sr_dbg("Sending SR_DF_META_ANALOG.");
399 packet.type = SR_DF_META_ANALOG;
400 packet.payload = &meta;
402 sr_session_send(devc->cb_data, &packet);
404 /* Poll every 100ms, or whenever some data comes in. */
405 sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
407 if (serial_write(devc->serial->fd, "QM\r", 3) == -1) {
408 sr_err("Unable to send QM: %s.", strerror(errno));
411 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
412 devc->expect_response = TRUE;
417 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
420 struct sr_datafeed_packet packet;
421 struct dev_context *devc;
423 if (sdi->status != SR_ST_ACTIVE)
426 if (!(devc = sdi->priv)) {
427 sr_err("sdi->priv was NULL.");
431 sr_dbg("Stopping acquisition.");
433 sr_source_remove(devc->serial->fd);
434 hw_dev_close((struct sr_dev_inst *)sdi);
436 /* Send end packet to the session bus. */
437 sr_dbg("Sending SR_DF_END.");
438 packet.type = SR_DF_END;
439 sr_session_send(cb_data, &packet);
444 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
446 .longname = "Fluke 18x/28x series DMMs",
449 .cleanup = hw_cleanup,
451 .dev_list = hw_dev_list,
452 .dev_clear = clear_instances,
453 .dev_open = hw_dev_open,
454 .dev_close = hw_dev_close,
455 .info_get = hw_info_get,
456 .dev_config_set = hw_dev_config_set,
457 .dev_acquisition_start = hw_dev_acquisition_start,
458 .dev_acquisition_stop = hw_dev_acquisition_stop,