]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_tcp.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / scpi / scpi_tcp.c
CommitLineData
08a35913
ML
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
4fd49223
GS
20#include "config.h"
21
08a35913 22#include <errno.h>
4fd49223 23#include <glib.h>
c1aae900 24#include <libsigrok/libsigrok.h>
4fd49223
GS
25#include <string.h>
26
515ab088 27#include "libsigrok-internal.h"
5a1afc09 28#include "scpi.h"
08a35913 29
3544f848 30#define LOG_PREFIX "scpi_tcp"
08a35913 31
4fd49223 32#define LENGTH_BYTES sizeof(uint32_t)
05c644ea 33
08a35913 34struct scpi_tcp {
4fd49223
GS
35 struct sr_tcp_dev_inst *tcp_dev;
36 uint8_t length_buf[LENGTH_BYTES];
37 size_t length_bytes_read;
38 size_t response_length;
39 size_t response_bytes_read;
08a35913
ML
40};
41
17bdda58
AJ
42static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
43 const char *resource, char **params, const char *serialcomm)
f754c146
AJ
44{
45 struct scpi_tcp *tcp = priv;
46
17bdda58 47 (void)drvc;
f754c146
AJ
48 (void)resource;
49 (void)serialcomm;
50
51 if (!params || !params[1] || !params[2]) {
52 sr_err("Invalid parameters.");
53 return SR_ERR;
54 }
55
4fd49223
GS
56 tcp->tcp_dev = sr_tcp_dev_inst_new(params[1], params[2]);
57 if (!tcp->tcp_dev)
58 return SR_ERR;
f754c146
AJ
59
60 return SR_OK;
61}
62
04229f7b 63static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi)
08a35913 64{
04229f7b 65 struct scpi_tcp *tcp = scpi->priv;
4fd49223 66 int ret;
08a35913 67
4fd49223
GS
68 ret = sr_tcp_connect(tcp->tcp_dev);
69 if (ret != SR_OK)
70 return ret;
08a35913
ML
71
72 return SR_OK;
73}
74
8107a9a6
FS
75static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi,
76 char **connection_id)
77{
78 struct scpi_tcp *tcp = scpi->priv;
4fd49223
GS
79 char conn_text[128];
80 int ret;
8107a9a6 81
4fd49223
GS
82 ret = sr_tcp_get_port_path(tcp->tcp_dev, scpi->prefix, '/',
83 conn_text, sizeof(conn_text));
84 if (ret != SR_OK)
85 return ret;
8107a9a6 86
4fd49223 87 *connection_id = g_strdup(conn_text);
8107a9a6
FS
88 return SR_OK;
89}
90
102f1239
BV
91static int scpi_tcp_source_add(struct sr_session *session, void *priv,
92 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
08a35913
ML
93{
94 struct scpi_tcp *tcp = priv;
95
4fd49223
GS
96 return sr_tcp_source_add(session, tcp->tcp_dev,
97 events, timeout, cb, cb_data);
08a35913
ML
98}
99
102f1239 100static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
08a35913
ML
101{
102 struct scpi_tcp *tcp = priv;
103
4fd49223 104 return sr_tcp_source_remove(session, tcp->tcp_dev);
08a35913
ML
105}
106
4fd49223 107/* Transmit text, usually a command. tcp-raw and tcp-rigol modes. */
d87c1766 108static int scpi_tcp_send(void *priv, const char *command)
08a35913
ML
109{
110 struct scpi_tcp *tcp = priv;
4fd49223
GS
111 const uint8_t *wrptr;
112 size_t wrlen, written;
113 int ret;
114
115 wrptr = (const uint8_t *)command;
116 wrlen = strlen(command);
117 ret = sr_tcp_write_bytes(tcp->tcp_dev, wrptr, wrlen);
118 if (ret < 0) {
7237e912 119 sr_err("Send error: %s", g_strerror(errno));
08a35913
ML
120 return SR_ERR;
121 }
4fd49223
GS
122 written = (size_t)ret;
123 if (written < wrlen) {
124 sr_dbg("Only sent %zu/%zu bytes of SCPI command: '%s'.",
125 written, wrlen, command);
08a35913
ML
126 }
127
128 sr_spew("Successfully sent SCPI command: '%s'.", command);
129
130 return SR_OK;
131}
132
4fd49223 133/* Start reception across multiple read calls. tcp-raw and tcp-rigol modes. */
d87c1766 134static int scpi_tcp_read_begin(void *priv)
08a35913
ML
135{
136 struct scpi_tcp *tcp = priv;
08a35913 137
05c644ea
ML
138 tcp->response_bytes_read = 0;
139 tcp->length_bytes_read = 0;
08a35913
ML
140
141 return SR_OK;
142}
143
4fd49223 144/* Receive response data. tcp-raw mode. */
d87c1766 145static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
104ed125
AJ
146{
147 struct scpi_tcp *tcp = priv;
4fd49223
GS
148 uint8_t *rdptr;
149 size_t rdlen, rcvd;
150 int ret;
151
152 /* Get another chunk of receive data. */
153 rdptr = (uint8_t *)buf;
154 rdlen = maxlen;
155 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
156 if (ret < 0) {
7237e912 157 sr_err("Receive error: %s", g_strerror(errno));
104ed125
AJ
158 return SR_ERR;
159 }
4fd49223
GS
160 rcvd = (size_t)ret;
161
162 /*
163 * Raw data mode (in contrast to Rigol mode). Prepare to answer
164 * the "completed" condition while the payload's length is not
165 * known. Pretend that the length buffer had been received.
166 * Assume that short reads correspond to the end of a response,
167 * while full reads of the caller specified size suggest that
168 * more data can follow.
169 */
104ed125 170 tcp->length_bytes_read = LENGTH_BYTES;
4fd49223
GS
171 tcp->response_length = rcvd < rdlen ? rcvd : rdlen + 1;
172 tcp->response_bytes_read = rcvd;
104ed125 173
4fd49223 174 return rcvd;
104ed125
AJ
175}
176
4fd49223 177/* Transmit data of given length. tcp-raw mode. */
b6be55ce
SS
178static int scpi_tcp_raw_write_data(void *priv, char *buf, int len)
179{
180 struct scpi_tcp *tcp = priv;
4fd49223
GS
181 const uint8_t *wrptr;
182 size_t wrlen, sent;
183 int ret;
184
185 wrptr = (const uint8_t *)buf;
186 wrlen = len;
187 ret = sr_tcp_write_bytes(tcp->tcp_dev, wrptr, wrlen);
188 if (ret < 0) {
b6be55ce
SS
189 sr_err("Send error: %s.", g_strerror(errno));
190 return SR_ERR;
191 }
4fd49223 192 sent = (size_t)ret;
b6be55ce 193
4fd49223 194 return sent;
b6be55ce
SS
195}
196
4fd49223 197/* Receive response data. tcp-rigol mode. */
d87c1766 198static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
08a35913
ML
199{
200 struct scpi_tcp *tcp = priv;
4fd49223
GS
201 uint8_t *rdptr;
202 size_t rdlen, rcvd;
203 int ret;
204
205 /*
206 * Rigol mode, chunks are prefixed by a length spec.
207 * Get more length bytes when we haven't read them before.
208 * Return "zero length read" if length has yet to get received.
209 * Otherwise get chunk length from length bytes buffer.
210 */
211 if (tcp->length_bytes_read < sizeof(tcp->length_buf)) {
212 rdptr = &tcp->length_buf[tcp->length_bytes_read];
213 rdlen = sizeof(tcp->length_buf) - tcp->length_bytes_read;
214 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
215 if (ret < 0) {
7237e912 216 sr_err("Receive error: %s", g_strerror(errno));
05c644ea
ML
217 return SR_ERR;
218 }
4fd49223
GS
219 rcvd = (size_t)ret;
220 tcp->length_bytes_read += rcvd;
221 if (tcp->length_bytes_read < sizeof(tcp->length_buf))
05c644ea 222 return 0;
4fd49223 223 tcp->response_length = read_u32le(tcp->length_buf);
05c644ea
ML
224 }
225
4fd49223 226 /* Received more chunk data than announced size? Fatal. */
05c644ea
ML
227 if (tcp->response_bytes_read >= tcp->response_length)
228 return SR_ERR;
229
4fd49223
GS
230 /* Read another chunk of the receive data. */
231 rdptr = (uint8_t *)buf;
232 rdlen = maxlen;
233 ret = sr_tcp_read_bytes(tcp->tcp_dev, rdptr, rdlen, FALSE);
234 if (ret < 0) {
7237e912 235 sr_err("Receive error: %s", g_strerror(errno));
08a35913
ML
236 return SR_ERR;
237 }
4fd49223
GS
238 rcvd = (size_t)ret;
239 tcp->response_bytes_read += rcvd;
08a35913 240
4fd49223 241 return rcvd;
08a35913
ML
242}
243
4fd49223 244/* Check reception completion. tcp-raw and tcp-rigol modes. */
d87c1766 245static int scpi_tcp_read_complete(void *priv)
05c644ea
ML
246{
247 struct scpi_tcp *tcp = priv;
4fd49223 248 gboolean have_length, have_response;
05c644ea 249
4fd49223
GS
250 have_length = tcp->length_bytes_read == LENGTH_BYTES;
251 have_response = tcp->response_bytes_read >= tcp->response_length;
252
253 return have_length && have_response;
05c644ea
ML
254}
255
04229f7b 256static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi)
08a35913 257{
04229f7b 258 struct scpi_tcp *tcp = scpi->priv;
4fd49223 259 int ret;
08a35913 260
4fd49223
GS
261 ret = sr_tcp_disconnect(tcp->tcp_dev);
262 if (ret != SR_OK)
263 return ret;
08a35913
ML
264
265 return SR_OK;
266}
267
d87c1766 268static void scpi_tcp_free(void *priv)
08a35913
ML
269{
270 struct scpi_tcp *tcp = priv;
271
4fd49223 272 sr_tcp_dev_inst_free(tcp->tcp_dev);
08a35913
ML
273}
274
104ed125
AJ
275SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
276 .name = "RAW TCP",
277 .prefix = "tcp-raw",
87aa1e63 278 .transport = SCPI_TRANSPORT_RAW_TCP,
104ed125
AJ
279 .priv_size = sizeof(struct scpi_tcp),
280 .dev_inst_new = scpi_tcp_dev_inst_new,
281 .open = scpi_tcp_open,
8107a9a6 282 .connection_id = scpi_tcp_connection_id,
104ed125
AJ
283 .source_add = scpi_tcp_source_add,
284 .source_remove = scpi_tcp_source_remove,
285 .send = scpi_tcp_send,
286 .read_begin = scpi_tcp_read_begin,
287 .read_data = scpi_tcp_raw_read_data,
b6be55ce 288 .write_data = scpi_tcp_raw_write_data,
104ed125
AJ
289 .read_complete = scpi_tcp_read_complete,
290 .close = scpi_tcp_close,
291 .free = scpi_tcp_free,
292};
293
294SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
295 .name = "RIGOL TCP",
296 .prefix = "tcp-rigol",
87aa1e63 297 .transport = SCPI_TRANSPORT_RIGOL_TCP,
f754c146
AJ
298 .priv_size = sizeof(struct scpi_tcp),
299 .dev_inst_new = scpi_tcp_dev_inst_new,
300 .open = scpi_tcp_open,
8107a9a6 301 .connection_id = scpi_tcp_connection_id,
f754c146
AJ
302 .source_add = scpi_tcp_source_add,
303 .source_remove = scpi_tcp_source_remove,
304 .send = scpi_tcp_send,
305 .read_begin = scpi_tcp_read_begin,
104ed125 306 .read_data = scpi_tcp_rigol_read_data,
f754c146
AJ
307 .read_complete = scpi_tcp_read_complete,
308 .close = scpi_tcp_close,
309 .free = scpi_tcp_free,
310};