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