2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "scpi_gpib"
33 static int scpi_gpib_dev_inst_new(void *priv, struct drv_context *drvc,
34 const char *resource, char **params, const char *serialcomm)
36 struct scpi_gpib *gscpi = priv;
42 if (!params || !params[1])
45 gscpi->name = g_strdup(params[1]);
50 static int scpi_gpib_open(void *priv)
52 struct scpi_gpib *gscpi = priv;
54 if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
60 static int scpi_gpib_source_add(struct sr_session *session, void *priv,
61 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
65 /* Hook up a dummy handler to receive data from the device. */
66 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
69 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
73 return sr_session_source_remove(session, -1);
76 static int scpi_gpib_send(void *priv, const char *command)
78 struct scpi_gpib *gscpi = priv;
79 int len = strlen(command);
81 ibwrt(gscpi->descriptor, command, len);
85 sr_err("Error while sending SCPI command: '%s': iberr = %d.",
92 sr_err("Failed to send all of SCPI command: '%s': "
93 "len = %d, ibcnt = .", command, len, ibcnt);
97 sr_spew("Successfully sent SCPI command: '%s'.", command);
102 static int scpi_gpib_read_begin(void *priv)
104 struct scpi_gpib *gscpi = priv;
106 gscpi->read_started = 0;
111 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
113 struct scpi_gpib *gscpi = priv;
115 ibrd(gscpi->descriptor, buf, maxlen);
119 sr_err("Error while reading SCPI response: iberr = %d.", iberr);
123 gscpi->read_started = 1;
128 static int scpi_gpib_read_complete(void *priv)
130 struct scpi_gpib *gscpi = priv;
132 return gscpi->read_started && (ibsta & END);
135 static int scpi_gpib_close(void *priv)
137 struct scpi_gpib *gscpi = priv;
139 ibonl(gscpi->descriptor, 0);
144 static void scpi_gpib_free(void *priv)
146 struct scpi_gpib *gscpi = priv;
151 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
154 .priv_size = sizeof(struct scpi_gpib),
155 .dev_inst_new = scpi_gpib_dev_inst_new,
156 .open = scpi_gpib_open,
157 .source_add = scpi_gpib_source_add,
158 .source_remove = scpi_gpib_source_remove,
159 .send = scpi_gpib_send,
160 .read_begin = scpi_gpib_read_begin,
161 .read_data = scpi_gpib_read_data,
162 .read_complete = scpi_gpib_read_complete,
163 .close = scpi_gpib_close,
164 .free = scpi_gpib_free,