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