]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_libgpib.c
scpi: Pass SCPI device instance to open and close callbacks.
[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 {
87 sr_err("Error while sending SCPI command: '%s': iberr = %d.",
88 command, iberr);
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 {
121 sr_err("Error while reading SCPI response: iberr = %d.", iberr);
122 return SR_ERR;
123 }
124
125 gscpi->read_started = 1;
126
127 return ibcnt;
128}
129
130static int scpi_gpib_read_complete(void *priv)
131{
132 struct scpi_gpib *gscpi = priv;
133
134 return gscpi->read_started && (ibsta & END);
135}
136
04229f7b 137static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
bb2a4ed4 138{
04229f7b 139 struct scpi_gpib *gscpi = scpi->priv;
bb2a4ed4
ML
140
141 ibonl(gscpi->descriptor, 0);
142
143 return SR_OK;
144}
145
146static void scpi_gpib_free(void *priv)
147{
148 struct scpi_gpib *gscpi = priv;
149
150 g_free(gscpi->name);
151}
152
7343ad1e 153SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
bb2a4ed4 154 .name = "GPIB",
7343ad1e 155 .prefix = "libgpib",
bb2a4ed4
ML
156 .priv_size = sizeof(struct scpi_gpib),
157 .dev_inst_new = scpi_gpib_dev_inst_new,
158 .open = scpi_gpib_open,
159 .source_add = scpi_gpib_source_add,
160 .source_remove = scpi_gpib_source_remove,
161 .send = scpi_gpib_send,
162 .read_begin = scpi_gpib_read_begin,
163 .read_data = scpi_gpib_read_data,
164 .read_complete = scpi_gpib_read_complete,
165 .close = scpi_gpib_close,
166 .free = scpi_gpib_free,
167};