]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/api.c
New driver API function: config_list()
[libsigrok.git] / hardware / fluke-dmm / api.c
CommitLineData
883a2e9e
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <glib.h>
4f958423
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <string.h>
25#include <errno.h>
6f22a8ef
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "fluke-dmm.h"
883a2e9e 29
d3f8f141 30static const int hwopts[] = {
1953564a
BV
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
d3f8f141
BV
33 0,
34};
35
36static const int hwcaps[] = {
1953564a
BV
37 SR_CONF_MULTIMETER,
38 SR_CONF_LIMIT_SAMPLES,
39 SR_CONF_LIMIT_MSEC,
40 SR_CONF_CONTINUOUS,
d3f8f141
BV
41 0,
42};
43
e7edd64f
BV
44SR_PRIV struct sr_dev_driver flukedmm_driver_info;
45static struct sr_dev_driver *di = &flukedmm_driver_info;
883a2e9e 46
9fa09680
BV
47static char *scan_conn[] = {
48 /* 287/289 */
49 "115200/8n1",
50 /* 187/189 */
51 "9600/8n1",
52 /* Scopemeter 190 series */
53 "1200/8n1",
54 NULL
55};
56
4f958423 57static const struct flukedmm_profile supported_flukedmm[] = {
d4b11de0
BV
58 { FLUKE_187, "187", 100, 1000 },
59 { FLUKE_287, "287", 100, 1000 },
60 { FLUKE_190, "199B", 1000, 3500 },
4f958423
BV
61};
62
883a2e9e
BV
63
64/* Properly close and free all devices. */
65static int clear_instances(void)
66{
67 struct sr_dev_inst *sdi;
68 struct drv_context *drvc;
69 struct dev_context *devc;
70 GSList *l;
71
4f958423
BV
72 if (!(drvc = di->priv))
73 return SR_OK;
74
883a2e9e
BV
75 drvc = di->priv;
76 for (l = drvc->instances; l; l = l->next) {
4f958423 77 if (!(sdi = l->data))
883a2e9e 78 continue;
4f958423 79 if (!(devc = sdi->priv))
883a2e9e 80 continue;
4f958423 81 sr_serial_dev_inst_free(devc->serial);
883a2e9e
BV
82 sr_dev_inst_free(sdi);
83 }
883a2e9e
BV
84 g_slist_free(drvc->instances);
85 drvc->instances = NULL;
86
87 return SR_OK;
88}
89
34f06b90 90static int hw_init(struct sr_context *sr_ctx)
883a2e9e
BV
91{
92 struct drv_context *drvc;
93
94 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
31d84da3 95 sr_err("Driver context malloc failed.");
886a52b6 96 return SR_ERR_MALLOC;
883a2e9e
BV
97 }
98
1ebe4b4e 99 drvc->sr_ctx = sr_ctx;
883a2e9e
BV
100 di->priv = drvc;
101
102 return SR_OK;
103}
104
41298320 105static GSList *fluke_scan(const char *conn, const char *serialcomm)
883a2e9e 106{
4f958423 107 struct sr_dev_inst *sdi;
883a2e9e 108 struct drv_context *drvc;
4f958423 109 struct dev_context *devc;
4f958423 110 struct sr_probe *probe;
58d03f03 111 struct sr_serial_dev_inst *serial;
41298320 112 GSList *devices;
58d03f03 113 int retry, len, i, s;
fb480d57 114 char buf[128], *b, **tokens;
883a2e9e 115
58d03f03 116 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
4f958423 117 return NULL;
58d03f03 118
a54dd31e 119 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
4f958423 120 return NULL;
4f958423 121
41298320 122 drvc = di->priv;
fb480d57
BV
123 b = buf;
124 retry = 0;
41298320 125 devices = NULL;
fb480d57
BV
126 /* We'll try the discovery sequence three times in case the device
127 * is not in an idle state when we send ID. */
128 while (!devices && retry < 3) {
129 retry++;
58d03f03
BV
130 serial_flush(serial);
131 if (serial_write(serial, "ID\r", 3) == -1) {
31d84da3
UH
132 sr_err("Unable to send ID string: %s.",
133 strerror(errno));
fb480d57
BV
134 continue;
135 }
4f958423 136
fb480d57
BV
137 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
138 * or '1' to signify an error. */
139 len = 128;
58d03f03 140 serial_readline(serial, &b, &len, 150);
fb480d57
BV
141 if (len != 1)
142 continue;
41298320 143 if (buf[0] != '0')
fb480d57 144 continue;
4f958423 145
fb480d57
BV
146 /* If CMD_ACK was OK, ID string follows. */
147 len = 128;
9fa09680 148 serial_readline(serial, &b, &len, 850);
fb480d57
BV
149 if (len < 10)
150 continue;
9fa09680
BV
151 if (strcspn(buf, ",") < 15)
152 /* Looks like it's comma-separated. */
153 tokens = g_strsplit(buf, ",", 3);
154 else
155 /* Fluke 199B, at least, uses semicolon. */
156 tokens = g_strsplit(buf, ";", 3);
fb480d57
BV
157 if (!strncmp("FLUKE", tokens[0], 5)
158 && tokens[1] && tokens[2]) {
159 for (i = 0; supported_flukedmm[i].model; i++) {
160 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
161 continue;
162 /* Skip leading spaces in version number. */
163 for (s = 0; tokens[1][s] == ' '; s++);
164 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
165 tokens[0] + 6, tokens[1] + s)))
166 return NULL;
167 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
31d84da3 168 sr_err("Device context malloc failed.");
fb480d57
BV
169 return NULL;
170 }
171 devc->profile = &supported_flukedmm[i];
58d03f03 172 devc->serial = serial;
fb480d57
BV
173 sdi->priv = devc;
174 sdi->driver = di;
175 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
176 return NULL;
177 sdi->probes = g_slist_append(sdi->probes, probe);
178 drvc->instances = g_slist_append(drvc->instances, sdi);
179 devices = g_slist_append(devices, sdi);
180 break;
4f958423 181 }
4f958423 182 }
fb480d57 183 g_strfreev(tokens);
9fa09680
BV
184 if (devices)
185 /* Found one. */
186 break;
4f958423 187 }
58d03f03
BV
188 serial_close(serial);
189 if (!devices)
190 sr_serial_dev_inst_free(serial);
883a2e9e
BV
191
192 return devices;
193}
194
41298320
BV
195static GSList *hw_scan(GSList *options)
196{
1987b8d6 197 struct sr_config *src;
41298320 198 GSList *l, *devices;
9fa09680 199 int i;
41298320
BV
200 const char *conn, *serialcomm;
201
202 conn = serialcomm = NULL;
203 for (l = options; l; l = l->next) {
1987b8d6
BV
204 src = l->data;
205 switch (src->key) {
1953564a 206 case SR_CONF_CONN:
1987b8d6 207 conn = src->value;
41298320 208 break;
1953564a 209 case SR_CONF_SERIALCOMM:
1987b8d6 210 serialcomm = src->value;
41298320
BV
211 break;
212 }
213 }
214 if (!conn)
215 return NULL;
216
217 if (serialcomm) {
218 /* Use the provided comm specs. */
219 devices = fluke_scan(conn, serialcomm);
220 } else {
9fa09680
BV
221 for (i = 0; scan_conn[i]; i++) {
222 if ((devices = fluke_scan(conn, scan_conn[i])))
223 break;
224 /* The Scopemeter 199B, at least, requires this
225 * after all the 115k/9.6k confusion. */
226 g_usleep(5000);
227 }
41298320
BV
228 }
229
230 return devices;
231}
232
883a2e9e
BV
233static GSList *hw_dev_list(void)
234{
235 struct drv_context *drvc;
236
237 drvc = di->priv;
238
239 return drvc->instances;
240}
241
242static int hw_dev_open(struct sr_dev_inst *sdi)
243{
41298320 244 struct dev_context *devc;
883a2e9e 245
41298320 246 if (!(devc = sdi->priv)) {
31d84da3 247 sr_err("sdi->priv was NULL.");
41298320
BV
248 return SR_ERR_BUG;
249 }
250
a54dd31e 251 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
41298320 252 return SR_ERR;
58d03f03 253
41298320 254 sdi->status = SR_ST_ACTIVE;
883a2e9e
BV
255
256 return SR_OK;
257}
258
259static int hw_dev_close(struct sr_dev_inst *sdi)
260{
d3f8f141 261 struct dev_context *devc;
883a2e9e 262
d3f8f141 263 if (!(devc = sdi->priv)) {
31d84da3 264 sr_err("sdi->priv was NULL.");
d3f8f141
BV
265 return SR_ERR_BUG;
266 }
267
268 if (devc->serial && devc->serial->fd != -1) {
58d03f03 269 serial_close(devc->serial);
d3f8f141
BV
270 sdi->status = SR_ST_INACTIVE;
271 }
883a2e9e
BV
272
273 return SR_OK;
274}
275
276static int hw_cleanup(void)
277{
278
279 clear_instances();
280
883a2e9e
BV
281 return SR_OK;
282}
283
035a1078 284static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
883a2e9e 285{
d3f8f141 286 (void)sdi;
883a2e9e 287
035a1078 288 switch (id) {
d3f8f141
BV
289 case SR_DI_HWOPTS:
290 *data = hwopts;
291 break;
292 case SR_DI_HWCAPS:
293 *data = hwcaps;
294 break;
883a2e9e
BV
295 default:
296 return SR_ERR_ARG;
297 }
298
299 return SR_OK;
300}
301
035a1078 302static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
883a2e9e 303{
d3f8f141 304 struct dev_context *devc;
883a2e9e
BV
305
306 if (sdi->status != SR_ST_ACTIVE)
307 return SR_ERR;
308
d3f8f141 309 if (!(devc = sdi->priv)) {
31d84da3 310 sr_err("sdi->priv was NULL.");
d3f8f141
BV
311 return SR_ERR_BUG;
312 }
313
035a1078 314 switch (id) {
1953564a 315 case SR_CONF_LIMIT_MSEC:
d3f8f141
BV
316 /* TODO: not yet implemented */
317 if (*(const uint64_t *)value == 0) {
31d84da3 318 sr_err("LIMIT_MSEC can't be 0.");
d3f8f141
BV
319 return SR_ERR;
320 }
321 devc->limit_msec = *(const uint64_t *)value;
31d84da3 322 sr_dbg("Setting time limit to %" PRIu64 "ms.",
d3f8f141
BV
323 devc->limit_msec);
324 break;
1953564a 325 case SR_CONF_LIMIT_SAMPLES:
d3f8f141 326 devc->limit_samples = *(const uint64_t *)value;
31d84da3 327 sr_dbg("Setting sample limit to %" PRIu64 ".",
d3f8f141
BV
328 devc->limit_samples);
329 break;
883a2e9e 330 default:
035a1078 331 sr_err("Unknown capability: %d.", id);
d3f8f141
BV
332 return SR_ERR;
333 break;
883a2e9e
BV
334 }
335
d3f8f141 336 return SR_OK;
883a2e9e
BV
337}
338
339static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
340 void *cb_data)
341{
d3f8f141
BV
342 struct sr_datafeed_packet packet;
343 struct sr_datafeed_header header;
d3f8f141
BV
344 struct dev_context *devc;
345
346 if (!(devc = sdi->priv)) {
31d84da3 347 sr_err("sdi->priv was NULL.");
d3f8f141
BV
348 return SR_ERR_BUG;
349 }
350
31d84da3 351 sr_dbg("Starting acquisition.");
d3f8f141
BV
352
353 devc->cb_data = cb_data;
354
355 /* Send header packet to the session bus. */
31d84da3 356 sr_dbg("Sending SR_DF_HEADER.");
d3f8f141
BV
357 packet.type = SR_DF_HEADER;
358 packet.payload = (uint8_t *)&header;
359 header.feed_version = 1;
360 gettimeofday(&header.starttime, NULL);
361 sr_session_send(devc->cb_data, &packet);
883a2e9e 362
d3f8f141 363 /* Poll every 100ms, or whenever some data comes in. */
d38d2ef0
BV
364 sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
365
58d03f03 366 if (serial_write(devc->serial, "QM\r", 3) == -1) {
31d84da3 367 sr_err("Unable to send QM: %s.", strerror(errno));
d38d2ef0
BV
368 return SR_ERR;
369 }
370 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
371 devc->expect_response = TRUE;
883a2e9e
BV
372
373 return SR_OK;
374}
375
69b07d14 376static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
883a2e9e 377{
d3f8f141
BV
378 struct sr_datafeed_packet packet;
379 struct dev_context *devc;
883a2e9e
BV
380
381 if (sdi->status != SR_ST_ACTIVE)
382 return SR_ERR;
383
d3f8f141 384 if (!(devc = sdi->priv)) {
31d84da3 385 sr_err("sdi->priv was NULL.");
d3f8f141
BV
386 return SR_ERR_BUG;
387 }
388
31d84da3 389 sr_dbg("Stopping acquisition.");
d3f8f141
BV
390
391 sr_source_remove(devc->serial->fd);
392 hw_dev_close((struct sr_dev_inst *)sdi);
393
394 /* Send end packet to the session bus. */
31d84da3 395 sr_dbg("Sending SR_DF_END.");
d3f8f141
BV
396 packet.type = SR_DF_END;
397 sr_session_send(cb_data, &packet);
883a2e9e
BV
398
399 return SR_OK;
400}
401
e7edd64f 402SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
883a2e9e
BV
403 .name = "fluke-dmm",
404 .longname = "Fluke 18x/28x series DMMs",
405 .api_version = 1,
406 .init = hw_init,
407 .cleanup = hw_cleanup,
408 .scan = hw_scan,
409 .dev_list = hw_dev_list,
410 .dev_clear = clear_instances,
035a1078
BV
411 .config_get = config_get,
412 .config_set = config_set,
883a2e9e
BV
413 .dev_open = hw_dev_open,
414 .dev_close = hw_dev_close,
883a2e9e
BV
415 .dev_acquisition_start = hw_dev_acquisition_start,
416 .dev_acquisition_stop = hw_dev_acquisition_stop,
417 .priv = NULL,
418};