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>
32 SR_PRIV struct sr_dev_driver driver_info;
33 static struct sr_dev_driver *di = &driver_info;
35 static const struct flukedmm_profile supported_flukedmm[] = {
40 /* Properly close and free all devices. */
41 static int clear_instances(void)
43 struct sr_dev_inst *sdi;
44 struct drv_context *drvc;
45 struct dev_context *devc;
48 if (!(drvc = di->priv))
52 for (l = drvc->instances; l; l = l->next) {
55 if (!(devc = sdi->priv))
57 sr_serial_dev_inst_free(devc->serial);
58 sr_dev_inst_free(sdi);
60 g_slist_free(drvc->instances);
61 drvc->instances = NULL;
66 static int hw_init(void)
68 struct drv_context *drvc;
70 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
71 sr_err("fluke-dmm: driver context malloc failed.");
80 static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
86 start = g_get_monotonic_time();
91 len = maxlen - *buflen - 1;
94 len = serial_read(fd, *buf + *buflen, len);
97 *(*buf + *buflen) = '\0';
98 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r')
102 if (g_get_monotonic_time() - start > timeout_ms)
110 if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n')
111 *(*buf + --*buflen) = '\0';
116 sr_dbg("fluke-dmm: received '%s'", *buf);
121 static GSList *hw_scan(GSList *options)
123 struct sr_dev_inst *sdi;
124 struct drv_context *drvc;
125 struct dev_context *devc;
126 struct sr_hwopt *opt;
127 struct sr_probe *probe;
130 const char *conn, *serialcomm;
134 drvc->instances = NULL;
137 conn = serialcomm = NULL;
138 for (l = options; l; l = l->next) {
140 switch (opt->hwopt) {
144 case SR_HWOPT_SERIALCOMM:
145 serialcomm = opt->value;
150 sr_dbg("fluke-dmm: no serial port provided");
154 sr_dbg("fluke-dmm: no serial communication parameters provided");
158 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
159 sr_err("fluke-dmm: unable to open %s: %s", conn, strerror(errno));
163 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
164 sr_err("fluke-dmm: unable to set serial parameters: %s",
170 if (serial_write(fd, "ID\r", 3) == -1) {
171 sr_err("fluke-dmm: unable to send identification string: %s",
177 buf = g_try_malloc(len);
178 serial_readline(fd, &buf, &len, 150);
182 if (len < 2 || buf[0] != '0' || buf[1] != '\r') {
183 sr_err("fluke-dmm: invalid response to identification string");
187 tokens = g_strsplit(buf + 2, ",", 3);
188 if (!strncmp("FLUKE", tokens[0], 5)
189 && tokens[1] && tokens[2]) {
190 for (i = 0; supported_flukedmm[i].model; i++) {
191 if (strcmp(supported_flukedmm[i].modelname, tokens[0]))
193 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
194 tokens[0] + 6, tokens[1])))
196 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
197 sr_dbg("fluke-dmm: failed to malloc devc");
200 devc->profile = &supported_flukedmm[i];
201 devc->serial = sr_serial_dev_inst_new(conn, -1);
204 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
206 sdi->probes = g_slist_append(sdi->probes, probe);
207 drvc->instances = g_slist_append(drvc->instances, sdi);
208 devices = g_slist_append(devices, sdi);
220 static GSList *hw_dev_list(void)
222 struct drv_context *drvc;
226 return drvc->instances;
229 static int hw_dev_open(struct sr_dev_inst *sdi)
237 static int hw_dev_close(struct sr_dev_inst *sdi)
245 static int hw_cleanup(void)
255 static int hw_info_get(int info_id, const void **data,
256 const struct sr_dev_inst *sdi)
269 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
274 if (sdi->status != SR_ST_ACTIVE)
287 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
296 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
302 if (sdi->status != SR_ST_ACTIVE)
310 SR_PRIV struct sr_dev_driver driver_info = {
312 .longname = "Fluke 18x/28x series DMMs",
315 .cleanup = hw_cleanup,
317 .dev_list = hw_dev_list,
318 .dev_clear = clear_instances,
319 .dev_open = hw_dev_open,
320 .dev_close = hw_dev_close,
321 .info_get = hw_info_get,
322 .dev_config_set = hw_dev_config_set,
323 .dev_acquisition_start = hw_dev_acquisition_start,
324 .dev_acquisition_stop = hw_dev_acquisition_stop,