]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_usbtmc.c
Revise SCPI read API to allow backend-independent data handling.
[libsigrok.git] / hardware / common / scpi_usbtmc.c
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
29 #define LOG_PREFIX "scpi_usbtmc"
30
31 #define MAX_READ_LENGTH 2048
32
33 struct 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
40 SR_PRIV int scpi_usbtmc_open(void *priv)
41 {
42         struct usbtmc_scpi *uscpi = priv;
43         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
44
45         if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
46                 return SR_ERR;
47
48         return SR_OK;
49 }
50
51 SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
52                         sr_receive_data_callback_t cb, void *cb_data)
53 {
54         struct usbtmc_scpi *uscpi = priv;
55         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
56
57         return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
58 }
59
60 SR_PRIV int scpi_usbtmc_source_remove(void *priv)
61 {
62         struct usbtmc_scpi *uscpi = priv;
63         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
64
65         return sr_source_remove(usbtmc->fd);
66 }
67
68 SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
69 {
70         struct usbtmc_scpi *uscpi = priv;
71         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
72         int len, out;
73
74         len = strlen(command);
75         out = write(usbtmc->fd, command, len);
76
77         if (out < 0) {
78                 sr_err("Write error: %s", strerror(errno));
79                 return SR_ERR;
80         }
81
82         if (out < len) {
83                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
84                        len, command);
85         }
86
87         sr_spew("Successfully sent SCPI command: '%s'.", command);
88
89         return SR_OK;
90 }
91
92 SR_PRIV int scpi_usbtmc_read_begin(void *priv)
93 {
94         struct usbtmc_scpi *uscpi = priv;
95         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
96         int len;
97
98         len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
99
100         if (len < 0) {
101                 sr_err("Read error: %s", strerror(errno));
102                 return SR_ERR;
103         }
104
105         uscpi->response_length = len;
106         uscpi->response_bytes_read = 0;
107
108         return SR_OK;
109 }
110
111 SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
112 {
113         struct usbtmc_scpi *uscpi = priv;
114         int read_length;
115
116         if (uscpi->response_bytes_read >= uscpi->response_length)
117                 return SR_ERR;
118
119         read_length = uscpi->response_length - uscpi->response_bytes_read;
120
121         if (read_length > maxlen)
122                 read_length = maxlen;
123
124         memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
125
126         uscpi->response_bytes_read += read_length;
127
128         return read_length;
129 }
130
131 SR_PRIV int scpi_usbtmc_read_complete(void *priv)
132 {
133         struct usbtmc_scpi *uscpi = priv;
134
135         return (uscpi->response_bytes_read >= uscpi->response_length);
136 }
137
138 SR_PRIV int scpi_usbtmc_close(void *priv)
139 {
140         struct usbtmc_scpi *uscpi = priv;
141         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
142
143         if (close(usbtmc->fd) < 0)
144                 return SR_ERR;
145
146         return SR_OK;
147 }
148
149 static void scpi_usbtmc_free(void *priv)
150 {
151         struct usbtmc_scpi *uscpi = priv;
152         struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
153
154         g_free(uscpi);
155         sr_usbtmc_dev_inst_free(usbtmc);
156 }
157
158 SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
159 {
160         struct sr_scpi_dev_inst *scpi;
161         struct usbtmc_scpi *uscpi;
162         struct sr_usbtmc_dev_inst *usbtmc;
163
164         if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
165                 return NULL;
166
167         uscpi = g_malloc(sizeof(struct usbtmc_scpi));
168
169         uscpi->usbtmc = usbtmc;
170
171         scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
172
173         scpi->open = scpi_usbtmc_open;
174         scpi->source_add = scpi_usbtmc_source_add;
175         scpi->source_remove = scpi_usbtmc_source_remove;
176         scpi->send = scpi_usbtmc_send;
177         scpi->read_begin = scpi_usbtmc_read_begin;
178         scpi->read_data = scpi_usbtmc_read_data;
179         scpi->read_complete = scpi_usbtmc_read_complete;
180         scpi->close = scpi_usbtmc_close;
181         scpi->free = scpi_usbtmc_free;
182         scpi->priv = uscpi;
183
184         return scpi;
185 }