]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com> | |
5 | * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <glib.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <libsigrok/libsigrok.h> | |
26 | #include "libsigrok-internal.h" | |
27 | #include "scpi.h" | |
28 | ||
29 | #define LOG_PREFIX "scpi_serial" | |
30 | ||
31 | #define BUFFER_SIZE 1024 | |
32 | ||
33 | struct scpi_serial { | |
34 | struct sr_serial_dev_inst *serial; | |
35 | char buffer[BUFFER_SIZE]; | |
36 | size_t count; | |
37 | size_t read; | |
38 | }; | |
39 | ||
40 | static const struct { | |
41 | uint16_t vendor_id; | |
42 | uint16_t product_id; | |
43 | const char *serialcomm; | |
44 | } scpi_serial_usb_ids[] = { | |
45 | { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */ | |
46 | { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */ | |
47 | { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO1002 */ | |
48 | }; | |
49 | ||
50 | static GSList *scpi_serial_scan(struct drv_context *drvc) | |
51 | { | |
52 | GSList *l, *r, *resources = NULL; | |
53 | gchar *res; | |
54 | unsigned i; | |
55 | ||
56 | (void)drvc; | |
57 | ||
58 | for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) { | |
59 | if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id, | |
60 | scpi_serial_usb_ids[i].product_id))) | |
61 | continue; | |
62 | for (r = l; r; r = r->next) { | |
63 | if (scpi_serial_usb_ids[i].serialcomm) | |
64 | res = g_strdup_printf("%s:%s", (char *) r->data, | |
65 | scpi_serial_usb_ids[i].serialcomm); | |
66 | else | |
67 | res = g_strdup(r->data); | |
68 | resources = g_slist_append(resources, res); | |
69 | } | |
70 | g_slist_free_full(l, g_free); | |
71 | } | |
72 | ||
73 | return resources; | |
74 | } | |
75 | ||
76 | static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc, | |
77 | const char *resource, char **params, const char *serialcomm) | |
78 | { | |
79 | struct scpi_serial *sscpi = priv; | |
80 | ||
81 | (void)drvc; | |
82 | (void)params; | |
83 | ||
84 | sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm); | |
85 | ||
86 | return SR_OK; | |
87 | } | |
88 | ||
89 | static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) | |
90 | { | |
91 | struct scpi_serial *sscpi = scpi->priv; | |
92 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
93 | ||
94 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
95 | return SR_ERR; | |
96 | ||
97 | if (serial_flush(serial) != SR_OK) | |
98 | return SR_ERR; | |
99 | ||
100 | sscpi->count = 0; | |
101 | sscpi->read = 0; | |
102 | ||
103 | return SR_OK; | |
104 | } | |
105 | ||
106 | static int scpi_serial_source_add(struct sr_session *session, void *priv, | |
107 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
108 | { | |
109 | struct scpi_serial *sscpi = priv; | |
110 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
111 | ||
112 | return serial_source_add(session, serial, events, timeout, cb, cb_data); | |
113 | } | |
114 | ||
115 | static int scpi_serial_source_remove(struct sr_session *session, void *priv) | |
116 | { | |
117 | struct scpi_serial *sscpi = priv; | |
118 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
119 | ||
120 | return serial_source_remove(session, serial); | |
121 | } | |
122 | ||
123 | static int scpi_serial_send(void *priv, const char *command) | |
124 | { | |
125 | int len, result, written; | |
126 | struct scpi_serial *sscpi = priv; | |
127 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
128 | ||
129 | len = strlen(command); | |
130 | written = 0; | |
131 | while (written < len) { | |
132 | result = serial_write_nonblocking(serial, | |
133 | command + written, len - written); | |
134 | if (result < 0) { | |
135 | sr_err("Error while sending SCPI command: '%s'.", command); | |
136 | return SR_ERR; | |
137 | } | |
138 | written += result; | |
139 | } | |
140 | ||
141 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
142 | ||
143 | return SR_OK; | |
144 | } | |
145 | ||
146 | static int scpi_serial_read_begin(void *priv) | |
147 | { | |
148 | (void) priv; | |
149 | ||
150 | return SR_OK; | |
151 | } | |
152 | ||
153 | static int scpi_serial_read_data(void *priv, char *buf, int maxlen) | |
154 | { | |
155 | struct scpi_serial *sscpi = priv; | |
156 | int len, ret; | |
157 | ||
158 | len = BUFFER_SIZE - sscpi->count; | |
159 | ||
160 | /* Try to read new data into the buffer if there is space. */ | |
161 | if (len > 0) { | |
162 | ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->count, | |
163 | BUFFER_SIZE - sscpi->count); | |
164 | ||
165 | if (ret < 0) | |
166 | return ret; | |
167 | ||
168 | sscpi->count += ret; | |
169 | ||
170 | if (ret > 0) | |
171 | sr_spew("Read %d bytes into buffer.", ret); | |
172 | } | |
173 | ||
174 | /* Return as many bytes as possible from buffer, excluding any trailing newline. */ | |
175 | if (sscpi->read < sscpi->count) { | |
176 | len = sscpi->count - sscpi->read; | |
177 | if (len > maxlen) | |
178 | len = maxlen; | |
179 | if (sscpi->buffer[sscpi->read + len - 1] == '\n') | |
180 | len--; | |
181 | sr_spew("Returning %d bytes from buffer.", len); | |
182 | memcpy(buf, sscpi->buffer + sscpi->read, len); | |
183 | sscpi->read += len; | |
184 | if (sscpi->read == BUFFER_SIZE) { | |
185 | sr_spew("Resetting buffer."); | |
186 | sscpi->count = 0; | |
187 | sscpi->read = 0; | |
188 | } | |
189 | return len; | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | static int scpi_serial_read_complete(void *priv) | |
196 | { | |
197 | struct scpi_serial *sscpi = priv; | |
198 | ||
199 | /* If the next character is a newline, discard it and report complete. */ | |
200 | if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') { | |
201 | sscpi->read++; | |
202 | return 1; | |
203 | } else { | |
204 | return 0; | |
205 | } | |
206 | } | |
207 | ||
208 | static int scpi_serial_close(struct sr_scpi_dev_inst *scpi) | |
209 | { | |
210 | struct scpi_serial *sscpi = scpi->priv; | |
211 | ||
212 | return serial_close(sscpi->serial); | |
213 | } | |
214 | ||
215 | static void scpi_serial_free(void *priv) | |
216 | { | |
217 | struct scpi_serial *sscpi = priv; | |
218 | ||
219 | sr_serial_dev_inst_free(sscpi->serial); | |
220 | } | |
221 | ||
222 | SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { | |
223 | .name = "serial", | |
224 | .prefix = "", | |
225 | .priv_size = sizeof(struct scpi_serial), | |
226 | .scan = scpi_serial_scan, | |
227 | .dev_inst_new = scpi_serial_dev_inst_new, | |
228 | .open = scpi_serial_open, | |
229 | .source_add = scpi_serial_source_add, | |
230 | .source_remove = scpi_serial_source_remove, | |
231 | .send = scpi_serial_send, | |
232 | .read_begin = scpi_serial_read_begin, | |
233 | .read_data = scpi_serial_read_data, | |
234 | .read_complete = scpi_serial_read_complete, | |
235 | .close = scpi_serial_close, | |
236 | .free = scpi_serial_free, | |
237 | }; |