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[] = {
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("fluke-dmm: 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) == '\n') {
117 /* Strip LF and terminate. */
118 *(*buf + --*buflen) = '\0';
122 if (g_get_monotonic_time() - start > timeout_ms)
127 sr_dbg("fluke-dmm: 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("fluke-dmm: unable to open %s: %s", conn, strerror(errno));
146 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
147 sr_err("fluke-dmm: 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("fluke-dmm: 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_dbg("fluke-dmm: failed to malloc devc");
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("fluke-dmm: 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("fluke-dmm: Couldn't open serial port '%s'.",
274 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
275 sr_err("fluke-dmm: unable to set serial parameters");
278 sdi->status = SR_ST_ACTIVE;
283 static int hw_dev_close(struct sr_dev_inst *sdi)
285 struct dev_context *devc;
287 if (!(devc = sdi->priv)) {
288 sr_err("fluke-dmm: sdi->priv was NULL.");
292 if (devc->serial && devc->serial->fd != -1) {
293 serial_close(devc->serial->fd);
294 devc->serial->fd = -1;
295 sdi->status = SR_ST_INACTIVE;
301 static int hw_cleanup(void)
309 static int hw_info_get(int info_id, const void **data,
310 const struct sr_dev_inst *sdi)
322 case SR_DI_NUM_PROBES:
323 *data = GINT_TO_POINTER(1);
325 case SR_DI_PROBE_NAMES:
335 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
338 struct dev_context *devc;
340 if (sdi->status != SR_ST_ACTIVE)
343 if (!(devc = sdi->priv)) {
344 sr_err("fluke-dmm: sdi->priv was NULL.");
349 case SR_HWCAP_LIMIT_MSEC:
350 /* TODO: not yet implemented */
351 if (*(const uint64_t *)value == 0) {
352 sr_err("fluke-dmm: LIMIT_MSEC can't be 0.");
355 devc->limit_msec = *(const uint64_t *)value;
356 sr_dbg("fluke-dmm: Setting time limit to %" PRIu64 "ms.",
359 case SR_HWCAP_LIMIT_SAMPLES:
360 devc->limit_samples = *(const uint64_t *)value;
361 sr_dbg("fluke-dmm: Setting sample limit to %" PRIu64 ".",
362 devc->limit_samples);
365 sr_err("fluke-dmm: Unknown capability: %d.", hwcap);
373 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
376 struct sr_datafeed_packet packet;
377 struct sr_datafeed_header header;
378 struct sr_datafeed_meta_analog meta;
379 struct dev_context *devc;
381 if (!(devc = sdi->priv)) {
382 sr_err("fluke-dmm: sdi->priv was NULL.");
386 sr_dbg("fluke-dmm: Starting acquisition.");
388 devc->cb_data = cb_data;
390 /* Send header packet to the session bus. */
391 sr_dbg("fluke-dmm: Sending SR_DF_HEADER.");
392 packet.type = SR_DF_HEADER;
393 packet.payload = (uint8_t *)&header;
394 header.feed_version = 1;
395 gettimeofday(&header.starttime, NULL);
396 sr_session_send(devc->cb_data, &packet);
398 /* Send metadata about the SR_DF_ANALOG packets to come. */
399 sr_dbg("fluke-dmm: Sending SR_DF_META_ANALOG.");
400 packet.type = SR_DF_META_ANALOG;
401 packet.payload = &meta;
403 sr_session_send(devc->cb_data, &packet);
405 /* Poll every 100ms, or whenever some data comes in. */
406 sr_source_add(devc->serial->fd, G_IO_IN, 100, fluke_receive_data, (void *)sdi);
411 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
414 struct sr_datafeed_packet packet;
415 struct dev_context *devc;
417 if (sdi->status != SR_ST_ACTIVE)
420 if (!(devc = sdi->priv)) {
421 sr_err("fluke-dmm: sdi->priv was NULL.");
425 sr_dbg("fluke-dmm: Stopping acquisition.");
427 sr_source_remove(devc->serial->fd);
428 hw_dev_close((struct sr_dev_inst *)sdi);
430 /* Send end packet to the session bus. */
431 sr_dbg("fluke-dmm: Sending SR_DF_END.");
432 packet.type = SR_DF_END;
433 sr_session_send(cb_data, &packet);
438 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
440 .longname = "Fluke 18x/28x series DMMs",
443 .cleanup = hw_cleanup,
445 .dev_list = hw_dev_list,
446 .dev_clear = clear_instances,
447 .dev_open = hw_dev_open,
448 .dev_close = hw_dev_close,
449 .info_get = hw_info_get,
450 .dev_config_set = hw_dev_config_set,
451 .dev_acquisition_start = hw_dev_acquisition_start,
452 .dev_acquisition_stop = hw_dev_acquisition_stop,