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