]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_tcp.c
Add implementation for SCPI over TCP.
[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 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
22
23 #include <glib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <errno.h>
31
32 /* Message logging helpers with subsystem-specific prefix string. */
33 #define LOG_PREFIX "scpi_tcp: "
34 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
35 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
36 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
37 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
38 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
39
40 struct scpi_tcp {
41         char *address;
42         char *port;
43         int socket;
44 };
45
46 SR_PRIV int scpi_tcp_open(void *priv)
47 {
48         struct scpi_tcp *tcp = priv;
49         struct addrinfo hints;
50         struct addrinfo *results, *res;
51         int err;
52
53         memset(&hints, 0, sizeof(hints));
54         hints.ai_family = AF_UNSPEC;
55         hints.ai_socktype = SOCK_STREAM;
56         hints.ai_protocol = IPPROTO_TCP;
57
58         err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
59
60         if (err) {
61                 sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
62                         gai_strerror(err));
63                 return SR_ERR;
64         }
65
66         for (res = results; res; res = res->ai_next) {
67                 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
68                                                 res->ai_protocol)) < 0)
69                         continue;
70                 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
71                         close(tcp->socket);
72                         tcp->socket = -1;
73                         continue;
74                 }
75                 break;
76         }
77
78         freeaddrinfo(results);
79
80         if (tcp->socket < 0) {
81                 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
82                                 strerror(errno));
83                 return SR_ERR;
84         }
85
86         return SR_OK;
87 }
88
89 SR_PRIV int scpi_tcp_source_add(void *priv, int events, int timeout,
90                         sr_receive_data_callback_t cb, void *cb_data)
91 {
92         struct scpi_tcp *tcp = priv;
93
94         return sr_source_add(tcp->socket, events, timeout, cb, cb_data);
95 }
96
97 SR_PRIV int scpi_tcp_source_remove(void *priv)
98 {
99         struct scpi_tcp *tcp = priv;
100
101         return sr_source_remove(tcp->socket);
102 }
103
104 SR_PRIV int scpi_tcp_send(void *priv, const char *command)
105 {
106         struct scpi_tcp *tcp = priv;
107         int len, out;
108
109         len = strlen(command);
110         out = send(tcp->socket, command, len, 0);
111
112         if (out < 0) {
113                 sr_err("Send error: %s", strerror(errno));
114                 return SR_ERR;
115         }
116
117         if (out < len) {
118                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
119                        len, command);
120         }
121
122         sr_spew("Successfully sent SCPI command: '%s'.", command);
123
124         return SR_OK;
125 }
126
127 SR_PRIV int scpi_tcp_receive(void *priv, char **scpi_response)
128 {
129         struct scpi_tcp *tcp = priv;
130         GString *response;
131         char buf[256];
132         int len;
133
134         response = g_string_sized_new(1024);
135
136         len = recv(tcp->socket, buf, sizeof(buf), 0);
137
138         if (len < 0) {
139                 sr_err("Receive error: %s", strerror(errno));
140                 g_string_free(response, TRUE);
141                 return SR_ERR;
142         }
143
144         response = g_string_append_len(response, buf, len);
145
146         *scpi_response = response->str;
147
148         sr_dbg("SCPI response received (length %d): '%.50s'",
149                response->len, response->str);
150
151         g_string_free(response, FALSE);
152
153         return SR_OK;
154 }
155
156 SR_PRIV int scpi_tcp_read(void *priv, char *buf, int maxlen)
157 {
158         struct scpi_tcp *tcp = priv;
159         int len;
160
161         len = recv(tcp->socket, buf, maxlen, 0);
162
163         if (len < 0) {
164                 sr_err("Receive error: %s", strerror(errno));
165                 return SR_ERR;
166         }
167
168         return len;
169 }
170
171 SR_PRIV int scpi_tcp_close(void *priv)
172 {
173         struct scpi_tcp *tcp = priv;
174
175         if (close(tcp->socket) < 0)
176                 return SR_ERR;
177
178         return SR_OK;
179 }
180
181 SR_PRIV void scpi_tcp_free(void *priv)
182 {
183         struct scpi_tcp *tcp = priv;
184
185         g_free(tcp->address);
186         g_free(tcp->port);
187         g_free(tcp);
188 }
189
190 SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address,
191                 const char *port)
192 {
193         struct sr_scpi_dev_inst *scpi;
194         struct scpi_tcp *tcp;
195
196         scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
197         tcp = g_malloc0(sizeof(struct scpi_tcp));
198
199         tcp->address = g_strdup(address);
200         tcp->port = g_strdup(port);
201         tcp->socket = -1;
202
203         scpi->open = scpi_tcp_open;
204         scpi->source_add = scpi_tcp_source_add;
205         scpi->source_remove = scpi_tcp_source_remove;
206         scpi->send = scpi_tcp_send;
207         scpi->receive = scpi_tcp_receive;
208         scpi->read = scpi_tcp_read;
209         scpi->close = scpi_tcp_close;
210         scpi->free = scpi_tcp_free;
211         scpi->priv = tcp;
212
213         return scpi;
214 }