]>
Commit | Line | Data |
---|---|---|
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 | ||
33 | struct 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 | ||
f754c146 AJ |
40 | static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource, |
41 | char **params, const char *serialcomm) | |
42 | { | |
43 | struct usbtmc_scpi *uscpi = priv; | |
44 | ||
45 | (void)params; | |
46 | (void)serialcomm; | |
47 | ||
48 | if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource))) | |
49 | return SR_ERR; | |
50 | ||
51 | return SR_OK; | |
52 | } | |
53 | ||
d87c1766 | 54 | static int scpi_usbtmc_open(void *priv) |
31034792 | 55 | { |
05c644ea ML |
56 | struct usbtmc_scpi *uscpi = priv; |
57 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
58 | |
59 | if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0) | |
60 | return SR_ERR; | |
61 | ||
62 | return SR_OK; | |
63 | } | |
64 | ||
d87c1766 | 65 | static int scpi_usbtmc_source_add(void *priv, int events, int timeout, |
31034792 ML |
66 | sr_receive_data_callback_t cb, void *cb_data) |
67 | { | |
05c644ea ML |
68 | struct usbtmc_scpi *uscpi = priv; |
69 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
70 | |
71 | return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data); | |
72 | } | |
73 | ||
d87c1766 | 74 | static int scpi_usbtmc_source_remove(void *priv) |
31034792 | 75 | { |
05c644ea ML |
76 | struct usbtmc_scpi *uscpi = priv; |
77 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
78 | |
79 | return sr_source_remove(usbtmc->fd); | |
80 | } | |
81 | ||
d87c1766 | 82 | static int scpi_usbtmc_send(void *priv, const char *command) |
31034792 | 83 | { |
05c644ea ML |
84 | struct usbtmc_scpi *uscpi = priv; |
85 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
86 | int len, out; |
87 | ||
88 | len = strlen(command); | |
89 | out = write(usbtmc->fd, command, len); | |
90 | ||
91 | if (out < 0) { | |
92 | sr_err("Write error: %s", strerror(errno)); | |
93 | return SR_ERR; | |
94 | } | |
95 | ||
96 | if (out < len) { | |
97 | sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out, | |
98 | len, command); | |
99 | } | |
100 | ||
101 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
102 | ||
103 | return SR_OK; | |
104 | } | |
105 | ||
d87c1766 | 106 | static int scpi_usbtmc_read_begin(void *priv) |
31034792 | 107 | { |
05c644ea ML |
108 | struct usbtmc_scpi *uscpi = priv; |
109 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
110 | int len; |
111 | ||
05c644ea | 112 | len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH); |
31034792 ML |
113 | |
114 | if (len < 0) { | |
115 | sr_err("Read error: %s", strerror(errno)); | |
31034792 ML |
116 | return SR_ERR; |
117 | } | |
118 | ||
05c644ea ML |
119 | uscpi->response_length = len; |
120 | uscpi->response_bytes_read = 0; | |
31034792 ML |
121 | |
122 | return SR_OK; | |
123 | } | |
124 | ||
d87c1766 | 125 | static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen) |
a1ff9c18 | 126 | { |
05c644ea ML |
127 | struct usbtmc_scpi *uscpi = priv; |
128 | int read_length; | |
a1ff9c18 | 129 | |
8ae157d9 AJ |
130 | if (uscpi->response_length == MAX_READ_LENGTH |
131 | && uscpi->response_bytes_read == uscpi->response_length) | |
132 | if (scpi_usbtmc_read_begin(uscpi) != SR_OK) | |
133 | return SR_ERR; | |
134 | ||
05c644ea | 135 | if (uscpi->response_bytes_read >= uscpi->response_length) |
a1ff9c18 | 136 | return SR_ERR; |
a1ff9c18 | 137 | |
05c644ea ML |
138 | read_length = uscpi->response_length - uscpi->response_bytes_read; |
139 | ||
140 | if (read_length > maxlen) | |
141 | read_length = maxlen; | |
142 | ||
143 | memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length); | |
144 | ||
145 | uscpi->response_bytes_read += read_length; | |
146 | ||
147 | return read_length; | |
148 | } | |
149 | ||
d87c1766 | 150 | static int scpi_usbtmc_read_complete(void *priv) |
05c644ea ML |
151 | { |
152 | struct usbtmc_scpi *uscpi = priv; | |
153 | ||
8ae157d9 AJ |
154 | if (uscpi->response_length == MAX_READ_LENGTH |
155 | && uscpi->response_bytes_read == uscpi->response_length) | |
156 | scpi_usbtmc_read_begin(uscpi); | |
157 | ||
05c644ea | 158 | return (uscpi->response_bytes_read >= uscpi->response_length); |
a1ff9c18 ML |
159 | } |
160 | ||
d87c1766 | 161 | static int scpi_usbtmc_close(void *priv) |
31034792 | 162 | { |
05c644ea | 163 | struct usbtmc_scpi *uscpi = priv; |
31034792 | 164 | |
f754c146 | 165 | if (close(uscpi->usbtmc->fd) < 0) |
31034792 ML |
166 | return SR_ERR; |
167 | ||
168 | return SR_OK; | |
169 | } | |
170 | ||
91e406b9 BV |
171 | static void scpi_usbtmc_free(void *priv) |
172 | { | |
05c644ea | 173 | struct usbtmc_scpi *uscpi = priv; |
05c644ea | 174 | |
f754c146 | 175 | sr_usbtmc_dev_inst_free(uscpi->usbtmc); |
91e406b9 BV |
176 | } |
177 | ||
f754c146 AJ |
178 | SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = { |
179 | .name = "USBTMC", | |
180 | .prefix = "/dev/usbtmc", | |
181 | .priv_size = sizeof(struct usbtmc_scpi), | |
182 | .dev_inst_new = scpi_usbtmc_dev_inst_new, | |
183 | .open = scpi_usbtmc_open, | |
184 | .source_add = scpi_usbtmc_source_add, | |
185 | .source_remove = scpi_usbtmc_source_remove, | |
186 | .send = scpi_usbtmc_send, | |
187 | .read_begin = scpi_usbtmc_read_begin, | |
188 | .read_data = scpi_usbtmc_read_data, | |
189 | .read_complete = scpi_usbtmc_read_complete, | |
190 | .close = scpi_usbtmc_close, | |
191 | .free = scpi_usbtmc_free, | |
192 | }; |