]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_libgpib.c
Build: Set local include directories in Makefile.am
[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 <gpib/ib.h>
21 #include <string.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24
25 #define LOG_PREFIX "scpi_gpib"
26
27 struct scpi_gpib {
28         char *name;
29         int descriptor;
30         int read_started;
31 };
32
33 static int scpi_gpib_dev_inst_new(void *priv, struct drv_context *drvc,
34                 const char *resource, char **params, const char *serialcomm)
35 {
36         struct scpi_gpib *gscpi = priv;
37
38         (void)drvc;
39         (void)resource;
40         (void)serialcomm;
41
42         if (!params || !params[1])
43                         return SR_ERR;
44
45         gscpi->name = g_strdup(params[1]);
46
47         return SR_OK;
48 }
49
50 static int scpi_gpib_open(void *priv)
51 {
52         struct scpi_gpib *gscpi = priv;
53
54         if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
55                 return SR_ERR;
56
57         return SR_OK;
58 }
59
60 static int scpi_gpib_source_add(struct sr_session *session, void *priv,
61                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
62 {
63         (void) priv;
64
65         /* Hook up a dummy handler to receive data from the device. */
66         return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
67 }
68
69 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
70 {
71         (void) priv;
72
73         return sr_session_source_remove(session, -1);
74 }
75
76 static int scpi_gpib_send(void *priv, const char *command)
77 {
78         struct scpi_gpib *gscpi = priv;
79         int len = strlen(command);
80
81         ibwrt(gscpi->descriptor, command, len);
82
83         if (ibsta & ERR)
84         {
85                 sr_err("Error while sending SCPI command: '%s': iberr = %d.",
86                                 command, iberr);
87                 return SR_ERR;
88         }
89
90         if (ibcnt < len)
91         {
92                 sr_err("Failed to send all of SCPI command: '%s': "
93                                 "len = %d, ibcnt = .", command, len, ibcnt);
94                 return SR_ERR;
95         }
96
97         sr_spew("Successfully sent SCPI command: '%s'.", command);
98
99         return SR_OK;
100 }
101
102 static int scpi_gpib_read_begin(void *priv)
103 {
104         struct scpi_gpib *gscpi = priv;
105
106         gscpi->read_started = 0;
107
108         return SR_OK;
109 }
110
111 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
112 {
113         struct scpi_gpib *gscpi = priv;
114
115         ibrd(gscpi->descriptor, buf, maxlen);
116
117         if (ibsta & ERR)
118         {
119                 sr_err("Error while reading SCPI response: iberr = %d.", iberr);
120                 return SR_ERR;
121         }
122
123         gscpi->read_started = 1;
124
125         return ibcnt;
126 }
127
128 static int scpi_gpib_read_complete(void *priv)
129 {
130         struct scpi_gpib *gscpi = priv;
131
132         return gscpi->read_started && (ibsta & END);
133 }
134
135 static int scpi_gpib_close(void *priv)
136 {
137         struct scpi_gpib *gscpi = priv;
138
139         ibonl(gscpi->descriptor, 0);
140
141         return SR_OK;
142 }
143
144 static void scpi_gpib_free(void *priv)
145 {
146         struct scpi_gpib *gscpi = priv;
147
148         g_free(gscpi->name);
149 }
150
151 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
152         .name = "GPIB",
153         .prefix = "libgpib",
154         .priv_size = sizeof(struct scpi_gpib),
155         .dev_inst_new = scpi_gpib_dev_inst_new,
156         .open = scpi_gpib_open,
157         .source_add = scpi_gpib_source_add,
158         .source_remove = scpi_gpib_source_remove,
159         .send = scpi_gpib_send,
160         .read_begin = scpi_gpib_read_begin,
161         .read_data = scpi_gpib_read_data,
162         .read_complete = scpi_gpib_read_complete,
163         .close = scpi_gpib_close,
164         .free = scpi_gpib_free,
165 };