]>
Commit | Line | Data |
---|---|---|
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 <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "fluke-dmm.h" | |
29 | ||
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 | }; | |
48 | ||
49 | SR_PRIV struct sr_dev_driver flukedmm_driver_info; | |
50 | static struct sr_dev_driver *di = &flukedmm_driver_info; | |
51 | ||
52 | static const struct flukedmm_profile supported_flukedmm[] = { | |
53 | { FLUKE_187, "187", 100 }, | |
54 | { FLUKE_287, "287", 100 }, | |
55 | }; | |
56 | ||
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 | ||
66 | if (!(drvc = di->priv)) | |
67 | return SR_OK; | |
68 | ||
69 | drvc = di->priv; | |
70 | for (l = drvc->instances; l; l = l->next) { | |
71 | if (!(sdi = l->data)) | |
72 | continue; | |
73 | if (!(devc = sdi->priv)) | |
74 | continue; | |
75 | sr_serial_dev_inst_free(devc->serial); | |
76 | sr_dev_inst_free(sdi); | |
77 | } | |
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)))) { | |
89 | sr_err("Driver context malloc failed."); | |
90 | return SR_ERR_MALLOC; | |
91 | } | |
92 | ||
93 | di->priv = drvc; | |
94 | ||
95 | return SR_OK; | |
96 | } | |
97 | ||
98 | static GSList *fluke_scan(const char *conn, const char *serialcomm) | |
99 | { | |
100 | struct sr_dev_inst *sdi; | |
101 | struct drv_context *drvc; | |
102 | struct dev_context *devc; | |
103 | struct sr_probe *probe; | |
104 | GSList *devices; | |
105 | int fd, retry, len, i, s; | |
106 | char buf[128], *b, **tokens; | |
107 | ||
108 | if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) { | |
109 | sr_err("Unable to open %s: %s.", conn, strerror(errno)); | |
110 | return NULL; | |
111 | } | |
112 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { | |
113 | sr_err("Unable to set serial parameters."); | |
114 | return NULL; | |
115 | } | |
116 | ||
117 | drvc = di->priv; | |
118 | b = buf; | |
119 | retry = 0; | |
120 | devices = NULL; | |
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) { | |
127 | sr_err("Unable to send ID string: %s.", | |
128 | strerror(errno)); | |
129 | continue; | |
130 | } | |
131 | ||
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; | |
138 | if (buf[0] != '0') | |
139 | continue; | |
140 | ||
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)))) { | |
158 | sr_err("Device context malloc failed."); | |
159 | return NULL; | |
160 | } | |
161 | devc->profile = &supported_flukedmm[i]; | |
162 | devc->serial = sr_serial_dev_inst_new(conn, -1); | |
163 | devc->serialcomm = g_strdup(serialcomm); | |
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; | |
172 | } | |
173 | } | |
174 | g_strfreev(tokens); | |
175 | } | |
176 | serial_close(fd); | |
177 | ||
178 | return devices; | |
179 | } | |
180 | ||
181 | static GSList *hw_scan(GSList *options) | |
182 | { | |
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 | ||
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 | { | |
227 | struct dev_context *devc; | |
228 | ||
229 | if (!(devc = sdi->priv)) { | |
230 | sr_err("sdi->priv was NULL."); | |
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) { | |
236 | sr_err("Couldn't open serial port '%s'.", devc->serial->port); | |
237 | return SR_ERR; | |
238 | } | |
239 | if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) { | |
240 | sr_err("Unable to set serial parameters."); | |
241 | return SR_ERR; | |
242 | } | |
243 | sdi->status = SR_ST_ACTIVE; | |
244 | ||
245 | return SR_OK; | |
246 | } | |
247 | ||
248 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
249 | { | |
250 | struct dev_context *devc; | |
251 | ||
252 | if (!(devc = sdi->priv)) { | |
253 | sr_err("sdi->priv was NULL."); | |
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 | } | |
262 | ||
263 | return SR_OK; | |
264 | } | |
265 | ||
266 | static int hw_cleanup(void) | |
267 | { | |
268 | ||
269 | clear_instances(); | |
270 | ||
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 | ||
278 | (void)sdi; | |
279 | ||
280 | switch (info_id) { | |
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; | |
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 | { | |
303 | struct dev_context *devc; | |
304 | ||
305 | if (sdi->status != SR_ST_ACTIVE) | |
306 | return SR_ERR; | |
307 | ||
308 | if (!(devc = sdi->priv)) { | |
309 | sr_err("sdi->priv was NULL."); | |
310 | return SR_ERR_BUG; | |
311 | } | |
312 | ||
313 | switch (hwcap) { | |
314 | case SR_HWCAP_LIMIT_MSEC: | |
315 | /* TODO: not yet implemented */ | |
316 | if (*(const uint64_t *)value == 0) { | |
317 | sr_err("LIMIT_MSEC can't be 0."); | |
318 | return SR_ERR; | |
319 | } | |
320 | devc->limit_msec = *(const uint64_t *)value; | |
321 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
322 | devc->limit_msec); | |
323 | break; | |
324 | case SR_HWCAP_LIMIT_SAMPLES: | |
325 | devc->limit_samples = *(const uint64_t *)value; | |
326 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
327 | devc->limit_samples); | |
328 | break; | |
329 | default: | |
330 | sr_err("Unknown capability: %d.", hwcap); | |
331 | return SR_ERR; | |
332 | break; | |
333 | } | |
334 | ||
335 | return SR_OK; | |
336 | } | |
337 | ||
338 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
339 | void *cb_data) | |
340 | { | |
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)) { | |
347 | sr_err("sdi->priv was NULL."); | |
348 | return SR_ERR_BUG; | |
349 | } | |
350 | ||
351 | sr_dbg("Starting acquisition."); | |
352 | ||
353 | devc->cb_data = cb_data; | |
354 | ||
355 | /* Send header packet to the session bus. */ | |
356 | sr_dbg("Sending SR_DF_HEADER."); | |
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); | |
362 | ||
363 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
364 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
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. */ | |
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) { | |
374 | sr_err("Unable to send QM: %s.", strerror(errno)); | |
375 | return SR_ERR; | |
376 | } | |
377 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; | |
378 | devc->expect_response = TRUE; | |
379 | ||
380 | return SR_OK; | |
381 | } | |
382 | ||
383 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, | |
384 | void *cb_data) | |
385 | { | |
386 | struct sr_datafeed_packet packet; | |
387 | struct dev_context *devc; | |
388 | ||
389 | if (sdi->status != SR_ST_ACTIVE) | |
390 | return SR_ERR; | |
391 | ||
392 | if (!(devc = sdi->priv)) { | |
393 | sr_err("sdi->priv was NULL."); | |
394 | return SR_ERR_BUG; | |
395 | } | |
396 | ||
397 | sr_dbg("Stopping acquisition."); | |
398 | ||
399 | sr_source_remove(devc->serial->fd); | |
400 | hw_dev_close((struct sr_dev_inst *)sdi); | |
401 | ||
402 | /* Send end packet to the session bus. */ | |
403 | sr_dbg("Sending SR_DF_END."); | |
404 | packet.type = SR_DF_END; | |
405 | sr_session_send(cb_data, &packet); | |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | SR_PRIV struct sr_dev_driver flukedmm_driver_info = { | |
411 | .name = "fluke-dmm", | |
412 | .longname = "Fluke 18x/28x series DMMs", | |
413 | .api_version = 1, | |
414 | .init = hw_init, | |
415 | .cleanup = hw_cleanup, | |
416 | .scan = hw_scan, | |
417 | .dev_list = hw_dev_list, | |
418 | .dev_clear = clear_instances, | |
419 | .dev_open = hw_dev_open, | |
420 | .dev_close = hw_dev_close, | |
421 | .info_get = hw_info_get, | |
422 | .dev_config_set = hw_dev_config_set, | |
423 | .dev_acquisition_start = hw_dev_acquisition_start, | |
424 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
425 | .priv = NULL, | |
426 | }; |