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