]>
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 <strings.h> | |
21 | #include <libsigrok/libsigrok.h> | |
22 | #include "libsigrok-internal.h" | |
23 | #include "scpi.h" | |
24 | ||
25 | #define LOG_PREFIX "scpi/helpers" | |
26 | ||
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" }, | |
34 | }; | |
35 | ||
36 | SR_PRIV const char *sr_vendor_alias(const char *raw_vendor) | |
37 | { | |
38 | unsigned int i; | |
39 | ||
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]; | |
43 | } | |
44 | ||
45 | return raw_vendor; | |
46 | } | |
47 | ||
48 | SR_PRIV const char *scpi_cmd_get(const struct scpi_command *cmdtable, int command) | |
49 | { | |
50 | unsigned int i; | |
51 | const char *cmd; | |
52 | ||
53 | if (!cmdtable) | |
54 | return NULL; | |
55 | ||
56 | cmd = NULL; | |
57 | for (i = 0; cmdtable[i].command; i++) { | |
58 | if (cmdtable[i].command == command) { | |
59 | cmd = cmdtable[i].string; | |
60 | break; | |
61 | } | |
62 | } | |
63 | ||
64 | return cmd; | |
65 | } | |
66 | ||
67 | SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable, | |
68 | int command, ...) | |
69 | { | |
70 | struct sr_scpi_dev_inst *scpi; | |
71 | va_list args; | |
72 | int ret; | |
73 | const char *cmd; | |
74 | ||
75 | if (!(cmd = scpi_cmd_get(cmdtable, command))) { | |
76 | /* Device does not implement this command, that's OK. */ | |
77 | return SR_OK; | |
78 | } | |
79 | ||
80 | scpi = sdi->conn; | |
81 | va_start(args, command); | |
82 | ret = sr_scpi_send_variadic(scpi, cmd, args); | |
83 | va_end(args); | |
84 | ||
85 | return ret; | |
86 | } | |
87 | ||
88 | SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable, | |
89 | GVariant **gvar, const GVariantType *gvtype, int command, ...) | |
90 | { | |
91 | struct sr_scpi_dev_inst *scpi; | |
92 | va_list args; | |
93 | double d; | |
94 | int ret; | |
95 | char *s; | |
96 | const char *cmd; | |
97 | ||
98 | if (!(cmd = scpi_cmd_get(cmdtable, command))) { | |
99 | /* Device does not implement this command, that's OK. */ | |
100 | return SR_OK; | |
101 | } | |
102 | ||
103 | scpi = sdi->conn; | |
104 | va_start(args, command); | |
105 | ret = sr_scpi_send_variadic(scpi, cmd, args); | |
106 | va_end(args); | |
107 | if (ret != SR_OK) | |
108 | return ret; | |
109 | ||
110 | /* Straight SCPI getters to GVariant types. */ | |
111 | if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) { | |
112 | if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK) | |
113 | return ret; | |
114 | if (!strcasecmp(s, "ON") || !strcasecmp(s, "1") | |
115 | || !strcasecmp(s, "YES")) | |
116 | *gvar = g_variant_new_boolean(TRUE); | |
117 | else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0") | |
118 | || !strcasecmp(s, "NO")) | |
119 | *gvar = g_variant_new_boolean(FALSE); | |
120 | else | |
121 | ret = SR_ERR; | |
122 | g_free(s); | |
123 | } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) { | |
124 | if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK) | |
125 | *gvar = g_variant_new_double(d); | |
126 | } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) { | |
127 | if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK) | |
128 | *gvar = g_variant_new_string(s); | |
129 | } else { | |
130 | sr_err("Unable to convert to desired GVariant type."); | |
131 | ret = SR_ERR_NA; | |
132 | } | |
133 | ||
134 | return ret; | |
135 | } |