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