]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_usbtmc.c
Implement SCPI over USBTMC.
[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
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
37SR_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
47SR_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
55SR_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
62SR_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
85SR_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
114SR_PRIV int scpi_usbtmc_close(void *priv)
115{
116 struct sr_usbtmc_dev_inst *usbtmc = priv;
117
118 if (close(usbtmc->fd) < 0)
119 return SR_ERR;
120
121 return SR_OK;
122}
123
124SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
125{
126 struct sr_scpi_dev_inst *scpi;
127 struct sr_usbtmc_dev_inst *usbtmc;
128
129 scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
130
131 if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
132 {
133 g_free(scpi);
134 return NULL;
135 }
136
137 scpi->open = scpi_usbtmc_open;
138 scpi->source_add = scpi_usbtmc_source_add;
139 scpi->source_remove = scpi_usbtmc_source_remove;
140 scpi->send = scpi_usbtmc_send;
141 scpi->receive = scpi_usbtmc_receive;
142 scpi->close = scpi_usbtmc_close;
143 scpi->free = sr_usbtmc_dev_inst_free;
144 scpi->priv = usbtmc;
145
146 return scpi;
147}