]>
Commit | Line | Data |
---|---|---|
23f43dff ML |
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 | ||
6ec6c43b | 21 | #include <config.h> |
23f43dff | 22 | #include <glib.h> |
b541f837 | 23 | #include <stdlib.h> |
23f43dff | 24 | #include <string.h> |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
515ab088 | 26 | #include "libsigrok-internal.h" |
5a1afc09 | 27 | #include "scpi.h" |
23f43dff | 28 | |
3544f848 | 29 | #define LOG_PREFIX "scpi_serial" |
23f43dff | 30 | |
48b7c346 GS |
31 | #ifdef HAVE_SERIAL_COMM |
32 | ||
05c644ea ML |
33 | struct scpi_serial { |
34 | struct sr_serial_dev_inst *serial; | |
35bb2fce | 35 | gboolean got_newline; |
05c644ea ML |
36 | }; |
37 | ||
33084500 | 38 | /* Default serial port options for some known USB devices */ |
329733d9 | 39 | static const struct { |
b541f837 AJ |
40 | uint16_t vendor_id; |
41 | uint16_t product_id; | |
42 | const char *serialcomm; | |
43 | } scpi_serial_usb_ids[] = { | |
44 | { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */ | |
45 | { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */ | |
33084500 GT |
46 | { 0x0aad, 0x0117, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */ |
47 | { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */ | |
48 | { 0x0aad, 0x0119, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */ | |
4e5ff004 | 49 | { 0x2184, 0x0058, "115200/8n1" }, /* GW-Instek GDM-9061 (USBCDC mode) */ |
b541f837 AJ |
50 | }; |
51 | ||
52 | static GSList *scpi_serial_scan(struct drv_context *drvc) | |
53 | { | |
54 | GSList *l, *r, *resources = NULL; | |
55 | gchar *res; | |
56 | unsigned i; | |
57 | ||
58 | (void)drvc; | |
59 | ||
60 | for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) { | |
98fec29e UH |
61 | if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id, |
62 | scpi_serial_usb_ids[i].product_id))) | |
b541f837 AJ |
63 | continue; |
64 | for (r = l; r; r = r->next) { | |
65 | if (scpi_serial_usb_ids[i].serialcomm) | |
66 | res = g_strdup_printf("%s:%s", (char *) r->data, | |
67 | scpi_serial_usb_ids[i].serialcomm); | |
68 | else | |
69 | res = g_strdup(r->data); | |
70 | resources = g_slist_append(resources, res); | |
71 | } | |
72 | g_slist_free_full(l, g_free); | |
73 | } | |
74 | ||
75 | return resources; | |
76 | } | |
77 | ||
17bdda58 AJ |
78 | static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc, |
79 | const char *resource, char **params, const char *serialcomm) | |
f754c146 | 80 | { |
232eb33c GT |
81 | GSList *l, *r; |
82 | unsigned i; | |
f754c146 AJ |
83 | struct scpi_serial *sscpi = priv; |
84 | ||
17bdda58 | 85 | (void)drvc; |
f754c146 AJ |
86 | (void)params; |
87 | ||
232eb33c GT |
88 | /* If no serial port option is specified on the command-line using the |
89 | * "serialcomm" driver option, but the device is connected through USB | |
90 | * and it requires a known default serial port option, then used it in | |
91 | * order to avoid data corruption or even worse problems. | |
92 | */ | |
93 | if (!serialcomm) { | |
94 | for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) { | |
95 | if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id, | |
96 | scpi_serial_usb_ids[i].product_id))) | |
97 | continue; | |
98 | for (r = l; r; r = r->next) | |
99 | if (!strcmp(resource, r->data) && scpi_serial_usb_ids[i].serialcomm) | |
100 | serialcomm = scpi_serial_usb_ids[i].serialcomm; | |
101 | g_slist_free_full(l, g_free); | |
102 | } | |
103 | } | |
104 | ||
91219afc | 105 | sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm); |
f754c146 AJ |
106 | |
107 | return SR_OK; | |
108 | } | |
109 | ||
04229f7b | 110 | static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) |
23f43dff | 111 | { |
04229f7b | 112 | struct scpi_serial *sscpi = scpi->priv; |
05c644ea | 113 | struct sr_serial_dev_inst *serial = sscpi->serial; |
23f43dff | 114 | |
406e0c79 | 115 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
9dfeb81b ML |
116 | return SR_ERR; |
117 | ||
35bb2fce | 118 | sscpi->got_newline = FALSE; |
8943049c | 119 | |
9dfeb81b | 120 | return SR_OK; |
23f43dff ML |
121 | } |
122 | ||
8107a9a6 FS |
123 | static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi, |
124 | char **connection_id) | |
125 | { | |
126 | struct scpi_serial *sscpi = scpi->priv; | |
127 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
128 | ||
129 | *connection_id = g_strdup(serial->port); | |
130 | ||
131 | return SR_OK; | |
132 | } | |
133 | ||
102f1239 BV |
134 | static int scpi_serial_source_add(struct sr_session *session, void *priv, |
135 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
23f43dff | 136 | { |
05c644ea ML |
137 | struct scpi_serial *sscpi = priv; |
138 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 139 | |
102f1239 | 140 | return serial_source_add(session, serial, events, timeout, cb, cb_data); |
23f43dff ML |
141 | } |
142 | ||
102f1239 | 143 | static int scpi_serial_source_remove(struct sr_session *session, void *priv) |
23f43dff | 144 | { |
05c644ea ML |
145 | struct scpi_serial *sscpi = priv; |
146 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 147 | |
102f1239 | 148 | return serial_source_remove(session, serial); |
23f43dff ML |
149 | } |
150 | ||
d87c1766 | 151 | static int scpi_serial_send(void *priv, const char *command) |
23f43dff | 152 | { |
379e95c5 | 153 | int result; |
05c644ea ML |
154 | struct scpi_serial *sscpi = priv; |
155 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 156 | |
379e95c5 UH |
157 | result = serial_write_blocking(serial, command, strlen(command), 0); |
158 | if (result < 0) { | |
0c8aedb5 UH |
159 | sr_err("Error while sending SCPI command '%s': %d.", |
160 | command, result); | |
379e95c5 | 161 | return SR_ERR; |
23f43dff ML |
162 | } |
163 | ||
164 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
165 | ||
166 | return SR_OK; | |
167 | } | |
168 | ||
d87c1766 | 169 | static int scpi_serial_read_begin(void *priv) |
05c644ea | 170 | { |
9ea4018f | 171 | struct scpi_serial *sscpi = priv; |
35bb2fce | 172 | sscpi->got_newline = FALSE; |
05c644ea ML |
173 | |
174 | return SR_OK; | |
175 | } | |
176 | ||
d87c1766 | 177 | static int scpi_serial_read_data(void *priv, char *buf, int maxlen) |
bc03a7cc | 178 | { |
05c644ea | 179 | struct scpi_serial *sscpi = priv; |
9ea4018f | 180 | int ret; |
8943049c | 181 | |
9ea4018f SB |
182 | /* Try to read new data into the buffer. */ |
183 | ret = serial_read_nonblocking(sscpi->serial, buf, maxlen); | |
05c644ea | 184 | |
9ea4018f SB |
185 | if (ret < 0) |
186 | return ret; | |
05c644ea | 187 | |
9ea4018f | 188 | if (ret > 0) { |
9ea4018f | 189 | if (buf[ret - 1] == '\n') { |
35bb2fce | 190 | sscpi->got_newline = TRUE; |
a0ade2f9 TK |
191 | sr_spew("Received NL terminator"); |
192 | } else if (ret > 1 && | |
193 | buf[ret - 2] == '\n' && buf[ret - 1] == '\r') { | |
194 | sscpi->got_newline = TRUE; | |
195 | sr_spew("Received NL+CR terminator"); | |
196 | ret--; | |
9ea4018f | 197 | } else { |
35bb2fce | 198 | sscpi->got_newline = FALSE; |
8943049c | 199 | } |
8943049c ML |
200 | } |
201 | ||
9ea4018f | 202 | return ret; |
bc03a7cc | 203 | } |
05c644ea | 204 | |
d87c1766 | 205 | static int scpi_serial_read_complete(void *priv) |
05c644ea ML |
206 | { |
207 | struct scpi_serial *sscpi = priv; | |
208 | ||
9ea4018f | 209 | return sscpi->got_newline; |
05c644ea ML |
210 | } |
211 | ||
04229f7b | 212 | static int scpi_serial_close(struct sr_scpi_dev_inst *scpi) |
bc03a7cc | 213 | { |
04229f7b | 214 | struct scpi_serial *sscpi = scpi->priv; |
05c644ea | 215 | |
f754c146 | 216 | return serial_close(sscpi->serial); |
bc03a7cc | 217 | } |
05c644ea | 218 | |
bc03a7cc BV |
219 | static void scpi_serial_free(void *priv) |
220 | { | |
05c644ea | 221 | struct scpi_serial *sscpi = priv; |
05c644ea | 222 | |
f754c146 | 223 | sr_serial_dev_inst_free(sscpi->serial); |
bc03a7cc BV |
224 | } |
225 | ||
f754c146 AJ |
226 | SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { |
227 | .name = "serial", | |
228 | .prefix = "", | |
87aa1e63 | 229 | .transport = SCPI_TRANSPORT_SERIAL, |
f754c146 | 230 | .priv_size = sizeof(struct scpi_serial), |
b541f837 | 231 | .scan = scpi_serial_scan, |
f754c146 AJ |
232 | .dev_inst_new = scpi_serial_dev_inst_new, |
233 | .open = scpi_serial_open, | |
8107a9a6 | 234 | .connection_id = scpi_serial_connection_id, |
f754c146 AJ |
235 | .source_add = scpi_serial_source_add, |
236 | .source_remove = scpi_serial_source_remove, | |
237 | .send = scpi_serial_send, | |
238 | .read_begin = scpi_serial_read_begin, | |
239 | .read_data = scpi_serial_read_data, | |
240 | .read_complete = scpi_serial_read_complete, | |
241 | .close = scpi_serial_close, | |
242 | .free = scpi_serial_free, | |
243 | }; | |
48b7c346 GS |
244 | |
245 | #endif |