]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_serial.c
Make SCPI functions device independent, with separate serial backend.
[libsigrok.git] / hardware / common / 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 "libsigrok.h"
22 #include "libsigrok-internal.h"
23
24 #include <glib.h>
25 #include <string.h>
26
27 /* Message logging helpers with subsystem-specific prefix string. */
28 #define LOG_PREFIX "scpi_serial: "
29 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
30 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
31 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
32 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
33 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
34
35 #define SCPI_READ_RETRIES 100
36 #define SCPI_READ_RETRY_TIMEOUT 10000
37
38 SR_PRIV int scpi_serial_open(void *priv)
39 {
40         struct sr_serial_dev_inst *serial = priv;
41
42         return serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK);
43 }
44
45 SR_PRIV int scpi_serial_source_add(void *priv, int events, int timeout,
46                         sr_receive_data_callback_t cb, void *cb_data)
47 {
48         struct sr_serial_dev_inst *serial = priv;
49
50         return sr_source_add(serial->fd, events, timeout, cb, cb_data);
51 }
52
53 SR_PRIV int scpi_serial_source_remove(void *priv)
54 {
55         struct sr_serial_dev_inst *serial = priv;
56
57         return sr_source_remove(serial->fd);
58 }
59
60 SR_PRIV int scpi_serial_send(void *priv, const char *command)
61 {
62         int len, out;
63         gchar *terminated_command;
64         struct sr_serial_dev_inst *serial = priv;
65
66         terminated_command = g_strconcat(command, "\n", NULL);
67         len = strlen(terminated_command);
68         out = serial_write(serial, terminated_command, len);
69         g_free(terminated_command);
70
71         if (out != len) {
72                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
73                        len, command);
74                 return SR_ERR;
75         }
76
77         sr_spew("Successfully sent SCPI command: '%s'.", command);
78
79         return SR_OK;
80 }
81
82 SR_PRIV int scpi_serial_receive(void *priv, char **scpi_response)
83 {
84         int len, ret;
85         char buf[256];
86         unsigned int i;
87         GString *response;
88         struct sr_serial_dev_inst *serial = priv;
89
90         response = g_string_sized_new(1024);
91
92         for (i = 0; i <= SCPI_READ_RETRIES; i++) {
93                 while ((len = serial_read(serial, buf, sizeof(buf))) > 0)
94                         response = g_string_append_len(response, buf, len);
95
96                 if (response->len > 0 &&
97                     response->str[response->len-1] == '\n') {
98                         sr_spew("Fetched full SCPI response.");
99                         break;
100                 }
101
102                 g_usleep(SCPI_READ_RETRY_TIMEOUT);
103         }
104
105         if (response->len == 0) {
106                 sr_dbg("No SCPI response received.");
107                 g_string_free(response, TRUE);
108                 *scpi_response = NULL;
109                 return SR_ERR;
110         } else if (response->str[response->len - 1] == '\n') {
111                 /*
112                  * The SCPI response contains a LF ('\n') at the end and we
113                  * don't need this so replace it with a '\0' and decrement
114                  * the length.
115                  */
116                 response->str[--response->len] = '\0';
117                 ret = SR_OK;
118         } else {
119                 sr_warn("Incomplete SCPI response received!");
120                 ret = SR_ERR;
121         }
122
123         /* Minor optimization: steal the string instead of copying. */
124         *scpi_response = response->str;
125
126         /* A SCPI response can be quite large, print at most 50 characters. */
127         sr_dbg("SCPI response received (length %d): '%.50s'",
128                response->len, response->str);
129
130         g_string_free(response, FALSE);
131
132         return ret;
133 }
134
135 SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port,
136                 const char *serialcomm)
137 {
138         struct sr_scpi_dev_inst *scpi;
139         struct sr_serial_dev_inst *serial;
140
141         scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
142
143         if (!(serial = sr_serial_dev_inst_new(port, serialcomm)))
144         {
145                 g_free(scpi);
146                 return NULL;
147         }
148
149         scpi->open = scpi_serial_open;
150         scpi->source_add = scpi_serial_source_add;
151         scpi->source_remove = scpi_serial_source_remove;
152         scpi->send = scpi_serial_send;
153         scpi->receive = scpi_serial_receive;
154         scpi->close = serial_close;
155         scpi->free = sr_serial_dev_inst_free;
156         scpi->priv = serial;
157
158         return scpi;
159 }