]>
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 "fluke-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 | SR_PRIV struct sr_dev_driver flukedmm_driver_info; | |
43 | static struct sr_dev_driver *di = &flukedmm_driver_info; | |
44 | ||
45 | static char *scan_conn[] = { | |
46 | /* 287/289 */ | |
47 | "115200/8n1", | |
48 | /* 187/189 */ | |
49 | "9600/8n1", | |
50 | /* Scopemeter 190 series */ | |
51 | "1200/8n1", | |
52 | NULL | |
53 | }; | |
54 | ||
55 | static const struct flukedmm_profile supported_flukedmm[] = { | |
56 | { FLUKE_187, "187", 100, 1000 }, | |
57 | { FLUKE_287, "287", 100, 1000 }, | |
58 | { FLUKE_190, "199B", 1000, 3500 }, | |
59 | }; | |
60 | ||
61 | static int dev_clear(void) | |
62 | { | |
63 | return std_dev_clear(di, NULL); | |
64 | } | |
65 | ||
66 | static int init(struct sr_context *sr_ctx) | |
67 | { | |
68 | return std_init(sr_ctx, di, LOG_PREFIX); | |
69 | } | |
70 | ||
71 | static GSList *fluke_scan(const char *conn, const char *serialcomm) | |
72 | { | |
73 | struct sr_dev_inst *sdi; | |
74 | struct drv_context *drvc; | |
75 | struct dev_context *devc; | |
76 | struct sr_probe *probe; | |
77 | struct sr_serial_dev_inst *serial; | |
78 | GSList *devices; | |
79 | int retry, len, i, s; | |
80 | char buf[128], *b, **tokens; | |
81 | ||
82 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
83 | return NULL; | |
84 | ||
85 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
86 | return NULL; | |
87 | ||
88 | drvc = di->priv; | |
89 | b = buf; | |
90 | retry = 0; | |
91 | devices = NULL; | |
92 | /* We'll try the discovery sequence three times in case the device | |
93 | * is not in an idle state when we send ID. */ | |
94 | while (!devices && retry < 3) { | |
95 | retry++; | |
96 | serial_flush(serial); | |
97 | if (serial_write(serial, "ID\r", 3) == -1) { | |
98 | sr_err("Unable to send ID string: %s.", | |
99 | strerror(errno)); | |
100 | continue; | |
101 | } | |
102 | ||
103 | /* Response is first a CMD_ACK byte (ASCII '0' for OK, | |
104 | * or '1' to signify an error. */ | |
105 | len = 128; | |
106 | serial_readline(serial, &b, &len, 150); | |
107 | if (len != 1) | |
108 | continue; | |
109 | if (buf[0] != '0') | |
110 | continue; | |
111 | ||
112 | /* If CMD_ACK was OK, ID string follows. */ | |
113 | len = 128; | |
114 | serial_readline(serial, &b, &len, 850); | |
115 | if (len < 10) | |
116 | continue; | |
117 | if (strcspn(buf, ",") < 15) | |
118 | /* Looks like it's comma-separated. */ | |
119 | tokens = g_strsplit(buf, ",", 3); | |
120 | else | |
121 | /* Fluke 199B, at least, uses semicolon. */ | |
122 | tokens = g_strsplit(buf, ";", 3); | |
123 | if (!strncmp("FLUKE", tokens[0], 5) | |
124 | && tokens[1] && tokens[2]) { | |
125 | for (i = 0; supported_flukedmm[i].model; i++) { | |
126 | if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6)) | |
127 | continue; | |
128 | /* Skip leading spaces in version number. */ | |
129 | for (s = 0; tokens[1][s] == ' '; s++); | |
130 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke", | |
131 | tokens[0] + 6, tokens[1] + s))) | |
132 | return NULL; | |
133 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
134 | sr_err("Device context malloc failed."); | |
135 | return NULL; | |
136 | } | |
137 | devc->profile = &supported_flukedmm[i]; | |
138 | sdi->inst_type = SR_INST_SERIAL; | |
139 | sdi->conn = serial; | |
140 | sdi->priv = devc; | |
141 | sdi->driver = di; | |
142 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
143 | return NULL; | |
144 | sdi->probes = g_slist_append(sdi->probes, probe); | |
145 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
146 | devices = g_slist_append(devices, sdi); | |
147 | break; | |
148 | } | |
149 | } | |
150 | g_strfreev(tokens); | |
151 | if (devices) | |
152 | /* Found one. */ | |
153 | break; | |
154 | } | |
155 | serial_close(serial); | |
156 | if (!devices) | |
157 | sr_serial_dev_inst_free(serial); | |
158 | ||
159 | return devices; | |
160 | } | |
161 | ||
162 | static GSList *scan(GSList *options) | |
163 | { | |
164 | struct sr_config *src; | |
165 | GSList *l, *devices; | |
166 | int i; | |
167 | const char *conn, *serialcomm; | |
168 | ||
169 | conn = serialcomm = NULL; | |
170 | for (l = options; l; l = l->next) { | |
171 | src = l->data; | |
172 | switch (src->key) { | |
173 | case SR_CONF_CONN: | |
174 | conn = g_variant_get_string(src->data, NULL); | |
175 | break; | |
176 | case SR_CONF_SERIALCOMM: | |
177 | serialcomm = g_variant_get_string(src->data, NULL); | |
178 | break; | |
179 | } | |
180 | } | |
181 | if (!conn) | |
182 | return NULL; | |
183 | ||
184 | if (serialcomm) { | |
185 | /* Use the provided comm specs. */ | |
186 | devices = fluke_scan(conn, serialcomm); | |
187 | } else { | |
188 | for (i = 0; scan_conn[i]; i++) { | |
189 | if ((devices = fluke_scan(conn, scan_conn[i]))) | |
190 | break; | |
191 | /* The Scopemeter 199B, at least, requires this | |
192 | * after all the 115k/9.6k confusion. */ | |
193 | g_usleep(5000); | |
194 | } | |
195 | } | |
196 | ||
197 | return devices; | |
198 | } | |
199 | ||
200 | static GSList *dev_list(void) | |
201 | { | |
202 | return ((struct drv_context *)(di->priv))->instances; | |
203 | } | |
204 | ||
205 | static int dev_open(struct sr_dev_inst *sdi) | |
206 | { | |
207 | struct sr_serial_dev_inst *serial; | |
208 | ||
209 | serial = sdi->conn; | |
210 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
211 | return SR_ERR; | |
212 | ||
213 | sdi->status = SR_ST_ACTIVE; | |
214 | ||
215 | return SR_OK; | |
216 | } | |
217 | ||
218 | static int dev_close(struct sr_dev_inst *sdi) | |
219 | { | |
220 | struct sr_serial_dev_inst *serial; | |
221 | ||
222 | serial = sdi->conn; | |
223 | if (serial && serial->fd != -1) { | |
224 | serial_close(serial); | |
225 | sdi->status = SR_ST_INACTIVE; | |
226 | } | |
227 | ||
228 | return SR_OK; | |
229 | } | |
230 | ||
231 | static int cleanup(void) | |
232 | { | |
233 | return dev_clear(); | |
234 | } | |
235 | ||
236 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
237 | const struct sr_probe_group *probe_group) | |
238 | { | |
239 | struct dev_context *devc; | |
240 | ||
241 | (void)probe_group; | |
242 | ||
243 | if (sdi->status != SR_ST_ACTIVE) | |
244 | return SR_ERR_DEV_CLOSED; | |
245 | ||
246 | if (!(devc = sdi->priv)) { | |
247 | sr_err("sdi->priv was NULL."); | |
248 | return SR_ERR_BUG; | |
249 | } | |
250 | ||
251 | switch (id) { | |
252 | case SR_CONF_LIMIT_MSEC: | |
253 | /* TODO: not yet implemented */ | |
254 | if (g_variant_get_uint64(data) == 0) { | |
255 | sr_err("LIMIT_MSEC can't be 0."); | |
256 | return SR_ERR; | |
257 | } | |
258 | devc->limit_msec = g_variant_get_uint64(data); | |
259 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
260 | devc->limit_msec); | |
261 | break; | |
262 | case SR_CONF_LIMIT_SAMPLES: | |
263 | devc->limit_samples = g_variant_get_uint64(data); | |
264 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
265 | devc->limit_samples); | |
266 | break; | |
267 | default: | |
268 | return SR_ERR_NA; | |
269 | } | |
270 | ||
271 | return SR_OK; | |
272 | } | |
273 | ||
274 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
275 | const struct sr_probe_group *probe_group) | |
276 | { | |
277 | (void)sdi; | |
278 | (void)probe_group; | |
279 | ||
280 | switch (key) { | |
281 | case SR_CONF_SCAN_OPTIONS: | |
282 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
283 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
284 | break; | |
285 | case SR_CONF_DEVICE_OPTIONS: | |
286 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
287 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
288 | break; | |
289 | default: | |
290 | return SR_ERR_NA; | |
291 | } | |
292 | ||
293 | return SR_OK; | |
294 | } | |
295 | ||
296 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
297 | { | |
298 | struct dev_context *devc; | |
299 | struct sr_serial_dev_inst *serial; | |
300 | ||
301 | if (sdi->status != SR_ST_ACTIVE) | |
302 | return SR_ERR_DEV_CLOSED; | |
303 | ||
304 | if (!(devc = sdi->priv)) { | |
305 | sr_err("sdi->priv was NULL."); | |
306 | return SR_ERR_BUG; | |
307 | } | |
308 | ||
309 | devc->cb_data = cb_data; | |
310 | ||
311 | /* Send header packet to the session bus. */ | |
312 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
313 | ||
314 | /* Poll every 100ms, or whenever some data comes in. */ | |
315 | serial = sdi->conn; | |
316 | sr_source_add(serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi); | |
317 | ||
318 | if (serial_write(serial, "QM\r", 3) == -1) { | |
319 | sr_err("Unable to send QM: %s.", strerror(errno)); | |
320 | return SR_ERR; | |
321 | } | |
322 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; | |
323 | devc->expect_response = TRUE; | |
324 | ||
325 | return SR_OK; | |
326 | } | |
327 | ||
328 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
329 | { | |
330 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, | |
331 | sdi->conn, LOG_PREFIX); | |
332 | } | |
333 | ||
334 | SR_PRIV struct sr_dev_driver flukedmm_driver_info = { | |
335 | .name = "fluke-dmm", | |
336 | .longname = "Fluke 18x/28x series DMMs", | |
337 | .api_version = 1, | |
338 | .init = init, | |
339 | .cleanup = cleanup, | |
340 | .scan = scan, | |
341 | .dev_list = dev_list, | |
342 | .dev_clear = dev_clear, | |
343 | .config_get = NULL, | |
344 | .config_set = config_set, | |
345 | .config_list = config_list, | |
346 | .dev_open = dev_open, | |
347 | .dev_close = dev_close, | |
348 | .dev_acquisition_start = dev_acquisition_start, | |
349 | .dev_acquisition_stop = dev_acquisition_stop, | |
350 | .priv = NULL, | |
351 | }; |