]>
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 | ||
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | ||
24 | #include <glib.h> | |
25 | #include <string.h> | |
26 | ||
3544f848 | 27 | #define LOG_PREFIX "scpi_serial" |
23f43dff ML |
28 | |
29 | #define SCPI_READ_RETRIES 100 | |
30 | #define SCPI_READ_RETRY_TIMEOUT 10000 | |
31 | ||
05c644ea ML |
32 | struct scpi_serial { |
33 | struct sr_serial_dev_inst *serial; | |
34 | char last_character; | |
35 | }; | |
36 | ||
f754c146 AJ |
37 | static int scpi_serial_dev_inst_new(void *priv, const char *resource, |
38 | char **params, const char *serialcomm) | |
39 | { | |
40 | struct scpi_serial *sscpi = priv; | |
41 | ||
42 | (void)params; | |
43 | ||
44 | if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm))) | |
45 | return SR_ERR; | |
46 | ||
47 | return SR_OK; | |
48 | } | |
49 | ||
23f43dff ML |
50 | SR_PRIV int scpi_serial_open(void *priv) |
51 | { | |
05c644ea ML |
52 | struct scpi_serial *sscpi = priv; |
53 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 54 | |
9dfeb81b ML |
55 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
56 | return SR_ERR; | |
57 | ||
58 | if (serial_flush(serial) != SR_OK) | |
59 | return SR_ERR; | |
60 | ||
61 | return SR_OK; | |
23f43dff ML |
62 | } |
63 | ||
64 | SR_PRIV int scpi_serial_source_add(void *priv, int events, int timeout, | |
65 | sr_receive_data_callback_t cb, void *cb_data) | |
66 | { | |
05c644ea ML |
67 | struct scpi_serial *sscpi = priv; |
68 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 69 | |
abc4b335 | 70 | return serial_source_add(serial, events, timeout, cb, cb_data); |
23f43dff ML |
71 | } |
72 | ||
73 | SR_PRIV int scpi_serial_source_remove(void *priv) | |
74 | { | |
05c644ea ML |
75 | struct scpi_serial *sscpi = priv; |
76 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff | 77 | |
7faa3e88 | 78 | return serial_source_remove(serial); |
23f43dff ML |
79 | } |
80 | ||
81 | SR_PRIV int scpi_serial_send(void *priv, const char *command) | |
82 | { | |
721fc227 | 83 | int len, result, written; |
23f43dff | 84 | gchar *terminated_command; |
05c644ea ML |
85 | struct scpi_serial *sscpi = priv; |
86 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff ML |
87 | |
88 | terminated_command = g_strconcat(command, "\n", NULL); | |
89 | len = strlen(terminated_command); | |
721fc227 ML |
90 | written = 0; |
91 | while (written < len) { | |
92 | result = serial_write(serial, terminated_command + written, len - written); | |
93 | if (result < 0) { | |
94 | sr_err("Error while sending SCPI command: '%s'.", command); | |
95 | g_free(terminated_command); | |
96 | return SR_ERR; | |
97 | } | |
98 | written += result; | |
23f43dff ML |
99 | } |
100 | ||
721fc227 ML |
101 | g_free(terminated_command); |
102 | ||
23f43dff ML |
103 | sr_spew("Successfully sent SCPI command: '%s'.", command); |
104 | ||
105 | return SR_OK; | |
106 | } | |
107 | ||
108 | SR_PRIV int scpi_serial_receive(void *priv, char **scpi_response) | |
109 | { | |
110 | int len, ret; | |
111 | char buf[256]; | |
112 | unsigned int i; | |
113 | GString *response; | |
05c644ea ML |
114 | struct scpi_serial *sscpi = priv; |
115 | struct sr_serial_dev_inst *serial = sscpi->serial; | |
23f43dff ML |
116 | |
117 | response = g_string_sized_new(1024); | |
118 | ||
119 | for (i = 0; i <= SCPI_READ_RETRIES; i++) { | |
120 | while ((len = serial_read(serial, buf, sizeof(buf))) > 0) | |
121 | response = g_string_append_len(response, buf, len); | |
122 | ||
123 | if (response->len > 0 && | |
124 | response->str[response->len-1] == '\n') { | |
125 | sr_spew("Fetched full SCPI response."); | |
126 | break; | |
127 | } | |
128 | ||
129 | g_usleep(SCPI_READ_RETRY_TIMEOUT); | |
130 | } | |
131 | ||
132 | if (response->len == 0) { | |
133 | sr_dbg("No SCPI response received."); | |
134 | g_string_free(response, TRUE); | |
135 | *scpi_response = NULL; | |
136 | return SR_ERR; | |
137 | } else if (response->str[response->len - 1] == '\n') { | |
138 | /* | |
139 | * The SCPI response contains a LF ('\n') at the end and we | |
140 | * don't need this so replace it with a '\0' and decrement | |
141 | * the length. | |
142 | */ | |
143 | response->str[--response->len] = '\0'; | |
144 | ret = SR_OK; | |
145 | } else { | |
146 | sr_warn("Incomplete SCPI response received!"); | |
147 | ret = SR_ERR; | |
148 | } | |
149 | ||
150 | /* Minor optimization: steal the string instead of copying. */ | |
151 | *scpi_response = response->str; | |
152 | ||
153 | /* A SCPI response can be quite large, print at most 50 characters. */ | |
154 | sr_dbg("SCPI response received (length %d): '%.50s'", | |
155 | response->len, response->str); | |
156 | ||
157 | g_string_free(response, FALSE); | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
05c644ea ML |
162 | SR_PRIV int scpi_serial_read_begin(void *priv) |
163 | { | |
164 | struct scpi_serial *sscpi = priv; | |
165 | ||
166 | sscpi->last_character = '\0'; | |
167 | ||
168 | return SR_OK; | |
169 | } | |
170 | ||
171 | SR_PRIV int scpi_serial_read_data(void *priv, char *buf, int maxlen) | |
bc03a7cc | 172 | { |
05c644ea ML |
173 | struct scpi_serial *sscpi = priv; |
174 | int ret; | |
175 | ||
176 | ret = serial_read(sscpi->serial, buf, maxlen); | |
177 | ||
178 | if (ret < 0) | |
179 | return ret; | |
180 | ||
181 | if (ret > 0) { | |
182 | sscpi->last_character = buf[ret - 1]; | |
183 | if (sscpi->last_character == '\n') | |
184 | ret--; | |
185 | } | |
186 | ||
187 | return ret; | |
bc03a7cc | 188 | } |
05c644ea ML |
189 | |
190 | SR_PRIV int scpi_serial_read_complete(void *priv) | |
191 | { | |
192 | struct scpi_serial *sscpi = priv; | |
193 | ||
194 | return (sscpi->last_character == '\n'); | |
195 | } | |
196 | ||
bc03a7cc BV |
197 | static int scpi_serial_close(void *priv) |
198 | { | |
05c644ea | 199 | struct scpi_serial *sscpi = priv; |
05c644ea | 200 | |
f754c146 | 201 | return serial_close(sscpi->serial); |
bc03a7cc | 202 | } |
05c644ea | 203 | |
bc03a7cc BV |
204 | static void scpi_serial_free(void *priv) |
205 | { | |
05c644ea | 206 | struct scpi_serial *sscpi = priv; |
05c644ea | 207 | |
f754c146 | 208 | sr_serial_dev_inst_free(sscpi->serial); |
bc03a7cc BV |
209 | } |
210 | ||
f754c146 AJ |
211 | SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { |
212 | .name = "serial", | |
213 | .prefix = "", | |
214 | .priv_size = sizeof(struct scpi_serial), | |
215 | .dev_inst_new = scpi_serial_dev_inst_new, | |
216 | .open = scpi_serial_open, | |
217 | .source_add = scpi_serial_source_add, | |
218 | .source_remove = scpi_serial_source_remove, | |
219 | .send = scpi_serial_send, | |
220 | .read_begin = scpi_serial_read_begin, | |
221 | .read_data = scpi_serial_read_data, | |
222 | .read_complete = scpi_serial_read_complete, | |
223 | .close = scpi_serial_close, | |
224 | .free = scpi_serial_free, | |
225 | }; |