]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_serial.c
udev: Add Siglent SDS1104X-E VID/PID.
[libsigrok.git] / src / scpi / scpi_serial.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <glib.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27 #include "scpi.h"
28
29 #define LOG_PREFIX "scpi_serial"
30
31 #ifdef HAVE_SERIAL_COMM
32
33 struct scpi_serial {
34         struct sr_serial_dev_inst *serial;
35         gboolean got_newline;
36 };
37
38 static const struct {
39         uint16_t vendor_id;
40         uint16_t product_id;
41         const char *serialcomm;
42 } scpi_serial_usb_ids[] = {
43         { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
44         { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
45         { 0x0aad, 0x0118, "115200/8n1" },        /* R&S HMO1002 */
46 };
47
48 static GSList *scpi_serial_scan(struct drv_context *drvc)
49 {
50         GSList *l, *r, *resources = NULL;
51         gchar *res;
52         unsigned i;
53
54         (void)drvc;
55
56         for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
57                 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
58                                         scpi_serial_usb_ids[i].product_id)))
59                         continue;
60                 for (r = l; r; r = r->next) {
61                         if (scpi_serial_usb_ids[i].serialcomm)
62                                 res = g_strdup_printf("%s:%s", (char *) r->data,
63                                                       scpi_serial_usb_ids[i].serialcomm);
64                         else
65                                 res = g_strdup(r->data);
66                         resources = g_slist_append(resources, res);
67                 }
68                 g_slist_free_full(l, g_free);
69         }
70
71         return resources;
72 }
73
74 static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
75                 const char *resource, char **params, const char *serialcomm)
76 {
77         struct scpi_serial *sscpi = priv;
78
79         (void)drvc;
80         (void)params;
81
82         sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
83
84         return SR_OK;
85 }
86
87 static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
88 {
89         struct scpi_serial *sscpi = scpi->priv;
90         struct sr_serial_dev_inst *serial = sscpi->serial;
91
92         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
93                 return SR_ERR;
94
95         if (serial_flush(serial) != SR_OK)
96                 return SR_ERR;
97
98         sscpi->got_newline = FALSE;
99
100         return SR_OK;
101 }
102
103 static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi,
104                 char **connection_id)
105 {
106         struct scpi_serial *sscpi = scpi->priv;
107         struct sr_serial_dev_inst *serial = sscpi->serial;
108
109         *connection_id = g_strdup(serial->port);
110
111         return SR_OK;
112 }
113
114 static int scpi_serial_source_add(struct sr_session *session, void *priv,
115                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
116 {
117         struct scpi_serial *sscpi = priv;
118         struct sr_serial_dev_inst *serial = sscpi->serial;
119
120         return serial_source_add(session, serial, events, timeout, cb, cb_data);
121 }
122
123 static int scpi_serial_source_remove(struct sr_session *session, void *priv)
124 {
125         struct scpi_serial *sscpi = priv;
126         struct sr_serial_dev_inst *serial = sscpi->serial;
127
128         return serial_source_remove(session, serial);
129 }
130
131 static int scpi_serial_send(void *priv, const char *command)
132 {
133         int result;
134         struct scpi_serial *sscpi = priv;
135         struct sr_serial_dev_inst *serial = sscpi->serial;
136
137         result = serial_write_blocking(serial, command, strlen(command), 0);
138         if (result < 0) {
139                 sr_err("Error while sending SCPI command '%s': %d.",
140                         command, result);
141                 return SR_ERR;
142         }
143
144         sr_spew("Successfully sent SCPI command: '%s'.", command);
145
146         return SR_OK;
147 }
148
149 static int scpi_serial_read_begin(void *priv)
150 {
151         struct scpi_serial *sscpi = priv;
152         sscpi->got_newline = FALSE;
153
154         return SR_OK;
155 }
156
157 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
158 {
159         struct scpi_serial *sscpi = priv;
160         int ret;
161
162         /* Try to read new data into the buffer. */
163         ret = serial_read_nonblocking(sscpi->serial, buf, maxlen);
164
165         if (ret < 0)
166                 return ret;
167
168         if (ret > 0) {
169                 if (buf[ret - 1] == '\n') {
170                         sscpi->got_newline = TRUE;
171                         sr_spew("Received terminator");
172                 } else {
173                         sscpi->got_newline = FALSE;
174                 }
175         }
176
177         return ret;
178 }
179
180 static int scpi_serial_read_complete(void *priv)
181 {
182         struct scpi_serial *sscpi = priv;
183
184         return sscpi->got_newline;
185 }
186
187 static int scpi_serial_close(struct sr_scpi_dev_inst *scpi)
188 {
189         struct scpi_serial *sscpi = scpi->priv;
190
191         return serial_close(sscpi->serial);
192 }
193
194 static void scpi_serial_free(void *priv)
195 {
196         struct scpi_serial *sscpi = priv;
197
198         sr_serial_dev_inst_free(sscpi->serial);
199 }
200
201 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
202         .name          = "serial",
203         .prefix        = "",
204         .transport     = SCPI_TRANSPORT_SERIAL,
205         .priv_size     = sizeof(struct scpi_serial),
206         .scan          = scpi_serial_scan,
207         .dev_inst_new  = scpi_serial_dev_inst_new,
208         .open          = scpi_serial_open,
209         .connection_id = scpi_serial_connection_id,
210         .source_add    = scpi_serial_source_add,
211         .source_remove = scpi_serial_source_remove,
212         .send          = scpi_serial_send,
213         .read_begin    = scpi_serial_read_begin,
214         .read_data     = scpi_serial_read_data,
215         .read_complete = scpi_serial_read_complete,
216         .close         = scpi_serial_close,
217         .free          = scpi_serial_free,
218 };
219
220 #endif