]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_libgpib.c
ipdbg-la: Minor cosmetic and comment fixes.
[libsigrok.git] / src / scpi / scpi_libgpib.c
CommitLineData
bb2a4ed4
ML
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
6ec6c43b 20#include <config.h>
bb2a4ed4
ML
21#include <gpib/ib.h>
22#include <string.h>
c1aae900 23#include <libsigrok/libsigrok.h>
515ab088 24#include "libsigrok-internal.h"
5a1afc09 25#include "scpi.h"
bb2a4ed4
ML
26
27#define LOG_PREFIX "scpi_gpib"
28
29struct scpi_gpib {
30 char *name;
31 int descriptor;
32 int read_started;
33};
34
35static 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
04229f7b 52static int scpi_gpib_open(struct sr_scpi_dev_inst *scpi)
bb2a4ed4 53{
04229f7b 54 struct scpi_gpib *gscpi = scpi->priv;
bb2a4ed4
ML
55
56 if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
57 return SR_ERR;
58
59 return SR_OK;
60}
61
62static 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
71static 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
78static 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 {
daf13c57
AG
87 sr_err("Error while sending SCPI command: '%s': iberr = %s.",
88 command, gpib_error_string(iberr));
bb2a4ed4
ML
89 return SR_ERR;
90 }
91
92 if (ibcnt < len)
93 {
94 sr_err("Failed to send all of SCPI command: '%s': "
27cd728f 95 "len = %d, ibcnt = %d.", command, len, ibcnt);
bb2a4ed4
ML
96 return SR_ERR;
97 }
98
99 sr_spew("Successfully sent SCPI command: '%s'.", command);
100
101 return SR_OK;
102}
103
104static 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
113static 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 {
ae328597
FS
121 sr_err("Error while reading SCPI response: "
122 "iberr = %s, ibsta = %d.",
123 gpib_error_string(iberr), ibsta);
bb2a4ed4
ML
124 return SR_ERR;
125 }
126
127 gscpi->read_started = 1;
128
129 return ibcnt;
130}
131
132static int scpi_gpib_read_complete(void *priv)
133{
134 struct scpi_gpib *gscpi = priv;
135
136 return gscpi->read_started && (ibsta & END);
137}
138
04229f7b 139static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
bb2a4ed4 140{
04229f7b 141 struct scpi_gpib *gscpi = scpi->priv;
bb2a4ed4 142
8b0ad3a5
AG
143 /* Put device in back local mode to prevent lock-out of front panel. */
144 ibloc(gscpi->descriptor);
145 /* Now it's safe to close the handle. */
bb2a4ed4
ML
146 ibonl(gscpi->descriptor, 0);
147
148 return SR_OK;
149}
150
151static void scpi_gpib_free(void *priv)
152{
153 struct scpi_gpib *gscpi = priv;
154
155 g_free(gscpi->name);
156}
157
7343ad1e 158SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
bb2a4ed4 159 .name = "GPIB",
7343ad1e 160 .prefix = "libgpib",
bb2a4ed4
ML
161 .priv_size = sizeof(struct scpi_gpib),
162 .dev_inst_new = scpi_gpib_dev_inst_new,
163 .open = scpi_gpib_open,
164 .source_add = scpi_gpib_source_add,
165 .source_remove = scpi_gpib_source_remove,
166 .send = scpi_gpib_send,
167 .read_begin = scpi_gpib_read_begin,
168 .read_data = scpi_gpib_read_data,
169 .read_complete = scpi_gpib_read_complete,
170 .close = scpi_gpib_close,
171 .free = scpi_gpib_free,
172};