]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok 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> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "agilent-dmm.h" | |
29 | ||
30 | static const int32_t hwopts[] = { | |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const int32_t hwcaps[] = { | |
36 | SR_CONF_MULTIMETER, | |
37 | SR_CONF_LIMIT_SAMPLES, | |
38 | SR_CONF_LIMIT_MSEC, | |
39 | SR_CONF_CONTINUOUS, | |
40 | }; | |
41 | ||
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[]; | |
46 | ||
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 | ||
51 | static const struct agdmm_profile supported_agdmm[] = { | |
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 } | |
59 | }; | |
60 | ||
61 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
62 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
63 | ||
64 | static int dev_clear(void) | |
65 | { | |
66 | return std_dev_clear(di, NULL); | |
67 | } | |
68 | ||
69 | static int init(struct sr_context *sr_ctx) | |
70 | { | |
71 | return std_init(sr_ctx, di, LOG_PREFIX); | |
72 | } | |
73 | ||
74 | static GSList *scan(GSList *options) | |
75 | { | |
76 | struct sr_dev_inst *sdi; | |
77 | struct drv_context *drvc; | |
78 | struct dev_context *devc; | |
79 | struct sr_config *src; | |
80 | struct sr_probe *probe; | |
81 | struct sr_serial_dev_inst *serial; | |
82 | GSList *l, *devices; | |
83 | int len, i; | |
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) { | |
93 | src = l->data; | |
94 | switch (src->key) { | |
95 | case SR_CONF_CONN: | |
96 | conn = g_variant_get_string(src->data, NULL); | |
97 | break; | |
98 | case SR_CONF_SERIALCOMM: | |
99 | serialcomm = g_variant_get_string(src->data, NULL); | |
100 | break; | |
101 | } | |
102 | } | |
103 | if (!conn) | |
104 | return NULL; | |
105 | if (!serialcomm) | |
106 | serialcomm = SERIALCOMM; | |
107 | ||
108 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
109 | return NULL; | |
110 | ||
111 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
112 | return NULL; | |
113 | ||
114 | serial_flush(serial); | |
115 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
116 | sr_err("Unable to send identification string: %s.", | |
117 | strerror(errno)); | |
118 | return NULL; | |
119 | } | |
120 | ||
121 | len = 128; | |
122 | if (!(buf = g_try_malloc(len))) { | |
123 | sr_err("Serial buffer malloc failed."); | |
124 | return NULL; | |
125 | } | |
126 | serial_readline(serial, &buf, &len, 150); | |
127 | if (!len) | |
128 | return NULL; | |
129 | ||
130 | tokens = g_strsplit(buf, ",", 4); | |
131 | if (!strcmp("Agilent Technologies", tokens[0]) | |
132 | && tokens[1] && tokens[2] && tokens[3]) { | |
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)))) { | |
140 | sr_err("Device context malloc failed."); | |
141 | return NULL; | |
142 | } | |
143 | devc->profile = &supported_agdmm[i]; | |
144 | devc->cur_mq = -1; | |
145 | sdi->inst_type = SR_INST_SERIAL; | |
146 | sdi->conn = serial; | |
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 | ||
160 | serial_close(serial); | |
161 | if (!devices) | |
162 | sr_serial_dev_inst_free(serial); | |
163 | ||
164 | return devices; | |
165 | } | |
166 | ||
167 | static GSList *dev_list(void) | |
168 | { | |
169 | return ((struct drv_context *)(di->priv))->instances; | |
170 | } | |
171 | ||
172 | static int dev_open(struct sr_dev_inst *sdi) | |
173 | { | |
174 | struct sr_serial_dev_inst *serial; | |
175 | ||
176 | serial = sdi->conn; | |
177 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
178 | return SR_ERR; | |
179 | ||
180 | sdi->status = SR_ST_ACTIVE; | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int dev_close(struct sr_dev_inst *sdi) | |
186 | { | |
187 | struct sr_serial_dev_inst *serial; | |
188 | ||
189 | serial = sdi->conn; | |
190 | if (serial && serial->fd != -1) { | |
191 | serial_close(serial); | |
192 | sdi->status = SR_ST_INACTIVE; | |
193 | } | |
194 | ||
195 | return SR_OK; | |
196 | } | |
197 | ||
198 | static int cleanup(void) | |
199 | { | |
200 | return dev_clear(); | |
201 | } | |
202 | ||
203 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) | |
204 | { | |
205 | struct dev_context *devc; | |
206 | ||
207 | if (sdi->status != SR_ST_ACTIVE) | |
208 | return SR_ERR_DEV_CLOSED; | |
209 | ||
210 | if (!(devc = sdi->priv)) { | |
211 | sr_err("sdi->priv was NULL."); | |
212 | return SR_ERR_BUG; | |
213 | } | |
214 | ||
215 | switch (id) { | |
216 | case SR_CONF_LIMIT_MSEC: | |
217 | /* TODO: not yet implemented */ | |
218 | if (g_variant_get_uint64(data) == 0) { | |
219 | sr_err("LIMIT_MSEC can't be 0."); | |
220 | return SR_ERR; | |
221 | } | |
222 | devc->limit_msec = g_variant_get_uint64(data); | |
223 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
224 | devc->limit_msec); | |
225 | break; | |
226 | case SR_CONF_LIMIT_SAMPLES: | |
227 | devc->limit_samples = g_variant_get_uint64(data); | |
228 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
229 | devc->limit_samples); | |
230 | break; | |
231 | default: | |
232 | return SR_ERR_NA; | |
233 | } | |
234 | ||
235 | return SR_OK; | |
236 | } | |
237 | ||
238 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
239 | { | |
240 | ||
241 | (void)sdi; | |
242 | ||
243 | switch (key) { | |
244 | case SR_CONF_SCAN_OPTIONS: | |
245 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
246 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
247 | break; | |
248 | case SR_CONF_DEVICE_OPTIONS: | |
249 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
250 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
251 | break; | |
252 | default: | |
253 | return SR_ERR_NA; | |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
260 | { | |
261 | struct dev_context *devc; | |
262 | struct sr_serial_dev_inst *serial; | |
263 | ||
264 | if (sdi->status != SR_ST_ACTIVE) | |
265 | return SR_ERR_DEV_CLOSED; | |
266 | ||
267 | if (!(devc = sdi->priv)) { | |
268 | sr_err("sdi->priv was NULL."); | |
269 | return SR_ERR_BUG; | |
270 | } | |
271 | ||
272 | devc->cb_data = cb_data; | |
273 | ||
274 | /* Send header packet to the session bus. */ | |
275 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
276 | ||
277 | /* Poll every 100ms, or whenever some data comes in. */ | |
278 | serial = sdi->conn; | |
279 | sr_source_add(serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
280 | ||
281 | return SR_OK; | |
282 | } | |
283 | ||
284 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
285 | { | |
286 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, | |
287 | sdi->conn, LOG_PREFIX); | |
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, | |
294 | .init = init, | |
295 | .cleanup = cleanup, | |
296 | .scan = scan, | |
297 | .dev_list = dev_list, | |
298 | .dev_clear = dev_clear, | |
299 | .config_get = NULL, | |
300 | .config_set = config_set, | |
301 | .config_list = config_list, | |
302 | .dev_open = dev_open, | |
303 | .dev_close = dev_close, | |
304 | .dev_acquisition_start = dev_acquisition_start, | |
305 | .dev_acquisition_stop = dev_acquisition_stop, | |
306 | .priv = NULL, | |
307 | }; |