]> sigrok.org Git - libsigrok.git/blame_incremental - src/scpi/scpi_serial.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / scpi / scpi_serial.c
... / ...
CommitLineData
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 <glib.h>
22#include <stdlib.h>
23#include <string.h>
24#include <libsigrok/libsigrok.h>
25#include "libsigrok-internal.h"
26
27#define LOG_PREFIX "scpi_serial"
28
29#define BUFFER_SIZE 1024
30
31struct scpi_serial {
32 struct sr_serial_dev_inst *serial;
33 char buffer[BUFFER_SIZE];
34 size_t count;
35 size_t read;
36};
37
38static const struct {
39 uint16_t vendor_id;
40 uint16_t product_id;
41 const char *serialcomm;
42} scpi_serial_usb_ids[] = {
43 { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
44 { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
45};
46
47static GSList *scpi_serial_scan(struct drv_context *drvc)
48{
49 GSList *l, *r, *resources = NULL;
50 gchar *res;
51 unsigned i;
52
53 (void)drvc;
54
55 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
56 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
57 scpi_serial_usb_ids[i].product_id)))
58 continue;
59 for (r = l; r; r = r->next) {
60 if (scpi_serial_usb_ids[i].serialcomm)
61 res = g_strdup_printf("%s:%s", (char *) r->data,
62 scpi_serial_usb_ids[i].serialcomm);
63 else
64 res = g_strdup(r->data);
65 resources = g_slist_append(resources, res);
66 }
67 g_slist_free_full(l, g_free);
68 }
69
70 return resources;
71}
72
73static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
74 const char *resource, char **params, const char *serialcomm)
75{
76 struct scpi_serial *sscpi = priv;
77
78 (void)drvc;
79 (void)params;
80
81 sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
82
83 return SR_OK;
84}
85
86static int scpi_serial_open(void *priv)
87{
88 struct scpi_serial *sscpi = priv;
89 struct sr_serial_dev_inst *serial = sscpi->serial;
90
91 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
92 return SR_ERR;
93
94 if (serial_flush(serial) != SR_OK)
95 return SR_ERR;
96
97 sscpi->count = 0;
98 sscpi->read = 0;
99
100 return SR_OK;
101}
102
103static int scpi_serial_source_add(struct sr_session *session, void *priv,
104 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
105{
106 struct scpi_serial *sscpi = priv;
107 struct sr_serial_dev_inst *serial = sscpi->serial;
108
109 return serial_source_add(session, serial, events, timeout, cb, cb_data);
110}
111
112static int scpi_serial_source_remove(struct sr_session *session, void *priv)
113{
114 struct scpi_serial *sscpi = priv;
115 struct sr_serial_dev_inst *serial = sscpi->serial;
116
117 return serial_source_remove(session, serial);
118}
119
120static int scpi_serial_send(void *priv, const char *command)
121{
122 int len, result, written;
123 gchar *terminated_command;
124 struct scpi_serial *sscpi = priv;
125 struct sr_serial_dev_inst *serial = sscpi->serial;
126
127 terminated_command = g_strconcat(command, "\n", NULL);
128 len = strlen(terminated_command);
129 written = 0;
130 while (written < len) {
131 result = serial_write_nonblocking(serial,
132 terminated_command + written, len - written);
133 if (result < 0) {
134 sr_err("Error while sending SCPI command: '%s'.", command);
135 g_free(terminated_command);
136 return SR_ERR;
137 }
138 written += result;
139 }
140
141 g_free(terminated_command);
142
143 sr_spew("Successfully sent SCPI command: '%s'.", command);
144
145 return SR_OK;
146}
147
148static int scpi_serial_read_begin(void *priv)
149{
150 (void) priv;
151
152 return SR_OK;
153}
154
155static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
156{
157 struct scpi_serial *sscpi = priv;
158 int len, ret;
159
160 len = BUFFER_SIZE - sscpi->count;
161
162 /* Try to read new data into the buffer if there is space. */
163 if (len > 0) {
164 ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->read,
165 BUFFER_SIZE - sscpi->count);
166
167 if (ret < 0)
168 return ret;
169
170 sscpi->count += ret;
171
172 if (ret > 0)
173 sr_spew("Read %d bytes into buffer.", ret);
174 }
175
176 /* Return as many bytes as possible from buffer, excluding any trailing newline. */
177 if (sscpi->read < sscpi->count) {
178 len = sscpi->count - sscpi->read;
179 if (len > maxlen)
180 len = maxlen;
181 if (sscpi->buffer[sscpi->read + len - 1] == '\n')
182 len--;
183 sr_spew("Returning %d bytes from buffer.", len);
184 memcpy(buf, sscpi->buffer + sscpi->read, len);
185 sscpi->read += len;
186 if (sscpi->read == BUFFER_SIZE) {
187 sr_spew("Resetting buffer.");
188 sscpi->count = 0;
189 sscpi->read = 0;
190 }
191 return len;
192 }
193
194 return 0;
195}
196
197static int scpi_serial_read_complete(void *priv)
198{
199 struct scpi_serial *sscpi = priv;
200
201 /* If the next character is a newline, discard it and report complete. */
202 if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') {
203 sscpi->read++;
204 return 1;
205 } else {
206 return 0;
207 }
208}
209
210static int scpi_serial_close(void *priv)
211{
212 struct scpi_serial *sscpi = priv;
213
214 return serial_close(sscpi->serial);
215}
216
217static void scpi_serial_free(void *priv)
218{
219 struct scpi_serial *sscpi = priv;
220
221 sr_serial_dev_inst_free(sscpi->serial);
222}
223
224SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
225 .name = "serial",
226 .prefix = "",
227 .priv_size = sizeof(struct scpi_serial),
228 .scan = scpi_serial_scan,
229 .dev_inst_new = scpi_serial_dev_inst_new,
230 .open = scpi_serial_open,
231 .source_add = scpi_serial_source_add,
232 .source_remove = scpi_serial_source_remove,
233 .send = scpi_serial_send,
234 .read_begin = scpi_serial_read_begin,
235 .read_data = scpi_serial_read_data,
236 .read_complete = scpi_serial_read_complete,
237 .close = scpi_serial_close,
238 .free = scpi_serial_free,
239};