]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_visa.c
scpi_vxi: fix memory leak for SCPI response data in VXI support code
[libsigrok.git] / src / scpi / scpi_visa.c
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 <config.h>
21 #include <visa.h>
22 #include <string.h>
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25 #include "scpi.h"
26
27 #define LOG_PREFIX "scpi_visa"
28
29 struct scpi_visa {
30         char *resource;
31         ViSession rmgr;
32         ViSession vi;
33 };
34
35 static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
36                 const char *resource, char **params, const char *serialcomm)
37 {
38         struct scpi_visa *vscpi = priv;
39
40         (void)drvc;
41         (void)resource;
42         (void)serialcomm;
43
44         if (!params || !params[1]) {
45                 sr_err("Invalid parameters.");
46                 return SR_ERR_BUG;
47         }
48
49         vscpi->resource = g_strdup(params[1]);
50
51         return SR_OK;
52 }
53
54 static int scpi_visa_open(struct sr_scpi_dev_inst *scpi)
55 {
56         struct scpi_visa *vscpi = scpi->priv;
57
58         if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
59                 sr_err("Cannot open default resource manager.");
60                 return SR_ERR;
61         }
62
63         if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
64                 sr_err("Cannot open resource.");
65                 return SR_ERR;
66         }
67
68         return SR_OK;
69 }
70
71 static int scpi_visa_connection_id(struct sr_scpi_dev_inst *scpi,
72                 char **connection_id)
73 {
74         struct scpi_visa *vscpi = scpi->priv;
75
76         *connection_id = g_strdup_printf("%s/%s", scpi->prefix, vscpi->resource);
77
78         return SR_OK;
79 }
80
81 static int scpi_visa_source_add(struct sr_session *session, void *priv,
82                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
83 {
84         (void) priv;
85
86         /* Hook up a dummy handler to receive data from the device. */
87         return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
88 }
89
90 static int scpi_visa_source_remove(struct sr_session *session, void *priv)
91 {
92         (void) priv;
93
94         return sr_session_source_remove(session, -1);
95 }
96
97 static int scpi_visa_send(void *priv, const char *command)
98 {
99         struct scpi_visa *vscpi = priv;
100         ViUInt32 written = 0;
101         int len;
102
103         len = strlen(command);
104         if (viWrite(vscpi->vi, (ViBuf) (command + written), len,
105                         &written) != VI_SUCCESS) {
106                 sr_err("Error while sending SCPI command: '%s'.", command);
107                 return SR_ERR;
108         }
109
110         sr_spew("Successfully sent SCPI command: '%s'.", command);
111
112         return SR_OK;
113 }
114
115 static int scpi_visa_read_begin(void *priv)
116 {
117         (void) priv;
118
119         return SR_OK;
120 }
121
122 static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
123 {
124         struct scpi_visa *vscpi = priv;
125         ViUInt32 count;
126
127         if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
128                 sr_err("Read failed.");
129                 return SR_ERR;
130         }
131
132         return count;
133 }
134
135 static int scpi_visa_read_complete(void *priv)
136 {
137         struct scpi_visa *vscpi = priv;
138         ViUInt16 status;
139
140         if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
141                 sr_err("Failed to read status.");
142                 return SR_ERR;
143         }
144
145         return !(status & 16);
146 }
147
148 static int scpi_visa_close(struct sr_scpi_dev_inst *scpi)
149 {
150         struct scpi_visa *vscpi = scpi->priv;
151
152         viClose(vscpi->vi);
153         viClose(vscpi->rmgr);
154
155         return SR_OK;
156 }
157
158 static void scpi_visa_free(void *priv)
159 {
160         struct scpi_visa *vscpi = priv;
161
162         g_free(vscpi->resource);
163         g_free(vscpi);
164 }
165
166 SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
167         .name          = "VISA",
168         .prefix        = "visa",
169         .transport     = SCPI_TRANSPORT_VISA,
170         .priv_size     = sizeof(struct scpi_visa),
171         .dev_inst_new  = scpi_visa_dev_inst_new,
172         .open          = scpi_visa_open,
173         .connection_id = scpi_visa_connection_id,
174         .source_add    = scpi_visa_source_add,
175         .source_remove = scpi_visa_source_remove,
176         .send          = scpi_visa_send,
177         .read_begin    = scpi_visa_read_begin,
178         .read_data     = scpi_visa_read_data,
179         .read_complete = scpi_visa_read_complete,
180         .close         = scpi_visa_close,
181         .free          = scpi_visa_free,
182 };