]>
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 | struct scpi_serial { | |
32 | struct sr_serial_dev_inst *serial; | |
33 | gboolean got_newline; | |
34 | }; | |
35 | ||
36 | static const struct { | |
37 | uint16_t vendor_id; | |
38 | uint16_t product_id; | |
39 | const char *serialcomm; | |
40 | } scpi_serial_usb_ids[] = { | |
41 | { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */ | |
42 | { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */ | |
43 | { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO1002 */ | |
44 | }; | |
45 | ||
46 | static GSList *scpi_serial_scan(struct drv_context *drvc) | |
47 | { | |
48 | GSList *l, *r, *resources = NULL; | |
49 | gchar *res; | |
50 | unsigned i; | |
51 | ||
52 | (void)drvc; | |
53 | ||
54 | for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) { | |
55 | if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id, | |
56 | scpi_serial_usb_ids[i].product_id))) | |
57 | continue; | |
58 | for (r = l; r; r = r->next) { | |
59 | if (scpi_serial_usb_ids[i].serialcomm) | |
60 | res = g_strdup_printf("%s:%s", (char *) r->data, | |
61 | scpi_serial_usb_ids[i].serialcomm); | |
62 | else | |
63 | res = g_strdup(r->data); | |
64 | resources = g_slist_append(resources, res); | |
65 | } | |
66 | g_slist_free_full(l, g_free); | |
67 | } | |
68 | ||
69 | return resources; | |
70 | } | |
71 | ||
72 | static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc, | |
73 | const char *resource, char **params, const char *serialcomm) | |
74 | { | |
75 | struct scpi_serial *sscpi = priv; | |
76 | ||
77 | (void)drvc; | |
78 | (void)params; | |
79 | ||
80 | sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm); | |
81 | ||
82 | return SR_OK; | |
83 | } | |
84 | ||
85 | static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) | |
86 | { | |
87 | struct scpi_serial *sscpi = scpi->priv; | |
88 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
89 | ||
90 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
91 | return SR_ERR; | |
92 | ||
93 | if (serial_flush(serial) != SR_OK) | |
94 | return SR_ERR; | |
95 | ||
96 | sscpi->got_newline = FALSE; | |
97 | ||
98 | return SR_OK; | |
99 | } | |
100 | ||
101 | static int scpi_serial_source_add(struct sr_session *session, void *priv, | |
102 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
103 | { | |
104 | struct scpi_serial *sscpi = priv; | |
105 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
106 | ||
107 | return serial_source_add(session, serial, events, timeout, cb, cb_data); | |
108 | } | |
109 | ||
110 | static int scpi_serial_source_remove(struct sr_session *session, void *priv) | |
111 | { | |
112 | struct scpi_serial *sscpi = priv; | |
113 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
114 | ||
115 | return serial_source_remove(session, serial); | |
116 | } | |
117 | ||
118 | static int scpi_serial_send(void *priv, const char *command) | |
119 | { | |
120 | int result; | |
121 | struct scpi_serial *sscpi = priv; | |
122 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
123 | ||
124 | result = serial_write_blocking(serial, command, strlen(command), 0); | |
125 | if (result < 0) { | |
126 | sr_err("Error while sending SCPI command '%s': %d.", | |
127 | command, result); | |
128 | return SR_ERR; | |
129 | } | |
130 | ||
131 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
132 | ||
133 | return SR_OK; | |
134 | } | |
135 | ||
136 | static int scpi_serial_read_begin(void *priv) | |
137 | { | |
138 | struct scpi_serial *sscpi = priv; | |
139 | sscpi->got_newline = FALSE; | |
140 | ||
141 | return SR_OK; | |
142 | } | |
143 | ||
144 | static int scpi_serial_read_data(void *priv, char *buf, int maxlen) | |
145 | { | |
146 | struct scpi_serial *sscpi = priv; | |
147 | int ret; | |
148 | ||
149 | /* Try to read new data into the buffer. */ | |
150 | ret = serial_read_nonblocking(sscpi->serial, buf, maxlen); | |
151 | ||
152 | if (ret < 0) | |
153 | return ret; | |
154 | ||
155 | if (ret > 0) { | |
156 | if (buf[ret - 1] == '\n') { | |
157 | sscpi->got_newline = TRUE; | |
158 | sr_spew("Received terminator"); | |
159 | } else { | |
160 | sscpi->got_newline = FALSE; | |
161 | } | |
162 | } | |
163 | ||
164 | return ret; | |
165 | } | |
166 | ||
167 | static int scpi_serial_read_complete(void *priv) | |
168 | { | |
169 | struct scpi_serial *sscpi = priv; | |
170 | ||
171 | return sscpi->got_newline; | |
172 | } | |
173 | ||
174 | static int scpi_serial_close(struct sr_scpi_dev_inst *scpi) | |
175 | { | |
176 | struct scpi_serial *sscpi = scpi->priv; | |
177 | ||
178 | return serial_close(sscpi->serial); | |
179 | } | |
180 | ||
181 | static void scpi_serial_free(void *priv) | |
182 | { | |
183 | struct scpi_serial *sscpi = priv; | |
184 | ||
185 | sr_serial_dev_inst_free(sscpi->serial); | |
186 | } | |
187 | ||
188 | SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { | |
189 | .name = "serial", | |
190 | .prefix = "", | |
191 | .priv_size = sizeof(struct scpi_serial), | |
192 | .scan = scpi_serial_scan, | |
193 | .dev_inst_new = scpi_serial_dev_inst_new, | |
194 | .open = scpi_serial_open, | |
195 | .source_add = scpi_serial_source_add, | |
196 | .source_remove = scpi_serial_source_remove, | |
197 | .send = scpi_serial_send, | |
198 | .read_begin = scpi_serial_read_begin, | |
199 | .read_data = scpi_serial_read_data, | |
200 | .read_complete = scpi_serial_read_complete, | |
201 | .close = scpi_serial_close, | |
202 | .free = scpi_serial_free, | |
203 | }; |