2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #include "agilent-dmm.h"
25 #include <sys/types.h>
31 static const int hwopts[] = {
37 static const int hwcaps[] = {
39 SR_HWCAP_LIMIT_SAMPLES,
45 static const char *probe_names[] = {
50 extern const struct agdmm_job u123x_jobs[];
51 extern const struct agdmm_recv u123x_recvs[];
53 static const struct agdmm_profile supported_agdmm[] = {
54 { AGILENT_U1232A, "U1232A", 9600, u123x_jobs, u123x_recvs },
55 { 0, NULL, 0, NULL, NULL }
58 SR_PRIV struct sr_dev_driver agdmm_driver_info;
59 static struct sr_dev_driver *di = &agdmm_driver_info;
61 /* Properly close and free all devices. */
62 static int clear_instances(void)
64 struct sr_dev_inst *sdi;
65 struct drv_context *drvc;
66 struct dev_context *devc;
69 if (!(drvc = di->priv))
73 for (l = drvc->instances; l; l = l->next) {
76 if (!(devc = sdi->priv))
78 sr_serial_dev_inst_free(devc->serial);
79 sr_dev_inst_free(sdi);
81 g_slist_free(drvc->instances);
82 drvc->instances = NULL;
87 static int hw_init(void)
89 struct drv_context *drvc;
91 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
92 sr_err("agilent-dmm: driver context malloc failed.");
101 static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
107 start = g_get_monotonic_time();
112 len = maxlen - *buflen - 1;
115 len = serial_read(fd, *buf + *buflen, len);
118 *(*buf + *buflen) = '\0';
119 if (*buflen > 0 && *(*buf + *buflen - 1) == '\n')
123 if (g_get_monotonic_time() - start > timeout_ms)
131 if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n')
132 *(*buf + --*buflen) = '\0';
137 sr_dbg("agilent-dmm: received '%s'", *buf);
142 static GSList *hw_scan(GSList *options)
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;
151 const char *conn, *serialcomm;
155 drvc->instances = NULL;
158 conn = serialcomm = NULL;
159 for (l = options; l; l = l->next) {
161 switch (opt->hwopt) {
165 case SR_HWOPT_SERIALCOMM:
166 serialcomm = opt->value;
171 sr_dbg("no serial port provided");
175 sr_dbg("no serial communication parameters provided");
179 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
180 sr_err("agilent-dmm: unable to open %s: %s", conn, strerror(errno));
184 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
185 sr_err("agilent-dmm: unable to set serial parameters: %s",
191 if (serial_write(fd, "*IDN?\r\n", 7) == -1) {
192 sr_err("agilent-dmm: unable to send identification string: %s",
198 buf = g_try_malloc(len);
199 serial_readline(fd, &buf, &len, 150);
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]))
209 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0],
210 tokens[1], tokens[3])))
212 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
213 sr_dbg("agilent-dmm: failed to malloc devc");
216 devc->profile = &supported_agdmm[i];
217 devc->serial = sr_serial_dev_inst_new(conn, -1);
221 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
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);
237 static GSList *hw_dev_list(void)
239 struct drv_context *drvc;
243 return drvc->instances;
246 static int hw_dev_open(struct sr_dev_inst *sdi)
248 struct dev_context *devc;
250 if (!(devc = sdi->priv)) {
251 sr_err("agilent-dmm: sdi->priv was NULL.");
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'.",
261 serial_set_params(devc->serial->fd, devc->profile->serial_speed, 8,
262 SERIAL_PARITY_NONE, 1, 0);
263 sdi->status = SR_ST_ACTIVE;
268 static int hw_dev_close(struct sr_dev_inst *sdi)
270 struct dev_context *devc;
272 if (!(devc = sdi->priv)) {
273 sr_err("agilent-dmm: sdi->priv was NULL.");
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;
286 static int hw_cleanup(void)
294 static int hw_info_get(int info_id, const void **data,
295 const struct sr_dev_inst *sdi)
307 case SR_DI_NUM_PROBES:
308 *data = GINT_TO_POINTER(1);
310 case SR_DI_PROBE_NAMES:
320 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
323 struct dev_context *devc;
325 if (sdi->status != SR_ST_ACTIVE)
328 if (!(devc = sdi->priv)) {
329 sr_err("agilent-dmm: sdi->priv was NULL.");
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.");
340 devc->limit_msec = *(const uint64_t *)value;
341 sr_dbg("agilent-dmm: Setting time limit to %" PRIu64 "ms.",
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);
350 sr_err("agilent-dmm: Unknown capability: %d.", hwcap);
358 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
361 struct sr_datafeed_packet packet;
362 struct sr_datafeed_header header;
363 struct sr_datafeed_meta_analog meta;
364 struct dev_context *devc;
366 if (!(devc = sdi->priv)) {
367 sr_err("agilent-dmm: sdi->priv was NULL.");
371 sr_dbg("agilent-dmm: Starting acquisition.");
373 devc->cb_data = cb_data;
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);
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;
388 sr_session_send(devc->cb_data, &packet);
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);
396 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
399 struct sr_datafeed_packet packet;
400 struct dev_context *devc;
402 if (sdi->status != SR_ST_ACTIVE)
405 if (!(devc = sdi->priv)) {
406 sr_err("agilent-dmm: sdi->priv was NULL.");
410 sr_dbg("agilent-dmm: Stopping acquisition.");
412 sr_source_remove(devc->serial->fd);
413 hw_dev_close((struct sr_dev_inst *)sdi);
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);
424 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
425 .name = "agilent-dmm",
426 .longname = "Agilent U12xx series DMMs",
429 .cleanup = hw_cleanup,
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,