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