]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_tcp.c
scpi_vxi: fix memory leak for SCPI response data in VXI support code
[libsigrok.git] / src / scpi / scpi_tcp.c
CommitLineData
08a35913
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
589edd9b 21#ifdef _WIN32
0f4a4350 22#define _WIN32_WINNT 0x0501
589edd9b
ML
23#include <winsock2.h>
24#include <ws2tcpip.h>
25#endif
08a35913
ML
26#include <glib.h>
27#include <string.h>
28#include <unistd.h>
589edd9b 29#ifndef _WIN32
08a35913
ML
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netdb.h>
987a0530 34#endif
08a35913 35#include <errno.h>
c1aae900 36#include <libsigrok/libsigrok.h>
515ab088 37#include "libsigrok-internal.h"
5a1afc09 38#include "scpi.h"
08a35913 39
3544f848 40#define LOG_PREFIX "scpi_tcp"
08a35913 41
05c644ea
ML
42#define LENGTH_BYTES 4
43
08a35913
ML
44struct scpi_tcp {
45 char *address;
46 char *port;
47 int socket;
05c644ea
ML
48 char length_buf[LENGTH_BYTES];
49 int length_bytes_read;
50 int response_length;
51 int response_bytes_read;
08a35913
ML
52};
53
17bdda58
AJ
54static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
55 const char *resource, char **params, const char *serialcomm)
f754c146
AJ
56{
57 struct scpi_tcp *tcp = priv;
58
17bdda58 59 (void)drvc;
f754c146
AJ
60 (void)resource;
61 (void)serialcomm;
62
63 if (!params || !params[1] || !params[2]) {
64 sr_err("Invalid parameters.");
65 return SR_ERR;
66 }
67
68 tcp->address = g_strdup(params[1]);
d9251a2c
UH
69 tcp->port = g_strdup(params[2]);
70 tcp->socket = -1;
f754c146
AJ
71
72 return SR_OK;
73}
74
04229f7b 75static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi)
08a35913 76{
04229f7b 77 struct scpi_tcp *tcp = scpi->priv;
08a35913
ML
78 struct addrinfo hints;
79 struct addrinfo *results, *res;
80 int err;
81
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_family = AF_UNSPEC;
84 hints.ai_socktype = SOCK_STREAM;
85 hints.ai_protocol = IPPROTO_TCP;
86
87 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
88
89 if (err) {
6433156c 90 sr_err("Address lookup failed: %s:%s: %s", tcp->address, tcp->port,
08a35913
ML
91 gai_strerror(err));
92 return SR_ERR;
93 }
94
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)
98 continue;
99 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
100 close(tcp->socket);
101 tcp->socket = -1;
102 continue;
103 }
104 break;
105 }
106
107 freeaddrinfo(results);
108
109 if (tcp->socket < 0) {
110 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
7237e912 111 g_strerror(errno));
08a35913
ML
112 return SR_ERR;
113 }
114
115 return SR_OK;
116}
117
8107a9a6
FS
118static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi,
119 char **connection_id)
120{
121 struct scpi_tcp *tcp = scpi->priv;
122
123 *connection_id = g_strdup_printf("%s/%s:%s",
124 scpi->prefix, tcp->address, tcp->port);
125
126 return SR_OK;
127}
128
102f1239
BV
129static int scpi_tcp_source_add(struct sr_session *session, void *priv,
130 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
08a35913
ML
131{
132 struct scpi_tcp *tcp = priv;
133
102f1239
BV
134 return sr_session_source_add(session, tcp->socket, events, timeout,
135 cb, cb_data);
08a35913
ML
136}
137
102f1239 138static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
08a35913
ML
139{
140 struct scpi_tcp *tcp = priv;
141
102f1239 142 return sr_session_source_remove(session, tcp->socket);
08a35913
ML
143}
144
d87c1766 145static int scpi_tcp_send(void *priv, const char *command)
08a35913
ML
146{
147 struct scpi_tcp *tcp = priv;
148 int len, out;
149
055804e8
SB
150 len = strlen(command);
151 out = send(tcp->socket, command, len, 0);
08a35913
ML
152
153 if (out < 0) {
7237e912 154 sr_err("Send error: %s", g_strerror(errno));
08a35913
ML
155 return SR_ERR;
156 }
157
158 if (out < len) {
159 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
160 len, command);
161 }
162
163 sr_spew("Successfully sent SCPI command: '%s'.", command);
164
165 return SR_OK;
166}
167
d87c1766 168static int scpi_tcp_read_begin(void *priv)
08a35913
ML
169{
170 struct scpi_tcp *tcp = priv;
08a35913 171
05c644ea
ML
172 tcp->response_bytes_read = 0;
173 tcp->length_bytes_read = 0;
08a35913
ML
174
175 return SR_OK;
176}
177
d87c1766 178static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
104ed125
AJ
179{
180 struct scpi_tcp *tcp = priv;
181 int len;
182
183 len = recv(tcp->socket, buf, maxlen, 0);
184
185 if (len < 0) {
7237e912 186 sr_err("Receive error: %s", g_strerror(errno));
104ed125
AJ
187 return SR_ERR;
188 }
189
190 tcp->length_bytes_read = LENGTH_BYTES;
191 tcp->response_length = len < maxlen ? len : maxlen + 1;
192 tcp->response_bytes_read = len;
193
194 return len;
195}
196
b6be55ce
SS
197static int scpi_tcp_raw_write_data(void *priv, char *buf, int len)
198{
199 struct scpi_tcp *tcp = priv;
200 int sentlen;
201
202 sentlen = send(tcp->socket, buf, len, 0);
203
204 if (sentlen < 0) {
205 sr_err("Send error: %s.", g_strerror(errno));
206 return SR_ERR;
207 }
208
209 return sentlen;
210}
211
d87c1766 212static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
08a35913
ML
213{
214 struct scpi_tcp *tcp = priv;
215 int len;
216
05c644ea
ML
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);
220 if (len < 0) {
7237e912 221 sr_err("Receive error: %s", g_strerror(errno));
05c644ea
ML
222 return SR_ERR;
223 }
224
225 tcp->length_bytes_read += len;
226
227 if (tcp->length_bytes_read < LENGTH_BYTES)
228 return 0;
229 else
230 tcp->response_length = RL32(tcp->length_buf);
231 }
232
233 if (tcp->response_bytes_read >= tcp->response_length)
234 return SR_ERR;
235
08a35913
ML
236 len = recv(tcp->socket, buf, maxlen, 0);
237
238 if (len < 0) {
7237e912 239 sr_err("Receive error: %s", g_strerror(errno));
08a35913
ML
240 return SR_ERR;
241 }
242
05c644ea
ML
243 tcp->response_bytes_read += len;
244
08a35913
ML
245 return len;
246}
247
d87c1766 248static int scpi_tcp_read_complete(void *priv)
05c644ea
ML
249{
250 struct scpi_tcp *tcp = priv;
251
252 return (tcp->length_bytes_read == LENGTH_BYTES &&
253 tcp->response_bytes_read >= tcp->response_length);
254}
255
04229f7b 256static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi)
08a35913 257{
04229f7b 258 struct scpi_tcp *tcp = scpi->priv;
08a35913
ML
259
260 if (close(tcp->socket) < 0)
261 return SR_ERR;
262
263 return SR_OK;
264}
265
d87c1766 266static void scpi_tcp_free(void *priv)
08a35913
ML
267{
268 struct scpi_tcp *tcp = priv;
269
270 g_free(tcp->address);
271 g_free(tcp->port);
08a35913
ML
272}
273
104ed125
AJ
274SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
275 .name = "RAW TCP",
276 .prefix = "tcp-raw",
87aa1e63 277 .transport = SCPI_TRANSPORT_RAW_TCP,
104ed125
AJ
278 .priv_size = sizeof(struct scpi_tcp),
279 .dev_inst_new = scpi_tcp_dev_inst_new,
280 .open = scpi_tcp_open,
8107a9a6 281 .connection_id = scpi_tcp_connection_id,
104ed125
AJ
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,
b6be55ce 287 .write_data = scpi_tcp_raw_write_data,
104ed125
AJ
288 .read_complete = scpi_tcp_read_complete,
289 .close = scpi_tcp_close,
290 .free = scpi_tcp_free,
291};
292
293SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
294 .name = "RIGOL TCP",
295 .prefix = "tcp-rigol",
87aa1e63 296 .transport = SCPI_TRANSPORT_RIGOL_TCP,
f754c146
AJ
297 .priv_size = sizeof(struct scpi_tcp),
298 .dev_inst_new = scpi_tcp_dev_inst_new,
299 .open = scpi_tcp_open,
8107a9a6 300 .connection_id = scpi_tcp_connection_id,
f754c146
AJ
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,
104ed125 305 .read_data = scpi_tcp_rigol_read_data,
f754c146
AJ
306 .read_complete = scpi_tcp_read_complete,
307 .close = scpi_tcp_close,
308 .free = scpi_tcp_free,
309};