]>
Commit | Line | Data |
---|---|---|
e93cdf42 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
e93cdf42 BV |
3 | * |
4 | * Copyright (C) 2012 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 <glib.h> | |
e93cdf42 BV |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
6f22a8ef UH |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
28 | #include "agilent-dmm.h" | |
e93cdf42 | 29 | |
e44ac12a | 30 | static const int32_t hwopts[] = { |
1953564a BV |
31 | SR_CONF_CONN, |
32 | SR_CONF_SERIALCOMM, | |
e93cdf42 BV |
33 | }; |
34 | ||
e44ac12a | 35 | static const int32_t hwcaps[] = { |
1953564a BV |
36 | SR_CONF_MULTIMETER, |
37 | SR_CONF_LIMIT_SAMPLES, | |
38 | SR_CONF_LIMIT_MSEC, | |
39 | SR_CONF_CONTINUOUS, | |
e93cdf42 BV |
40 | }; |
41 | ||
8c0152f2 BV |
42 | extern const struct agdmm_job agdmm_jobs_u123x[]; |
43 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
44 | extern const struct agdmm_job agdmm_jobs_u125x[]; | |
45 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
e93cdf42 | 46 | |
74ac7d7f BV |
47 | /* This works on all the Agilent U12xxA series, although the |
48 | * U127xA can apparently also run at 19200/8n1. */ | |
49 | #define SERIALCOMM "9600/8n1" | |
50 | ||
e93cdf42 | 51 | static const struct agdmm_profile supported_agdmm[] = { |
74ac7d7f BV |
52 | { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x }, |
53 | { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
54 | { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
55 | { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
56 | { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
57 | { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
58 | { 0, NULL, NULL, NULL } | |
e93cdf42 BV |
59 | }; |
60 | ||
61 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
62 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
63 | ||
3b412e3a | 64 | static int dev_clear(void) |
e93cdf42 | 65 | { |
676c28a8 | 66 | return std_dev_clear(di, NULL); |
e93cdf42 BV |
67 | } |
68 | ||
6078d2c9 | 69 | static int init(struct sr_context *sr_ctx) |
e93cdf42 | 70 | { |
f6beaac5 | 71 | return std_init(sr_ctx, di, LOG_PREFIX); |
e93cdf42 BV |
72 | } |
73 | ||
6078d2c9 | 74 | static GSList *scan(GSList *options) |
e93cdf42 BV |
75 | { |
76 | struct sr_dev_inst *sdi; | |
77 | struct drv_context *drvc; | |
78 | struct dev_context *devc; | |
1987b8d6 | 79 | struct sr_config *src; |
e93cdf42 | 80 | struct sr_probe *probe; |
109a3ba4 | 81 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 82 | GSList *l, *devices; |
109a3ba4 | 83 | int len, i; |
e93cdf42 BV |
84 | const char *conn, *serialcomm; |
85 | char *buf, **tokens; | |
86 | ||
87 | drvc = di->priv; | |
88 | drvc->instances = NULL; | |
89 | ||
90 | devices = NULL; | |
91 | conn = serialcomm = NULL; | |
92 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
93 | src = l->data; |
94 | switch (src->key) { | |
1953564a | 95 | case SR_CONF_CONN: |
e44ac12a | 96 | conn = g_variant_get_string(src->data, NULL); |
e93cdf42 | 97 | break; |
1953564a | 98 | case SR_CONF_SERIALCOMM: |
e44ac12a | 99 | serialcomm = g_variant_get_string(src->data, NULL); |
e93cdf42 BV |
100 | break; |
101 | } | |
102 | } | |
74ac7d7f | 103 | if (!conn) |
e93cdf42 | 104 | return NULL; |
109a3ba4 BV |
105 | if (!serialcomm) |
106 | serialcomm = SERIALCOMM; | |
e93cdf42 | 107 | |
109a3ba4 | 108 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
e93cdf42 | 109 | return NULL; |
e93cdf42 | 110 | |
a54dd31e | 111 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 112 | return NULL; |
e93cdf42 | 113 | |
109a3ba4 BV |
114 | serial_flush(serial); |
115 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
116 | sr_err("Unable to send identification string: %s.", |
117 | strerror(errno)); | |
e93cdf42 BV |
118 | return NULL; |
119 | } | |
120 | ||
121 | len = 128; | |
886a52b6 UH |
122 | if (!(buf = g_try_malloc(len))) { |
123 | sr_err("Serial buffer malloc failed."); | |
124 | return NULL; | |
125 | } | |
109a3ba4 | 126 | serial_readline(serial, &buf, &len, 150); |
e93cdf42 BV |
127 | if (!len) |
128 | return NULL; | |
129 | ||
130 | tokens = g_strsplit(buf, ",", 4); | |
131 | if (!strcmp("Agilent Technologies", tokens[0]) | |
fbf07e02 | 132 | && tokens[1] && tokens[2] && tokens[3]) { |
e93cdf42 BV |
133 | for (i = 0; supported_agdmm[i].model; i++) { |
134 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
135 | continue; | |
136 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
137 | tokens[1], tokens[3]))) | |
138 | return NULL; | |
139 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 140 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
141 | return NULL; |
142 | } | |
143 | devc->profile = &supported_agdmm[i]; | |
e93cdf42 | 144 | devc->cur_mq = -1; |
fb3a1505 BV |
145 | sdi->inst_type = SR_INST_SERIAL; |
146 | sdi->conn = serial; | |
e93cdf42 BV |
147 | sdi->priv = devc; |
148 | sdi->driver = di; | |
149 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
150 | return NULL; | |
151 | sdi->probes = g_slist_append(sdi->probes, probe); | |
152 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
153 | devices = g_slist_append(devices, sdi); | |
154 | break; | |
155 | } | |
156 | } | |
157 | g_strfreev(tokens); | |
158 | g_free(buf); | |
159 | ||
109a3ba4 BV |
160 | serial_close(serial); |
161 | if (!devices) | |
162 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
163 | |
164 | return devices; | |
165 | } | |
166 | ||
6078d2c9 | 167 | static GSList *dev_list(void) |
e93cdf42 | 168 | { |
0e94d524 | 169 | return ((struct drv_context *)(di->priv))->instances; |
e93cdf42 BV |
170 | } |
171 | ||
6078d2c9 | 172 | static int cleanup(void) |
e93cdf42 | 173 | { |
3b412e3a | 174 | return dev_clear(); |
e93cdf42 BV |
175 | } |
176 | ||
8f996b89 ML |
177 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
178 | const struct sr_probe_group *probe_group) | |
e93cdf42 BV |
179 | { |
180 | struct dev_context *devc; | |
181 | ||
8f996b89 ML |
182 | (void)probe_group; |
183 | ||
e93cdf42 | 184 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 185 | return SR_ERR_DEV_CLOSED; |
e93cdf42 BV |
186 | |
187 | if (!(devc = sdi->priv)) { | |
38d326e8 | 188 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
189 | return SR_ERR_BUG; |
190 | } | |
191 | ||
035a1078 | 192 | switch (id) { |
1953564a | 193 | case SR_CONF_LIMIT_MSEC: |
e93cdf42 | 194 | /* TODO: not yet implemented */ |
e44ac12a | 195 | if (g_variant_get_uint64(data) == 0) { |
38d326e8 | 196 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
197 | return SR_ERR; |
198 | } | |
e44ac12a | 199 | devc->limit_msec = g_variant_get_uint64(data); |
38d326e8 | 200 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
201 | devc->limit_msec); |
202 | break; | |
1953564a | 203 | case SR_CONF_LIMIT_SAMPLES: |
e44ac12a | 204 | devc->limit_samples = g_variant_get_uint64(data); |
38d326e8 | 205 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
206 | devc->limit_samples); |
207 | break; | |
208 | default: | |
bd6fbf62 | 209 | return SR_ERR_NA; |
e93cdf42 BV |
210 | } |
211 | ||
212 | return SR_OK; | |
213 | } | |
214 | ||
8f996b89 ML |
215 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
216 | const struct sr_probe_group *probe_group) | |
a1c743fc | 217 | { |
a1c743fc | 218 | (void)sdi; |
8f996b89 | 219 | (void)probe_group; |
a1c743fc BV |
220 | |
221 | switch (key) { | |
0d485e30 | 222 | case SR_CONF_SCAN_OPTIONS: |
e44ac12a BV |
223 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
224 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 225 | break; |
9a6517d1 | 226 | case SR_CONF_DEVICE_OPTIONS: |
e44ac12a BV |
227 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
228 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 229 | break; |
a1c743fc | 230 | default: |
bd6fbf62 | 231 | return SR_ERR_NA; |
a1c743fc BV |
232 | } |
233 | ||
234 | return SR_OK; | |
235 | } | |
236 | ||
6078d2c9 | 237 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 238 | { |
e93cdf42 | 239 | struct dev_context *devc; |
fb3a1505 | 240 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 241 | |
e73ffd42 BV |
242 | if (sdi->status != SR_ST_ACTIVE) |
243 | return SR_ERR_DEV_CLOSED; | |
244 | ||
e93cdf42 | 245 | if (!(devc = sdi->priv)) { |
38d326e8 | 246 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
247 | return SR_ERR_BUG; |
248 | } | |
249 | ||
e93cdf42 BV |
250 | devc->cb_data = cb_data; |
251 | ||
252 | /* Send header packet to the session bus. */ | |
29a27196 | 253 | std_session_send_df_header(cb_data, LOG_PREFIX); |
e93cdf42 | 254 | |
e93cdf42 | 255 | /* Poll every 100ms, or whenever some data comes in. */ |
fb3a1505 | 256 | serial = sdi->conn; |
abc4b335 | 257 | serial_source_add(serial, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); |
e93cdf42 BV |
258 | |
259 | return SR_OK; | |
260 | } | |
261 | ||
6078d2c9 | 262 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 263 | { |
d43b0908 | 264 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, |
29a27196 | 265 | sdi->conn, LOG_PREFIX); |
e93cdf42 BV |
266 | } |
267 | ||
268 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
269 | .name = "agilent-dmm", | |
270 | .longname = "Agilent U12xx series DMMs", | |
271 | .api_version = 1, | |
6078d2c9 UH |
272 | .init = init, |
273 | .cleanup = cleanup, | |
274 | .scan = scan, | |
275 | .dev_list = dev_list, | |
3b412e3a | 276 | .dev_clear = dev_clear, |
6fab7b8f | 277 | .config_get = NULL, |
035a1078 | 278 | .config_set = config_set, |
a1c743fc | 279 | .config_list = config_list, |
854434de | 280 | .dev_open = std_serial_dev_open, |
bf2c987f | 281 | .dev_close = std_serial_dev_close, |
6078d2c9 UH |
282 | .dev_acquisition_start = dev_acquisition_start, |
283 | .dev_acquisition_stop = dev_acquisition_stop, | |
e93cdf42 BV |
284 | .priv = NULL, |
285 | }; |