]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_tcp.c
Revise SCPI read API to allow backend-independent data handling.
[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 SR_PRIV int scpi_tcp_open(void *priv)
55 {
56         struct scpi_tcp *tcp = priv;
57         struct addrinfo hints;
58         struct addrinfo *results, *res;
59         int err;
60
61         memset(&hints, 0, sizeof(hints));
62         hints.ai_family = AF_UNSPEC;
63         hints.ai_socktype = SOCK_STREAM;
64         hints.ai_protocol = IPPROTO_TCP;
65
66         err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
67
68         if (err) {
69                 sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
70                         gai_strerror(err));
71                 return SR_ERR;
72         }
73
74         for (res = results; res; res = res->ai_next) {
75                 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
76                                                 res->ai_protocol)) < 0)
77                         continue;
78                 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
79                         close(tcp->socket);
80                         tcp->socket = -1;
81                         continue;
82                 }
83                 break;
84         }
85
86         freeaddrinfo(results);
87
88         if (tcp->socket < 0) {
89                 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
90                                 strerror(errno));
91                 return SR_ERR;
92         }
93
94         return SR_OK;
95 }
96
97 SR_PRIV int scpi_tcp_source_add(void *priv, int events, int timeout,
98                         sr_receive_data_callback_t cb, void *cb_data)
99 {
100         struct scpi_tcp *tcp = priv;
101
102         return sr_source_add(tcp->socket, events, timeout, cb, cb_data);
103 }
104
105 SR_PRIV int scpi_tcp_source_remove(void *priv)
106 {
107         struct scpi_tcp *tcp = priv;
108
109         return sr_source_remove(tcp->socket);
110 }
111
112 SR_PRIV int scpi_tcp_send(void *priv, const char *command)
113 {
114         struct scpi_tcp *tcp = priv;
115         int len, out;
116         char *terminated_command;
117
118         terminated_command = g_strdup_printf("%s\r\n", command);
119         len = strlen(terminated_command);
120         out = send(tcp->socket, terminated_command, len, 0);
121         g_free(terminated_command);
122
123         if (out < 0) {
124                 sr_err("Send error: %s", strerror(errno));
125                 return SR_ERR;
126         }
127
128         if (out < len) {
129                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
130                        len, command);
131         }
132
133         sr_spew("Successfully sent SCPI command: '%s'.", command);
134
135         return SR_OK;
136 }
137
138 SR_PRIV int scpi_tcp_read_begin(void *priv)
139 {
140         struct scpi_tcp *tcp = priv;
141
142         tcp->response_bytes_read = 0;
143         tcp->length_bytes_read = 0;
144
145         return SR_OK;
146 }
147
148 SR_PRIV int scpi_tcp_read_data(void *priv, char *buf, int maxlen)
149 {
150         struct scpi_tcp *tcp = priv;
151         int len;
152
153         if (tcp->length_bytes_read < LENGTH_BYTES) {
154                 len = recv(tcp->socket, tcp->length_buf + tcp->length_bytes_read,
155                                 LENGTH_BYTES - tcp->length_bytes_read, 0);
156                 if (len < 0) {
157                         sr_err("Receive error: %s", strerror(errno));
158                         return SR_ERR;
159                 }
160
161                 tcp->length_bytes_read += len;
162
163                 if (tcp->length_bytes_read < LENGTH_BYTES)
164                         return 0;
165                 else
166                         tcp->response_length = RL32(tcp->length_buf);
167         }
168
169         if (tcp->response_bytes_read >= tcp->response_length)
170                 return SR_ERR;
171
172         len = recv(tcp->socket, buf, maxlen, 0);
173
174         if (len < 0) {
175                 sr_err("Receive error: %s", strerror(errno));
176                 return SR_ERR;
177         }
178
179         tcp->response_bytes_read += len;
180
181         return len;
182 }
183
184 SR_PRIV int scpi_tcp_read_complete(void *priv)
185 {
186         struct scpi_tcp *tcp = priv;
187
188         return (tcp->length_bytes_read == LENGTH_BYTES &&
189                         tcp->response_bytes_read >= tcp->response_length);
190 }
191
192 SR_PRIV int scpi_tcp_close(void *priv)
193 {
194         struct scpi_tcp *tcp = priv;
195
196         if (close(tcp->socket) < 0)
197                 return SR_ERR;
198
199         return SR_OK;
200 }
201
202 SR_PRIV void scpi_tcp_free(void *priv)
203 {
204         struct scpi_tcp *tcp = priv;
205
206         g_free(tcp->address);
207         g_free(tcp->port);
208         g_free(tcp);
209 }
210
211 SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address,
212                 const char *port)
213 {
214         struct sr_scpi_dev_inst *scpi;
215         struct scpi_tcp *tcp;
216
217         scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
218         tcp = g_malloc0(sizeof(struct scpi_tcp));
219
220         tcp->address = g_strdup(address);
221         tcp->port = g_strdup(port);
222         tcp->socket = -1;
223
224         scpi->open = scpi_tcp_open;
225         scpi->source_add = scpi_tcp_source_add;
226         scpi->source_remove = scpi_tcp_source_remove;
227         scpi->send = scpi_tcp_send;
228         scpi->read_begin = scpi_tcp_read_begin;
229         scpi->read_data = scpi_tcp_read_data;
230         scpi->read_complete = scpi_tcp_read_complete;
231         scpi->close = scpi_tcp_close;
232         scpi->free = scpi_tcp_free;
233         scpi->priv = tcp;
234
235         return scpi;
236 }