2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 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/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "scpi_usbtmc"
31 #define MAX_READ_LENGTH 2048
34 struct sr_usbtmc_dev_inst *usbtmc;
35 char response_buffer[MAX_READ_LENGTH];
37 int response_bytes_read;
40 static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource,
41 char **params, const char *serialcomm)
43 struct usbtmc_scpi *uscpi = priv;
48 if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource)))
54 static int scpi_usbtmc_open(void *priv)
56 struct usbtmc_scpi *uscpi = priv;
57 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
59 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
65 static int scpi_usbtmc_source_add(void *priv, int events, int timeout,
66 sr_receive_data_callback_t cb, void *cb_data)
68 struct usbtmc_scpi *uscpi = priv;
69 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
71 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
74 static int scpi_usbtmc_source_remove(void *priv)
76 struct usbtmc_scpi *uscpi = priv;
77 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
79 return sr_source_remove(usbtmc->fd);
82 static int scpi_usbtmc_send(void *priv, const char *command)
84 struct usbtmc_scpi *uscpi = priv;
85 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
88 len = strlen(command);
89 out = write(usbtmc->fd, command, len);
92 sr_err("Write error: %s", strerror(errno));
97 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
101 sr_spew("Successfully sent SCPI command: '%s'.", command);
106 static int scpi_usbtmc_read_begin(void *priv)
108 struct usbtmc_scpi *uscpi = priv;
109 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
112 len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
115 sr_err("Read error: %s", strerror(errno));
119 uscpi->response_length = len;
120 uscpi->response_bytes_read = 0;
122 sr_spew("Read %d bytes from device into buffer", len);
127 static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
129 struct usbtmc_scpi *uscpi = priv;
132 sr_spew("%d bytes requested", maxlen);
134 if (uscpi->response_bytes_read == uscpi->response_length) {
135 sr_spew("Buffer is empty.");
136 if (uscpi->response_length == MAX_READ_LENGTH) {
137 sr_spew("Previous read was of maximum length, reading again.");
138 if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
145 read_length = uscpi->response_length - uscpi->response_bytes_read;
147 if (read_length > maxlen)
148 read_length = maxlen;
150 memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
152 uscpi->response_bytes_read += read_length;
154 sr_spew("Returned %d bytes from buffer, %d/%d bytes of buffer now read",
155 read_length, uscpi->response_bytes_read, uscpi->response_length);
160 static int scpi_usbtmc_read_complete(void *priv)
162 struct usbtmc_scpi *uscpi = priv;
164 if (uscpi->response_length == MAX_READ_LENGTH
165 && uscpi->response_bytes_read == uscpi->response_length)
166 scpi_usbtmc_read_begin(uscpi);
168 return (uscpi->response_bytes_read >= uscpi->response_length);
171 static int scpi_usbtmc_close(void *priv)
173 struct usbtmc_scpi *uscpi = priv;
175 if (close(uscpi->usbtmc->fd) < 0)
181 static void scpi_usbtmc_free(void *priv)
183 struct usbtmc_scpi *uscpi = priv;
185 sr_usbtmc_dev_inst_free(uscpi->usbtmc);
188 SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
190 .prefix = "/dev/usbtmc",
191 .priv_size = sizeof(struct usbtmc_scpi),
192 .dev_inst_new = scpi_usbtmc_dev_inst_new,
193 .open = scpi_usbtmc_open,
194 .source_add = scpi_usbtmc_source_add,
195 .source_remove = scpi_usbtmc_source_remove,
196 .send = scpi_usbtmc_send,
197 .read_begin = scpi_usbtmc_read_begin,
198 .read_data = scpi_usbtmc_read_data,
199 .read_complete = scpi_usbtmc_read_complete,
200 .close = scpi_usbtmc_close,
201 .free = scpi_usbtmc_free,