]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_tcp.c
0de2ff54e47129209319a04204c122db99e72fcf
[libsigrok.git] / hardware / common / scpi_tcp.c
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
20 #ifdef _WIN32
21 #define _WIN32_WINNT 0x0501
22 #include <winsock2.h>
23 #include <ws2tcpip.h>
24 #endif
25
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 #include <glib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #ifndef _WIN32
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #endif
38 #include <errno.h>
39
40 #define LOG_PREFIX "scpi_tcp"
41
42 #define LENGTH_BYTES 4
43
44 struct scpi_tcp {
45         char *address;
46         char *port;
47         int socket;
48         char length_buf[LENGTH_BYTES];
49         int length_bytes_read;
50         int response_length;
51         int response_bytes_read;
52 };
53
54 static int scpi_tcp_dev_inst_new(void *priv, const char *resource,
55                 char **params, const char *serialcomm)
56 {
57         struct scpi_tcp *tcp = priv;
58
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
74 SR_PRIV int scpi_tcp_open(void *priv)
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) {
89                 sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
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,
110                                 strerror(errno));
111                 return SR_ERR;
112         }
113
114         return SR_OK;
115 }
116
117 SR_PRIV int scpi_tcp_source_add(void *priv, int events, int timeout,
118                         sr_receive_data_callback_t cb, void *cb_data)
119 {
120         struct scpi_tcp *tcp = priv;
121
122         return sr_source_add(tcp->socket, events, timeout, cb, cb_data);
123 }
124
125 SR_PRIV int scpi_tcp_source_remove(void *priv)
126 {
127         struct scpi_tcp *tcp = priv;
128
129         return sr_source_remove(tcp->socket);
130 }
131
132 SR_PRIV int scpi_tcp_send(void *priv, const char *command)
133 {
134         struct scpi_tcp *tcp = priv;
135         int len, out;
136         char *terminated_command;
137
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);
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
158 SR_PRIV int scpi_tcp_read_begin(void *priv)
159 {
160         struct scpi_tcp *tcp = priv;
161
162         tcp->response_bytes_read = 0;
163         tcp->length_bytes_read = 0;
164
165         return SR_OK;
166 }
167
168 SR_PRIV int scpi_tcp_read_data(void *priv, char *buf, int maxlen)
169 {
170         struct scpi_tcp *tcp = priv;
171         int len;
172
173         if (tcp->length_bytes_read < LENGTH_BYTES) {
174                 len = recv(tcp->socket, tcp->length_buf + tcp->length_bytes_read,
175                                 LENGTH_BYTES - tcp->length_bytes_read, 0);
176                 if (len < 0) {
177                         sr_err("Receive error: %s", strerror(errno));
178                         return SR_ERR;
179                 }
180
181                 tcp->length_bytes_read += len;
182
183                 if (tcp->length_bytes_read < LENGTH_BYTES)
184                         return 0;
185                 else
186                         tcp->response_length = RL32(tcp->length_buf);
187         }
188
189         if (tcp->response_bytes_read >= tcp->response_length)
190                 return SR_ERR;
191
192         len = recv(tcp->socket, buf, maxlen, 0);
193
194         if (len < 0) {
195                 sr_err("Receive error: %s", strerror(errno));
196                 return SR_ERR;
197         }
198
199         tcp->response_bytes_read += len;
200
201         return len;
202 }
203
204 SR_PRIV int scpi_tcp_read_complete(void *priv)
205 {
206         struct scpi_tcp *tcp = priv;
207
208         return (tcp->length_bytes_read == LENGTH_BYTES &&
209                         tcp->response_bytes_read >= tcp->response_length);
210 }
211
212 SR_PRIV int scpi_tcp_close(void *priv)
213 {
214         struct scpi_tcp *tcp = priv;
215
216         if (close(tcp->socket) < 0)
217                 return SR_ERR;
218
219         return SR_OK;
220 }
221
222 SR_PRIV void scpi_tcp_free(void *priv)
223 {
224         struct scpi_tcp *tcp = priv;
225
226         g_free(tcp->address);
227         g_free(tcp->port);
228 }
229
230 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_dev = {
231         .name          = "TCP",
232         .prefix        = "tcp",
233         .priv_size     = sizeof(struct scpi_tcp),
234         .dev_inst_new  = scpi_tcp_dev_inst_new,
235         .open          = scpi_tcp_open,
236         .source_add    = scpi_tcp_source_add,
237         .source_remove = scpi_tcp_source_remove,
238         .send          = scpi_tcp_send,
239         .read_begin    = scpi_tcp_read_begin,
240         .read_data     = scpi_tcp_read_data,
241         .read_complete = scpi_tcp_read_complete,
242         .close         = scpi_tcp_close,
243         .free          = scpi_tcp_free,
244 };