]>
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 dev_open(struct sr_dev_inst *sdi) |
e93cdf42 | 173 | { |
fb3a1505 | 174 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 175 | |
fb3a1505 BV |
176 | serial = sdi->conn; |
177 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
e93cdf42 | 178 | return SR_ERR; |
109a3ba4 | 179 | |
e93cdf42 BV |
180 | sdi->status = SR_ST_ACTIVE; |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
6078d2c9 | 185 | static int dev_close(struct sr_dev_inst *sdi) |
e93cdf42 | 186 | { |
fb3a1505 | 187 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 188 | |
fb3a1505 BV |
189 | serial = sdi->conn; |
190 | if (serial && serial->fd != -1) { | |
191 | serial_close(serial); | |
e93cdf42 BV |
192 | sdi->status = SR_ST_INACTIVE; |
193 | } | |
194 | ||
195 | return SR_OK; | |
196 | } | |
197 | ||
6078d2c9 | 198 | static int cleanup(void) |
e93cdf42 | 199 | { |
3b412e3a | 200 | return dev_clear(); |
e93cdf42 BV |
201 | } |
202 | ||
e44ac12a | 203 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
e93cdf42 BV |
204 | { |
205 | struct dev_context *devc; | |
206 | ||
207 | if (sdi->status != SR_ST_ACTIVE) | |
e73ffd42 | 208 | return SR_ERR_DEV_CLOSED; |
e93cdf42 BV |
209 | |
210 | if (!(devc = sdi->priv)) { | |
38d326e8 | 211 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
212 | return SR_ERR_BUG; |
213 | } | |
214 | ||
035a1078 | 215 | switch (id) { |
1953564a | 216 | case SR_CONF_LIMIT_MSEC: |
e93cdf42 | 217 | /* TODO: not yet implemented */ |
e44ac12a | 218 | if (g_variant_get_uint64(data) == 0) { |
38d326e8 | 219 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
220 | return SR_ERR; |
221 | } | |
e44ac12a | 222 | devc->limit_msec = g_variant_get_uint64(data); |
38d326e8 | 223 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
224 | devc->limit_msec); |
225 | break; | |
1953564a | 226 | case SR_CONF_LIMIT_SAMPLES: |
e44ac12a | 227 | devc->limit_samples = g_variant_get_uint64(data); |
38d326e8 | 228 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
229 | devc->limit_samples); |
230 | break; | |
231 | default: | |
bd6fbf62 | 232 | return SR_ERR_NA; |
e93cdf42 BV |
233 | } |
234 | ||
235 | return SR_OK; | |
236 | } | |
237 | ||
e44ac12a | 238 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
239 | { |
240 | ||
241 | (void)sdi; | |
242 | ||
243 | switch (key) { | |
0d485e30 | 244 | case SR_CONF_SCAN_OPTIONS: |
e44ac12a BV |
245 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
246 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 247 | break; |
9a6517d1 | 248 | case SR_CONF_DEVICE_OPTIONS: |
e44ac12a BV |
249 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
250 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 251 | break; |
a1c743fc | 252 | default: |
bd6fbf62 | 253 | return SR_ERR_NA; |
a1c743fc BV |
254 | } |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
6078d2c9 | 259 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 260 | { |
e93cdf42 | 261 | struct dev_context *devc; |
fb3a1505 | 262 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 263 | |
e73ffd42 BV |
264 | if (sdi->status != SR_ST_ACTIVE) |
265 | return SR_ERR_DEV_CLOSED; | |
266 | ||
e93cdf42 | 267 | if (!(devc = sdi->priv)) { |
38d326e8 | 268 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
269 | return SR_ERR_BUG; |
270 | } | |
271 | ||
e93cdf42 BV |
272 | devc->cb_data = cb_data; |
273 | ||
274 | /* Send header packet to the session bus. */ | |
29a27196 | 275 | std_session_send_df_header(cb_data, LOG_PREFIX); |
e93cdf42 | 276 | |
e93cdf42 | 277 | /* Poll every 100ms, or whenever some data comes in. */ |
fb3a1505 BV |
278 | serial = sdi->conn; |
279 | sr_source_add(serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
e93cdf42 BV |
280 | |
281 | return SR_OK; | |
282 | } | |
283 | ||
6078d2c9 | 284 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 285 | { |
f6beaac5 | 286 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, |
29a27196 | 287 | sdi->conn, LOG_PREFIX); |
e93cdf42 BV |
288 | } |
289 | ||
290 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
291 | .name = "agilent-dmm", | |
292 | .longname = "Agilent U12xx series DMMs", | |
293 | .api_version = 1, | |
6078d2c9 UH |
294 | .init = init, |
295 | .cleanup = cleanup, | |
296 | .scan = scan, | |
297 | .dev_list = dev_list, | |
3b412e3a | 298 | .dev_clear = dev_clear, |
6fab7b8f | 299 | .config_get = NULL, |
035a1078 | 300 | .config_set = config_set, |
a1c743fc | 301 | .config_list = config_list, |
6078d2c9 UH |
302 | .dev_open = dev_open, |
303 | .dev_close = dev_close, | |
304 | .dev_acquisition_start = dev_acquisition_start, | |
305 | .dev_acquisition_stop = dev_acquisition_stop, | |
e93cdf42 BV |
306 | .priv = NULL, |
307 | }; |