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