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