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