]>
Commit | Line | Data |
---|---|---|
e93cdf42 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> | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | #include "config.h" | |
24 | #include "agilent-dmm.h" | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <fcntl.h> | |
28 | #include <string.h> | |
29 | #include <errno.h> | |
30 | ||
31 | static const int hwopts[] = { | |
32 | SR_HWOPT_CONN, | |
33 | SR_HWOPT_SERIALCOMM, | |
34 | 0, | |
35 | }; | |
36 | ||
37 | static const int hwcaps[] = { | |
38 | SR_HWCAP_MULTIMETER, | |
39 | SR_HWCAP_LIMIT_SAMPLES, | |
40 | SR_HWCAP_LIMIT_MSEC, | |
41 | SR_HWCAP_CONTINUOUS, | |
42 | 0, | |
43 | }; | |
44 | ||
45 | static const char *probe_names[] = { | |
46 | "Probe", | |
47 | NULL, | |
48 | }; | |
49 | ||
a4394fb3 BV |
50 | extern const struct agdmm_job agdmm_u123x_jobs[]; |
51 | extern const struct agdmm_recv agdmm_recvs[]; | |
e93cdf42 BV |
52 | |
53 | static const struct agdmm_profile supported_agdmm[] = { | |
a4394fb3 | 54 | { AGILENT_U1232A, "U1232A", 9600, agdmm_u123x_jobs, agdmm_recvs }, |
e93cdf42 BV |
55 | { 0, NULL, 0, NULL, NULL } |
56 | }; | |
57 | ||
58 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
59 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
60 | ||
61 | /* Properly close and free all devices. */ | |
62 | static int clear_instances(void) | |
63 | { | |
64 | struct sr_dev_inst *sdi; | |
65 | struct drv_context *drvc; | |
66 | struct dev_context *devc; | |
67 | GSList *l; | |
68 | ||
69 | if (!(drvc = di->priv)) | |
70 | return SR_OK; | |
71 | ||
72 | drvc = di->priv; | |
73 | for (l = drvc->instances; l; l = l->next) { | |
74 | if (!(sdi = l->data)) | |
75 | continue; | |
76 | if (!(devc = sdi->priv)) | |
77 | continue; | |
78 | sr_serial_dev_inst_free(devc->serial); | |
79 | sr_dev_inst_free(sdi); | |
80 | } | |
81 | g_slist_free(drvc->instances); | |
82 | drvc->instances = NULL; | |
83 | ||
84 | return SR_OK; | |
85 | } | |
86 | ||
87 | static int hw_init(void) | |
88 | { | |
89 | struct drv_context *drvc; | |
90 | ||
91 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
92 | sr_err("agilent-dmm: driver context malloc failed."); | |
93 | return SR_ERR; | |
94 | } | |
95 | ||
96 | di->priv = drvc; | |
97 | ||
98 | return SR_OK; | |
99 | } | |
100 | ||
101 | static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms) | |
102 | { | |
103 | uint64_t start; | |
104 | int maxlen, len; | |
105 | ||
106 | timeout_ms *= 1000; | |
107 | start = g_get_monotonic_time(); | |
108 | ||
109 | maxlen = *buflen; | |
110 | *buflen = len = 0; | |
111 | while(1) { | |
112 | len = maxlen - *buflen - 1; | |
113 | if (len < 1) | |
114 | break; | |
115 | len = serial_read(fd, *buf + *buflen, len); | |
116 | if (len > 0) { | |
117 | *buflen += len; | |
118 | *(*buf + *buflen) = '\0'; | |
119 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\n') | |
120 | /* End of line */ | |
121 | break; | |
122 | } | |
123 | if (g_get_monotonic_time() - start > timeout_ms) | |
124 | /* Timeout */ | |
125 | break; | |
126 | g_usleep(2000); | |
127 | } | |
128 | ||
129 | /* Strip CRLF */ | |
130 | while (*buflen) { | |
131 | if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n') | |
132 | *(*buf + --*buflen) = '\0'; | |
133 | else | |
134 | break; | |
135 | } | |
136 | if (*buflen) | |
137 | sr_dbg("agilent-dmm: received '%s'", *buf); | |
138 | ||
139 | return SR_OK; | |
140 | } | |
141 | ||
142 | static GSList *hw_scan(GSList *options) | |
143 | { | |
144 | struct sr_dev_inst *sdi; | |
145 | struct drv_context *drvc; | |
146 | struct dev_context *devc; | |
147 | struct sr_hwopt *opt; | |
148 | struct sr_probe *probe; | |
149 | GSList *l, *devices; | |
150 | int len, fd, i; | |
151 | const char *conn, *serialcomm; | |
152 | char *buf, **tokens; | |
153 | ||
154 | drvc = di->priv; | |
155 | drvc->instances = NULL; | |
156 | ||
157 | devices = NULL; | |
158 | conn = serialcomm = NULL; | |
159 | for (l = options; l; l = l->next) { | |
160 | opt = l->data; | |
161 | switch (opt->hwopt) { | |
162 | case SR_HWOPT_CONN: | |
163 | conn = opt->value; | |
164 | break; | |
165 | case SR_HWOPT_SERIALCOMM: | |
166 | serialcomm = opt->value; | |
167 | break; | |
168 | } | |
169 | } | |
170 | if (!conn) { | |
171 | sr_dbg("no serial port provided"); | |
172 | return NULL; | |
173 | } | |
174 | if (!serialcomm) { | |
175 | sr_dbg("no serial communication parameters provided"); | |
176 | return NULL; | |
177 | } | |
178 | ||
179 | if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) { | |
180 | sr_err("agilent-dmm: unable to open %s: %s", conn, strerror(errno)); | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { | |
185 | sr_err("agilent-dmm: unable to set serial parameters: %s", | |
186 | strerror(errno)); | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | serial_flush(fd); | |
191 | if (serial_write(fd, "*IDN?\r\n", 7) == -1) { | |
192 | sr_err("agilent-dmm: unable to send identification string: %s", | |
193 | strerror(errno)); | |
194 | return NULL; | |
195 | } | |
196 | ||
197 | len = 128; | |
198 | buf = g_try_malloc(len); | |
199 | serial_readline(fd, &buf, &len, 150); | |
200 | if (!len) | |
201 | return NULL; | |
202 | ||
203 | tokens = g_strsplit(buf, ",", 4); | |
204 | if (!strcmp("Agilent Technologies", tokens[0]) | |
205 | && tokens[2] && tokens[3]) { | |
206 | for (i = 0; supported_agdmm[i].model; i++) { | |
207 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
208 | continue; | |
209 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
210 | tokens[1], tokens[3]))) | |
211 | return NULL; | |
212 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
213 | sr_dbg("agilent-dmm: failed to malloc devc"); | |
214 | return NULL; | |
215 | } | |
216 | devc->profile = &supported_agdmm[i]; | |
217 | devc->serial = sr_serial_dev_inst_new(conn, -1); | |
218 | devc->cur_mq = -1; | |
219 | sdi->priv = devc; | |
220 | sdi->driver = di; | |
221 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
222 | return NULL; | |
223 | sdi->probes = g_slist_append(sdi->probes, probe); | |
224 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
225 | devices = g_slist_append(devices, sdi); | |
226 | break; | |
227 | } | |
228 | } | |
229 | g_strfreev(tokens); | |
230 | g_free(buf); | |
231 | ||
232 | serial_close(fd); | |
233 | ||
234 | return devices; | |
235 | } | |
236 | ||
237 | static GSList *hw_dev_list(void) | |
238 | { | |
239 | struct drv_context *drvc; | |
240 | ||
241 | drvc = di->priv; | |
242 | ||
243 | return drvc->instances; | |
244 | } | |
245 | ||
246 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
247 | { | |
248 | struct dev_context *devc; | |
249 | ||
250 | if (!(devc = sdi->priv)) { | |
251 | sr_err("agilent-dmm: sdi->priv was NULL."); | |
252 | return SR_ERR_BUG; | |
253 | } | |
254 | ||
255 | devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); | |
256 | if (devc->serial->fd == -1) { | |
257 | sr_err("agilent-dmm: Couldn't open serial port '%s'.", | |
258 | devc->serial->port); | |
259 | return SR_ERR; | |
260 | } | |
261 | serial_set_params(devc->serial->fd, devc->profile->serial_speed, 8, | |
262 | SERIAL_PARITY_NONE, 1, 0); | |
263 | sdi->status = SR_ST_ACTIVE; | |
264 | ||
265 | return SR_OK; | |
266 | } | |
267 | ||
268 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
269 | { | |
270 | struct dev_context *devc; | |
271 | ||
272 | if (!(devc = sdi->priv)) { | |
273 | sr_err("agilent-dmm: sdi->priv was NULL."); | |
274 | return SR_ERR_BUG; | |
275 | } | |
276 | ||
277 | if (devc->serial && devc->serial->fd != -1) { | |
278 | serial_close(devc->serial->fd); | |
279 | devc->serial->fd = -1; | |
280 | sdi->status = SR_ST_INACTIVE; | |
281 | } | |
282 | ||
283 | return SR_OK; | |
284 | } | |
285 | ||
286 | static int hw_cleanup(void) | |
287 | { | |
288 | ||
289 | clear_instances(); | |
290 | ||
291 | return SR_OK; | |
292 | } | |
293 | ||
294 | static int hw_info_get(int info_id, const void **data, | |
295 | const struct sr_dev_inst *sdi) | |
296 | { | |
297 | ||
298 | (void)sdi; | |
299 | ||
300 | switch (info_id) { | |
301 | case SR_DI_HWOPTS: | |
302 | *data = hwopts; | |
303 | break; | |
304 | case SR_DI_HWCAPS: | |
305 | *data = hwcaps; | |
306 | break; | |
307 | case SR_DI_NUM_PROBES: | |
308 | *data = GINT_TO_POINTER(1); | |
309 | break; | |
310 | case SR_DI_PROBE_NAMES: | |
311 | *data = probe_names; | |
312 | break; | |
313 | default: | |
314 | return SR_ERR_ARG; | |
315 | } | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
321 | const void *value) | |
322 | { | |
323 | struct dev_context *devc; | |
324 | ||
325 | if (sdi->status != SR_ST_ACTIVE) | |
326 | return SR_ERR; | |
327 | ||
328 | if (!(devc = sdi->priv)) { | |
329 | sr_err("agilent-dmm: sdi->priv was NULL."); | |
330 | return SR_ERR_BUG; | |
331 | } | |
332 | ||
333 | switch (hwcap) { | |
334 | case SR_HWCAP_LIMIT_MSEC: | |
335 | /* TODO: not yet implemented */ | |
336 | if (*(const uint64_t *)value == 0) { | |
337 | sr_err("agilent-dmm: LIMIT_MSEC can't be 0."); | |
338 | return SR_ERR; | |
339 | } | |
340 | devc->limit_msec = *(const uint64_t *)value; | |
341 | sr_dbg("agilent-dmm: Setting time limit to %" PRIu64 "ms.", | |
342 | devc->limit_msec); | |
343 | break; | |
344 | case SR_HWCAP_LIMIT_SAMPLES: | |
345 | devc->limit_samples = *(const uint64_t *)value; | |
346 | sr_dbg("agilent-dmm: Setting sample limit to %" PRIu64 ".", | |
347 | devc->limit_samples); | |
348 | break; | |
349 | default: | |
350 | sr_err("agilent-dmm: Unknown capability: %d.", hwcap); | |
351 | return SR_ERR; | |
352 | break; | |
353 | } | |
354 | ||
355 | return SR_OK; | |
356 | } | |
357 | ||
358 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
359 | void *cb_data) | |
360 | { | |
361 | struct sr_datafeed_packet packet; | |
362 | struct sr_datafeed_header header; | |
363 | struct sr_datafeed_meta_analog meta; | |
364 | struct dev_context *devc; | |
365 | ||
366 | if (!(devc = sdi->priv)) { | |
367 | sr_err("agilent-dmm: sdi->priv was NULL."); | |
368 | return SR_ERR_BUG; | |
369 | } | |
370 | ||
371 | sr_dbg("agilent-dmm: Starting acquisition."); | |
372 | ||
373 | devc->cb_data = cb_data; | |
374 | ||
375 | /* Send header packet to the session bus. */ | |
376 | sr_dbg("agilent-dmm: Sending SR_DF_HEADER."); | |
377 | packet.type = SR_DF_HEADER; | |
378 | packet.payload = (uint8_t *)&header; | |
379 | header.feed_version = 1; | |
380 | gettimeofday(&header.starttime, NULL); | |
381 | sr_session_send(devc->cb_data, &packet); | |
382 | ||
383 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
384 | sr_dbg("agilent-dmm: Sending SR_DF_META_ANALOG."); | |
385 | packet.type = SR_DF_META_ANALOG; | |
386 | packet.payload = &meta; | |
387 | meta.num_probes = 1; | |
388 | sr_session_send(devc->cb_data, &packet); | |
389 | ||
390 | /* Poll every 100ms, or whenever some data comes in. */ | |
391 | sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
392 | ||
393 | return SR_OK; | |
394 | } | |
395 | ||
396 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, | |
397 | void *cb_data) | |
398 | { | |
399 | struct sr_datafeed_packet packet; | |
400 | struct dev_context *devc; | |
401 | ||
402 | if (sdi->status != SR_ST_ACTIVE) | |
403 | return SR_ERR; | |
404 | ||
405 | if (!(devc = sdi->priv)) { | |
406 | sr_err("agilent-dmm: sdi->priv was NULL."); | |
407 | return SR_ERR_BUG; | |
408 | } | |
409 | ||
410 | sr_dbg("agilent-dmm: Stopping acquisition."); | |
411 | ||
412 | sr_source_remove(devc->serial->fd); | |
413 | hw_dev_close((struct sr_dev_inst *)sdi); | |
414 | ||
415 | /* Send end packet to the session bus. */ | |
416 | sr_dbg("agilent-dmm: Sending SR_DF_END."); | |
417 | packet.type = SR_DF_END; | |
418 | sr_session_send(cb_data, &packet); | |
419 | ||
420 | ||
421 | return SR_OK; | |
422 | } | |
423 | ||
424 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
425 | .name = "agilent-dmm", | |
426 | .longname = "Agilent U12xx series DMMs", | |
427 | .api_version = 1, | |
428 | .init = hw_init, | |
429 | .cleanup = hw_cleanup, | |
430 | .scan = hw_scan, | |
431 | .dev_list = hw_dev_list, | |
432 | .dev_clear = clear_instances, | |
433 | .dev_open = hw_dev_open, | |
434 | .dev_close = hw_dev_close, | |
435 | .info_get = hw_info_get, | |
436 | .dev_config_set = hw_dev_config_set, | |
437 | .dev_acquisition_start = hw_dev_acquisition_start, | |
438 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
439 | .priv = NULL, | |
440 | }; |