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