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