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