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