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