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/>.
24 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "scpi_tcp"
32 #define LENGTH_BYTES sizeof(uint32_t)
35 struct sr_tcp_dev_inst *tcp_dev;
36 uint8_t length_buf[LENGTH_BYTES];
37 size_t length_bytes_read;
38 size_t response_length;
39 size_t response_bytes_read;
42 static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
43 const char *resource, char **params, const char *serialcomm)
45 struct scpi_tcp *tcp = priv;
51 if (!params || !params[1] || !params[2]) {
52 sr_err("Invalid parameters.");
56 tcp->tcp_dev = sr_tcp_dev_inst_new(params[1], params[2]);
63 static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi)
65 struct scpi_tcp *tcp = scpi->priv;
68 ret = sr_tcp_connect(tcp->tcp_dev);
75 static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi,
78 struct scpi_tcp *tcp = scpi->priv;
82 ret = sr_tcp_get_port_path(tcp->tcp_dev, scpi->prefix, '/',
83 conn_text, sizeof(conn_text));
87 *connection_id = g_strdup(conn_text);
91 static int scpi_tcp_source_add(struct sr_session *session, void *priv,
92 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
94 struct scpi_tcp *tcp = priv;
96 return sr_tcp_source_add(session, tcp->tcp_dev,
97 events, timeout, cb, cb_data);
100 static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
102 struct scpi_tcp *tcp = priv;
104 return sr_tcp_source_remove(session, tcp->tcp_dev);
107 /* Transmit text, usually a command. tcp-raw and tcp-rigol modes. */
108 static int scpi_tcp_send(void *priv, const char *command)
110 struct scpi_tcp *tcp = priv;
111 const uint8_t *wrptr;
112 size_t wrlen, written;
115 wrptr = (const uint8_t *)command;
116 wrlen = strlen(command);
117 ret = sr_tcp_write_bytes(tcp->tcp_dev, wrptr, wrlen);
119 sr_err("Send error: %s", g_strerror(errno));
122 written = (size_t)ret;
123 if (written < wrlen) {
124 sr_dbg("Only sent %zu/%zu bytes of SCPI command: '%s'.",
125 written, wrlen, command);
128 sr_spew("Successfully sent SCPI command: '%s'.", command);
133 /* Start reception across multiple read calls. tcp-raw and tcp-rigol modes. */
134 static int scpi_tcp_read_begin(void *priv)
136 struct scpi_tcp *tcp = priv;
138 tcp->response_bytes_read = 0;
139 tcp->length_bytes_read = 0;
144 /* Receive response data. tcp-raw mode. */
145 static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
147 struct scpi_tcp *tcp = priv;
152 /* Get another chunk of receive data. */
153 rdptr = (uint8_t *)buf;
155 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
157 sr_err("Receive error: %s", g_strerror(errno));
163 * Raw data mode (in contrast to Rigol mode). Prepare to answer
164 * the "completed" condition while the payload's length is not
165 * known. Pretend that the length buffer had been received.
166 * Assume that short reads correspond to the end of a response,
167 * while full reads of the caller specified size suggest that
168 * more data can follow.
170 tcp->length_bytes_read = LENGTH_BYTES;
171 tcp->response_length = rcvd < rdlen ? rcvd : rdlen + 1;
172 tcp->response_bytes_read = rcvd;
177 /* Transmit data of given length. tcp-raw mode. */
178 static int scpi_tcp_raw_write_data(void *priv, char *buf, int len)
180 struct scpi_tcp *tcp = priv;
181 const uint8_t *wrptr;
185 wrptr = (const uint8_t *)buf;
187 ret = sr_tcp_write_bytes(tcp->tcp_dev, wrptr, wrlen);
189 sr_err("Send error: %s.", g_strerror(errno));
197 /* Receive response data. tcp-rigol mode. */
198 static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
200 struct scpi_tcp *tcp = priv;
206 * Rigol mode, chunks are prefixed by a length spec.
207 * Get more length bytes when we haven't read them before.
208 * Return "zero length read" if length has yet to get received.
209 * Otherwise get chunk length from length bytes buffer.
211 if (tcp->length_bytes_read < sizeof(tcp->length_buf)) {
212 rdptr = &tcp->length_buf[tcp->length_bytes_read];
213 rdlen = sizeof(tcp->length_buf) - tcp->length_bytes_read;
214 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
216 sr_err("Receive error: %s", g_strerror(errno));
220 tcp->length_bytes_read += rcvd;
221 if (tcp->length_bytes_read < sizeof(tcp->length_buf))
223 tcp->response_length = read_u32le(tcp->length_buf);
226 /* Received more chunk data than announced size? Fatal. */
227 if (tcp->response_bytes_read >= tcp->response_length)
230 /* Read another chunk of the receive data. */
231 rdptr = (uint8_t *)buf;
233 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
235 sr_err("Receive error: %s", g_strerror(errno));
239 tcp->response_bytes_read += rcvd;
244 /* Check reception completion. tcp-raw and tcp-rigol modes. */
245 static int scpi_tcp_read_complete(void *priv)
247 struct scpi_tcp *tcp = priv;
248 gboolean have_length, have_response;
250 have_length = tcp->length_bytes_read == LENGTH_BYTES;
251 have_response = tcp->response_bytes_read >= tcp->response_length;
253 return have_length && have_response;
256 static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi)
258 struct scpi_tcp *tcp = scpi->priv;
261 ret = sr_tcp_disconnect(tcp->tcp_dev);
268 static void scpi_tcp_free(void *priv)
270 struct scpi_tcp *tcp = priv;
272 sr_tcp_dev_inst_free(tcp->tcp_dev);
275 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
278 .transport = SCPI_TRANSPORT_RAW_TCP,
279 .priv_size = sizeof(struct scpi_tcp),
280 .dev_inst_new = scpi_tcp_dev_inst_new,
281 .open = scpi_tcp_open,
282 .connection_id = scpi_tcp_connection_id,
283 .source_add = scpi_tcp_source_add,
284 .source_remove = scpi_tcp_source_remove,
285 .send = scpi_tcp_send,
286 .read_begin = scpi_tcp_read_begin,
287 .read_data = scpi_tcp_raw_read_data,
288 .write_data = scpi_tcp_raw_write_data,
289 .read_complete = scpi_tcp_read_complete,
290 .close = scpi_tcp_close,
291 .free = scpi_tcp_free,
294 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
296 .prefix = "tcp-rigol",
297 .transport = SCPI_TRANSPORT_RIGOL_TCP,
298 .priv_size = sizeof(struct scpi_tcp),
299 .dev_inst_new = scpi_tcp_dev_inst_new,
300 .open = scpi_tcp_open,
301 .connection_id = scpi_tcp_connection_id,
302 .source_add = scpi_tcp_source_add,
303 .source_remove = scpi_tcp_source_remove,
304 .send = scpi_tcp_send,
305 .read_begin = scpi_tcp_read_begin,
306 .read_data = scpi_tcp_rigol_read_data,
307 .read_complete = scpi_tcp_read_complete,
308 .close = scpi_tcp_close,
309 .free = scpi_tcp_free,