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/>.
21 #define _WIN32_WINNT 0x0501
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
40 /* Message logging helpers with subsystem-specific prefix string. */
41 #define LOG_PREFIX "scpi_tcp: "
42 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
43 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
44 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
45 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
46 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
54 SR_PRIV int scpi_tcp_open(void *priv)
56 struct scpi_tcp *tcp = priv;
57 struct addrinfo hints;
58 struct addrinfo *results, *res;
61 memset(&hints, 0, sizeof(hints));
62 hints.ai_family = AF_UNSPEC;
63 hints.ai_socktype = SOCK_STREAM;
64 hints.ai_protocol = IPPROTO_TCP;
66 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
69 sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
74 for (res = results; res; res = res->ai_next) {
75 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
76 res->ai_protocol)) < 0)
78 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
86 freeaddrinfo(results);
88 if (tcp->socket < 0) {
89 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
97 SR_PRIV int scpi_tcp_source_add(void *priv, int events, int timeout,
98 sr_receive_data_callback_t cb, void *cb_data)
100 struct scpi_tcp *tcp = priv;
102 return sr_source_add(tcp->socket, events, timeout, cb, cb_data);
105 SR_PRIV int scpi_tcp_source_remove(void *priv)
107 struct scpi_tcp *tcp = priv;
109 return sr_source_remove(tcp->socket);
112 SR_PRIV int scpi_tcp_send(void *priv, const char *command)
114 struct scpi_tcp *tcp = priv;
116 char *terminated_command;
118 terminated_command = g_strdup_printf("%s\r\n", command);
119 len = strlen(terminated_command);
120 out = send(tcp->socket, terminated_command, len, 0);
121 g_free(terminated_command);
124 sr_err("Send error: %s", strerror(errno));
129 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
133 sr_spew("Successfully sent SCPI command: '%s'.", command);
138 SR_PRIV int scpi_tcp_receive(void *priv, char **scpi_response)
140 struct scpi_tcp *tcp = priv;
145 response = g_string_sized_new(1024);
147 len = recv(tcp->socket, buf, sizeof(buf), 0);
150 sr_err("Receive error: %s", strerror(errno));
151 g_string_free(response, TRUE);
155 response = g_string_append_len(response, buf + 4, len - 4);
157 *scpi_response = response->str;
159 sr_dbg("SCPI response received (length %d): '%.50s'",
160 response->len, response->str);
162 g_string_free(response, FALSE);
167 SR_PRIV int scpi_tcp_read(void *priv, char *buf, int maxlen)
169 struct scpi_tcp *tcp = priv;
172 len = recv(tcp->socket, buf, maxlen, 0);
175 sr_err("Receive error: %s", strerror(errno));
182 SR_PRIV int scpi_tcp_close(void *priv)
184 struct scpi_tcp *tcp = priv;
186 if (close(tcp->socket) < 0)
192 SR_PRIV void scpi_tcp_free(void *priv)
194 struct scpi_tcp *tcp = priv;
196 g_free(tcp->address);
201 SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address,
204 struct sr_scpi_dev_inst *scpi;
205 struct scpi_tcp *tcp;
207 scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
208 tcp = g_malloc0(sizeof(struct scpi_tcp));
210 tcp->address = g_strdup(address);
211 tcp->port = g_strdup(port);
214 scpi->open = scpi_tcp_open;
215 scpi->source_add = scpi_tcp_source_add;
216 scpi->source_remove = scpi_tcp_source_remove;
217 scpi->send = scpi_tcp_send;
218 scpi->receive = scpi_tcp_receive;
219 scpi->read = scpi_tcp_read;
220 scpi->close = scpi_tcp_close;
221 scpi->free = scpi_tcp_free;