]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi.c
scpi: Add helper functions for SCPI communication.
[libsigrok.git] / hardware / common / scpi.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
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
26 /* Message logging helpers with subsystem-specific prefix string. */
27 #define LOG_PREFIX "scpi: "
28 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
29 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
30 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
31 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
32 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
33
34 #define SCPI_READ_RETRIES 100
35 #define SCPI_READ_RETRY_TIMEOUT 10000
36
37 /**
38  * Send a SCPI command.
39  *
40  * @param serial Previously initialized serial port structure.
41  * @param command The SCPI command to send to the device.
42  *
43  * @return SR_OK on success, SR_ERR on failure.
44  */
45 SR_PRIV int sr_scpi_send(struct sr_serial_dev_inst *serial,
46                          const char *command)
47 {
48         int len;
49         int out;
50         gchar *terminated_command;
51
52         terminated_command = g_strconcat(command, "\n", NULL);
53         len = strlen(terminated_command);
54
55         out = serial_write(serial, terminated_command,
56                            strlen(terminated_command));
57
58         g_free(terminated_command);
59
60         if (out != len) {
61                 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
62                        len, command);
63                 return SR_ERR;
64         }
65
66         sr_spew("Successfully sent SCPI command: '%s'.", command);
67
68         return SR_OK;
69 }
70
71 /**
72  * Send a SCPI command, receive the reply and store the reply in scpi_response.
73  *
74  * @param serial Previously initialized serial port structure.
75  * @param command The SCPI command to send to the device (can be NULL).
76  * @param scpi_response Pointer where to store the scpi response.
77  *
78  * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching a
79  * incomplete or no response. The allocated response must be freed by the caller
80  * in the case of a full response as well in the case of an incomplete.
81  */
82 SR_PRIV int sr_scpi_get_string(struct sr_serial_dev_inst *serial,
83                                  const char *command, char **scpi_response)
84 {
85         int len;
86         int ret;
87         char buf[256];
88         unsigned int i;
89         GString *response;
90
91         if (command)
92                 if (sr_scpi_send(serial, command) != SR_OK)
93                         return SR_ERR;
94
95         response = g_string_sized_new(1024);
96
97         for (i = 0; i <= SCPI_READ_RETRIES; i++) {
98                 while ((len = serial_read(serial, buf, sizeof(buf))) > 0)
99                         response = g_string_append_len(response, buf, len);
100
101                 if (response->len > 0 &&
102                     response->str[response->len-1] == '\n') {
103                         sr_spew("Fetched full SCPI response");
104                         break;
105                 }
106
107                 g_usleep(SCPI_READ_RETRY_TIMEOUT);
108         }
109
110         if (response->len == 0) {
111                 sr_dbg("No SCPI response received");
112                 g_string_free(response, TRUE);
113                 *scpi_response = NULL;
114                 return SR_ERR;
115
116         } else if (response->str[response->len-1] == '\n') {
117                 /*
118                  * The SCPI response contains a LF ('\n') at the end and we
119                  * don't need this so replace it with a '\0' and decrement
120                  * the length.
121                  */
122                 response->str[--response->len] = '\0';
123                 ret = SR_OK;
124
125         } else {
126                 sr_warn("Incomplete SCPI response received!");
127                 ret = SR_ERR;
128         }
129
130         /* Minor optimization: steal the string instead of copying. */
131         *scpi_response = response->str;
132
133         /* A SCPI response can be quite large, print at most 50 characters */
134         sr_dbg("SCPI response for command %s received (length %d): '%.50s'",
135                command, response->len, response->str);
136
137         g_string_free(response, FALSE);
138
139         return ret;
140 }
141
142 /**
143  * Send the *IDN? SCPI command, receive the reply, parse it and store the
144  * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
145  *
146  * @param serial Previously initialized serial port structure.
147  * @param scpi_response Pointer where to store the hw_info structure.
148  *
149  * @return SR_OK upon success, SR_ERR on failure.
150  * The hw_info structure must be freed by the caller with sr_scpi_hw_info_free().
151  */
152 SR_PRIV int sr_scpi_get_hw_id(struct sr_serial_dev_inst *serial,
153                               struct sr_scpi_hw_info **scpi_response)
154 {
155         int num_tokens;
156         char *response;
157         gchar **tokens;
158
159         struct sr_scpi_hw_info *hw_info;
160
161         response = NULL;
162         tokens = NULL;
163
164         if (sr_scpi_get_string(serial, SCPI_CMD_IDN, &response) != SR_OK)
165                 if (!response)
166                         return SR_ERR;
167
168         /*
169          * The response to a '*IDN?' is specified by the SCPI spec. It contains
170          * a comma-separated list containing the manufacturer name, instrument
171          * model, serial number of the instrument and the firmware version.
172          */
173         tokens = g_strsplit(response, ",", 0);
174
175         for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
176
177         if (num_tokens != 4) {
178                 sr_dbg("IDN response not according to spec: %80.s", response);
179                 g_strfreev(tokens);
180                 g_free(response);
181                 return SR_ERR;
182         }
183         g_free(response);
184
185         hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
186         if (!hw_info) {
187                 g_strfreev(tokens);
188                 return SR_ERR_MALLOC;
189         }
190
191         hw_info->manufacturer = g_strdup(tokens[0]);
192         hw_info->model = g_strdup(tokens[1]);
193         hw_info->serial_number = g_strdup(tokens[2]);
194         hw_info->firmware_version = g_strdup(tokens[3]);
195
196         g_strfreev(tokens);
197
198         *scpi_response = hw_info;
199
200         return SR_OK;
201 }
202
203 /**
204  * Free a sr_scpi_hw_info struct.
205  *
206  * @param hw_info Pointer to the struct to free.
207  *
208  * This function is safe to call with a NULL pointer.
209  */
210 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
211 {
212         if (hw_info) {
213                 g_free(hw_info->manufacturer);
214                 g_free(hw_info->model);
215                 g_free(hw_info->serial_number);
216                 g_free(hw_info->firmware_version);
217                 g_free(hw_info);
218         }
219 }