]>
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" | |
e93cdf42 BV |
23 | #include "agilent-dmm.h" |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #include <string.h> | |
28 | #include <errno.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 | ||
8c0152f2 BV |
49 | extern const struct agdmm_job agdmm_jobs_u123x[]; |
50 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
51 | extern const struct agdmm_job agdmm_jobs_u125x[]; | |
52 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
e93cdf42 BV |
53 | |
54 | static const struct agdmm_profile supported_agdmm[] = { | |
b186aa78 | 55 | { AGILENT_U1231A, "U1231A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x }, |
8c0152f2 | 56 | { AGILENT_U1232A, "U1232A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x }, |
b186aa78 BV |
57 | { AGILENT_U1233A, "U1233A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x }, |
58 | { AGILENT_U1251A, "U1251A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
59 | { AGILENT_U1252A, "U1252A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
60 | { AGILENT_U1253A, "U1253A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
e93cdf42 BV |
61 | { 0, NULL, 0, NULL, NULL } |
62 | }; | |
63 | ||
64 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
65 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
66 | ||
67 | /* Properly close and free all devices. */ | |
68 | static int clear_instances(void) | |
69 | { | |
70 | struct sr_dev_inst *sdi; | |
71 | struct drv_context *drvc; | |
72 | struct dev_context *devc; | |
73 | GSList *l; | |
74 | ||
75 | if (!(drvc = di->priv)) | |
76 | return SR_OK; | |
77 | ||
78 | drvc = di->priv; | |
79 | for (l = drvc->instances; l; l = l->next) { | |
80 | if (!(sdi = l->data)) | |
81 | continue; | |
82 | if (!(devc = sdi->priv)) | |
83 | continue; | |
84 | sr_serial_dev_inst_free(devc->serial); | |
85 | sr_dev_inst_free(sdi); | |
86 | } | |
87 | g_slist_free(drvc->instances); | |
88 | drvc->instances = NULL; | |
89 | ||
90 | return SR_OK; | |
91 | } | |
92 | ||
93 | static int hw_init(void) | |
94 | { | |
95 | struct drv_context *drvc; | |
96 | ||
97 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
38d326e8 | 98 | sr_err("Driver context malloc failed."); |
e93cdf42 BV |
99 | return SR_ERR; |
100 | } | |
101 | ||
102 | di->priv = drvc; | |
103 | ||
104 | return SR_OK; | |
105 | } | |
106 | ||
107 | static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms) | |
108 | { | |
109 | uint64_t start; | |
110 | int maxlen, len; | |
111 | ||
112 | timeout_ms *= 1000; | |
113 | start = g_get_monotonic_time(); | |
114 | ||
115 | maxlen = *buflen; | |
116 | *buflen = len = 0; | |
117 | while(1) { | |
118 | len = maxlen - *buflen - 1; | |
119 | if (len < 1) | |
120 | break; | |
121 | len = serial_read(fd, *buf + *buflen, len); | |
122 | if (len > 0) { | |
123 | *buflen += len; | |
124 | *(*buf + *buflen) = '\0'; | |
125 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\n') | |
126 | /* End of line */ | |
127 | break; | |
128 | } | |
129 | if (g_get_monotonic_time() - start > timeout_ms) | |
130 | /* Timeout */ | |
131 | break; | |
132 | g_usleep(2000); | |
133 | } | |
134 | ||
135 | /* Strip CRLF */ | |
136 | while (*buflen) { | |
137 | if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n') | |
138 | *(*buf + --*buflen) = '\0'; | |
139 | else | |
140 | break; | |
141 | } | |
142 | if (*buflen) | |
38d326e8 | 143 | sr_dbg("Received '%s'.", *buf); |
e93cdf42 BV |
144 | |
145 | return SR_OK; | |
146 | } | |
147 | ||
148 | static GSList *hw_scan(GSList *options) | |
149 | { | |
150 | struct sr_dev_inst *sdi; | |
151 | struct drv_context *drvc; | |
152 | struct dev_context *devc; | |
153 | struct sr_hwopt *opt; | |
154 | struct sr_probe *probe; | |
155 | GSList *l, *devices; | |
156 | int len, fd, i; | |
157 | const char *conn, *serialcomm; | |
158 | char *buf, **tokens; | |
159 | ||
160 | drvc = di->priv; | |
161 | drvc->instances = NULL; | |
162 | ||
163 | devices = NULL; | |
164 | conn = serialcomm = NULL; | |
165 | for (l = options; l; l = l->next) { | |
166 | opt = l->data; | |
167 | switch (opt->hwopt) { | |
168 | case SR_HWOPT_CONN: | |
169 | conn = opt->value; | |
170 | break; | |
171 | case SR_HWOPT_SERIALCOMM: | |
172 | serialcomm = opt->value; | |
173 | break; | |
174 | } | |
175 | } | |
c2016fa0 | 176 | if (!conn || !serialcomm) |
e93cdf42 | 177 | return NULL; |
e93cdf42 BV |
178 | |
179 | if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) { | |
38d326e8 | 180 | sr_err("Unable to open %s: %s.", conn, strerror(errno)); |
e93cdf42 BV |
181 | return NULL; |
182 | } | |
183 | ||
184 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { | |
38d326e8 UH |
185 | sr_err("Unable to set serial parameters: %s.", |
186 | strerror(errno)); | |
e93cdf42 BV |
187 | return NULL; |
188 | } | |
189 | ||
190 | serial_flush(fd); | |
191 | if (serial_write(fd, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
192 | sr_err("Unable to send identification string: %s.", |
193 | strerror(errno)); | |
e93cdf42 BV |
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)))) { | |
38d326e8 | 213 | sr_dbg("failed to malloc devc"); |
e93cdf42 BV |
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)) { | |
38d326e8 | 251 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
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) { | |
38d326e8 | 257 | sr_err("Couldn't open serial port '%s'.", |
e93cdf42 BV |
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)) { | |
38d326e8 | 273 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
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)) { | |
38d326e8 | 329 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
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) { | |
38d326e8 | 337 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
338 | return SR_ERR; |
339 | } | |
340 | devc->limit_msec = *(const uint64_t *)value; | |
38d326e8 | 341 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
342 | devc->limit_msec); |
343 | break; | |
344 | case SR_HWCAP_LIMIT_SAMPLES: | |
345 | devc->limit_samples = *(const uint64_t *)value; | |
38d326e8 | 346 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
347 | devc->limit_samples); |
348 | break; | |
349 | default: | |
38d326e8 | 350 | sr_err("Unknown capability: %d.", hwcap); |
e93cdf42 BV |
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)) { | |
38d326e8 | 367 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
368 | return SR_ERR_BUG; |
369 | } | |
370 | ||
38d326e8 | 371 | sr_dbg("Starting acquisition."); |
e93cdf42 BV |
372 | |
373 | devc->cb_data = cb_data; | |
374 | ||
375 | /* Send header packet to the session bus. */ | |
38d326e8 | 376 | sr_dbg("Sending SR_DF_HEADER."); |
e93cdf42 BV |
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. */ | |
38d326e8 | 384 | sr_dbg("Sending SR_DF_META_ANALOG."); |
e93cdf42 BV |
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)) { | |
38d326e8 | 406 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
407 | return SR_ERR_BUG; |
408 | } | |
409 | ||
38d326e8 | 410 | sr_dbg("Stopping acquisition."); |
e93cdf42 BV |
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. */ | |
38d326e8 | 416 | sr_dbg("Sending SR_DF_END."); |
e93cdf42 BV |
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 | }; |