]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_vxi.c
scpi_tcp: split into scpi_tcp_raw and scpi_tcp_rigol
[libsigrok.git] / hardware / common / 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
23#include <rpc/rpc.h>
24#include "vxi.h"
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27
28#define LOG_PREFIX "scpi_vxi"
29#define VXI_DEFAULT_TIMEOUT 2000 /* in ms */
30
31struct scpi_vxi {
32 char *address;
33 char *instrument;
34 CLIENT *client;
35 Device_Link link;
36 unsigned int max_send_size;
37 unsigned int read_complete;
38};
39
f754c146
AJ
40static int scpi_vxi_dev_inst_new(void *priv, const char *resource,
41 char **params, const char *serialcomm)
42{
43 struct scpi_vxi *vxi = priv;
44
45 (void)resource;
46 (void)serialcomm;
47
48 if (!params || !params[1]) {
49 sr_err("Invalid parameters.");
50 return SR_ERR;
51 }
52
53 vxi->address = g_strdup(params[1]);
54 vxi->instrument = g_strdup(params[2] ? params[2] : "inst0");
55
56 return SR_OK;
57}
58
d5876cfb
AJ
59static int scpi_vxi_open(void *priv)
60{
61 struct scpi_vxi *vxi = priv;
62 Create_LinkParms link_parms;
63 Create_LinkResp *link_resp;
64
65 vxi->client = clnt_create(vxi->address, DEVICE_CORE, DEVICE_CORE_VERSION, "tcp");
66 if (vxi->client == NULL) {
67 sr_err("Client creation failed for %s", vxi->address);
68 return SR_ERR;
69 }
70
71 /* Set link parameters */
72 link_parms.clientId = (long) vxi->client;
73 link_parms.lockDevice = 0;
74 link_parms.lock_timeout = VXI_DEFAULT_TIMEOUT;
75 link_parms.device = "inst0";
76
77 if (!(link_resp = create_link_1(&link_parms, vxi->client))) {
78 sr_err("Link creation failed for %s", vxi->address);
79 return SR_ERR;
80 }
81 vxi->link = link_resp->lid;
82 vxi->max_send_size = link_resp->maxRecvSize;
83
84 /* Set a default maxRecvSize for devices which do not specify it */
85 if (vxi->max_send_size <= 0)
86 vxi->max_send_size = 4096;
87
88 return SR_OK;
89}
90
91static int scpi_vxi_source_add(void *priv, int events, int timeout,
92 sr_receive_data_callback_t cb, void *cb_data)
93{
94 (void)priv;
95
96 /* Hook up a dummy handler to receive data from the device. */
97 return sr_source_add(-1, events, timeout, cb, cb_data);
98}
99
100static int scpi_vxi_source_remove(void *priv)
101{
102 (void)priv;
103
104 return sr_source_remove(-1);
105}
106
107/* Operation Flags */
108#define DF_WAITLOCK 0x01 /* wait if the operation is locked by another link */
109#define DF_END 0x08 /* an END indicator is sent with last byte of buffer */
110#define DF_TERM 0x80 /* a termination char is set during a read */
111
112static int scpi_vxi_send(void *priv, const char *command)
113{
114 struct scpi_vxi *vxi = priv;
115 Device_WriteResp *write_resp;
116 Device_WriteParms write_parms;
117 char *terminated_command;
118 unsigned int len;
119
120 terminated_command = g_strdup_printf("%s\r\n", command);
121 len = strlen(terminated_command);
122
123 write_parms.lid = vxi->link;
124 write_parms.io_timeout = VXI_DEFAULT_TIMEOUT;
125 write_parms.lock_timeout = VXI_DEFAULT_TIMEOUT;
126 write_parms.flags = DF_END;
127 write_parms.data.data_len = MIN(len, vxi->max_send_size);
128 write_parms.data.data_val = terminated_command;
129
130 if (!(write_resp = device_write_1(&write_parms, vxi->client))
131 || write_resp->error) {
132 sr_err("Device write failed for %s with error %d",
133 vxi->address, write_resp->error);
134 return SR_ERR;
135 }
136
137 g_free(terminated_command);
138
139 if (write_resp->size < len)
140 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.",
141 write_resp->size, len, command);
142 else
143 sr_spew("Successfully sent SCPI command: '%s'.", command);
144
145 return SR_OK;
146}
147
148static int scpi_vxi_read_begin(void *priv)
149{
150 struct scpi_vxi *vxi = priv;
151
152 vxi->read_complete = 0;
153
154 return SR_OK;
155}
156
157/* Read Response Reason Flags */
158#define RRR_SIZE 0x01 /* requestSize bytes have been transferred */
159#define RRR_TERM 0x02 /* a termination char has been read */
160#define RRR_END 0x04 /* an END indicator has been read */
161
162static int scpi_vxi_read_data(void *priv, char *buf, int maxlen)
163{
164 struct scpi_vxi *vxi = priv;
165 Device_ReadParms read_parms;
166 Device_ReadResp *read_resp;
167
168 read_parms.lid = vxi->link;
169 read_parms.io_timeout = VXI_DEFAULT_TIMEOUT;
170 read_parms.lock_timeout = VXI_DEFAULT_TIMEOUT;
171 read_parms.flags = 0;
172 read_parms.termChar = 0;
173 read_parms.requestSize = maxlen;
174
175 if (!(read_resp = device_read_1(&read_parms, vxi->client))
176 || read_resp->error) {
177 sr_err("Device read failed for %s with error %d",
178 vxi->address, read_resp->error);
179 return SR_ERR;
180 }
181
182 memcpy(buf, read_resp->data.data_val, read_resp->data.data_len);
183 vxi->read_complete = read_resp->reason & (RRR_SIZE | RRR_TERM | RRR_END);
184 return read_resp->data.data_len; /* actual number of bytes received */
185}
186
187static int scpi_vxi_read_complete(void *priv)
188{
189 struct scpi_vxi *vxi = priv;
190
191 return vxi->read_complete;
192}
193
194static int scpi_vxi_close(void *priv)
195{
196 struct scpi_vxi *vxi = priv;
197 Device_Error *dev_error;
198
199 if (!(dev_error = destroy_link_1(&vxi->link, vxi->client))) {
200 sr_err("Link destruction failed for %s", vxi->address);
201 return SR_ERR;
202 }
203
204 clnt_destroy(vxi->client);
205
206 return SR_OK;
207}
208
209static void scpi_vxi_free(void *priv)
210{
211 struct scpi_vxi *vxi = priv;
212
213 g_free(vxi->address);
214 g_free(vxi->instrument);
d5876cfb
AJ
215}
216
f754c146
AJ
217SR_PRIV const struct sr_scpi_dev_inst scpi_vxi_dev = {
218 .name = "VXI",
219 .prefix = "vxi",
220 .priv_size = sizeof(struct scpi_vxi),
221 .dev_inst_new = scpi_vxi_dev_inst_new,
222 .open = scpi_vxi_open,
223 .source_add = scpi_vxi_source_add,
224 .source_remove = scpi_vxi_source_remove,
225 .send = scpi_vxi_send,
226 .read_begin = scpi_vxi_read_begin,
227 .read_data = scpi_vxi_read_data,
228 .read_complete = scpi_vxi_read_complete,
229 .close = scpi_vxi_close,
230 .free = scpi_vxi_free,
231};