]>
Commit | Line | Data |
---|---|---|
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: " | |
122 | "iberr = %s, ibsta = %d.", | |
123 | gpib_error_string(iberr), ibsta); | |
124 | return SR_ERR; | |
125 | } | |
126 | ||
127 | gscpi->read_started = 1; | |
128 | ||
129 | return ibcnt; | |
130 | } | |
131 | ||
132 | static int scpi_gpib_read_complete(void *priv) | |
133 | { | |
134 | struct scpi_gpib *gscpi = priv; | |
135 | ||
136 | return gscpi->read_started && (ibsta & END); | |
137 | } | |
138 | ||
139 | static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi) | |
140 | { | |
141 | struct scpi_gpib *gscpi = scpi->priv; | |
142 | ||
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. */ | |
146 | ibonl(gscpi->descriptor, 0); | |
147 | ||
148 | return SR_OK; | |
149 | } | |
150 | ||
151 | static void scpi_gpib_free(void *priv) | |
152 | { | |
153 | struct scpi_gpib *gscpi = priv; | |
154 | ||
155 | g_free(gscpi->name); | |
156 | } | |
157 | ||
158 | SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = { | |
159 | .name = "GPIB", | |
160 | .prefix = "libgpib", | |
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 | }; |