]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Bert Vermeulen <bert@biot.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 <config.h> | |
21 | #include <strings.h> | |
22 | #include <libsigrok/libsigrok.h> | |
23 | #include "libsigrok-internal.h" | |
24 | #include "scpi.h" | |
25 | ||
26 | #define LOG_PREFIX "scpi/helpers" | |
27 | ||
28 | static const char *scpi_vendors[][2] = { | |
29 | { "HEWLETT-PACKARD", "HP" }, | |
30 | { "Agilent Technologies", "Agilent" }, | |
31 | { "RIGOL TECHNOLOGIES", "Rigol" }, | |
32 | { "PHILIPS", "Philips" }, | |
33 | { "CHROMA", "Chroma" }, | |
34 | { "Chroma ATE", "Chroma" }, | |
35 | }; | |
36 | ||
37 | SR_PRIV const char *sr_vendor_alias(const char *raw_vendor) | |
38 | { | |
39 | unsigned int i; | |
40 | ||
41 | for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) { | |
42 | if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0])) | |
43 | return scpi_vendors[i][1]; | |
44 | } | |
45 | ||
46 | return raw_vendor; | |
47 | } | |
48 | ||
49 | SR_PRIV const char *scpi_cmd_get(const struct scpi_command *cmdtable, int command) | |
50 | { | |
51 | unsigned int i; | |
52 | const char *cmd; | |
53 | ||
54 | if (!cmdtable) | |
55 | return NULL; | |
56 | ||
57 | cmd = NULL; | |
58 | for (i = 0; cmdtable[i].string; i++) { | |
59 | if (cmdtable[i].command == command) { | |
60 | cmd = cmdtable[i].string; | |
61 | break; | |
62 | } | |
63 | } | |
64 | ||
65 | return cmd; | |
66 | } | |
67 | ||
68 | SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable, | |
69 | int command, ...) | |
70 | { | |
71 | struct sr_scpi_dev_inst *scpi; | |
72 | va_list args; | |
73 | int ret; | |
74 | const char *cmd; | |
75 | ||
76 | if (!(cmd = scpi_cmd_get(cmdtable, command))) { | |
77 | /* Device does not implement this command, that's OK. */ | |
78 | return SR_OK; | |
79 | } | |
80 | ||
81 | scpi = sdi->conn; | |
82 | va_start(args, command); | |
83 | ret = sr_scpi_send_variadic(scpi, cmd, args); | |
84 | va_end(args); | |
85 | ||
86 | return ret; | |
87 | } | |
88 | ||
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, ...) | |
91 | { | |
92 | struct sr_scpi_dev_inst *scpi; | |
93 | va_list args; | |
94 | double d; | |
95 | int ret; | |
96 | char *s; | |
97 | const char *cmd; | |
98 | ||
99 | if (!(cmd = scpi_cmd_get(cmdtable, command))) { | |
100 | /* Device does not implement this command. */ | |
101 | return SR_ERR_NA; | |
102 | } | |
103 | ||
104 | scpi = sdi->conn; | |
105 | va_start(args, command); | |
106 | ret = sr_scpi_send_variadic(scpi, cmd, args); | |
107 | va_end(args); | |
108 | if (ret != SR_OK) | |
109 | return ret; | |
110 | ||
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) | |
114 | return ret; | |
115 | if (!g_ascii_strcasecmp(s, "ON") || !g_ascii_strcasecmp(s, "1") | |
116 | || !g_ascii_strcasecmp(s, "YES")) | |
117 | *gvar = g_variant_new_boolean(TRUE); | |
118 | else if (!g_ascii_strcasecmp(s, "OFF") || !g_ascii_strcasecmp(s, "0") | |
119 | || !g_ascii_strcasecmp(s, "NO")) | |
120 | *gvar = g_variant_new_boolean(FALSE); | |
121 | else | |
122 | ret = SR_ERR; | |
123 | g_free(s); | |
124 | } else 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 | } else 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); | |
130 | } else { | |
131 | sr_err("Unable to convert to desired GVariant type."); | |
132 | ret = SR_ERR_NA; | |
133 | } | |
134 | ||
135 | return ret; | |
136 | } |