]>
Commit | Line | Data |
---|---|---|
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> |
d40b8557 | 24 | #include "vxi.h" |
d5876cfb | 25 | #include <rpc/rpc.h> |
dc05dd60 | 26 | #include <string.h> |
c1aae900 | 27 | #include <libsigrok/libsigrok.h> |
d5876cfb | 28 | #include "libsigrok-internal.h" |
5a1afc09 | 29 | #include "scpi.h" |
d5876cfb AJ |
30 | |
31 | #define LOG_PREFIX "scpi_vxi" | |
1a46cc62 | 32 | #define VXI_DEFAULT_TIMEOUT_MS 2000 |
d5876cfb AJ |
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 | ||
17bdda58 AJ |
43 | static int scpi_vxi_dev_inst_new(void *priv, struct drv_context *drvc, |
44 | const char *resource, char **params, const char *serialcomm) | |
f754c146 AJ |
45 | { |
46 | struct scpi_vxi *vxi = priv; | |
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 | ||
57 | vxi->address = g_strdup(params[1]); | |
58 | vxi->instrument = g_strdup(params[2] ? params[2] : "inst0"); | |
59 | ||
60 | return SR_OK; | |
61 | } | |
62 | ||
04229f7b | 63 | static int scpi_vxi_open(struct sr_scpi_dev_inst *scpi) |
d5876cfb | 64 | { |
04229f7b | 65 | struct scpi_vxi *vxi = scpi->priv; |
d5876cfb AJ |
66 | Create_LinkParms link_parms; |
67 | Create_LinkResp *link_resp; | |
68 | ||
69 | vxi->client = clnt_create(vxi->address, DEVICE_CORE, DEVICE_CORE_VERSION, "tcp"); | |
98fec29e | 70 | if (!vxi->client) { |
d5876cfb AJ |
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; | |
1a46cc62 | 78 | link_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS; |
2c240774 | 79 | link_parms.device = (char *)"inst0"; |
d5876cfb AJ |
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 | ||
102f1239 BV |
95 | static int scpi_vxi_source_add(struct sr_session *session, void *priv, |
96 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
d5876cfb AJ |
97 | { |
98 | (void)priv; | |
99 | ||
100 | /* Hook up a dummy handler to receive data from the device. */ | |
102f1239 | 101 | return sr_session_source_add(session, -1, events, timeout, cb, cb_data); |
d5876cfb AJ |
102 | } |
103 | ||
102f1239 | 104 | static int scpi_vxi_source_remove(struct sr_session *session, void *priv) |
d5876cfb AJ |
105 | { |
106 | (void)priv; | |
107 | ||
102f1239 | 108 | return sr_session_source_remove(session, -1); |
d5876cfb AJ |
109 | } |
110 | ||
111 | /* Operation Flags */ | |
112 | #define DF_WAITLOCK 0x01 /* wait if the operation is locked by another link */ | |
113 | #define DF_END 0x08 /* an END indicator is sent with last byte of buffer */ | |
114 | #define DF_TERM 0x80 /* a termination char is set during a read */ | |
115 | ||
116 | static int scpi_vxi_send(void *priv, const char *command) | |
117 | { | |
118 | struct scpi_vxi *vxi = priv; | |
119 | Device_WriteResp *write_resp; | |
120 | Device_WriteParms write_parms; | |
121 | char *terminated_command; | |
6433156c | 122 | unsigned long len; |
d5876cfb AJ |
123 | |
124 | terminated_command = g_strdup_printf("%s\r\n", command); | |
125 | len = strlen(terminated_command); | |
126 | ||
127 | write_parms.lid = vxi->link; | |
1a46cc62 UH |
128 | write_parms.io_timeout = VXI_DEFAULT_TIMEOUT_MS; |
129 | write_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS; | |
d5876cfb AJ |
130 | write_parms.flags = DF_END; |
131 | write_parms.data.data_len = MIN(len, vxi->max_send_size); | |
132 | write_parms.data.data_val = terminated_command; | |
133 | ||
134 | if (!(write_resp = device_write_1(&write_parms, vxi->client)) | |
135 | || write_resp->error) { | |
6433156c | 136 | sr_err("Device write failed for %s with error %ld", |
f3616a08 | 137 | vxi->address, write_resp ? write_resp->error : 0); |
d5876cfb AJ |
138 | return SR_ERR; |
139 | } | |
140 | ||
141 | g_free(terminated_command); | |
142 | ||
143 | if (write_resp->size < len) | |
6433156c | 144 | sr_dbg("Only sent %lu/%lu bytes of SCPI command: '%s'.", |
d5876cfb AJ |
145 | write_resp->size, len, command); |
146 | else | |
147 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
148 | ||
149 | return SR_OK; | |
150 | } | |
151 | ||
152 | static int scpi_vxi_read_begin(void *priv) | |
153 | { | |
154 | struct scpi_vxi *vxi = priv; | |
155 | ||
156 | vxi->read_complete = 0; | |
157 | ||
158 | return SR_OK; | |
159 | } | |
160 | ||
161 | /* Read Response Reason Flags */ | |
162 | #define RRR_SIZE 0x01 /* requestSize bytes have been transferred */ | |
163 | #define RRR_TERM 0x02 /* a termination char has been read */ | |
164 | #define RRR_END 0x04 /* an END indicator has been read */ | |
165 | ||
166 | static int scpi_vxi_read_data(void *priv, char *buf, int maxlen) | |
167 | { | |
168 | struct scpi_vxi *vxi = priv; | |
169 | Device_ReadParms read_parms; | |
170 | Device_ReadResp *read_resp; | |
171 | ||
172 | read_parms.lid = vxi->link; | |
1a46cc62 UH |
173 | read_parms.io_timeout = VXI_DEFAULT_TIMEOUT_MS; |
174 | read_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS; | |
d5876cfb AJ |
175 | read_parms.flags = 0; |
176 | read_parms.termChar = 0; | |
177 | read_parms.requestSize = maxlen; | |
178 | ||
179 | if (!(read_resp = device_read_1(&read_parms, vxi->client)) | |
180 | || read_resp->error) { | |
6433156c | 181 | sr_err("Device read failed for %s with error %ld", |
f3616a08 | 182 | vxi->address, read_resp ? read_resp->error : 0); |
d5876cfb AJ |
183 | return SR_ERR; |
184 | } | |
185 | ||
186 | memcpy(buf, read_resp->data.data_val, read_resp->data.data_len); | |
187 | vxi->read_complete = read_resp->reason & (RRR_SIZE | RRR_TERM | RRR_END); | |
188 | return read_resp->data.data_len; /* actual number of bytes received */ | |
189 | } | |
190 | ||
191 | static int scpi_vxi_read_complete(void *priv) | |
192 | { | |
193 | struct scpi_vxi *vxi = priv; | |
194 | ||
195 | return vxi->read_complete; | |
196 | } | |
197 | ||
04229f7b | 198 | static int scpi_vxi_close(struct sr_scpi_dev_inst *scpi) |
d5876cfb | 199 | { |
04229f7b | 200 | struct scpi_vxi *vxi = scpi->priv; |
d5876cfb AJ |
201 | Device_Error *dev_error; |
202 | ||
dc3b3be5 AJ |
203 | if (!vxi->client) |
204 | return SR_ERR; | |
205 | ||
d5876cfb AJ |
206 | if (!(dev_error = destroy_link_1(&vxi->link, vxi->client))) { |
207 | sr_err("Link destruction failed for %s", vxi->address); | |
208 | return SR_ERR; | |
209 | } | |
210 | ||
211 | clnt_destroy(vxi->client); | |
dc3b3be5 | 212 | vxi->client = NULL; |
d5876cfb AJ |
213 | |
214 | return SR_OK; | |
215 | } | |
216 | ||
217 | static void scpi_vxi_free(void *priv) | |
218 | { | |
219 | struct scpi_vxi *vxi = priv; | |
220 | ||
221 | g_free(vxi->address); | |
222 | g_free(vxi->instrument); | |
d5876cfb AJ |
223 | } |
224 | ||
f754c146 AJ |
225 | SR_PRIV const struct sr_scpi_dev_inst scpi_vxi_dev = { |
226 | .name = "VXI", | |
227 | .prefix = "vxi", | |
228 | .priv_size = sizeof(struct scpi_vxi), | |
229 | .dev_inst_new = scpi_vxi_dev_inst_new, | |
230 | .open = scpi_vxi_open, | |
231 | .source_add = scpi_vxi_source_add, | |
232 | .source_remove = scpi_vxi_source_remove, | |
233 | .send = scpi_vxi_send, | |
234 | .read_begin = scpi_vxi_read_begin, | |
235 | .read_data = scpi_vxi_read_data, | |
236 | .read_complete = scpi_vxi_read_complete, | |
237 | .close = scpi_vxi_close, | |
238 | .free = scpi_vxi_free, | |
239 | }; |