2 * This file is part of the libsigrok 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 int32_t hwopts[] = {
35 static const int32_t hwcaps[] = {
37 SR_CONF_LIMIT_SAMPLES,
42 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
43 static struct sr_dev_driver *di = &flukedmm_driver_info;
45 static char *scan_conn[] = {
50 /* Scopemeter 190 series */
55 static const struct flukedmm_profile supported_flukedmm[] = {
56 { FLUKE_187, "187", 100, 1000 },
57 { FLUKE_287, "287", 100, 1000 },
58 { FLUKE_190, "199B", 1000, 3500 },
61 static int clear_instances(void)
63 return std_dev_clear(di, NULL);
66 static int init(struct sr_context *sr_ctx)
68 return std_hw_init(sr_ctx, di, LOG_PREFIX);
71 static GSList *fluke_scan(const char *conn, const char *serialcomm)
73 struct sr_dev_inst *sdi;
74 struct drv_context *drvc;
75 struct dev_context *devc;
76 struct sr_probe *probe;
77 struct sr_serial_dev_inst *serial;
80 char buf[128], *b, **tokens;
82 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
85 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
92 /* We'll try the discovery sequence three times in case the device
93 * is not in an idle state when we send ID. */
94 while (!devices && retry < 3) {
97 if (serial_write(serial, "ID\r", 3) == -1) {
98 sr_err("Unable to send ID string: %s.",
103 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
104 * or '1' to signify an error. */
106 serial_readline(serial, &b, &len, 150);
112 /* If CMD_ACK was OK, ID string follows. */
114 serial_readline(serial, &b, &len, 850);
117 if (strcspn(buf, ",") < 15)
118 /* Looks like it's comma-separated. */
119 tokens = g_strsplit(buf, ",", 3);
121 /* Fluke 199B, at least, uses semicolon. */
122 tokens = g_strsplit(buf, ";", 3);
123 if (!strncmp("FLUKE", tokens[0], 5)
124 && tokens[1] && tokens[2]) {
125 for (i = 0; supported_flukedmm[i].model; i++) {
126 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
128 /* Skip leading spaces in version number. */
129 for (s = 0; tokens[1][s] == ' '; s++);
130 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
131 tokens[0] + 6, tokens[1] + s)))
133 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
134 sr_err("Device context malloc failed.");
137 devc->profile = &supported_flukedmm[i];
138 sdi->inst_type = SR_INST_SERIAL;
142 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
144 sdi->probes = g_slist_append(sdi->probes, probe);
145 drvc->instances = g_slist_append(drvc->instances, sdi);
146 devices = g_slist_append(devices, sdi);
155 serial_close(serial);
157 sr_serial_dev_inst_free(serial);
162 static GSList *scan(GSList *options)
164 struct sr_config *src;
167 const char *conn, *serialcomm;
169 conn = serialcomm = NULL;
170 for (l = options; l; l = l->next) {
174 conn = g_variant_get_string(src->data, NULL);
176 case SR_CONF_SERIALCOMM:
177 serialcomm = g_variant_get_string(src->data, NULL);
185 /* Use the provided comm specs. */
186 devices = fluke_scan(conn, serialcomm);
188 for (i = 0; scan_conn[i]; i++) {
189 if ((devices = fluke_scan(conn, scan_conn[i])))
191 /* The Scopemeter 199B, at least, requires this
192 * after all the 115k/9.6k confusion. */
200 static GSList *dev_list(void)
202 return ((struct drv_context *)(di->priv))->instances;
205 static int dev_open(struct sr_dev_inst *sdi)
207 struct sr_serial_dev_inst *serial;
210 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
213 sdi->status = SR_ST_ACTIVE;
218 static int dev_close(struct sr_dev_inst *sdi)
220 struct sr_serial_dev_inst *serial;
223 if (serial && serial->fd != -1) {
224 serial_close(serial);
225 sdi->status = SR_ST_INACTIVE;
231 static int cleanup(void)
233 return clear_instances();
236 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
238 struct dev_context *devc;
240 if (sdi->status != SR_ST_ACTIVE)
241 return SR_ERR_DEV_CLOSED;
243 if (!(devc = sdi->priv)) {
244 sr_err("sdi->priv was NULL.");
249 case SR_CONF_LIMIT_MSEC:
250 /* TODO: not yet implemented */
251 if (g_variant_get_uint64(data) == 0) {
252 sr_err("LIMIT_MSEC can't be 0.");
255 devc->limit_msec = g_variant_get_uint64(data);
256 sr_dbg("Setting time limit to %" PRIu64 "ms.",
259 case SR_CONF_LIMIT_SAMPLES:
260 devc->limit_samples = g_variant_get_uint64(data);
261 sr_dbg("Setting sample limit to %" PRIu64 ".",
262 devc->limit_samples);
271 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
277 case SR_CONF_SCAN_OPTIONS:
278 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
279 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
281 case SR_CONF_DEVICE_OPTIONS:
282 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
283 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
292 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
294 struct dev_context *devc;
295 struct sr_serial_dev_inst *serial;
297 if (sdi->status != SR_ST_ACTIVE)
298 return SR_ERR_DEV_CLOSED;
300 if (!(devc = sdi->priv)) {
301 sr_err("sdi->priv was NULL.");
305 devc->cb_data = cb_data;
307 /* Send header packet to the session bus. */
308 std_session_send_df_header(cb_data, LOG_PREFIX);
310 /* Poll every 100ms, or whenever some data comes in. */
312 sr_source_add(serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
314 if (serial_write(serial, "QM\r", 3) == -1) {
315 sr_err("Unable to send QM: %s.", strerror(errno));
318 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
319 devc->expect_response = TRUE;
324 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
326 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
327 sdi->conn, LOG_PREFIX);
330 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
332 .longname = "Fluke 18x/28x series DMMs",
337 .dev_list = dev_list,
338 .dev_clear = clear_instances,
340 .config_set = config_set,
341 .config_list = config_list,
342 .dev_open = dev_open,
343 .dev_close = dev_close,
344 .dev_acquisition_start = dev_acquisition_start,
345 .dev_acquisition_stop = dev_acquisition_stop,