]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_tcp.c
build: Portability fixes.
[libsigrok.git] / hardware / common / 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
589edd9b 20#ifdef _WIN32
0f4a4350 21#define _WIN32_WINNT 0x0501
589edd9b
ML
22#include <winsock2.h>
23#include <ws2tcpip.h>
24#endif
0f4a4350 25
08a35913
ML
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28
29#include <glib.h>
30#include <string.h>
31#include <unistd.h>
589edd9b 32#ifndef _WIN32
08a35913
ML
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <netdb.h>
987a0530 37#endif
08a35913
ML
38#include <errno.h>
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]);
69 tcp->port = g_strdup(params[2]);
70 tcp->socket = -1;
71
72 return SR_OK;
73}
74
d87c1766 75static int scpi_tcp_open(void *priv)
08a35913
ML
76{
77 struct scpi_tcp *tcp = priv;
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) {
90 sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
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,
111 strerror(errno));
112 return SR_ERR;
113 }
114
115 return SR_OK;
116}
117
102f1239
BV
118static int scpi_tcp_source_add(struct sr_session *session, void *priv,
119 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
08a35913
ML
120{
121 struct scpi_tcp *tcp = priv;
122
102f1239
BV
123 return sr_session_source_add(session, tcp->socket, events, timeout,
124 cb, cb_data);
08a35913
ML
125}
126
102f1239 127static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
08a35913
ML
128{
129 struct scpi_tcp *tcp = priv;
130
102f1239 131 return sr_session_source_remove(session, tcp->socket);
08a35913
ML
132}
133
d87c1766 134static int scpi_tcp_send(void *priv, const char *command)
08a35913
ML
135{
136 struct scpi_tcp *tcp = priv;
137 int len, out;
b8705e99 138 char *terminated_command;
08a35913 139
b8705e99
ML
140 terminated_command = g_strdup_printf("%s\r\n", command);
141 len = strlen(terminated_command);
142 out = send(tcp->socket, terminated_command, len, 0);
143 g_free(terminated_command);
08a35913
ML
144
145 if (out < 0) {
146 sr_err("Send error: %s", strerror(errno));
147 return SR_ERR;
148 }
149
150 if (out < len) {
151 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
152 len, command);
153 }
154
155 sr_spew("Successfully sent SCPI command: '%s'.", command);
156
157 return SR_OK;
158}
159
d87c1766 160static int scpi_tcp_read_begin(void *priv)
08a35913
ML
161{
162 struct scpi_tcp *tcp = priv;
08a35913 163
05c644ea
ML
164 tcp->response_bytes_read = 0;
165 tcp->length_bytes_read = 0;
08a35913
ML
166
167 return SR_OK;
168}
169
d87c1766 170static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
104ed125
AJ
171{
172 struct scpi_tcp *tcp = priv;
173 int len;
174
175 len = recv(tcp->socket, buf, maxlen, 0);
176
177 if (len < 0) {
178 sr_err("Receive error: %s", strerror(errno));
179 return SR_ERR;
180 }
181
182 tcp->length_bytes_read = LENGTH_BYTES;
183 tcp->response_length = len < maxlen ? len : maxlen + 1;
184 tcp->response_bytes_read = len;
185
186 return len;
187}
188
d87c1766 189static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
08a35913
ML
190{
191 struct scpi_tcp *tcp = priv;
192 int len;
193
05c644ea
ML
194 if (tcp->length_bytes_read < LENGTH_BYTES) {
195 len = recv(tcp->socket, tcp->length_buf + tcp->length_bytes_read,
196 LENGTH_BYTES - tcp->length_bytes_read, 0);
197 if (len < 0) {
198 sr_err("Receive error: %s", strerror(errno));
199 return SR_ERR;
200 }
201
202 tcp->length_bytes_read += len;
203
204 if (tcp->length_bytes_read < LENGTH_BYTES)
205 return 0;
206 else
207 tcp->response_length = RL32(tcp->length_buf);
208 }
209
210 if (tcp->response_bytes_read >= tcp->response_length)
211 return SR_ERR;
212
08a35913
ML
213 len = recv(tcp->socket, buf, maxlen, 0);
214
215 if (len < 0) {
216 sr_err("Receive error: %s", strerror(errno));
217 return SR_ERR;
218 }
219
05c644ea
ML
220 tcp->response_bytes_read += len;
221
08a35913
ML
222 return len;
223}
224
d87c1766 225static int scpi_tcp_read_complete(void *priv)
05c644ea
ML
226{
227 struct scpi_tcp *tcp = priv;
228
229 return (tcp->length_bytes_read == LENGTH_BYTES &&
230 tcp->response_bytes_read >= tcp->response_length);
231}
232
d87c1766 233static int scpi_tcp_close(void *priv)
08a35913
ML
234{
235 struct scpi_tcp *tcp = priv;
236
237 if (close(tcp->socket) < 0)
238 return SR_ERR;
239
240 return SR_OK;
241}
242
d87c1766 243static void scpi_tcp_free(void *priv)
08a35913
ML
244{
245 struct scpi_tcp *tcp = priv;
246
247 g_free(tcp->address);
248 g_free(tcp->port);
08a35913
ML
249}
250
104ed125
AJ
251SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
252 .name = "RAW TCP",
253 .prefix = "tcp-raw",
254 .priv_size = sizeof(struct scpi_tcp),
255 .dev_inst_new = scpi_tcp_dev_inst_new,
256 .open = scpi_tcp_open,
257 .source_add = scpi_tcp_source_add,
258 .source_remove = scpi_tcp_source_remove,
259 .send = scpi_tcp_send,
260 .read_begin = scpi_tcp_read_begin,
261 .read_data = scpi_tcp_raw_read_data,
262 .read_complete = scpi_tcp_read_complete,
263 .close = scpi_tcp_close,
264 .free = scpi_tcp_free,
265};
266
267SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
268 .name = "RIGOL TCP",
269 .prefix = "tcp-rigol",
f754c146
AJ
270 .priv_size = sizeof(struct scpi_tcp),
271 .dev_inst_new = scpi_tcp_dev_inst_new,
272 .open = scpi_tcp_open,
273 .source_add = scpi_tcp_source_add,
274 .source_remove = scpi_tcp_source_remove,
275 .send = scpi_tcp_send,
276 .read_begin = scpi_tcp_read_begin,
104ed125 277 .read_data = scpi_tcp_rigol_read_data,
f754c146
AJ
278 .read_complete = scpi_tcp_read_complete,
279 .close = scpi_tcp_close,
280 .free = scpi_tcp_free,
281};