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