]>
Commit | Line | Data |
---|---|---|
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 | 30 | static const int hwopts[] = { |
1953564a BV |
31 | SR_CONF_CONN, |
32 | SR_CONF_SERIALCOMM, | |
d3f8f141 BV |
33 | 0, |
34 | }; | |
35 | ||
36 | static 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 |
44 | SR_PRIV struct sr_dev_driver flukedmm_driver_info; |
45 | static struct sr_dev_driver *di = &flukedmm_driver_info; | |
883a2e9e | 46 | |
9fa09680 BV |
47 | static 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 | 57 | static 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. */ | |
65 | static 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 | 90 | static 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 | 105 | static 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 |
195 | static 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 |
233 | static GSList *hw_dev_list(void) |
234 | { | |
235 | struct drv_context *drvc; | |
236 | ||
237 | drvc = di->priv; | |
238 | ||
239 | return drvc->instances; | |
240 | } | |
241 | ||
242 | static 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 | ||
259 | static 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 | ||
276 | static int hw_cleanup(void) | |
277 | { | |
278 | ||
279 | clear_instances(); | |
280 | ||
883a2e9e BV |
281 | return SR_OK; |
282 | } | |
283 | ||
035a1078 | 284 | static 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; | |
883a2e9e BV |
292 | default: |
293 | return SR_ERR_ARG; | |
294 | } | |
295 | ||
296 | return SR_OK; | |
297 | } | |
298 | ||
035a1078 | 299 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
883a2e9e | 300 | { |
d3f8f141 | 301 | struct dev_context *devc; |
883a2e9e BV |
302 | |
303 | if (sdi->status != SR_ST_ACTIVE) | |
304 | return SR_ERR; | |
305 | ||
d3f8f141 | 306 | if (!(devc = sdi->priv)) { |
31d84da3 | 307 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
308 | return SR_ERR_BUG; |
309 | } | |
310 | ||
035a1078 | 311 | switch (id) { |
1953564a | 312 | case SR_CONF_LIMIT_MSEC: |
d3f8f141 BV |
313 | /* TODO: not yet implemented */ |
314 | if (*(const uint64_t *)value == 0) { | |
31d84da3 | 315 | sr_err("LIMIT_MSEC can't be 0."); |
d3f8f141 BV |
316 | return SR_ERR; |
317 | } | |
318 | devc->limit_msec = *(const uint64_t *)value; | |
31d84da3 | 319 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
d3f8f141 BV |
320 | devc->limit_msec); |
321 | break; | |
1953564a | 322 | case SR_CONF_LIMIT_SAMPLES: |
d3f8f141 | 323 | devc->limit_samples = *(const uint64_t *)value; |
31d84da3 | 324 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
d3f8f141 BV |
325 | devc->limit_samples); |
326 | break; | |
883a2e9e | 327 | default: |
035a1078 | 328 | sr_err("Unknown capability: %d.", id); |
d3f8f141 BV |
329 | return SR_ERR; |
330 | break; | |
883a2e9e BV |
331 | } |
332 | ||
d3f8f141 | 333 | return SR_OK; |
883a2e9e BV |
334 | } |
335 | ||
a1c743fc BV |
336 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
337 | { | |
338 | ||
339 | (void)sdi; | |
340 | ||
341 | switch (key) { | |
9a6517d1 BV |
342 | case SR_CONF_DEVICE_OPTIONS: |
343 | *data = hwcaps; | |
344 | break; | |
a1c743fc BV |
345 | default: |
346 | return SR_ERR_ARG; | |
347 | } | |
348 | ||
349 | return SR_OK; | |
350 | } | |
351 | ||
883a2e9e BV |
352 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
353 | void *cb_data) | |
354 | { | |
d3f8f141 BV |
355 | struct sr_datafeed_packet packet; |
356 | struct sr_datafeed_header header; | |
d3f8f141 BV |
357 | struct dev_context *devc; |
358 | ||
359 | if (!(devc = sdi->priv)) { | |
31d84da3 | 360 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
361 | return SR_ERR_BUG; |
362 | } | |
363 | ||
31d84da3 | 364 | sr_dbg("Starting acquisition."); |
d3f8f141 BV |
365 | |
366 | devc->cb_data = cb_data; | |
367 | ||
368 | /* Send header packet to the session bus. */ | |
31d84da3 | 369 | sr_dbg("Sending SR_DF_HEADER."); |
d3f8f141 BV |
370 | packet.type = SR_DF_HEADER; |
371 | packet.payload = (uint8_t *)&header; | |
372 | header.feed_version = 1; | |
373 | gettimeofday(&header.starttime, NULL); | |
374 | sr_session_send(devc->cb_data, &packet); | |
883a2e9e | 375 | |
d3f8f141 | 376 | /* Poll every 100ms, or whenever some data comes in. */ |
d38d2ef0 BV |
377 | sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi); |
378 | ||
58d03f03 | 379 | if (serial_write(devc->serial, "QM\r", 3) == -1) { |
31d84da3 | 380 | sr_err("Unable to send QM: %s.", strerror(errno)); |
d38d2ef0 BV |
381 | return SR_ERR; |
382 | } | |
383 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; | |
384 | devc->expect_response = TRUE; | |
883a2e9e BV |
385 | |
386 | return SR_OK; | |
387 | } | |
388 | ||
69b07d14 | 389 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
883a2e9e | 390 | { |
d3f8f141 BV |
391 | struct sr_datafeed_packet packet; |
392 | struct dev_context *devc; | |
883a2e9e BV |
393 | |
394 | if (sdi->status != SR_ST_ACTIVE) | |
395 | return SR_ERR; | |
396 | ||
d3f8f141 | 397 | if (!(devc = sdi->priv)) { |
31d84da3 | 398 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
399 | return SR_ERR_BUG; |
400 | } | |
401 | ||
31d84da3 | 402 | sr_dbg("Stopping acquisition."); |
d3f8f141 BV |
403 | |
404 | sr_source_remove(devc->serial->fd); | |
405 | hw_dev_close((struct sr_dev_inst *)sdi); | |
406 | ||
407 | /* Send end packet to the session bus. */ | |
31d84da3 | 408 | sr_dbg("Sending SR_DF_END."); |
d3f8f141 BV |
409 | packet.type = SR_DF_END; |
410 | sr_session_send(cb_data, &packet); | |
883a2e9e BV |
411 | |
412 | return SR_OK; | |
413 | } | |
414 | ||
e7edd64f | 415 | SR_PRIV struct sr_dev_driver flukedmm_driver_info = { |
883a2e9e BV |
416 | .name = "fluke-dmm", |
417 | .longname = "Fluke 18x/28x series DMMs", | |
418 | .api_version = 1, | |
419 | .init = hw_init, | |
420 | .cleanup = hw_cleanup, | |
421 | .scan = hw_scan, | |
422 | .dev_list = hw_dev_list, | |
423 | .dev_clear = clear_instances, | |
035a1078 BV |
424 | .config_get = config_get, |
425 | .config_set = config_set, | |
a1c743fc | 426 | .config_list = config_list, |
883a2e9e BV |
427 | .dev_open = hw_dev_open, |
428 | .dev_close = hw_dev_close, | |
883a2e9e BV |
429 | .dev_acquisition_start = hw_dev_acquisition_start, |
430 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
431 | .priv = NULL, | |
432 | }; |