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"
24 #include "fluke-dmm.h"
25 #include <sys/types.h>
31 static const int hwopts[] = {
37 static const int hwcaps[] = {
39 SR_HWCAP_LIMIT_SAMPLES,
45 static const char *probe_names[] = {
50 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
51 static struct sr_dev_driver *di = &flukedmm_driver_info;
53 static const struct flukedmm_profile supported_flukedmm[] = {
54 { FLUKE_187, "187", 100 },
55 { FLUKE_287, "287", 100 },
59 /* Properly close and free all devices. */
60 static int clear_instances(void)
62 struct sr_dev_inst *sdi;
63 struct drv_context *drvc;
64 struct dev_context *devc;
67 if (!(drvc = di->priv))
71 for (l = drvc->instances; l; l = l->next) {
74 if (!(devc = sdi->priv))
76 sr_serial_dev_inst_free(devc->serial);
77 sr_dev_inst_free(sdi);
79 g_slist_free(drvc->instances);
80 drvc->instances = NULL;
85 static int hw_init(void)
87 struct drv_context *drvc;
89 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
90 sr_err("fluke-dmm: driver context malloc failed.");
99 static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
105 start = g_get_monotonic_time();
110 len = maxlen - *buflen - 1;
113 len = serial_read(fd, *buf + *buflen, 1);
116 *(*buf + *buflen) = '\0';
117 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
118 /* Strip LF and terminate. */
119 *(*buf + --*buflen) = '\0';
123 if (g_get_monotonic_time() - start > timeout_ms)
128 sr_dbg("fluke-dmm: received %d: '%s'", *buflen, *buf);
133 static GSList *fluke_scan(const char *conn, const char *serialcomm)
135 struct sr_dev_inst *sdi;
136 struct drv_context *drvc;
137 struct dev_context *devc;
138 struct sr_probe *probe;
140 int fd, retry, len, i, s;
141 char buf[128], *b, **tokens;
143 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
144 sr_err("fluke-dmm: unable to open %s: %s", conn, strerror(errno));
147 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
148 sr_err("fluke-dmm: unable to set serial parameters");
156 /* We'll try the discovery sequence three times in case the device
157 * is not in an idle state when we send ID. */
158 while (!devices && retry < 3) {
161 if (serial_write(fd, "ID\r", 3) == -1) {
162 sr_err("fluke-dmm: unable to send ID string: %s",
167 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
168 * or '1' to signify an error. */
170 serial_readline(fd, &b, &len, 150);
176 /* If CMD_ACK was OK, ID string follows. */
178 serial_readline(fd, &b, &len, 150);
181 tokens = g_strsplit(buf, ",", 3);
182 if (!strncmp("FLUKE", tokens[0], 5)
183 && tokens[1] && tokens[2]) {
184 for (i = 0; supported_flukedmm[i].model; i++) {
185 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
187 /* Skip leading spaces in version number. */
188 for (s = 0; tokens[1][s] == ' '; s++);
189 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
190 tokens[0] + 6, tokens[1] + s)))
192 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
193 sr_dbg("fluke-dmm: failed to malloc devc");
196 devc->profile = &supported_flukedmm[i];
197 devc->serial = sr_serial_dev_inst_new(conn, -1);
198 devc->serialcomm = g_strdup(serialcomm);
201 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
203 sdi->probes = g_slist_append(sdi->probes, probe);
204 drvc->instances = g_slist_append(drvc->instances, sdi);
205 devices = g_slist_append(devices, sdi);
216 static GSList *hw_scan(GSList *options)
218 struct sr_hwopt *opt;
220 const char *conn, *serialcomm;
222 conn = serialcomm = NULL;
223 for (l = options; l; l = l->next) {
225 switch (opt->hwopt) {
229 case SR_HWOPT_SERIALCOMM:
230 serialcomm = opt->value;
238 /* Use the provided comm specs. */
239 devices = fluke_scan(conn, serialcomm);
241 /* Try 115200, as used on 287/289. */
242 devices = fluke_scan(conn, "115200/8n1");
244 /* Fall back to 9600 for 187/189. */
245 devices = fluke_scan(conn, "9600/8n1");
251 static GSList *hw_dev_list(void)
253 struct drv_context *drvc;
257 return drvc->instances;
260 static int hw_dev_open(struct sr_dev_inst *sdi)
262 struct dev_context *devc;
264 if (!(devc = sdi->priv)) {
265 sr_err("fluke-dmm: sdi->priv was NULL.");
269 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
270 if (devc->serial->fd == -1) {
271 sr_err("fluke-dmm: Couldn't open serial port '%s'.",
275 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
276 sr_err("fluke-dmm: unable to set serial parameters");
279 sdi->status = SR_ST_ACTIVE;
284 static int hw_dev_close(struct sr_dev_inst *sdi)
286 struct dev_context *devc;
288 if (!(devc = sdi->priv)) {
289 sr_err("fluke-dmm: sdi->priv was NULL.");
293 if (devc->serial && devc->serial->fd != -1) {
294 serial_close(devc->serial->fd);
295 devc->serial->fd = -1;
296 sdi->status = SR_ST_INACTIVE;
302 static int hw_cleanup(void)
310 static int hw_info_get(int info_id, const void **data,
311 const struct sr_dev_inst *sdi)
323 case SR_DI_NUM_PROBES:
324 *data = GINT_TO_POINTER(1);
326 case SR_DI_PROBE_NAMES:
336 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
339 struct dev_context *devc;
341 if (sdi->status != SR_ST_ACTIVE)
344 if (!(devc = sdi->priv)) {
345 sr_err("fluke-dmm: sdi->priv was NULL.");
350 case SR_HWCAP_LIMIT_MSEC:
351 /* TODO: not yet implemented */
352 if (*(const uint64_t *)value == 0) {
353 sr_err("fluke-dmm: LIMIT_MSEC can't be 0.");
356 devc->limit_msec = *(const uint64_t *)value;
357 sr_dbg("fluke-dmm: Setting time limit to %" PRIu64 "ms.",
360 case SR_HWCAP_LIMIT_SAMPLES:
361 devc->limit_samples = *(const uint64_t *)value;
362 sr_dbg("fluke-dmm: Setting sample limit to %" PRIu64 ".",
363 devc->limit_samples);
366 sr_err("fluke-dmm: Unknown capability: %d.", hwcap);
374 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
377 struct sr_datafeed_packet packet;
378 struct sr_datafeed_header header;
379 struct sr_datafeed_meta_analog meta;
380 struct dev_context *devc;
382 if (!(devc = sdi->priv)) {
383 sr_err("fluke-dmm: sdi->priv was NULL.");
387 sr_dbg("fluke-dmm: Starting acquisition.");
389 devc->cb_data = cb_data;
391 /* Send header packet to the session bus. */
392 sr_dbg("fluke-dmm: Sending SR_DF_HEADER.");
393 packet.type = SR_DF_HEADER;
394 packet.payload = (uint8_t *)&header;
395 header.feed_version = 1;
396 gettimeofday(&header.starttime, NULL);
397 sr_session_send(devc->cb_data, &packet);
399 /* Send metadata about the SR_DF_ANALOG packets to come. */
400 sr_dbg("fluke-dmm: Sending SR_DF_META_ANALOG.");
401 packet.type = SR_DF_META_ANALOG;
402 packet.payload = &meta;
404 sr_session_send(devc->cb_data, &packet);
406 /* Poll every 100ms, or whenever some data comes in. */
407 sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
409 if (serial_write(devc->serial->fd, "QM\r", 3) == -1) {
410 sr_err("fluke-dmm: unable to send QM: %s", strerror(errno));
413 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
414 devc->expect_response = TRUE;
419 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
422 struct sr_datafeed_packet packet;
423 struct dev_context *devc;
425 if (sdi->status != SR_ST_ACTIVE)
428 if (!(devc = sdi->priv)) {
429 sr_err("fluke-dmm: sdi->priv was NULL.");
433 sr_dbg("fluke-dmm: Stopping acquisition.");
435 sr_source_remove(devc->serial->fd);
436 hw_dev_close((struct sr_dev_inst *)sdi);
438 /* Send end packet to the session bus. */
439 sr_dbg("fluke-dmm: Sending SR_DF_END.");
440 packet.type = SR_DF_END;
441 sr_session_send(cb_data, &packet);
446 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
448 .longname = "Fluke 18x/28x series DMMs",
451 .cleanup = hw_cleanup,
453 .dev_list = hw_dev_list,
454 .dev_clear = clear_instances,
455 .dev_open = hw_dev_open,
456 .dev_close = hw_dev_close,
457 .info_get = hw_info_get,
458 .dev_config_set = hw_dev_config_set,
459 .dev_acquisition_start = hw_dev_acquisition_start,
460 .dev_acquisition_stop = hw_dev_acquisition_stop,