]>
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_u12xx[]; | |
43 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
44 | extern const struct agdmm_recv agdmm_recvs_u124x[]; | |
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_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
53 | { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
54 | { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
55 | ||
56 | { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
57 | { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
58 | ||
59 | { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
60 | { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
61 | { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
62 | { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
63 | { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
64 | { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
65 | ALL_ZERO | |
66 | }; | |
67 | ||
68 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
69 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
70 | ||
71 | static int init(struct sr_context *sr_ctx) | |
72 | { | |
73 | return std_init(sr_ctx, di, LOG_PREFIX); | |
74 | } | |
75 | ||
76 | static GSList *scan(GSList *options) | |
77 | { | |
78 | struct sr_dev_inst *sdi; | |
79 | struct drv_context *drvc; | |
80 | struct dev_context *devc; | |
81 | struct sr_config *src; | |
82 | struct sr_channel *ch; | |
83 | struct sr_serial_dev_inst *serial; | |
84 | GSList *l, *devices; | |
85 | int len, i; | |
86 | const char *conn, *serialcomm; | |
87 | char *buf, **tokens; | |
88 | ||
89 | drvc = di->priv; | |
90 | drvc->instances = NULL; | |
91 | ||
92 | devices = NULL; | |
93 | conn = serialcomm = NULL; | |
94 | for (l = options; l; l = l->next) { | |
95 | src = l->data; | |
96 | switch (src->key) { | |
97 | case SR_CONF_CONN: | |
98 | conn = g_variant_get_string(src->data, NULL); | |
99 | break; | |
100 | case SR_CONF_SERIALCOMM: | |
101 | serialcomm = g_variant_get_string(src->data, NULL); | |
102 | break; | |
103 | } | |
104 | } | |
105 | if (!conn) | |
106 | return NULL; | |
107 | if (!serialcomm) | |
108 | serialcomm = SERIALCOMM; | |
109 | ||
110 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
111 | return NULL; | |
112 | ||
113 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
114 | return NULL; | |
115 | ||
116 | serial_flush(serial); | |
117 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
118 | sr_err("Unable to send identification string: %s.", | |
119 | strerror(errno)); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | len = 128; | |
124 | if (!(buf = g_try_malloc(len))) { | |
125 | sr_err("Serial buffer malloc failed."); | |
126 | return NULL; | |
127 | } | |
128 | serial_readline(serial, &buf, &len, 150); | |
129 | if (!len) | |
130 | return NULL; | |
131 | ||
132 | tokens = g_strsplit(buf, ",", 4); | |
133 | if (!strcmp("Agilent Technologies", tokens[0]) | |
134 | && tokens[1] && tokens[2] && tokens[3]) { | |
135 | for (i = 0; supported_agdmm[i].model; i++) { | |
136 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
137 | continue; | |
138 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Agilent", | |
139 | tokens[1], tokens[3]))) | |
140 | return NULL; | |
141 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
142 | sr_err("Device context malloc failed."); | |
143 | return NULL; | |
144 | } | |
145 | devc->profile = &supported_agdmm[i]; | |
146 | devc->cur_mq = -1; | |
147 | sdi->inst_type = SR_INST_SERIAL; | |
148 | sdi->conn = serial; | |
149 | sdi->priv = devc; | |
150 | sdi->driver = di; | |
151 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) | |
152 | return NULL; | |
153 | sdi->channels = g_slist_append(sdi->channels, ch); | |
154 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
155 | devices = g_slist_append(devices, sdi); | |
156 | break; | |
157 | } | |
158 | } | |
159 | g_strfreev(tokens); | |
160 | g_free(buf); | |
161 | ||
162 | serial_close(serial); | |
163 | if (!devices) | |
164 | sr_serial_dev_inst_free(serial); | |
165 | ||
166 | return devices; | |
167 | } | |
168 | ||
169 | static GSList *dev_list(void) | |
170 | { | |
171 | return ((struct drv_context *)(di->priv))->instances; | |
172 | } | |
173 | ||
174 | static int cleanup(void) | |
175 | { | |
176 | return std_dev_clear(di, NULL); | |
177 | } | |
178 | ||
179 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
180 | const struct sr_channel_group *cg) | |
181 | { | |
182 | struct dev_context *devc; | |
183 | ||
184 | (void)cg; | |
185 | ||
186 | if (sdi->status != SR_ST_ACTIVE) | |
187 | return SR_ERR_DEV_CLOSED; | |
188 | ||
189 | if (!(devc = sdi->priv)) { | |
190 | sr_err("sdi->priv was NULL."); | |
191 | return SR_ERR_BUG; | |
192 | } | |
193 | ||
194 | switch (id) { | |
195 | case SR_CONF_LIMIT_MSEC: | |
196 | /* TODO: not yet implemented */ | |
197 | if (g_variant_get_uint64(data) == 0) { | |
198 | sr_err("LIMIT_MSEC can't be 0."); | |
199 | return SR_ERR; | |
200 | } | |
201 | devc->limit_msec = g_variant_get_uint64(data); | |
202 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
203 | devc->limit_msec); | |
204 | break; | |
205 | case SR_CONF_LIMIT_SAMPLES: | |
206 | devc->limit_samples = g_variant_get_uint64(data); | |
207 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
208 | devc->limit_samples); | |
209 | break; | |
210 | default: | |
211 | return SR_ERR_NA; | |
212 | } | |
213 | ||
214 | return SR_OK; | |
215 | } | |
216 | ||
217 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
218 | const struct sr_channel_group *cg) | |
219 | { | |
220 | (void)sdi; | |
221 | (void)cg; | |
222 | ||
223 | switch (key) { | |
224 | case SR_CONF_SCAN_OPTIONS: | |
225 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
226 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
227 | break; | |
228 | case SR_CONF_DEVICE_OPTIONS: | |
229 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
230 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
231 | break; | |
232 | default: | |
233 | return SR_ERR_NA; | |
234 | } | |
235 | ||
236 | return SR_OK; | |
237 | } | |
238 | ||
239 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
240 | { | |
241 | struct dev_context *devc; | |
242 | struct sr_serial_dev_inst *serial; | |
243 | ||
244 | if (sdi->status != SR_ST_ACTIVE) | |
245 | return SR_ERR_DEV_CLOSED; | |
246 | ||
247 | if (!(devc = sdi->priv)) { | |
248 | sr_err("sdi->priv was NULL."); | |
249 | return SR_ERR_BUG; | |
250 | } | |
251 | ||
252 | devc->cb_data = cb_data; | |
253 | ||
254 | /* Send header packet to the session bus. */ | |
255 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
256 | ||
257 | /* Poll every 100ms, or whenever some data comes in. */ | |
258 | serial = sdi->conn; | |
259 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
260 | agdmm_receive_data, (void *)sdi); | |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
265 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
266 | { | |
267 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, | |
268 | sdi->conn, LOG_PREFIX); | |
269 | } | |
270 | ||
271 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
272 | .name = "agilent-dmm", | |
273 | .longname = "Agilent U12xx series DMMs", | |
274 | .api_version = 1, | |
275 | .init = init, | |
276 | .cleanup = cleanup, | |
277 | .scan = scan, | |
278 | .dev_list = dev_list, | |
279 | .dev_clear = NULL, | |
280 | .config_get = NULL, | |
281 | .config_set = config_set, | |
282 | .config_list = config_list, | |
283 | .dev_open = std_serial_dev_open, | |
284 | .dev_close = std_serial_dev_close, | |
285 | .dev_acquisition_start = dev_acquisition_start, | |
286 | .dev_acquisition_stop = dev_acquisition_stop, | |
287 | .priv = NULL, | |
288 | }; |