]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_usbtmc.c
9648722c20415c125de663461c95a1d421daea5e
[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 /* Message logging helpers with subsystem-specific prefix string. */
30 #define LOG_PREFIX "scpi_usbtmc: "
31 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
32 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
33 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
34 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
35 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
36
37 SR_PRIV int scpi_usbtmc_open(void *priv)
38 {
39         struct sr_usbtmc_dev_inst *usbtmc = priv;
40
41         if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
42                 return SR_ERR;
43
44         return SR_OK;
45 }
46
47 SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
48                         sr_receive_data_callback_t cb, void *cb_data)
49 {
50         struct sr_usbtmc_dev_inst *usbtmc = priv;
51
52         return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
53 }
54
55 SR_PRIV int scpi_usbtmc_source_remove(void *priv)
56 {
57         struct sr_usbtmc_dev_inst *usbtmc = priv;
58
59         return sr_source_remove(usbtmc->fd);
60 }
61
62 SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
63 {
64         struct sr_usbtmc_dev_inst *usbtmc = priv;
65         int len, out;
66
67         len = strlen(command);
68         out = write(usbtmc->fd, command, len);
69
70         if (out < 0) {
71                 sr_err("Write error: %s", strerror(errno));
72                 return SR_ERR;
73         }
74
75         if (out < len) {
76                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
77                        len, command);
78         }
79
80         sr_spew("Successfully sent SCPI command: '%s'.", command);
81
82         return SR_OK;
83 }
84
85 SR_PRIV int scpi_usbtmc_receive(void *priv, char **scpi_response)
86 {
87         struct sr_usbtmc_dev_inst *usbtmc = priv;
88         GString *response;
89         char buf[256];
90         int len;
91
92         response = g_string_sized_new(1024);
93
94         len = read(usbtmc->fd, buf, sizeof(buf));
95
96         if (len < 0) {
97                 sr_err("Read error: %s", strerror(errno));
98                 g_string_free(response, TRUE);
99                 return SR_ERR;
100         }
101
102         response = g_string_append_len(response, buf, len);
103
104         *scpi_response = response->str;
105
106         sr_dbg("SCPI response received (length %d): '%.50s'",
107                response->len, response->str);
108
109         g_string_free(response, FALSE);
110
111         return SR_OK;
112 }
113
114 SR_PRIV int scpi_usbtmc_read(void *priv, char *buf, int maxlen)
115 {
116         struct sr_usbtmc_dev_inst *usbtmc = priv;
117         int len;
118
119         len = read(usbtmc->fd, buf, maxlen);
120
121         if (len < 0) {
122                 sr_err("Read error: %s", strerror(errno));
123                 return SR_ERR;
124         }
125
126         return len;
127 }
128
129 SR_PRIV int scpi_usbtmc_close(void *priv)
130 {
131         struct sr_usbtmc_dev_inst *usbtmc = priv;
132
133         if (close(usbtmc->fd) < 0)
134                 return SR_ERR;
135
136         return SR_OK;
137 }
138
139 static void scpi_usbtmc_free(void *priv)
140 {
141         return sr_usbtmc_dev_inst_free(priv);
142 }
143
144 SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
145 {
146         struct sr_scpi_dev_inst *scpi;
147         struct sr_usbtmc_dev_inst *usbtmc;
148
149         scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
150
151         if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
152         {
153                 g_free(scpi);
154                 return NULL;
155         }
156
157         scpi->open = scpi_usbtmc_open;
158         scpi->source_add = scpi_usbtmc_source_add;
159         scpi->source_remove = scpi_usbtmc_source_remove;
160         scpi->send = scpi_usbtmc_send;
161         scpi->receive = scpi_usbtmc_receive;
162         scpi->read = scpi_usbtmc_read;
163         scpi->close = scpi_usbtmc_close;
164         scpi->free = scpi_usbtmc_free;
165         scpi->priv = usbtmc;
166
167         return scpi;
168 }