]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_vxi.c
scpi_vxi: fix memory leak for SCPI response data in VXI support code
[libsigrok.git] / src / scpi / scpi_vxi.c
CommitLineData
d5876cfb
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * Inspired by the VXI11 Ethernet Protocol for Linux:
7 * http://optics.eee.nottingham.ac.uk/vxi11/
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
6ec6c43b 23#include <config.h>
d5876cfb 24#include <rpc/rpc.h>
dc05dd60 25#include <string.h>
c1aae900 26#include <libsigrok/libsigrok.h>
d5876cfb 27#include "libsigrok-internal.h"
5a1afc09 28#include "scpi.h"
7414fb55 29#include "vxi.h"
d5876cfb
AJ
30
31#define LOG_PREFIX "scpi_vxi"
1a46cc62 32#define VXI_DEFAULT_TIMEOUT_MS 2000
d5876cfb
AJ
33
34struct scpi_vxi {
35 char *address;
36 char *instrument;
37 CLIENT *client;
38 Device_Link link;
39 unsigned int max_send_size;
40 unsigned int read_complete;
41};
42
17bdda58
AJ
43static int scpi_vxi_dev_inst_new(void *priv, struct drv_context *drvc,
44 const char *resource, char **params, const char *serialcomm)
f754c146 45{
7414fb55 46 struct scpi_vxi *vxi;
f754c146 47
17bdda58 48 (void)drvc;
f754c146
AJ
49 (void)resource;
50 (void)serialcomm;
51
52 if (!params || !params[1]) {
53 sr_err("Invalid parameters.");
54 return SR_ERR;
55 }
56
7414fb55 57 vxi = priv;
f754c146
AJ
58 vxi->address = g_strdup(params[1]);
59 vxi->instrument = g_strdup(params[2] ? params[2] : "inst0");
60
61 return SR_OK;
62}
63
04229f7b 64static int scpi_vxi_open(struct sr_scpi_dev_inst *scpi)
d5876cfb 65{
7414fb55 66 struct scpi_vxi *vxi;
d5876cfb
AJ
67 Create_LinkParms link_parms;
68 Create_LinkResp *link_resp;
69
7414fb55
GS
70 vxi = scpi->priv;
71 vxi->client = clnt_create(vxi->address,
72 DEVICE_CORE, DEVICE_CORE_VERSION, "tcp");
98fec29e 73 if (!vxi->client) {
d5876cfb
AJ
74 sr_err("Client creation failed for %s", vxi->address);
75 return SR_ERR;
76 }
77
78 /* Set link parameters */
79 link_parms.clientId = (long) vxi->client;
80 link_parms.lockDevice = 0;
1a46cc62 81 link_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS;
2c240774 82 link_parms.device = (char *)"inst0";
d5876cfb 83
7414fb55
GS
84 link_resp = create_link_1(&link_parms, vxi->client);
85 if (!link_resp) {
d5876cfb
AJ
86 sr_err("Link creation failed for %s", vxi->address);
87 return SR_ERR;
88 }
89 vxi->link = link_resp->lid;
90 vxi->max_send_size = link_resp->maxRecvSize;
91
92 /* Set a default maxRecvSize for devices which do not specify it */
93 if (vxi->max_send_size <= 0)
94 vxi->max_send_size = 4096;
95
96 return SR_OK;
97}
98
8107a9a6
FS
99static int scpi_vxi_connection_id(struct sr_scpi_dev_inst *scpi,
100 char **connection_id)
101{
7414fb55 102 struct scpi_vxi *vxi;
8107a9a6 103
7414fb55 104 vxi = scpi->priv;
8107a9a6
FS
105 *connection_id = g_strdup_printf("%s/%s", scpi->prefix, vxi->address);
106
107 return SR_OK;
108}
109
102f1239
BV
110static int scpi_vxi_source_add(struct sr_session *session, void *priv,
111 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
d5876cfb
AJ
112{
113 (void)priv;
114
115 /* Hook up a dummy handler to receive data from the device. */
102f1239 116 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
d5876cfb
AJ
117}
118
102f1239 119static int scpi_vxi_source_remove(struct sr_session *session, void *priv)
d5876cfb
AJ
120{
121 (void)priv;
122
102f1239 123 return sr_session_source_remove(session, -1);
d5876cfb
AJ
124}
125
126/* Operation Flags */
127#define DF_WAITLOCK 0x01 /* wait if the operation is locked by another link */
128#define DF_END 0x08 /* an END indicator is sent with last byte of buffer */
129#define DF_TERM 0x80 /* a termination char is set during a read */
130
131static int scpi_vxi_send(void *priv, const char *command)
132{
7414fb55 133 struct scpi_vxi *vxi;
d5876cfb
AJ
134 Device_WriteResp *write_resp;
135 Device_WriteParms write_parms;
6433156c 136 unsigned long len;
d5876cfb 137
055804e8 138 len = strlen(command);
d5876cfb 139
7414fb55 140 vxi = priv;
d5876cfb 141 write_parms.lid = vxi->link;
1a46cc62
UH
142 write_parms.io_timeout = VXI_DEFAULT_TIMEOUT_MS;
143 write_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS;
d5876cfb
AJ
144 write_parms.flags = DF_END;
145 write_parms.data.data_len = MIN(len, vxi->max_send_size);
f0729866 146 write_parms.data.data_val = (char *)command;
d5876cfb 147
7414fb55
GS
148 write_resp = device_write_1(&write_parms, vxi->client);
149 if (!write_resp || write_resp->error) {
6433156c 150 sr_err("Device write failed for %s with error %ld",
f3616a08 151 vxi->address, write_resp ? write_resp->error : 0);
d5876cfb
AJ
152 return SR_ERR;
153 }
154
7414fb55 155 if (write_resp->size < len) {
6433156c 156 sr_dbg("Only sent %lu/%lu bytes of SCPI command: '%s'.",
d5876cfb 157 write_resp->size, len, command);
7414fb55 158 } else {
d5876cfb 159 sr_spew("Successfully sent SCPI command: '%s'.", command);
7414fb55 160 }
d5876cfb
AJ
161
162 return SR_OK;
163}
164
165static int scpi_vxi_read_begin(void *priv)
166{
7414fb55 167 struct scpi_vxi *vxi;
d5876cfb 168
7414fb55 169 vxi = priv;
d5876cfb
AJ
170 vxi->read_complete = 0;
171
172 return SR_OK;
173}
174
175/* Read Response Reason Flags */
176#define RRR_SIZE 0x01 /* requestSize bytes have been transferred */
177#define RRR_TERM 0x02 /* a termination char has been read */
178#define RRR_END 0x04 /* an END indicator has been read */
179
180static int scpi_vxi_read_data(void *priv, char *buf, int maxlen)
181{
7414fb55 182 struct scpi_vxi *vxi;
d5876cfb
AJ
183 Device_ReadParms read_parms;
184 Device_ReadResp *read_resp;
185
7414fb55 186 vxi = priv;
d5876cfb 187 read_parms.lid = vxi->link;
1a46cc62
UH
188 read_parms.io_timeout = VXI_DEFAULT_TIMEOUT_MS;
189 read_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS;
d5876cfb
AJ
190 read_parms.flags = 0;
191 read_parms.termChar = 0;
192 read_parms.requestSize = maxlen;
193
7414fb55
GS
194 read_resp = device_read_1(&read_parms, vxi->client);
195 if (!read_resp || read_resp->error) {
6433156c 196 sr_err("Device read failed for %s with error %ld",
f3616a08 197 vxi->address, read_resp ? read_resp->error : 0);
ed787682
DA
198 if (read_resp) {
199 g_free(read_resp->data.data_val);
200 read_resp->data.data_val = NULL;
201 }
d5876cfb
AJ
202 return SR_ERR;
203 }
204
205 memcpy(buf, read_resp->data.data_val, read_resp->data.data_len);
ed787682
DA
206 g_free(read_resp->data.data_val);
207 read_resp->data.data_val = NULL;
b951fae3 208 vxi->read_complete = read_resp->reason & (RRR_TERM | RRR_END);
d5876cfb
AJ
209 return read_resp->data.data_len; /* actual number of bytes received */
210}
211
212static int scpi_vxi_read_complete(void *priv)
213{
214 struct scpi_vxi *vxi = priv;
215
216 return vxi->read_complete;
217}
218
04229f7b 219static int scpi_vxi_close(struct sr_scpi_dev_inst *scpi)
d5876cfb 220{
7414fb55 221 struct scpi_vxi *vxi;
d5876cfb
AJ
222 Device_Error *dev_error;
223
7414fb55 224 vxi = scpi->priv;
dc3b3be5
AJ
225 if (!vxi->client)
226 return SR_ERR;
227
7414fb55
GS
228 dev_error = destroy_link_1(&vxi->link, vxi->client);
229 if (!dev_error) {
d5876cfb
AJ
230 sr_err("Link destruction failed for %s", vxi->address);
231 return SR_ERR;
232 }
233
234 clnt_destroy(vxi->client);
dc3b3be5 235 vxi->client = NULL;
d5876cfb
AJ
236
237 return SR_OK;
238}
239
240static void scpi_vxi_free(void *priv)
241{
7414fb55 242 struct scpi_vxi *vxi;
d5876cfb 243
7414fb55 244 vxi = priv;
d5876cfb
AJ
245 g_free(vxi->address);
246 g_free(vxi->instrument);
d5876cfb
AJ
247}
248
f754c146
AJ
249SR_PRIV const struct sr_scpi_dev_inst scpi_vxi_dev = {
250 .name = "VXI",
251 .prefix = "vxi",
87aa1e63 252 .transport = SCPI_TRANSPORT_VXI,
f754c146
AJ
253 .priv_size = sizeof(struct scpi_vxi),
254 .dev_inst_new = scpi_vxi_dev_inst_new,
255 .open = scpi_vxi_open,
8107a9a6 256 .connection_id = scpi_vxi_connection_id,
f754c146
AJ
257 .source_add = scpi_vxi_source_add,
258 .source_remove = scpi_vxi_source_remove,
259 .send = scpi_vxi_send,
260 .read_begin = scpi_vxi_read_begin,
261 .read_data = scpi_vxi_read_data,
262 .read_complete = scpi_vxi_read_complete,
263 .close = scpi_vxi_close,
264 .free = scpi_vxi_free,
265};