]>
Commit | Line | Data |
---|---|---|
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 "config.h" | |
21 | ||
22 | #include <errno.h> | |
23 | #include <glib.h> | |
24 | #include <libsigrok/libsigrok.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include "libsigrok-internal.h" | |
28 | #include "scpi.h" | |
29 | ||
30 | #define LOG_PREFIX "scpi_tcp" | |
31 | ||
32 | #define LENGTH_BYTES sizeof(uint32_t) | |
33 | ||
34 | struct scpi_tcp { | |
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; | |
40 | }; | |
41 | ||
42 | static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc, | |
43 | const char *resource, char **params, const char *serialcomm) | |
44 | { | |
45 | struct scpi_tcp *tcp = priv; | |
46 | ||
47 | (void)drvc; | |
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 | ||
56 | tcp->tcp_dev = sr_tcp_dev_inst_new(params[1], params[2]); | |
57 | if (!tcp->tcp_dev) | |
58 | return SR_ERR; | |
59 | ||
60 | return SR_OK; | |
61 | } | |
62 | ||
63 | static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi) | |
64 | { | |
65 | struct scpi_tcp *tcp = scpi->priv; | |
66 | int ret; | |
67 | ||
68 | ret = sr_tcp_connect(tcp->tcp_dev); | |
69 | if (ret != SR_OK) | |
70 | return ret; | |
71 | ||
72 | return SR_OK; | |
73 | } | |
74 | ||
75 | static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi, | |
76 | char **connection_id) | |
77 | { | |
78 | struct scpi_tcp *tcp = scpi->priv; | |
79 | char conn_text[128]; | |
80 | int ret; | |
81 | ||
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; | |
86 | ||
87 | *connection_id = g_strdup(conn_text); | |
88 | return SR_OK; | |
89 | } | |
90 | ||
91 | static int scpi_tcp_source_add(struct sr_session *session, void *priv, | |
92 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
93 | { | |
94 | struct scpi_tcp *tcp = priv; | |
95 | ||
96 | return sr_tcp_source_add(session, tcp->tcp_dev, | |
97 | events, timeout, cb, cb_data); | |
98 | } | |
99 | ||
100 | static int scpi_tcp_source_remove(struct sr_session *session, void *priv) | |
101 | { | |
102 | struct scpi_tcp *tcp = priv; | |
103 | ||
104 | return sr_tcp_source_remove(session, tcp->tcp_dev); | |
105 | } | |
106 | ||
107 | /* Transmit text, usually a command. tcp-raw and tcp-rigol modes. */ | |
108 | static int scpi_tcp_send(void *priv, const char *command) | |
109 | { | |
110 | struct scpi_tcp *tcp = priv; | |
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) { | |
119 | sr_err("Send error: %s", g_strerror(errno)); | |
120 | return SR_ERR; | |
121 | } | |
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); | |
126 | } | |
127 | ||
128 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
129 | ||
130 | return SR_OK; | |
131 | } | |
132 | ||
133 | /* Start reception across multiple read calls. tcp-raw and tcp-rigol modes. */ | |
134 | static int scpi_tcp_read_begin(void *priv) | |
135 | { | |
136 | struct scpi_tcp *tcp = priv; | |
137 | ||
138 | tcp->response_bytes_read = 0; | |
139 | tcp->length_bytes_read = 0; | |
140 | ||
141 | return SR_OK; | |
142 | } | |
143 | ||
144 | /* Receive response data. tcp-raw mode. */ | |
145 | static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen) | |
146 | { | |
147 | struct scpi_tcp *tcp = priv; | |
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) { | |
157 | sr_err("Receive error: %s", g_strerror(errno)); | |
158 | return SR_ERR; | |
159 | } | |
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 | */ | |
170 | tcp->length_bytes_read = LENGTH_BYTES; | |
171 | tcp->response_length = rcvd < rdlen ? rcvd : rdlen + 1; | |
172 | tcp->response_bytes_read = rcvd; | |
173 | ||
174 | return rcvd; | |
175 | } | |
176 | ||
177 | /* Transmit data of given length. tcp-raw mode. */ | |
178 | static int scpi_tcp_raw_write_data(void *priv, char *buf, int len) | |
179 | { | |
180 | struct scpi_tcp *tcp = priv; | |
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) { | |
189 | sr_err("Send error: %s.", g_strerror(errno)); | |
190 | return SR_ERR; | |
191 | } | |
192 | sent = (size_t)ret; | |
193 | ||
194 | return sent; | |
195 | } | |
196 | ||
197 | /* Receive response data. tcp-rigol mode. */ | |
198 | static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen) | |
199 | { | |
200 | struct scpi_tcp *tcp = priv; | |
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) { | |
216 | sr_err("Receive error: %s", g_strerror(errno)); | |
217 | return SR_ERR; | |
218 | } | |
219 | rcvd = (size_t)ret; | |
220 | tcp->length_bytes_read += rcvd; | |
221 | if (tcp->length_bytes_read < sizeof(tcp->length_buf)) | |
222 | return 0; | |
223 | tcp->response_length = read_u32le(tcp->length_buf); | |
224 | } | |
225 | ||
226 | /* Received more chunk data than announced size? Fatal. */ | |
227 | if (tcp->response_bytes_read >= tcp->response_length) | |
228 | return SR_ERR; | |
229 | ||
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) { | |
235 | sr_err("Receive error: %s", g_strerror(errno)); | |
236 | return SR_ERR; | |
237 | } | |
238 | rcvd = (size_t)ret; | |
239 | tcp->response_bytes_read += rcvd; | |
240 | ||
241 | return rcvd; | |
242 | } | |
243 | ||
244 | /* Check reception completion. tcp-raw and tcp-rigol modes. */ | |
245 | static int scpi_tcp_read_complete(void *priv) | |
246 | { | |
247 | struct scpi_tcp *tcp = priv; | |
248 | gboolean have_length, have_response; | |
249 | ||
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; | |
254 | } | |
255 | ||
256 | static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi) | |
257 | { | |
258 | struct scpi_tcp *tcp = scpi->priv; | |
259 | int ret; | |
260 | ||
261 | ret = sr_tcp_disconnect(tcp->tcp_dev); | |
262 | if (ret != SR_OK) | |
263 | return ret; | |
264 | ||
265 | return SR_OK; | |
266 | } | |
267 | ||
268 | static void scpi_tcp_free(void *priv) | |
269 | { | |
270 | struct scpi_tcp *tcp = priv; | |
271 | ||
272 | sr_tcp_dev_inst_free(tcp->tcp_dev); | |
273 | } | |
274 | ||
275 | SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = { | |
276 | .name = "RAW TCP", | |
277 | .prefix = "tcp-raw", | |
278 | .transport = SCPI_TRANSPORT_RAW_TCP, | |
279 | .priv_size = sizeof(struct scpi_tcp), | |
280 | .dev_inst_new = scpi_tcp_dev_inst_new, | |
281 | .open = scpi_tcp_open, | |
282 | .connection_id = scpi_tcp_connection_id, | |
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, | |
288 | .write_data = scpi_tcp_raw_write_data, | |
289 | .read_complete = scpi_tcp_read_complete, | |
290 | .close = scpi_tcp_close, | |
291 | .free = scpi_tcp_free, | |
292 | }; | |
293 | ||
294 | SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = { | |
295 | .name = "RIGOL TCP", | |
296 | .prefix = "tcp-rigol", | |
297 | .transport = SCPI_TRANSPORT_RIGOL_TCP, | |
298 | .priv_size = sizeof(struct scpi_tcp), | |
299 | .dev_inst_new = scpi_tcp_dev_inst_new, | |
300 | .open = scpi_tcp_open, | |
301 | .connection_id = scpi_tcp_connection_id, | |
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, | |
306 | .read_data = scpi_tcp_rigol_read_data, | |
307 | .read_complete = scpi_tcp_read_complete, | |
308 | .close = scpi_tcp_close, | |
309 | .free = scpi_tcp_free, | |
310 | }; |