]>
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[] = { |
d38d2ef0 BV |
53 | { FLUKE_187, "187", 100 }, |
54 | { FLUKE_287, "287", 100 }, | |
4f958423 BV |
55 | }; |
56 | ||
883a2e9e BV |
57 | |
58 | /* Properly close and free all devices. */ | |
59 | static int clear_instances(void) | |
60 | { | |
61 | struct sr_dev_inst *sdi; | |
62 | struct drv_context *drvc; | |
63 | struct dev_context *devc; | |
64 | GSList *l; | |
65 | ||
4f958423 BV |
66 | if (!(drvc = di->priv)) |
67 | return SR_OK; | |
68 | ||
883a2e9e BV |
69 | drvc = di->priv; |
70 | for (l = drvc->instances; l; l = l->next) { | |
4f958423 | 71 | if (!(sdi = l->data)) |
883a2e9e | 72 | continue; |
4f958423 | 73 | if (!(devc = sdi->priv)) |
883a2e9e | 74 | continue; |
4f958423 | 75 | sr_serial_dev_inst_free(devc->serial); |
883a2e9e BV |
76 | sr_dev_inst_free(sdi); |
77 | } | |
883a2e9e BV |
78 | g_slist_free(drvc->instances); |
79 | drvc->instances = NULL; | |
80 | ||
81 | return SR_OK; | |
82 | } | |
83 | ||
84 | static int hw_init(void) | |
85 | { | |
86 | struct drv_context *drvc; | |
87 | ||
88 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
31d84da3 | 89 | sr_err("Driver context malloc failed."); |
886a52b6 | 90 | return SR_ERR_MALLOC; |
883a2e9e BV |
91 | } |
92 | ||
883a2e9e BV |
93 | di->priv = drvc; |
94 | ||
95 | return SR_OK; | |
96 | } | |
97 | ||
41298320 | 98 | static GSList *fluke_scan(const char *conn, const char *serialcomm) |
883a2e9e | 99 | { |
4f958423 | 100 | struct sr_dev_inst *sdi; |
883a2e9e | 101 | struct drv_context *drvc; |
4f958423 | 102 | struct dev_context *devc; |
4f958423 | 103 | struct sr_probe *probe; |
41298320 BV |
104 | GSList *devices; |
105 | int fd, retry, len, i, s; | |
fb480d57 | 106 | char buf[128], *b, **tokens; |
883a2e9e | 107 | |
4f958423 | 108 | if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) { |
31d84da3 | 109 | sr_err("Unable to open %s: %s.", conn, strerror(errno)); |
4f958423 BV |
110 | return NULL; |
111 | } | |
4f958423 | 112 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { |
31d84da3 | 113 | sr_err("Unable to set serial parameters."); |
4f958423 BV |
114 | return NULL; |
115 | } | |
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++; | |
125 | serial_flush(fd); | |
126 | if (serial_write(fd, "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; | |
135 | serial_readline(fd, &b, &len, 150); | |
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; | |
143 | serial_readline(fd, &b, &len, 150); | |
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]; | |
162 | devc->serial = sr_serial_dev_inst_new(conn, -1); | |
41298320 | 163 | devc->serialcomm = g_strdup(serialcomm); |
fb480d57 BV |
164 | sdi->priv = devc; |
165 | sdi->driver = di; | |
166 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
167 | return NULL; | |
168 | sdi->probes = g_slist_append(sdi->probes, probe); | |
169 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
170 | devices = g_slist_append(devices, sdi); | |
171 | break; | |
4f958423 | 172 | } |
4f958423 | 173 | } |
fb480d57 | 174 | g_strfreev(tokens); |
4f958423 | 175 | } |
4f958423 | 176 | serial_close(fd); |
883a2e9e BV |
177 | |
178 | return devices; | |
179 | } | |
180 | ||
41298320 BV |
181 | static GSList *hw_scan(GSList *options) |
182 | { | |
41298320 BV |
183 | struct sr_hwopt *opt; |
184 | GSList *l, *devices; | |
185 | const char *conn, *serialcomm; | |
186 | ||
187 | conn = serialcomm = NULL; | |
188 | for (l = options; l; l = l->next) { | |
189 | opt = l->data; | |
190 | switch (opt->hwopt) { | |
191 | case SR_HWOPT_CONN: | |
192 | conn = opt->value; | |
193 | break; | |
194 | case SR_HWOPT_SERIALCOMM: | |
195 | serialcomm = opt->value; | |
196 | break; | |
197 | } | |
198 | } | |
199 | if (!conn) | |
200 | return NULL; | |
201 | ||
202 | if (serialcomm) { | |
203 | /* Use the provided comm specs. */ | |
204 | devices = fluke_scan(conn, serialcomm); | |
205 | } else { | |
206 | /* Try 115200, as used on 287/289. */ | |
207 | devices = fluke_scan(conn, "115200/8n1"); | |
208 | if (!devices) | |
209 | /* Fall back to 9600 for 187/189. */ | |
210 | devices = fluke_scan(conn, "9600/8n1"); | |
211 | } | |
212 | ||
213 | return devices; | |
214 | } | |
215 | ||
883a2e9e BV |
216 | static GSList *hw_dev_list(void) |
217 | { | |
218 | struct drv_context *drvc; | |
219 | ||
220 | drvc = di->priv; | |
221 | ||
222 | return drvc->instances; | |
223 | } | |
224 | ||
225 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
226 | { | |
41298320 | 227 | struct dev_context *devc; |
883a2e9e | 228 | |
41298320 | 229 | if (!(devc = sdi->priv)) { |
31d84da3 | 230 | sr_err("sdi->priv was NULL."); |
41298320 BV |
231 | return SR_ERR_BUG; |
232 | } | |
233 | ||
234 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); | |
235 | if (devc->serial->fd == -1) { | |
31d84da3 | 236 | sr_err("Couldn't open serial port '%s'.", devc->serial->port); |
41298320 BV |
237 | return SR_ERR; |
238 | } | |
239 | if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) { | |
31d84da3 | 240 | sr_err("Unable to set serial parameters."); |
41298320 BV |
241 | return SR_ERR; |
242 | } | |
243 | sdi->status = SR_ST_ACTIVE; | |
883a2e9e BV |
244 | |
245 | return SR_OK; | |
246 | } | |
247 | ||
248 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
249 | { | |
d3f8f141 | 250 | struct dev_context *devc; |
883a2e9e | 251 | |
d3f8f141 | 252 | if (!(devc = sdi->priv)) { |
31d84da3 | 253 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
254 | return SR_ERR_BUG; |
255 | } | |
256 | ||
257 | if (devc->serial && devc->serial->fd != -1) { | |
258 | serial_close(devc->serial->fd); | |
259 | devc->serial->fd = -1; | |
260 | sdi->status = SR_ST_INACTIVE; | |
261 | } | |
883a2e9e BV |
262 | |
263 | return SR_OK; | |
264 | } | |
265 | ||
266 | static int hw_cleanup(void) | |
267 | { | |
268 | ||
269 | clear_instances(); | |
270 | ||
883a2e9e BV |
271 | return SR_OK; |
272 | } | |
273 | ||
274 | static int hw_info_get(int info_id, const void **data, | |
275 | const struct sr_dev_inst *sdi) | |
276 | { | |
277 | ||
d3f8f141 | 278 | (void)sdi; |
883a2e9e BV |
279 | |
280 | switch (info_id) { | |
d3f8f141 BV |
281 | case SR_DI_HWOPTS: |
282 | *data = hwopts; | |
283 | break; | |
284 | case SR_DI_HWCAPS: | |
285 | *data = hwcaps; | |
286 | break; | |
287 | case SR_DI_NUM_PROBES: | |
288 | *data = GINT_TO_POINTER(1); | |
289 | break; | |
290 | case SR_DI_PROBE_NAMES: | |
291 | *data = probe_names; | |
292 | break; | |
883a2e9e BV |
293 | default: |
294 | return SR_ERR_ARG; | |
295 | } | |
296 | ||
297 | return SR_OK; | |
298 | } | |
299 | ||
300 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
301 | const void *value) | |
302 | { | |
d3f8f141 | 303 | struct dev_context *devc; |
883a2e9e BV |
304 | |
305 | if (sdi->status != SR_ST_ACTIVE) | |
306 | return SR_ERR; | |
307 | ||
d3f8f141 | 308 | if (!(devc = sdi->priv)) { |
31d84da3 | 309 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
310 | return SR_ERR_BUG; |
311 | } | |
312 | ||
883a2e9e | 313 | switch (hwcap) { |
d3f8f141 BV |
314 | case SR_HWCAP_LIMIT_MSEC: |
315 | /* TODO: not yet implemented */ | |
316 | if (*(const uint64_t *)value == 0) { | |
31d84da3 | 317 | sr_err("LIMIT_MSEC can't be 0."); |
d3f8f141 BV |
318 | return SR_ERR; |
319 | } | |
320 | devc->limit_msec = *(const uint64_t *)value; | |
31d84da3 | 321 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
d3f8f141 BV |
322 | devc->limit_msec); |
323 | break; | |
324 | case SR_HWCAP_LIMIT_SAMPLES: | |
325 | devc->limit_samples = *(const uint64_t *)value; | |
31d84da3 | 326 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
d3f8f141 BV |
327 | devc->limit_samples); |
328 | break; | |
883a2e9e | 329 | default: |
31d84da3 | 330 | sr_err("Unknown capability: %d.", hwcap); |
d3f8f141 BV |
331 | return SR_ERR; |
332 | break; | |
883a2e9e BV |
333 | } |
334 | ||
d3f8f141 | 335 | return SR_OK; |
883a2e9e BV |
336 | } |
337 | ||
338 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
339 | void *cb_data) | |
340 | { | |
d3f8f141 BV |
341 | struct sr_datafeed_packet packet; |
342 | struct sr_datafeed_header header; | |
343 | struct sr_datafeed_meta_analog meta; | |
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 | /* Send metadata about the SR_DF_ANALOG packets to come. */ |
31d84da3 | 364 | sr_dbg("Sending SR_DF_META_ANALOG."); |
d3f8f141 BV |
365 | packet.type = SR_DF_META_ANALOG; |
366 | packet.payload = &meta; | |
367 | meta.num_probes = 1; | |
368 | sr_session_send(devc->cb_data, &packet); | |
369 | ||
370 | /* Poll every 100ms, or whenever some data comes in. */ | |
d38d2ef0 BV |
371 | sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi); |
372 | ||
373 | if (serial_write(devc->serial->fd, "QM\r", 3) == -1) { | |
31d84da3 | 374 | sr_err("Unable to send QM: %s.", strerror(errno)); |
d38d2ef0 BV |
375 | return SR_ERR; |
376 | } | |
377 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; | |
378 | devc->expect_response = TRUE; | |
883a2e9e BV |
379 | |
380 | return SR_OK; | |
381 | } | |
382 | ||
69b07d14 | 383 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
883a2e9e | 384 | { |
d3f8f141 BV |
385 | struct sr_datafeed_packet packet; |
386 | struct dev_context *devc; | |
883a2e9e BV |
387 | |
388 | if (sdi->status != SR_ST_ACTIVE) | |
389 | return SR_ERR; | |
390 | ||
d3f8f141 | 391 | if (!(devc = sdi->priv)) { |
31d84da3 | 392 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
393 | return SR_ERR_BUG; |
394 | } | |
395 | ||
31d84da3 | 396 | sr_dbg("Stopping acquisition."); |
d3f8f141 BV |
397 | |
398 | sr_source_remove(devc->serial->fd); | |
399 | hw_dev_close((struct sr_dev_inst *)sdi); | |
400 | ||
401 | /* Send end packet to the session bus. */ | |
31d84da3 | 402 | sr_dbg("Sending SR_DF_END."); |
d3f8f141 BV |
403 | packet.type = SR_DF_END; |
404 | sr_session_send(cb_data, &packet); | |
883a2e9e BV |
405 | |
406 | return SR_OK; | |
407 | } | |
408 | ||
e7edd64f | 409 | SR_PRIV struct sr_dev_driver flukedmm_driver_info = { |
883a2e9e BV |
410 | .name = "fluke-dmm", |
411 | .longname = "Fluke 18x/28x series DMMs", | |
412 | .api_version = 1, | |
413 | .init = hw_init, | |
414 | .cleanup = hw_cleanup, | |
415 | .scan = hw_scan, | |
416 | .dev_list = hw_dev_list, | |
417 | .dev_clear = clear_instances, | |
418 | .dev_open = hw_dev_open, | |
419 | .dev_close = hw_dev_close, | |
420 | .info_get = hw_info_get, | |
421 | .dev_config_set = hw_dev_config_set, | |
422 | .dev_acquisition_start = hw_dev_acquisition_start, | |
423 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
424 | .priv = NULL, | |
425 | }; |