]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_usbtmc.c
scpi: add a struct drv_context parameter to scpi_dev_inst_new()
[libsigrok.git] / hardware / common / scpi_usbtmc.c
CommitLineData
31034792
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "libsigrok.h"
21#include "libsigrok-internal.h"
22
23#include <glib.h>
24#include <string.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
28
3544f848 29#define LOG_PREFIX "scpi_usbtmc"
31034792 30
05c644ea
ML
31#define MAX_READ_LENGTH 2048
32
33struct usbtmc_scpi {
34 struct sr_usbtmc_dev_inst *usbtmc;
35 char response_buffer[MAX_READ_LENGTH];
36 int response_length;
37 int response_bytes_read;
38};
39
17bdda58
AJ
40static int scpi_usbtmc_dev_inst_new(void *priv, struct drv_context *drvc,
41 const char *resource, char **params, const char *serialcomm)
f754c146
AJ
42{
43 struct usbtmc_scpi *uscpi = priv;
44
17bdda58 45 (void)drvc;
f754c146
AJ
46 (void)params;
47 (void)serialcomm;
48
49 if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource)))
50 return SR_ERR;
51
52 return SR_OK;
53}
54
d87c1766 55static int scpi_usbtmc_open(void *priv)
31034792 56{
05c644ea
ML
57 struct usbtmc_scpi *uscpi = priv;
58 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
59
60 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
61 return SR_ERR;
62
63 return SR_OK;
64}
65
d87c1766 66static int scpi_usbtmc_source_add(void *priv, int events, int timeout,
31034792
ML
67 sr_receive_data_callback_t cb, void *cb_data)
68{
05c644ea
ML
69 struct usbtmc_scpi *uscpi = priv;
70 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
71
72 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
73}
74
d87c1766 75static int scpi_usbtmc_source_remove(void *priv)
31034792 76{
05c644ea
ML
77 struct usbtmc_scpi *uscpi = priv;
78 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
79
80 return sr_source_remove(usbtmc->fd);
81}
82
d87c1766 83static int scpi_usbtmc_send(void *priv, const char *command)
31034792 84{
05c644ea
ML
85 struct usbtmc_scpi *uscpi = priv;
86 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
87 int len, out;
88
89 len = strlen(command);
90 out = write(usbtmc->fd, command, len);
91
92 if (out < 0) {
93 sr_err("Write error: %s", strerror(errno));
94 return SR_ERR;
95 }
96
97 if (out < len) {
98 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
99 len, command);
100 }
101
102 sr_spew("Successfully sent SCPI command: '%s'.", command);
103
104 return SR_OK;
105}
106
d87c1766 107static int scpi_usbtmc_read_begin(void *priv)
31034792 108{
05c644ea
ML
109 struct usbtmc_scpi *uscpi = priv;
110 struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
31034792
ML
111 int len;
112
05c644ea 113 len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
31034792
ML
114
115 if (len < 0) {
116 sr_err("Read error: %s", strerror(errno));
31034792
ML
117 return SR_ERR;
118 }
119
05c644ea
ML
120 uscpi->response_length = len;
121 uscpi->response_bytes_read = 0;
31034792 122
a849c43a
ML
123 sr_spew("Read %d bytes from device into buffer", len);
124
31034792
ML
125 return SR_OK;
126}
127
d87c1766 128static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
a1ff9c18 129{
05c644ea
ML
130 struct usbtmc_scpi *uscpi = priv;
131 int read_length;
a1ff9c18 132
a849c43a 133 sr_spew("%d bytes requested", maxlen);
8ae157d9 134
a849c43a
ML
135 if (uscpi->response_bytes_read == uscpi->response_length) {
136 sr_spew("Buffer is empty.");
137 if (uscpi->response_length == MAX_READ_LENGTH) {
138 sr_spew("Previous read was of maximum length, reading again.");
139 if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
140 return SR_ERR;
141 } else {
142 return SR_ERR;
143 }
144 }
a1ff9c18 145
05c644ea
ML
146 read_length = uscpi->response_length - uscpi->response_bytes_read;
147
148 if (read_length > maxlen)
149 read_length = maxlen;
150
151 memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
152
153 uscpi->response_bytes_read += read_length;
154
a849c43a
ML
155 sr_spew("Returned %d bytes from buffer, %d/%d bytes of buffer now read",
156 read_length, uscpi->response_bytes_read, uscpi->response_length);
157
05c644ea
ML
158 return read_length;
159}
160
d87c1766 161static int scpi_usbtmc_read_complete(void *priv)
05c644ea
ML
162{
163 struct usbtmc_scpi *uscpi = priv;
164
8ae157d9
AJ
165 if (uscpi->response_length == MAX_READ_LENGTH
166 && uscpi->response_bytes_read == uscpi->response_length)
167 scpi_usbtmc_read_begin(uscpi);
168
05c644ea 169 return (uscpi->response_bytes_read >= uscpi->response_length);
a1ff9c18
ML
170}
171
d87c1766 172static int scpi_usbtmc_close(void *priv)
31034792 173{
05c644ea 174 struct usbtmc_scpi *uscpi = priv;
31034792 175
f754c146 176 if (close(uscpi->usbtmc->fd) < 0)
31034792
ML
177 return SR_ERR;
178
179 return SR_OK;
180}
181
91e406b9
BV
182static void scpi_usbtmc_free(void *priv)
183{
05c644ea 184 struct usbtmc_scpi *uscpi = priv;
05c644ea 185
f754c146 186 sr_usbtmc_dev_inst_free(uscpi->usbtmc);
91e406b9
BV
187}
188
f754c146
AJ
189SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
190 .name = "USBTMC",
191 .prefix = "/dev/usbtmc",
192 .priv_size = sizeof(struct usbtmc_scpi),
193 .dev_inst_new = scpi_usbtmc_dev_inst_new,
194 .open = scpi_usbtmc_open,
195 .source_add = scpi_usbtmc_source_add,
196 .source_remove = scpi_usbtmc_source_remove,
197 .send = scpi_usbtmc_send,
198 .read_begin = scpi_usbtmc_read_begin,
199 .read_data = scpi_usbtmc_read_data,
200 .read_complete = scpi_usbtmc_read_complete,
201 .close = scpi_usbtmc_close,
202 .free = scpi_usbtmc_free,
203};