]>
Commit | Line | Data |
---|---|---|
7dc55d93 AG |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <glib.h> | |
22 | #include "libsigrok.h" | |
23 | #include "libsigrok-internal.h" | |
24 | #include "protocol.h" | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <fcntl.h> | |
28 | #include <string.h> | |
29 | #include <errno.h> | |
30 | ||
31 | static const int hwopts[] = { | |
32 | SR_HWOPT_CONN, | |
33 | SR_HWOPT_SERIALCOMM, | |
34 | 0, | |
35 | }; | |
36 | ||
37 | static const int hwcaps[] = { | |
38 | SR_HWCAP_MULTIMETER, | |
39 | SR_HWCAP_LIMIT_SAMPLES, | |
40 | SR_HWCAP_CONTINUOUS, | |
41 | 0, | |
42 | }; | |
43 | ||
44 | static const char *probe_names[] = { | |
45 | "Probe", | |
46 | NULL, | |
47 | }; | |
48 | ||
49 | SR_PRIV struct sr_dev_driver tekpower_driver_info; | |
50 | static struct sr_dev_driver *di = &tekpower_driver_info; | |
51 | ||
52 | /* Properly close and free all devices. */ | |
53 | static int clear_instances(void) | |
54 | { | |
55 | struct sr_dev_inst *sdi; | |
56 | struct drv_context *drvc; | |
57 | struct dev_context *devc; | |
58 | GSList *l; | |
59 | ||
60 | if (!(drvc = di->priv)) | |
61 | return SR_OK; | |
62 | ||
63 | drvc = di->priv; | |
64 | for (l = drvc->instances; l; l = l->next) { | |
65 | if (!(sdi = l->data)) | |
66 | continue; | |
67 | if (!(devc = sdi->priv)) | |
68 | continue; | |
69 | sr_serial_dev_inst_free(devc->serial); | |
70 | sr_dev_inst_free(sdi); | |
71 | } | |
72 | g_slist_free(drvc->instances); | |
73 | drvc->instances = NULL; | |
74 | ||
75 | return SR_OK; | |
76 | } | |
77 | ||
78 | static int hw_init(void) | |
79 | { | |
80 | struct drv_context *drvc; | |
81 | ||
82 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
83 | sr_err("driver context malloc failed."); | |
84 | return SR_ERR; | |
85 | } | |
86 | ||
87 | di->priv = drvc; | |
88 | ||
89 | return SR_OK; | |
90 | } | |
91 | ||
92 | static int serial_readline(int fd, char **buf, size_t *buflen, | |
93 | uint64_t timeout_ms) | |
94 | { | |
95 | uint64_t start; | |
96 | int maxlen, len; | |
97 | ||
98 | timeout_ms *= 1000; | |
99 | start = g_get_monotonic_time(); | |
100 | ||
101 | maxlen = *buflen; | |
102 | *buflen = len = 0; | |
103 | while(1) { | |
104 | len = maxlen - *buflen - 1; | |
105 | if (len < 1) | |
106 | break; | |
107 | len = serial_read(fd, *buf + *buflen, 1); | |
108 | if (len > 0) { | |
109 | *buflen += len; | |
110 | *(*buf + *buflen) = '\0'; | |
111 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { | |
112 | /* Strip LF and terminate. */ | |
113 | *(*buf + --*buflen) = '\0'; | |
114 | break; | |
115 | } | |
116 | } | |
117 | if (g_get_monotonic_time() - start > timeout_ms) | |
118 | /* Timeout */ | |
119 | break; | |
120 | g_usleep(2000); | |
121 | } | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static GSList *lcd14_scan(const char *conn, const char *serialcomm) | |
127 | { | |
128 | struct sr_dev_inst *sdi; | |
129 | struct drv_context *drvc; | |
130 | struct dev_context *devc; | |
131 | struct sr_probe *probe; | |
132 | GSList *devices; | |
133 | int fd, retry; | |
134 | size_t len; | |
135 | char buf[128], *b; | |
136 | ||
137 | if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) { | |
138 | sr_err("unable to open %s: %s", | |
139 | conn, strerror(errno)); | |
140 | return NULL; | |
141 | } | |
142 | if (serial_set_paramstr(fd, serialcomm) != SR_OK) { | |
143 | sr_err("unable to set serial parameters"); | |
144 | return NULL; | |
145 | } | |
146 | ||
147 | sr_info("probing port %s readonly", conn); | |
148 | ||
149 | drvc = di->priv; | |
150 | b = buf; | |
151 | retry = 0; | |
152 | devices = NULL; | |
153 | serial_flush(fd); | |
154 | /* There's no way to get an ID from the multimeter. It just sends data | |
155 | * periodically, so the best we can do is check if the packets match the | |
156 | * expected format. */ | |
157 | while (!devices && retry < 3) | |
158 | { | |
159 | size_t i; | |
160 | size_t good_packets = 0; | |
161 | retry++; | |
162 | ||
163 | /* Let's get a bit of data and see if we can find a packet */ | |
164 | len = sizeof(buf); | |
165 | serial_readline(fd, &b, &len, 500); | |
166 | if( (len == 0) || (len < LCD14_PACKET_SIZE) ) { | |
167 | /* Not enough data received, is the DMM connected ? */ | |
168 | continue; | |
169 | } | |
170 | ||
171 | /* Let's treat our buffer like a stream, and find any | |
172 | * valid packets */ | |
173 | for( i = 0; i < len - LCD14_PACKET_SIZE + 1; | |
174 | /* don't increment i here */ ) | |
175 | { | |
176 | const lcd14_packet *packet = (void *)(&buf[i]); | |
177 | if( !lcd14_is_packet_valid(packet, NULL) ){ | |
178 | i++; | |
179 | continue; | |
180 | } | |
181 | good_packets++; | |
182 | i += LCD14_PACKET_SIZE; | |
183 | } | |
184 | ||
185 | /* If we dropped more than two packets worth of data, something | |
186 | * is wrong */ | |
187 | size_t dropped = len - (good_packets * LCD14_PACKET_SIZE); | |
188 | if(dropped > 2 * LCD14_PACKET_SIZE) | |
189 | continue; | |
190 | ||
191 | /* Let's see if we have anything good */ | |
192 | if (good_packets == 0) | |
193 | continue; | |
194 | ||
195 | sr_info("found device on port %s", conn); | |
196 | ||
197 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower", | |
198 | "TP4000ZC", ""))) | |
199 | return NULL; | |
200 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
201 | sr_dbg("failed to malloc devc"); | |
202 | return NULL; | |
203 | } | |
204 | ||
205 | /* devc->profile = RADIOSHACK_22_812; */ | |
206 | devc->serial = sr_serial_dev_inst_new(conn, -1); | |
207 | devc->serialcomm = g_strdup(serialcomm); | |
208 | ||
209 | sdi->priv = devc; | |
210 | sdi->driver = di; | |
211 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
212 | return NULL; | |
213 | sdi->probes = g_slist_append(sdi->probes, probe); | |
214 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
215 | devices = g_slist_append(devices, sdi); | |
216 | break; | |
217 | } | |
218 | ||
219 | serial_close(fd); | |
220 | return devices; | |
221 | } | |
222 | ||
223 | static GSList *hw_scan(GSList *options) | |
224 | { | |
225 | struct sr_hwopt *opt; | |
226 | GSList *l, *devices; | |
227 | const char *conn, *serialcomm; | |
228 | ||
229 | conn = serialcomm = NULL; | |
230 | for (l = options; l; l = l->next) { | |
231 | opt = l->data; | |
232 | switch (opt->hwopt) { | |
233 | case SR_HWOPT_CONN: | |
234 | conn = opt->value; | |
235 | break; | |
236 | case SR_HWOPT_SERIALCOMM: | |
237 | serialcomm = opt->value; | |
238 | break; | |
239 | } | |
240 | } | |
241 | if (!conn) | |
242 | return NULL; | |
243 | ||
244 | if (serialcomm) { | |
245 | /* Use the provided comm specs. */ | |
246 | devices = lcd14_scan(conn, serialcomm); | |
247 | } else { | |
248 | /* Then try the default 2400 8n1 */ | |
249 | devices = lcd14_scan(conn, "2400/8n1"); | |
250 | } | |
251 | ||
252 | return devices; | |
253 | } | |
254 | ||
255 | static GSList *hw_dev_list(void) | |
256 | { | |
257 | struct drv_context *drvc; | |
258 | ||
259 | drvc = di->priv; | |
260 | ||
261 | return drvc->instances; | |
262 | } | |
263 | ||
264 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
265 | { | |
266 | struct dev_context *devc; | |
267 | ||
268 | if (!(devc = sdi->priv)) { | |
269 | sr_err("sdi->priv was NULL."); | |
270 | return SR_ERR_BUG; | |
271 | } | |
272 | ||
273 | devc->serial->fd = serial_open(devc->serial->port, O_RDONLY); | |
274 | if (devc->serial->fd == -1) { | |
275 | sr_err("Couldn't open serial port '%s'.", | |
276 | devc->serial->port); | |
277 | return SR_ERR; | |
278 | } | |
279 | if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) { | |
280 | sr_err("unable to set serial parameters"); | |
281 | return SR_ERR; | |
282 | } | |
283 | sdi->status = SR_ST_ACTIVE; | |
284 | ||
285 | return SR_OK; | |
286 | } | |
287 | ||
288 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
289 | { | |
290 | struct dev_context *devc; | |
291 | ||
292 | if (!(devc = sdi->priv)) { | |
293 | sr_err("sdi->priv was NULL."); | |
294 | return SR_ERR_BUG; | |
295 | } | |
296 | ||
297 | if (devc->serial && devc->serial->fd != -1) { | |
298 | serial_close(devc->serial->fd); | |
299 | devc->serial->fd = -1; | |
300 | sdi->status = SR_ST_INACTIVE; | |
301 | } | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
306 | static int hw_cleanup(void) | |
307 | { | |
308 | clear_instances(); | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
313 | static int hw_info_get(int info_id, const void **data, | |
314 | const struct sr_dev_inst *sdi) | |
315 | { | |
316 | (void)sdi; /* Does nothing. prevents "unused parameter" warning */ | |
317 | ||
318 | switch (info_id) { | |
319 | case SR_DI_HWOPTS: | |
320 | *data = hwopts; | |
321 | break; | |
322 | case SR_DI_HWCAPS: | |
323 | *data = hwcaps; | |
324 | break; | |
325 | case SR_DI_NUM_PROBES: | |
326 | *data = GINT_TO_POINTER(1); | |
327 | break; | |
328 | case SR_DI_PROBE_NAMES: | |
329 | *data = probe_names; | |
330 | break; | |
331 | default: | |
332 | return SR_ERR_ARG; | |
333 | } | |
334 | ||
335 | return SR_OK; | |
336 | } | |
337 | ||
338 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
339 | const void *value) | |
340 | { | |
341 | struct dev_context *devc; | |
342 | ||
343 | if (sdi->status != SR_ST_ACTIVE) | |
344 | return SR_ERR; | |
345 | ||
346 | if (!(devc = sdi->priv)) { | |
347 | sr_err("sdi->priv was NULL."); | |
348 | return SR_ERR_BUG; | |
349 | } | |
350 | ||
351 | switch (hwcap) { | |
352 | case SR_HWCAP_LIMIT_SAMPLES: | |
353 | devc->limit_samples = *(const uint64_t *)value; | |
354 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
355 | devc->limit_samples); | |
356 | break; | |
357 | default: | |
358 | sr_err("Unknown capability: %d.", hwcap); | |
359 | return SR_ERR; | |
360 | break; | |
361 | } | |
362 | ||
363 | return SR_OK; | |
364 | } | |
365 | ||
366 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
367 | void *cb_data) | |
368 | { | |
369 | struct sr_datafeed_packet packet; | |
370 | struct sr_datafeed_header header; | |
371 | struct sr_datafeed_meta_analog meta; | |
372 | struct dev_context *devc; | |
373 | ||
374 | if (!(devc = sdi->priv)) { | |
375 | sr_err("sdi->priv was NULL."); | |
376 | return SR_ERR_BUG; | |
377 | } | |
378 | ||
379 | sr_dbg("Starting acquisition."); | |
380 | ||
381 | devc->cb_data = cb_data; | |
382 | ||
383 | /* Reset the number of samples to take. If we've already collected our | |
384 | * quota, but we start a new session, and don't reset this, we'll just | |
385 | * quit without aquiring any new samples */ | |
386 | devc->num_samples = 0; | |
387 | ||
388 | /* Send header packet to the session bus. */ | |
389 | sr_dbg("Sending SR_DF_HEADER."); | |
390 | packet.type = SR_DF_HEADER; | |
391 | packet.payload = (uint8_t *)&header; | |
392 | header.feed_version = 1; | |
393 | gettimeofday(&header.starttime, NULL); | |
394 | sr_session_send(devc->cb_data, &packet); | |
395 | ||
396 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
397 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
398 | packet.type = SR_DF_META_ANALOG; | |
399 | packet.payload = &meta; | |
400 | meta.num_probes = 1; | |
401 | sr_session_send(devc->cb_data, &packet); | |
402 | ||
403 | /* Poll every 100ms, or whenever some data comes in. */ | |
404 | sr_source_add(devc->serial->fd, G_IO_IN, 50, | |
405 | lcd14_receive_data, (void *)sdi ); | |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, | |
411 | void *cb_data) | |
412 | { | |
413 | struct sr_datafeed_packet packet; | |
414 | struct dev_context *devc; | |
415 | ||
416 | if (sdi->status != SR_ST_ACTIVE) | |
417 | return SR_ERR; | |
418 | ||
419 | if (!(devc = sdi->priv)) { | |
420 | sr_err("sdi->priv was NULL."); | |
421 | return SR_ERR_BUG; | |
422 | } | |
423 | ||
424 | sr_dbg("Stopping acquisition."); | |
425 | ||
426 | sr_source_remove(devc->serial->fd); | |
427 | hw_dev_close((struct sr_dev_inst *)sdi); | |
428 | ||
429 | /* Send end packet to the session bus. */ | |
430 | sr_dbg("Sending SR_DF_END."); | |
431 | packet.type = SR_DF_END; | |
432 | sr_session_send(cb_data, &packet); | |
433 | ||
434 | return SR_OK; | |
435 | } | |
436 | ||
437 | SR_PRIV struct sr_dev_driver tekpower_driver_info = { | |
438 | .name = "tekpower-dmm", | |
439 | .longname = "TekPower/Digitek 4000ZC", | |
440 | .api_version = 1, | |
441 | .init = hw_init, | |
442 | .cleanup = hw_cleanup, | |
443 | .scan = hw_scan, | |
444 | .dev_list = hw_dev_list, | |
445 | .dev_clear = clear_instances, | |
446 | .dev_open = hw_dev_open, | |
447 | .dev_close = hw_dev_close, | |
448 | .info_get = hw_info_get, | |
449 | .dev_config_set = hw_dev_config_set, | |
450 | .dev_acquisition_start = hw_dev_acquisition_start, | |
451 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
452 | .priv = NULL, | |
453 | }; |