]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_libgpib.c
scpi_vxi: fix memory leak for SCPI response data in VXI support code
[libsigrok.git] / src / scpi / scpi_libgpib.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 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 <gpib/ib.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_gpib"
28
29 struct scpi_gpib {
30         char *name;
31         int descriptor;
32         int read_started;
33 };
34
35 static int scpi_gpib_dev_inst_new(void *priv, struct drv_context *drvc,
36                 const char *resource, char **params, const char *serialcomm)
37 {
38         struct scpi_gpib *gscpi = priv;
39
40         (void)drvc;
41         (void)resource;
42         (void)serialcomm;
43
44         if (!params || !params[1])
45                         return SR_ERR;
46
47         gscpi->name = g_strdup(params[1]);
48
49         return SR_OK;
50 }
51
52 static int scpi_gpib_open(struct sr_scpi_dev_inst *scpi)
53 {
54         struct scpi_gpib *gscpi = scpi->priv;
55
56         if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
57                 return SR_ERR;
58
59         return SR_OK;
60 }
61
62 static int scpi_gpib_connection_id(struct sr_scpi_dev_inst *scpi,
63                 char **connection_id)
64 {
65         struct scpi_gpib *gscpi = scpi->priv;
66
67         *connection_id = g_strdup_printf("%s/%s", scpi->prefix, gscpi->name);
68
69         return SR_OK;
70 }
71
72 static int scpi_gpib_source_add(struct sr_session *session, void *priv,
73                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
74 {
75         (void) priv;
76
77         /* Hook up a dummy handler to receive data from the device. */
78         return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
79 }
80
81 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
82 {
83         (void) priv;
84
85         return sr_session_source_remove(session, -1);
86 }
87
88 static int scpi_gpib_send(void *priv, const char *command)
89 {
90         struct scpi_gpib *gscpi = priv;
91         int len = strlen(command);
92
93         ibwrt(gscpi->descriptor, command, len);
94
95         if (ibsta & ERR)
96         {
97                 sr_err("Error while sending SCPI command: '%s': iberr = %s.",
98                         command, gpib_error_string(iberr));
99                 return SR_ERR;
100         }
101
102         if (ibcnt < len)
103         {
104                 sr_err("Failed to send all of SCPI command: '%s': "
105                                 "len = %d, ibcnt = %d.", command, len, ibcnt);
106                 return SR_ERR;
107         }
108
109         sr_spew("Successfully sent SCPI command: '%s'.", command);
110
111         return SR_OK;
112 }
113
114 static int scpi_gpib_read_begin(void *priv)
115 {
116         struct scpi_gpib *gscpi = priv;
117
118         gscpi->read_started = 0;
119
120         return SR_OK;
121 }
122
123 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
124 {
125         struct scpi_gpib *gscpi = priv;
126
127         ibrd(gscpi->descriptor, buf, maxlen);
128
129         if (ibsta & ERR)
130         {
131                 sr_err("Error while reading SCPI response: "
132                         "iberr = %s, ibsta = %d.",
133                         gpib_error_string(iberr), ibsta);
134                 return SR_ERR;
135         }
136
137         gscpi->read_started = 1;
138
139         return ibcnt;
140 }
141
142 static int scpi_gpib_read_complete(void *priv)
143 {
144         struct scpi_gpib *gscpi = priv;
145
146         return gscpi->read_started && (ibsta & END);
147 }
148
149 static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
150 {
151         struct scpi_gpib *gscpi = scpi->priv;
152
153         /* Put device in back local mode to prevent lock-out of front panel. */
154         ibloc(gscpi->descriptor);
155         /* Now it's safe to close the handle. */
156         ibonl(gscpi->descriptor, 0);
157
158         return SR_OK;
159 }
160
161 static void scpi_gpib_free(void *priv)
162 {
163         struct scpi_gpib *gscpi = priv;
164
165         g_free(gscpi->name);
166 }
167
168 SR_PRIV int sr_scpi_gpib_spoll(struct sr_scpi_dev_inst *scpi, char *buf)
169 {
170         struct scpi_gpib *gscpi = scpi->priv;
171
172         g_mutex_lock(&scpi->scpi_mutex);
173         ibrsp(gscpi->descriptor, buf);
174
175         if (ibsta & ERR) {
176                 sr_err("Error while serial polling: iberr = %s.",
177                         gpib_error_string(iberr));
178                 g_mutex_unlock(&scpi->scpi_mutex);
179                 return SR_ERR;
180         }
181         g_mutex_unlock(&scpi->scpi_mutex);
182         sr_spew("Successful serial poll: 0x%x", (uint8_t)buf[0]);
183
184         return SR_OK;
185 }
186
187 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
188         .name          = "GPIB",
189         .prefix        = "libgpib",
190         .transport     = SCPI_TRANSPORT_LIBGPIB,
191         .priv_size     = sizeof(struct scpi_gpib),
192         .dev_inst_new  = scpi_gpib_dev_inst_new,
193         .open          = scpi_gpib_open,
194         .connection_id = scpi_gpib_connection_id,
195         .source_add    = scpi_gpib_source_add,
196         .source_remove = scpi_gpib_source_remove,
197         .send          = scpi_gpib_send,
198         .read_begin    = scpi_gpib_read_begin,
199         .read_data     = scpi_gpib_read_data,
200         .read_complete = scpi_gpib_read_complete,
201         .close         = scpi_gpib_close,
202         .free          = scpi_gpib_free,
203 };