]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_libgpib.c
scpi/libgpib: Print error string instead of number on errors
[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_source_add(struct sr_session *session, void *priv,
63                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
64 {
65         (void) priv;
66
67         /* Hook up a dummy handler to receive data from the device. */
68         return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
69 }
70
71 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
72 {
73         (void) priv;
74
75         return sr_session_source_remove(session, -1);
76 }
77
78 static int scpi_gpib_send(void *priv, const char *command)
79 {
80         struct scpi_gpib *gscpi = priv;
81         int len = strlen(command);
82
83         ibwrt(gscpi->descriptor, command, len);
84
85         if (ibsta & ERR)
86         {
87                 sr_err("Error while sending SCPI command: '%s': iberr = %s.",
88                         command, gpib_error_string(iberr));
89                 return SR_ERR;
90         }
91
92         if (ibcnt < len)
93         {
94                 sr_err("Failed to send all of SCPI command: '%s': "
95                                 "len = %d, ibcnt = %d.", command, len, ibcnt);
96                 return SR_ERR;
97         }
98
99         sr_spew("Successfully sent SCPI command: '%s'.", command);
100
101         return SR_OK;
102 }
103
104 static int scpi_gpib_read_begin(void *priv)
105 {
106         struct scpi_gpib *gscpi = priv;
107
108         gscpi->read_started = 0;
109
110         return SR_OK;
111 }
112
113 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
114 {
115         struct scpi_gpib *gscpi = priv;
116
117         ibrd(gscpi->descriptor, buf, maxlen);
118
119         if (ibsta & ERR)
120         {
121                 sr_err("Error while reading SCPI response: iberr = %s.",
122                         gpib_error_string(iberr));
123                 return SR_ERR;
124         }
125
126         gscpi->read_started = 1;
127
128         return ibcnt;
129 }
130
131 static int scpi_gpib_read_complete(void *priv)
132 {
133         struct scpi_gpib *gscpi = priv;
134
135         return gscpi->read_started && (ibsta & END);
136 }
137
138 static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
139 {
140         struct scpi_gpib *gscpi = scpi->priv;
141
142         ibonl(gscpi->descriptor, 0);
143
144         return SR_OK;
145 }
146
147 static void scpi_gpib_free(void *priv)
148 {
149         struct scpi_gpib *gscpi = priv;
150
151         g_free(gscpi->name);
152 }
153
154 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
155         .name = "GPIB",
156         .prefix = "libgpib",
157         .priv_size = sizeof(struct scpi_gpib),
158         .dev_inst_new = scpi_gpib_dev_inst_new,
159         .open = scpi_gpib_open,
160         .source_add = scpi_gpib_source_add,
161         .source_remove = scpi_gpib_source_remove,
162         .send = scpi_gpib_send,
163         .read_begin = scpi_gpib_read_begin,
164         .read_data = scpi_gpib_read_data,
165         .read_complete = scpi_gpib_read_complete,
166         .close = scpi_gpib_close,
167         .free = scpi_gpib_free,
168 };