]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_usbtmc.c
Don't define names ending with _t (POSIX reserved).
[libsigrok.git] / hardware / common / scpi_usbtmc.c
CommitLineData
31034792
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 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 "libsigrok.h"
21#include "libsigrok-internal.h"
22
23#include <glib.h>
24#include <string.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
28
3544f848 29#define LOG_PREFIX "scpi_usbtmc"
31034792 30
05c644ea
ML
31#define MAX_READ_LENGTH 2048
32
33struct usbtmc_scpi {
34 struct sr_usbtmc_dev_inst *usbtmc;
35 char response_buffer[MAX_READ_LENGTH];
36 int response_length;
37 int response_bytes_read;
38};
39
b541f837
AJ
40static GSList *scpi_usbtmc_scan(struct drv_context *drvc)
41{
42 GSList *resources = NULL;
43 GDir *dir;
44 const char *dev_name;
45 char *resource;
46
47 (void)drvc;
48
49 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
50 if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
51 return NULL;
52 while ((dev_name = g_dir_read_name(dir))) {
53 if (strncmp(dev_name, "usbtmc", 6))
54 continue;
55 resource = g_strconcat("/dev/", dev_name, NULL);
56 resources = g_slist_append(resources, resource);
57 }
58 g_dir_close(dir);
59
60 return resources;
61}
62
17bdda58
AJ
63static int scpi_usbtmc_dev_inst_new(void *priv, struct drv_context *drvc,
64 const char *resource, char **params, const char *serialcomm)
f754c146
AJ
65{
66 struct usbtmc_scpi *uscpi = priv;
67
17bdda58 68 (void)drvc;
f754c146
AJ
69 (void)params;
70 (void)serialcomm;
71
72 if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource)))
73 return SR_ERR;
74
75 return SR_OK;
76}
77
d87c1766 78static int scpi_usbtmc_open(void *priv)
31034792 79{
05c644ea
ML
80 struct usbtmc_scpi *uscpi = priv;
81 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
82
83 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
84 return SR_ERR;
85
86 return SR_OK;
87}
88
d87c1766 89static int scpi_usbtmc_source_add(void *priv, int events, int timeout,
144f6660 90 sr_receive_data_callback cb, void *cb_data)
31034792 91{
05c644ea
ML
92 struct usbtmc_scpi *uscpi = priv;
93 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
94
95 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
96}
97
d87c1766 98static int scpi_usbtmc_source_remove(void *priv)
31034792 99{
05c644ea
ML
100 struct usbtmc_scpi *uscpi = priv;
101 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
102
103 return sr_source_remove(usbtmc->fd);
104}
105
d87c1766 106static int scpi_usbtmc_send(void *priv, const char *command)
31034792 107{
05c644ea
ML
108 struct usbtmc_scpi *uscpi = priv;
109 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
110 int len, out;
111
112 len = strlen(command);
113 out = write(usbtmc->fd, command, len);
114
115 if (out < 0) {
116 sr_err("Write error: %s", strerror(errno));
117 return SR_ERR;
118 }
119
120 if (out < len) {
121 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
122 len, command);
123 }
124
125 sr_spew("Successfully sent SCPI command: '%s'.", command);
126
127 return SR_OK;
128}
129
d87c1766 130static int scpi_usbtmc_read_begin(void *priv)
31034792 131{
05c644ea
ML
132 struct usbtmc_scpi *uscpi = priv;
133 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
134 int len;
135
05c644ea 136 len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
31034792
ML
137
138 if (len < 0) {
139 sr_err("Read error: %s", strerror(errno));
31034792
ML
140 return SR_ERR;
141 }
142
05c644ea
ML
143 uscpi->response_length = len;
144 uscpi->response_bytes_read = 0;
31034792 145
a849c43a
ML
146 sr_spew("Read %d bytes from device into buffer", len);
147
31034792
ML
148 return SR_OK;
149}
150
d87c1766 151static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
a1ff9c18 152{
05c644ea
ML
153 struct usbtmc_scpi *uscpi = priv;
154 int read_length;
a1ff9c18 155
a849c43a 156 sr_spew("%d bytes requested", maxlen);
8ae157d9 157
a849c43a
ML
158 if (uscpi->response_bytes_read == uscpi->response_length) {
159 sr_spew("Buffer is empty.");
160 if (uscpi->response_length == MAX_READ_LENGTH) {
161 sr_spew("Previous read was of maximum length, reading again.");
162 if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
163 return SR_ERR;
164 } else {
165 return SR_ERR;
166 }
167 }
a1ff9c18 168
05c644ea
ML
169 read_length = uscpi->response_length - uscpi->response_bytes_read;
170
171 if (read_length > maxlen)
172 read_length = maxlen;
173
174 memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
175
176 uscpi->response_bytes_read += read_length;
177
a849c43a
ML
178 sr_spew("Returned %d bytes from buffer, %d/%d bytes of buffer now read",
179 read_length, uscpi->response_bytes_read, uscpi->response_length);
180
05c644ea
ML
181 return read_length;
182}
183
d87c1766 184static int scpi_usbtmc_read_complete(void *priv)
05c644ea
ML
185{
186 struct usbtmc_scpi *uscpi = priv;
187
8ae157d9
AJ
188 if (uscpi->response_length == MAX_READ_LENGTH
189 && uscpi->response_bytes_read == uscpi->response_length)
190 scpi_usbtmc_read_begin(uscpi);
191
05c644ea 192 return (uscpi->response_bytes_read >= uscpi->response_length);
a1ff9c18
ML
193}
194
d87c1766 195static int scpi_usbtmc_close(void *priv)
31034792 196{
05c644ea 197 struct usbtmc_scpi *uscpi = priv;
31034792 198
f754c146 199 if (close(uscpi->usbtmc->fd) < 0)
31034792
ML
200 return SR_ERR;
201
202 return SR_OK;
203}
204
91e406b9
BV
205static void scpi_usbtmc_free(void *priv)
206{
05c644ea 207 struct usbtmc_scpi *uscpi = priv;
05c644ea 208
f754c146 209 sr_usbtmc_dev_inst_free(uscpi->usbtmc);
91e406b9
BV
210}
211
f754c146
AJ
212SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
213 .name = "USBTMC",
214 .prefix = "/dev/usbtmc",
215 .priv_size = sizeof(struct usbtmc_scpi),
b541f837 216 .scan = scpi_usbtmc_scan,
f754c146
AJ
217 .dev_inst_new = scpi_usbtmc_dev_inst_new,
218 .open = scpi_usbtmc_open,
219 .source_add = scpi_usbtmc_source_add,
220 .source_remove = scpi_usbtmc_source_remove,
221 .send = scpi_usbtmc_send,
222 .read_begin = scpi_usbtmc_read_begin,
223 .read_data = scpi_usbtmc_read_data,
224 .read_complete = scpi_usbtmc_read_complete,
225 .close = scpi_usbtmc_close,
226 .free = scpi_usbtmc_free,
227};