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/>.
22 #define _WIN32_WINNT 0x0501
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
36 #include <libsigrok/libsigrok.h>
37 #include "libsigrok-internal.h"
40 #define LOG_PREFIX "scpi_tcp"
42 #define LENGTH_BYTES 4
48 char length_buf[LENGTH_BYTES];
49 int length_bytes_read;
51 int response_bytes_read;
54 static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
55 const char *resource, char **params, const char *serialcomm)
57 struct scpi_tcp *tcp = priv;
63 if (!params || !params[1] || !params[2]) {
64 sr_err("Invalid parameters.");
68 tcp->address = g_strdup(params[1]);
69 tcp->port = g_strdup(params[2]);
75 static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi)
77 struct scpi_tcp *tcp = scpi->priv;
78 struct addrinfo hints;
79 struct addrinfo *results, *res;
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_family = AF_UNSPEC;
84 hints.ai_socktype = SOCK_STREAM;
85 hints.ai_protocol = IPPROTO_TCP;
87 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
90 sr_err("Address lookup failed: %s:%s: %s", tcp->address, tcp->port,
95 for (res = results; res; res = res->ai_next) {
96 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
97 res->ai_protocol)) < 0)
99 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
107 freeaddrinfo(results);
109 if (tcp->socket < 0) {
110 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
118 static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi,
119 char **connection_id)
121 struct scpi_tcp *tcp = scpi->priv;
123 *connection_id = g_strdup_printf("%s/%s:%s",
124 scpi->prefix, tcp->address, tcp->port);
129 static int scpi_tcp_source_add(struct sr_session *session, void *priv,
130 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
132 struct scpi_tcp *tcp = priv;
134 return sr_session_source_add(session, tcp->socket, events, timeout,
138 static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
140 struct scpi_tcp *tcp = priv;
142 return sr_session_source_remove(session, tcp->socket);
145 static int scpi_tcp_send(void *priv, const char *command)
147 struct scpi_tcp *tcp = priv;
150 len = strlen(command);
151 out = send(tcp->socket, command, len, 0);
154 sr_err("Send error: %s", g_strerror(errno));
159 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
163 sr_spew("Successfully sent SCPI command: '%s'.", command);
168 static int scpi_tcp_read_begin(void *priv)
170 struct scpi_tcp *tcp = priv;
172 tcp->response_bytes_read = 0;
173 tcp->length_bytes_read = 0;
178 static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
180 struct scpi_tcp *tcp = priv;
183 len = recv(tcp->socket, buf, maxlen, 0);
186 sr_err("Receive error: %s", g_strerror(errno));
190 tcp->length_bytes_read = LENGTH_BYTES;
191 tcp->response_length = len < maxlen ? len : maxlen + 1;
192 tcp->response_bytes_read = len;
197 static int scpi_tcp_raw_write_data(void *priv, char *buf, int len)
199 struct scpi_tcp *tcp = priv;
202 sentlen = send(tcp->socket, buf, len, 0);
205 sr_err("Send error: %s.", g_strerror(errno));
212 static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
214 struct scpi_tcp *tcp = priv;
217 if (tcp->length_bytes_read < LENGTH_BYTES) {
218 len = recv(tcp->socket, tcp->length_buf + tcp->length_bytes_read,
219 LENGTH_BYTES - tcp->length_bytes_read, 0);
221 sr_err("Receive error: %s", g_strerror(errno));
225 tcp->length_bytes_read += len;
227 if (tcp->length_bytes_read < LENGTH_BYTES)
230 tcp->response_length = RL32(tcp->length_buf);
233 if (tcp->response_bytes_read >= tcp->response_length)
236 len = recv(tcp->socket, buf, maxlen, 0);
239 sr_err("Receive error: %s", g_strerror(errno));
243 tcp->response_bytes_read += len;
248 static int scpi_tcp_read_complete(void *priv)
250 struct scpi_tcp *tcp = priv;
252 return (tcp->length_bytes_read == LENGTH_BYTES &&
253 tcp->response_bytes_read >= tcp->response_length);
256 static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi)
258 struct scpi_tcp *tcp = scpi->priv;
260 if (close(tcp->socket) < 0)
266 static void scpi_tcp_free(void *priv)
268 struct scpi_tcp *tcp = priv;
270 g_free(tcp->address);
274 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
277 .transport = SCPI_TRANSPORT_RAW_TCP,
278 .priv_size = sizeof(struct scpi_tcp),
279 .dev_inst_new = scpi_tcp_dev_inst_new,
280 .open = scpi_tcp_open,
281 .connection_id = scpi_tcp_connection_id,
282 .source_add = scpi_tcp_source_add,
283 .source_remove = scpi_tcp_source_remove,
284 .send = scpi_tcp_send,
285 .read_begin = scpi_tcp_read_begin,
286 .read_data = scpi_tcp_raw_read_data,
287 .write_data = scpi_tcp_raw_write_data,
288 .read_complete = scpi_tcp_read_complete,
289 .close = scpi_tcp_close,
290 .free = scpi_tcp_free,
293 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
295 .prefix = "tcp-rigol",
296 .transport = SCPI_TRANSPORT_RIGOL_TCP,
297 .priv_size = sizeof(struct scpi_tcp),
298 .dev_inst_new = scpi_tcp_dev_inst_new,
299 .open = scpi_tcp_open,
300 .connection_id = scpi_tcp_connection_id,
301 .source_add = scpi_tcp_source_add,
302 .source_remove = scpi_tcp_source_remove,
303 .send = scpi_tcp_send,
304 .read_begin = scpi_tcp_read_begin,
305 .read_data = scpi_tcp_rigol_read_data,
306 .read_complete = scpi_tcp_read_complete,
307 .close = scpi_tcp_close,
308 .free = scpi_tcp_free,