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