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