]>
Commit | Line | Data |
---|---|---|
e93cdf42 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 | ||
64 | /* Properly close and free all devices. */ | |
65 | static int clear_instances(void) | |
66 | { | |
67 | struct sr_dev_inst *sdi; | |
68 | struct drv_context *drvc; | |
69 | struct dev_context *devc; | |
70 | GSList *l; | |
71 | ||
72 | if (!(drvc = di->priv)) | |
73 | return SR_OK; | |
74 | ||
75 | drvc = di->priv; | |
76 | for (l = drvc->instances; l; l = l->next) { | |
77 | if (!(sdi = l->data)) | |
78 | continue; | |
79 | if (!(devc = sdi->priv)) | |
80 | continue; | |
81 | sr_serial_dev_inst_free(devc->serial); | |
82 | sr_dev_inst_free(sdi); | |
83 | } | |
84 | g_slist_free(drvc->instances); | |
85 | drvc->instances = NULL; | |
86 | ||
87 | return SR_OK; | |
88 | } | |
89 | ||
34f06b90 | 90 | static int hw_init(struct sr_context *sr_ctx) |
e93cdf42 | 91 | { |
063e7aef | 92 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); |
e93cdf42 BV |
93 | } |
94 | ||
e93cdf42 BV |
95 | static GSList *hw_scan(GSList *options) |
96 | { | |
97 | struct sr_dev_inst *sdi; | |
98 | struct drv_context *drvc; | |
99 | struct dev_context *devc; | |
1987b8d6 | 100 | struct sr_config *src; |
e93cdf42 | 101 | struct sr_probe *probe; |
109a3ba4 | 102 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 103 | GSList *l, *devices; |
109a3ba4 | 104 | int len, i; |
e93cdf42 BV |
105 | const char *conn, *serialcomm; |
106 | char *buf, **tokens; | |
107 | ||
108 | drvc = di->priv; | |
109 | drvc->instances = NULL; | |
110 | ||
111 | devices = NULL; | |
112 | conn = serialcomm = NULL; | |
113 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
114 | src = l->data; |
115 | switch (src->key) { | |
1953564a | 116 | case SR_CONF_CONN: |
e44ac12a | 117 | conn = g_variant_get_string(src->data, NULL); |
e93cdf42 | 118 | break; |
1953564a | 119 | case SR_CONF_SERIALCOMM: |
e44ac12a | 120 | serialcomm = g_variant_get_string(src->data, NULL); |
e93cdf42 BV |
121 | break; |
122 | } | |
123 | } | |
74ac7d7f | 124 | if (!conn) |
e93cdf42 | 125 | return NULL; |
109a3ba4 BV |
126 | if (!serialcomm) |
127 | serialcomm = SERIALCOMM; | |
e93cdf42 | 128 | |
109a3ba4 | 129 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
e93cdf42 | 130 | return NULL; |
e93cdf42 | 131 | |
a54dd31e | 132 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 133 | return NULL; |
e93cdf42 | 134 | |
109a3ba4 BV |
135 | serial_flush(serial); |
136 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
137 | sr_err("Unable to send identification string: %s.", |
138 | strerror(errno)); | |
e93cdf42 BV |
139 | return NULL; |
140 | } | |
141 | ||
142 | len = 128; | |
886a52b6 UH |
143 | if (!(buf = g_try_malloc(len))) { |
144 | sr_err("Serial buffer malloc failed."); | |
145 | return NULL; | |
146 | } | |
109a3ba4 | 147 | serial_readline(serial, &buf, &len, 150); |
e93cdf42 BV |
148 | if (!len) |
149 | return NULL; | |
150 | ||
151 | tokens = g_strsplit(buf, ",", 4); | |
152 | if (!strcmp("Agilent Technologies", tokens[0]) | |
153 | && tokens[2] && tokens[3]) { | |
154 | for (i = 0; supported_agdmm[i].model; i++) { | |
155 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
156 | continue; | |
157 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
158 | tokens[1], tokens[3]))) | |
159 | return NULL; | |
160 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 161 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
162 | return NULL; |
163 | } | |
164 | devc->profile = &supported_agdmm[i]; | |
109a3ba4 | 165 | devc->serial = serial; |
e93cdf42 BV |
166 | devc->cur_mq = -1; |
167 | sdi->priv = devc; | |
168 | sdi->driver = di; | |
169 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
170 | return NULL; | |
171 | sdi->probes = g_slist_append(sdi->probes, probe); | |
172 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
173 | devices = g_slist_append(devices, sdi); | |
174 | break; | |
175 | } | |
176 | } | |
177 | g_strfreev(tokens); | |
178 | g_free(buf); | |
179 | ||
109a3ba4 BV |
180 | serial_close(serial); |
181 | if (!devices) | |
182 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
183 | |
184 | return devices; | |
185 | } | |
186 | ||
187 | static GSList *hw_dev_list(void) | |
188 | { | |
0e94d524 | 189 | return ((struct drv_context *)(di->priv))->instances; |
e93cdf42 BV |
190 | } |
191 | ||
192 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
193 | { | |
194 | struct dev_context *devc; | |
195 | ||
196 | if (!(devc = sdi->priv)) { | |
38d326e8 | 197 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
198 | return SR_ERR_BUG; |
199 | } | |
200 | ||
a54dd31e | 201 | if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 202 | return SR_ERR; |
109a3ba4 | 203 | |
e93cdf42 BV |
204 | sdi->status = SR_ST_ACTIVE; |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
209 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
210 | { | |
211 | struct dev_context *devc; | |
212 | ||
961009b0 | 213 | devc = sdi->priv; |
e93cdf42 BV |
214 | |
215 | if (devc->serial && devc->serial->fd != -1) { | |
109a3ba4 | 216 | serial_close(devc->serial); |
e93cdf42 BV |
217 | sdi->status = SR_ST_INACTIVE; |
218 | } | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | static int hw_cleanup(void) | |
224 | { | |
225 | ||
226 | clear_instances(); | |
227 | ||
228 | return SR_OK; | |
229 | } | |
230 | ||
e44ac12a | 231 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
e93cdf42 BV |
232 | { |
233 | struct dev_context *devc; | |
234 | ||
235 | if (sdi->status != SR_ST_ACTIVE) | |
236 | return SR_ERR; | |
237 | ||
238 | if (!(devc = sdi->priv)) { | |
38d326e8 | 239 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
240 | return SR_ERR_BUG; |
241 | } | |
242 | ||
035a1078 | 243 | switch (id) { |
1953564a | 244 | case SR_CONF_LIMIT_MSEC: |
e93cdf42 | 245 | /* TODO: not yet implemented */ |
e44ac12a | 246 | if (g_variant_get_uint64(data) == 0) { |
38d326e8 | 247 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
248 | return SR_ERR; |
249 | } | |
e44ac12a | 250 | devc->limit_msec = g_variant_get_uint64(data); |
38d326e8 | 251 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
252 | devc->limit_msec); |
253 | break; | |
1953564a | 254 | case SR_CONF_LIMIT_SAMPLES: |
e44ac12a | 255 | devc->limit_samples = g_variant_get_uint64(data); |
38d326e8 | 256 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
257 | devc->limit_samples); |
258 | break; | |
259 | default: | |
035a1078 | 260 | sr_err("Unknown capability: %d.", id); |
e93cdf42 BV |
261 | return SR_ERR; |
262 | break; | |
263 | } | |
264 | ||
265 | return SR_OK; | |
266 | } | |
267 | ||
e44ac12a | 268 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
269 | { |
270 | ||
271 | (void)sdi; | |
272 | ||
273 | switch (key) { | |
0d485e30 | 274 | case SR_CONF_SCAN_OPTIONS: |
e44ac12a BV |
275 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
276 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 277 | break; |
9a6517d1 | 278 | case SR_CONF_DEVICE_OPTIONS: |
e44ac12a BV |
279 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
280 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 281 | break; |
a1c743fc BV |
282 | default: |
283 | return SR_ERR_ARG; | |
284 | } | |
285 | ||
286 | return SR_OK; | |
287 | } | |
288 | ||
e93cdf42 BV |
289 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
290 | void *cb_data) | |
291 | { | |
e93cdf42 BV |
292 | struct dev_context *devc; |
293 | ||
294 | if (!(devc = sdi->priv)) { | |
38d326e8 | 295 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
296 | return SR_ERR_BUG; |
297 | } | |
298 | ||
e93cdf42 BV |
299 | devc->cb_data = cb_data; |
300 | ||
301 | /* Send header packet to the session bus. */ | |
4afdfd46 | 302 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); |
e93cdf42 | 303 | |
e93cdf42 BV |
304 | /* Poll every 100ms, or whenever some data comes in. */ |
305 | sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
306 | ||
307 | return SR_OK; | |
308 | } | |
309 | ||
69b07d14 | 310 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 311 | { |
cd2f0fe2 UH |
312 | return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, |
313 | ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); | |
e93cdf42 BV |
314 | } |
315 | ||
316 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
317 | .name = "agilent-dmm", | |
318 | .longname = "Agilent U12xx series DMMs", | |
319 | .api_version = 1, | |
320 | .init = hw_init, | |
321 | .cleanup = hw_cleanup, | |
322 | .scan = hw_scan, | |
323 | .dev_list = hw_dev_list, | |
324 | .dev_clear = clear_instances, | |
6fab7b8f | 325 | .config_get = NULL, |
035a1078 | 326 | .config_set = config_set, |
a1c743fc | 327 | .config_list = config_list, |
e93cdf42 BV |
328 | .dev_open = hw_dev_open, |
329 | .dev_close = hw_dev_close, | |
e93cdf42 BV |
330 | .dev_acquisition_start = hw_dev_acquisition_start, |
331 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
332 | .priv = NULL, | |
333 | }; |