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