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