]>
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 | |
05c644ea | 116 | if (uscpi->response_bytes_read >= uscpi->response_length) |
a1ff9c18 | 117 | return SR_ERR; |
a1ff9c18 | 118 | |
05c644ea ML |
119 | read_length = uscpi->response_length - uscpi->response_bytes_read; |
120 | ||
121 | if (read_length > maxlen) | |
122 | read_length = maxlen; | |
123 | ||
124 | memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length); | |
125 | ||
126 | uscpi->response_bytes_read += read_length; | |
127 | ||
128 | return read_length; | |
129 | } | |
130 | ||
131 | SR_PRIV int scpi_usbtmc_read_complete(void *priv) | |
132 | { | |
133 | struct usbtmc_scpi *uscpi = priv; | |
134 | ||
135 | return (uscpi->response_bytes_read >= uscpi->response_length); | |
a1ff9c18 ML |
136 | } |
137 | ||
31034792 ML |
138 | SR_PRIV int scpi_usbtmc_close(void *priv) |
139 | { | |
05c644ea ML |
140 | struct usbtmc_scpi *uscpi = priv; |
141 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
31034792 ML |
142 | |
143 | if (close(usbtmc->fd) < 0) | |
144 | return SR_ERR; | |
145 | ||
146 | return SR_OK; | |
147 | } | |
148 | ||
91e406b9 BV |
149 | static void scpi_usbtmc_free(void *priv) |
150 | { | |
05c644ea ML |
151 | struct usbtmc_scpi *uscpi = priv; |
152 | struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; | |
153 | ||
154 | g_free(uscpi); | |
155 | sr_usbtmc_dev_inst_free(usbtmc); | |
91e406b9 BV |
156 | } |
157 | ||
31034792 ML |
158 | SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device) |
159 | { | |
160 | struct sr_scpi_dev_inst *scpi; | |
05c644ea | 161 | struct usbtmc_scpi *uscpi; |
31034792 ML |
162 | struct sr_usbtmc_dev_inst *usbtmc; |
163 | ||
31034792 | 164 | if (!(usbtmc = sr_usbtmc_dev_inst_new(device))) |
31034792 | 165 | return NULL; |
05c644ea ML |
166 | |
167 | uscpi = g_malloc(sizeof(struct usbtmc_scpi)); | |
168 | ||
169 | uscpi->usbtmc = usbtmc; | |
170 | ||
171 | scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); | |
31034792 ML |
172 | |
173 | scpi->open = scpi_usbtmc_open; | |
174 | scpi->source_add = scpi_usbtmc_source_add; | |
175 | scpi->source_remove = scpi_usbtmc_source_remove; | |
176 | scpi->send = scpi_usbtmc_send; | |
05c644ea ML |
177 | scpi->read_begin = scpi_usbtmc_read_begin; |
178 | scpi->read_data = scpi_usbtmc_read_data; | |
179 | scpi->read_complete = scpi_usbtmc_read_complete; | |
31034792 | 180 | scpi->close = scpi_usbtmc_close; |
91e406b9 | 181 | scpi->free = scpi_usbtmc_free; |
05c644ea | 182 | scpi->priv = uscpi; |
31034792 ML |
183 | |
184 | return scpi; | |
185 | } |