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