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 SR_PRIV int scpi_usbtmc_open(void *priv)
42 struct usbtmc_scpi *uscpi = priv;
43 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
45 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
51 SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
52 sr_receive_data_callback_t cb, void *cb_data)
54 struct usbtmc_scpi *uscpi = priv;
55 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
57 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
60 SR_PRIV int scpi_usbtmc_source_remove(void *priv)
62 struct usbtmc_scpi *uscpi = priv;
63 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
65 return sr_source_remove(usbtmc->fd);
68 SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
70 struct usbtmc_scpi *uscpi = priv;
71 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
74 len = strlen(command);
75 out = write(usbtmc->fd, command, len);
78 sr_err("Write error: %s", strerror(errno));
83 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
87 sr_spew("Successfully sent SCPI command: '%s'.", command);
92 SR_PRIV int scpi_usbtmc_read_begin(void *priv)
94 struct usbtmc_scpi *uscpi = priv;
95 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
98 len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
101 sr_err("Read error: %s", strerror(errno));
105 uscpi->response_length = len;
106 uscpi->response_bytes_read = 0;
111 SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
113 struct usbtmc_scpi *uscpi = priv;
116 if (uscpi->response_length == MAX_READ_LENGTH
117 && uscpi->response_bytes_read == uscpi->response_length)
118 if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
121 if (uscpi->response_bytes_read >= uscpi->response_length)
124 read_length = uscpi->response_length - uscpi->response_bytes_read;
126 if (read_length > maxlen)
127 read_length = maxlen;
129 memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
131 uscpi->response_bytes_read += read_length;
136 SR_PRIV int scpi_usbtmc_read_complete(void *priv)
138 struct usbtmc_scpi *uscpi = priv;
140 if (uscpi->response_length == MAX_READ_LENGTH
141 && uscpi->response_bytes_read == uscpi->response_length)
142 scpi_usbtmc_read_begin(uscpi);
144 return (uscpi->response_bytes_read >= uscpi->response_length);
147 SR_PRIV int scpi_usbtmc_close(void *priv)
149 struct usbtmc_scpi *uscpi = priv;
150 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
152 if (close(usbtmc->fd) < 0)
158 static void scpi_usbtmc_free(void *priv)
160 struct usbtmc_scpi *uscpi = priv;
161 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
164 sr_usbtmc_dev_inst_free(usbtmc);
167 SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
169 struct sr_scpi_dev_inst *scpi;
170 struct usbtmc_scpi *uscpi;
171 struct sr_usbtmc_dev_inst *usbtmc;
173 if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
176 uscpi = g_malloc(sizeof(struct usbtmc_scpi));
178 uscpi->usbtmc = usbtmc;
180 scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
182 scpi->open = scpi_usbtmc_open;
183 scpi->source_add = scpi_usbtmc_source_add;
184 scpi->source_remove = scpi_usbtmc_source_remove;
185 scpi->send = scpi_usbtmc_send;
186 scpi->read_begin = scpi_usbtmc_read_begin;
187 scpi->read_data = scpi_usbtmc_read_data;
188 scpi->read_complete = scpi_usbtmc_read_complete;
189 scpi->close = scpi_usbtmc_close;
190 scpi->free = scpi_usbtmc_free;