]>
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 | |
05c644ea ML |
31 | struct scpi_serial { |
32 | struct sr_serial_dev_inst *serial; | |
35bb2fce | 33 | gboolean got_newline; |
05c644ea ML |
34 | }; |
35 | ||
329733d9 | 36 | static const struct { |
b541f837 AJ |
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 */ | |
d38f4e7a | 43 | { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO1002 */ |
b541f837 AJ |
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++) { | |
98fec29e UH |
55 | if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id, |
56 | scpi_serial_usb_ids[i].product_id))) | |
b541f837 AJ |
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 | ||
17bdda58 AJ |
72 | static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc, |
73 | const char *resource, char **params, const char *serialcomm) | |
f754c146 AJ |
74 | { |
75 | struct scpi_serial *sscpi = priv; | |
76 | ||
17bdda58 | 77 | (void)drvc; |
f754c146 AJ |
78 | (void)params; |
79 | ||
91219afc | 80 | sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm); |
f754c146 AJ |
81 | |
82 | return SR_OK; | |
83 | } | |
84 | ||
04229f7b | 85 | static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) |
23f43dff | 86 | { |
04229f7b | 87 | struct scpi_serial *sscpi = scpi->priv; |
05c644ea | 88 | struct sr_serial_dev_inst *serial = sscpi->serial; |
23f43dff | 89 | |
406e0c79 | 90 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
9dfeb81b ML |
91 | return SR_ERR; |
92 | ||
93 | if (serial_flush(serial) != SR_OK) | |
94 | return SR_ERR; | |
95 | ||
35bb2fce | 96 | sscpi->got_newline = FALSE; |
8943049c | 97 | |
9dfeb81b | 98 | return SR_OK; |
23f43dff ML |
99 | } |
100 | ||
8107a9a6 FS |
101 | static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi, |
102 | char **connection_id) | |
103 | { | |
104 | struct scpi_serial *sscpi = scpi->priv; | |
105 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
106 | ||
107 | *connection_id = g_strdup(serial->port); | |
108 | ||
109 | return SR_OK; | |
110 | } | |
111 | ||
102f1239 BV |
112 | static int scpi_serial_source_add(struct sr_session *session, void *priv, |
113 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
23f43dff | 114 | { |
05c644ea ML |
115 | struct scpi_serial *sscpi = priv; |
116 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 117 | |
102f1239 | 118 | return serial_source_add(session, serial, events, timeout, cb, cb_data); |
23f43dff ML |
119 | } |
120 | ||
102f1239 | 121 | static int scpi_serial_source_remove(struct sr_session *session, void *priv) |
23f43dff | 122 | { |
05c644ea ML |
123 | struct scpi_serial *sscpi = priv; |
124 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 125 | |
102f1239 | 126 | return serial_source_remove(session, serial); |
23f43dff ML |
127 | } |
128 | ||
d87c1766 | 129 | static int scpi_serial_send(void *priv, const char *command) |
23f43dff | 130 | { |
379e95c5 | 131 | int result; |
05c644ea ML |
132 | struct scpi_serial *sscpi = priv; |
133 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 134 | |
379e95c5 UH |
135 | result = serial_write_blocking(serial, command, strlen(command), 0); |
136 | if (result < 0) { | |
0c8aedb5 UH |
137 | sr_err("Error while sending SCPI command '%s': %d.", |
138 | command, result); | |
379e95c5 | 139 | return SR_ERR; |
23f43dff ML |
140 | } |
141 | ||
142 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
143 | ||
144 | return SR_OK; | |
145 | } | |
146 | ||
d87c1766 | 147 | static int scpi_serial_read_begin(void *priv) |
05c644ea | 148 | { |
9ea4018f | 149 | struct scpi_serial *sscpi = priv; |
35bb2fce | 150 | sscpi->got_newline = FALSE; |
05c644ea ML |
151 | |
152 | return SR_OK; | |
153 | } | |
154 | ||
d87c1766 | 155 | static int scpi_serial_read_data(void *priv, char *buf, int maxlen) |
bc03a7cc | 156 | { |
05c644ea | 157 | struct scpi_serial *sscpi = priv; |
9ea4018f | 158 | int ret; |
8943049c | 159 | |
9ea4018f SB |
160 | /* Try to read new data into the buffer. */ |
161 | ret = serial_read_nonblocking(sscpi->serial, buf, maxlen); | |
05c644ea | 162 | |
9ea4018f SB |
163 | if (ret < 0) |
164 | return ret; | |
05c644ea | 165 | |
9ea4018f | 166 | if (ret > 0) { |
9ea4018f | 167 | if (buf[ret - 1] == '\n') { |
35bb2fce | 168 | sscpi->got_newline = TRUE; |
9ea4018f SB |
169 | sr_spew("Received terminator"); |
170 | } else { | |
35bb2fce | 171 | sscpi->got_newline = FALSE; |
8943049c | 172 | } |
8943049c ML |
173 | } |
174 | ||
9ea4018f | 175 | return ret; |
bc03a7cc | 176 | } |
05c644ea | 177 | |
d87c1766 | 178 | static int scpi_serial_read_complete(void *priv) |
05c644ea ML |
179 | { |
180 | struct scpi_serial *sscpi = priv; | |
181 | ||
9ea4018f | 182 | return sscpi->got_newline; |
05c644ea ML |
183 | } |
184 | ||
04229f7b | 185 | static int scpi_serial_close(struct sr_scpi_dev_inst *scpi) |
bc03a7cc | 186 | { |
04229f7b | 187 | struct scpi_serial *sscpi = scpi->priv; |
05c644ea | 188 | |
f754c146 | 189 | return serial_close(sscpi->serial); |
bc03a7cc | 190 | } |
05c644ea | 191 | |
bc03a7cc BV |
192 | static void scpi_serial_free(void *priv) |
193 | { | |
05c644ea | 194 | struct scpi_serial *sscpi = priv; |
05c644ea | 195 | |
f754c146 | 196 | sr_serial_dev_inst_free(sscpi->serial); |
bc03a7cc BV |
197 | } |
198 | ||
f754c146 AJ |
199 | SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { |
200 | .name = "serial", | |
201 | .prefix = "", | |
202 | .priv_size = sizeof(struct scpi_serial), | |
b541f837 | 203 | .scan = scpi_serial_scan, |
f754c146 AJ |
204 | .dev_inst_new = scpi_serial_dev_inst_new, |
205 | .open = scpi_serial_open, | |
8107a9a6 | 206 | .connection_id = scpi_serial_connection_id, |
f754c146 AJ |
207 | .source_add = scpi_serial_source_add, |
208 | .source_remove = scpi_serial_source_remove, | |
209 | .send = scpi_serial_send, | |
210 | .read_begin = scpi_serial_read_begin, | |
211 | .read_data = scpi_serial_read_data, | |
212 | .read_complete = scpi_serial_read_complete, | |
213 | .close = scpi_serial_close, | |
214 | .free = scpi_serial_free, | |
215 | }; |