]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_serial.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / scpi / scpi_serial.c
CommitLineData
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
23f43dff 21#include <glib.h>
b541f837 22#include <stdlib.h>
23f43dff 23#include <string.h>
c1aae900 24#include <libsigrok/libsigrok.h>
515ab088 25#include "libsigrok-internal.h"
23f43dff 26
3544f848 27#define LOG_PREFIX "scpi_serial"
23f43dff 28
8943049c 29#define BUFFER_SIZE 1024
23f43dff 30
05c644ea
ML
31struct scpi_serial {
32 struct sr_serial_dev_inst *serial;
8943049c
ML
33 char buffer[BUFFER_SIZE];
34 size_t count;
35 size_t read;
05c644ea
ML
36};
37
329733d9 38static const struct {
b541f837
AJ
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++) {
98fec29e
UH
56 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
57 scpi_serial_usb_ids[i].product_id)))
b541f837
AJ
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
17bdda58
AJ
73static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
74 const char *resource, char **params, const char *serialcomm)
f754c146
AJ
75{
76 struct scpi_serial *sscpi = priv;
77
17bdda58 78 (void)drvc;
f754c146
AJ
79 (void)params;
80
91219afc 81 sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
f754c146
AJ
82
83 return SR_OK;
84}
85
d87c1766 86static int scpi_serial_open(void *priv)
23f43dff 87{
05c644ea
ML
88 struct scpi_serial *sscpi = priv;
89 struct sr_serial_dev_inst *serial = sscpi->serial;
23f43dff 90
406e0c79 91 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
9dfeb81b
ML
92 return SR_ERR;
93
94 if (serial_flush(serial) != SR_OK)
95 return SR_ERR;
96
8943049c
ML
97 sscpi->count = 0;
98 sscpi->read = 0;
99
9dfeb81b 100 return SR_OK;
23f43dff
ML
101}
102
102f1239
BV
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)
23f43dff 105{
05c644ea
ML
106 struct scpi_serial *sscpi = priv;
107 struct sr_serial_dev_inst *serial = sscpi->serial;
23f43dff 108
102f1239 109 return serial_source_add(session, serial, events, timeout, cb, cb_data);
23f43dff
ML
110}
111
102f1239 112static int scpi_serial_source_remove(struct sr_session *session, void *priv)
23f43dff 113{
05c644ea
ML
114 struct scpi_serial *sscpi = priv;
115 struct sr_serial_dev_inst *serial = sscpi->serial;
23f43dff 116
102f1239 117 return serial_source_remove(session, serial);
23f43dff
ML
118}
119
d87c1766 120static int scpi_serial_send(void *priv, const char *command)
23f43dff 121{
721fc227 122 int len, result, written;
23f43dff 123 gchar *terminated_command;
05c644ea
ML
124 struct scpi_serial *sscpi = priv;
125 struct sr_serial_dev_inst *serial = sscpi->serial;
23f43dff
ML
126
127 terminated_command = g_strconcat(command, "\n", NULL);
128 len = strlen(terminated_command);
721fc227
ML
129 written = 0;
130 while (written < len) {
406e0c79
ML
131 result = serial_write_nonblocking(serial,
132 terminated_command + written, len - written);
721fc227
ML
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;
23f43dff
ML
139 }
140
721fc227
ML
141 g_free(terminated_command);
142
23f43dff
ML
143 sr_spew("Successfully sent SCPI command: '%s'.", command);
144
145 return SR_OK;
146}
147
d87c1766 148static int scpi_serial_read_begin(void *priv)
05c644ea 149{
8943049c 150 (void) priv;
05c644ea
ML
151
152 return SR_OK;
153}
154
d87c1766 155static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
bc03a7cc 156{
05c644ea 157 struct scpi_serial *sscpi = priv;
8943049c
ML
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) {
406e0c79 164 ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->read,
8943049c 165 BUFFER_SIZE - sscpi->count);
05c644ea 166
8943049c
ML
167 if (ret < 0)
168 return ret;
05c644ea 169
8943049c 170 sscpi->count += ret;
05c644ea 171
8943049c
ML
172 if (ret > 0)
173 sr_spew("Read %d bytes into buffer.", ret);
05c644ea
ML
174 }
175
8943049c
ML
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;
bc03a7cc 195}
05c644ea 196
d87c1766 197static int scpi_serial_read_complete(void *priv)
05c644ea
ML
198{
199 struct scpi_serial *sscpi = priv;
200
8943049c
ML
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 }
05c644ea
ML
208}
209
bc03a7cc
BV
210static int scpi_serial_close(void *priv)
211{
05c644ea 212 struct scpi_serial *sscpi = priv;
05c644ea 213
f754c146 214 return serial_close(sscpi->serial);
bc03a7cc 215}
05c644ea 216
bc03a7cc
BV
217static void scpi_serial_free(void *priv)
218{
05c644ea 219 struct scpi_serial *sscpi = priv;
05c644ea 220
f754c146 221 sr_serial_dev_inst_free(sscpi->serial);
bc03a7cc
BV
222}
223
f754c146
AJ
224SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
225 .name = "serial",
226 .prefix = "",
227 .priv_size = sizeof(struct scpi_serial),
b541f837 228 .scan = scpi_serial_scan,
f754c146
AJ
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};