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