]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_visa.c
build: Portability fixes.
[libsigrok.git] / hardware / common / scpi_visa.c
CommitLineData
1fb2312f
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "libsigrok.h"
21#include "libsigrok-internal.h"
22
23#include <visa.h>
24#include <string.h>
25
26#define LOG_PREFIX "scpi_visa"
27
28struct scpi_visa {
29 char *resource;
30 ViSession rmgr;
31 ViSession vi;
32};
33
d8b6b28b
DE
34static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
35 const char *resource, char **params, const char *serialcomm)
1fb2312f
ML
36{
37 struct scpi_visa *vscpi = priv;
38
d8b6b28b 39 (void)drvc;
1fb2312f
ML
40 (void)resource;
41 (void)serialcomm;
42
43 if (!params || !params[1]) {
44 sr_err("Invalid parameters.");
7ad4e2b8 45 return SR_ERR_BUG;
1fb2312f
ML
46 }
47
48 vscpi->resource = g_strdup(params[1]);
49
50 return SR_OK;
51}
52
53static int scpi_visa_open(void *priv)
54{
55 struct scpi_visa *vscpi = priv;
56
57 if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
58 sr_err("Cannot open default resource manager.");
59 return SR_ERR;
60 }
61
7ad4e2b8 62 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
1fb2312f
ML
63 sr_err("Cannot open resource.");
64 return SR_ERR;
65 }
66
67 return SR_OK;
68}
69
102f1239
BV
70static int scpi_visa_source_add(struct sr_session *session, void *priv,
71 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
1fb2312f
ML
72{
73 (void) priv;
74
75 /* Hook up a dummy handler to receive data from the device. */
102f1239 76 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
1fb2312f
ML
77}
78
102f1239 79static int scpi_visa_source_remove(struct sr_session *session, void *priv)
1fb2312f
ML
80{
81 (void) priv;
82
102f1239 83 return sr_session_source_remove(session, -1);
1fb2312f
ML
84}
85
86static int scpi_visa_send(void *priv, const char *command)
87{
88 struct scpi_visa *vscpi = priv;
89 gchar *terminated_command;
dc05dd60 90 ViUInt32 written = 0;
1fb2312f
ML
91 int len;
92
93 terminated_command = g_strconcat(command, "\n", NULL);
94 len = strlen(terminated_command);
95 if (viWrite(vscpi->vi, (ViBuf) (terminated_command + written), len,
96 &written) != VI_SUCCESS) {
97 sr_err("Error while sending SCPI command: '%s'.", command);
98 g_free(terminated_command);
99 return SR_ERR;
100 }
101
102 g_free(terminated_command);
103
104 sr_spew("Successfully sent SCPI command: '%s'.", command);
105
106 return SR_OK;
107}
108
109static int scpi_visa_read_begin(void *priv)
110{
111 (void) priv;
112
113 return SR_OK;
114}
115
116static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
117{
118 struct scpi_visa *vscpi = priv;
119 ViUInt32 count;
120
7ad4e2b8
UH
121 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
122 sr_err("Read failed.");
1fb2312f
ML
123 return SR_ERR;
124 }
125
126 return count;
127}
128
129static int scpi_visa_read_complete(void *priv)
130{
131 struct scpi_visa *vscpi = priv;
132 ViUInt16 status;
133
134 if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
7ad4e2b8 135 sr_err("Failed to read status.");
1fb2312f
ML
136 return SR_ERR;
137 }
138
139 return !(status & 16);
140}
141
142static int scpi_visa_close(void *priv)
143{
144 struct scpi_visa *vscpi = priv;
145
146 viClose(vscpi->vi);
147 viClose(vscpi->rmgr);
148
149 return SR_OK;
150}
151
152static void scpi_visa_free(void *priv)
153{
154 struct scpi_visa *vscpi = priv;
155
156 g_free(vscpi->resource);
157 g_free(vscpi);
158}
159
160SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
161 .name = "VISA",
162 .prefix = "visa",
163 .priv_size = sizeof(struct scpi_visa),
164 .dev_inst_new = scpi_visa_dev_inst_new,
165 .open = scpi_visa_open,
166 .source_add = scpi_visa_source_add,
167 .source_remove = scpi_visa_source_remove,
168 .send = scpi_visa_send,
169 .read_begin = scpi_visa_read_begin,
170 .read_data = scpi_visa_read_data,
171 .read_complete = scpi_visa_read_complete,
172 .close = scpi_visa_close,
173 .free = scpi_visa_free,
174};