2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <libsigrok/libsigrok.h>
22 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "scpi/helpers"
27 static const char *scpi_vendors[][2] = {
28 { "HEWLETT-PACKARD", "HP" },
29 { "Agilent Technologies", "Agilent" },
30 { "RIGOL TECHNOLOGIES", "Rigol" },
31 { "PHILIPS", "Philips" },
32 { "CHROMA", "Chroma" },
33 { "Chroma ATE", "Chroma" },
36 SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
40 for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
41 if (!strcasecmp(raw_vendor, scpi_vendors[i][0]))
42 return scpi_vendors[i][1];
48 SR_PRIV const char *scpi_cmd_get(const struct scpi_command *cmdtable, int command)
57 for (i = 0; cmdtable[i].command; i++) {
58 if (cmdtable[i].command == command) {
59 cmd = cmdtable[i].string;
67 SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
70 struct sr_scpi_dev_inst *scpi;
75 if (!(cmd = scpi_cmd_get(cmdtable, command))) {
76 /* Device does not implement this command, that's OK. */
77 /* TODO: deprecate SR_OK_CONTINUE */
78 return SR_OK_CONTINUE;
82 va_start(args, command);
83 ret = sr_scpi_send_variadic(scpi, cmd, args);
89 SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
90 GVariant **gvar, const GVariantType *gvtype, int command, ...)
92 struct sr_scpi_dev_inst *scpi;
99 if (!(cmd = scpi_cmd_get(cmdtable, command))) {
100 /* Device does not implement this command, that's OK. */
101 return SR_OK_CONTINUE;
105 va_start(args, command);
106 ret = sr_scpi_send_variadic(scpi, cmd, args);
111 /* Straight SCPI getters to GVariant types. */
112 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
113 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
115 if (!strcasecmp(s, "ON") || !strcasecmp(s, "1")
116 || !strcasecmp(s, "YES"))
117 *gvar = g_variant_new_boolean(TRUE);
118 else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0")
119 || !strcasecmp(s, "NO"))
120 *gvar = g_variant_new_boolean(FALSE);
124 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
125 if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK)
126 *gvar = g_variant_new_double(d);
127 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
128 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK)
129 *gvar = g_variant_new_string(s);
131 sr_err("Unable to convert to desired GVariant type.");