]>
Commit | Line | Data |
---|---|---|
d375b3c3 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> | |
d375b3c3 AG |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
6f22a8ef UH |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
936e27f1 | 29 | #include "protocol.h" |
d375b3c3 | 30 | |
401476da BV |
31 | #define SERIALCOMM "4800/8n1" |
32 | ||
d375b3c3 AG |
33 | static const int hwopts[] = { |
34 | SR_HWOPT_CONN, | |
35 | SR_HWOPT_SERIALCOMM, | |
36 | 0, | |
37 | }; | |
38 | ||
39 | static const int hwcaps[] = { | |
40 | SR_HWCAP_MULTIMETER, | |
41 | SR_HWCAP_LIMIT_SAMPLES, | |
42 | SR_HWCAP_CONTINUOUS, | |
43 | 0, | |
44 | }; | |
45 | ||
46 | static const char *probe_names[] = { | |
47 | "Probe", | |
48 | NULL, | |
49 | }; | |
50 | ||
51 | SR_PRIV struct sr_dev_driver radioshackdmm_driver_info; | |
52 | static struct sr_dev_driver *di = &radioshackdmm_driver_info; | |
53 | ||
d375b3c3 AG |
54 | /* Properly close and free all devices. */ |
55 | static int clear_instances(void) | |
56 | { | |
57 | struct sr_dev_inst *sdi; | |
58 | struct drv_context *drvc; | |
59 | struct dev_context *devc; | |
60 | GSList *l; | |
61 | ||
62 | if (!(drvc = di->priv)) | |
63 | return SR_OK; | |
64 | ||
65 | drvc = di->priv; | |
66 | for (l = drvc->instances; l; l = l->next) { | |
67 | if (!(sdi = l->data)) | |
68 | continue; | |
69 | if (!(devc = sdi->priv)) | |
70 | continue; | |
71 | sr_serial_dev_inst_free(devc->serial); | |
72 | sr_dev_inst_free(sdi); | |
73 | } | |
74 | g_slist_free(drvc->instances); | |
75 | drvc->instances = NULL; | |
76 | ||
77 | return SR_OK; | |
78 | } | |
79 | ||
80 | static int hw_init(void) | |
81 | { | |
82 | struct drv_context *drvc; | |
83 | ||
84 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
dccbd0ed | 85 | sr_err("Driver context malloc failed."); |
886a52b6 | 86 | return SR_ERR_MALLOC; |
d375b3c3 AG |
87 | } |
88 | ||
89 | di->priv = drvc; | |
90 | ||
91 | return SR_OK; | |
92 | } | |
93 | ||
d375b3c3 AG |
94 | static GSList *rs_22_812_scan(const char *conn, const char *serialcomm) |
95 | { | |
96 | struct sr_dev_inst *sdi; | |
97 | struct drv_context *drvc; | |
98 | struct dev_context *devc; | |
99 | struct sr_probe *probe; | |
401476da | 100 | struct sr_serial_dev_inst *serial; |
d375b3c3 | 101 | GSList *devices; |
401476da | 102 | int retry; |
ba6383f8 | 103 | size_t len, i, good_packets, dropped; |
d375b3c3 | 104 | char buf[128], *b; |
ba6383f8 | 105 | const struct rs_22_812_packet *rs_packet; |
d375b3c3 | 106 | |
401476da | 107 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
d375b3c3 | 108 | return NULL; |
ba6383f8 | 109 | |
401476da | 110 | if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK) |
d375b3c3 | 111 | return NULL; |
d375b3c3 | 112 | |
ba6383f8 | 113 | sr_info("Probing port '%s' readonly.", conn); |
be8dbf3a | 114 | |
d375b3c3 AG |
115 | drvc = di->priv; |
116 | b = buf; | |
117 | retry = 0; | |
118 | devices = NULL; | |
ba6383f8 UH |
119 | |
120 | /* | |
121 | * There's no way to get an ID from the multimeter. It just sends data | |
122 | * periodically, so the best we can do is check if the packets match | |
123 | * the expected format. | |
124 | */ | |
125 | while (!devices && retry < 3) { | |
126 | good_packets = 0; | |
d375b3c3 | 127 | retry++; |
401476da | 128 | serial_flush(serial); |
d375b3c3 | 129 | |
ba6383f8 | 130 | /* Let's get a bit of data and see if we can find a packet. */ |
d375b3c3 | 131 | len = sizeof(buf); |
401476da | 132 | serial_readline(serial, &b, &len, 250); |
ba6383f8 UH |
133 | if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) { |
134 | /* Not enough data received, is the DMM connected? */ | |
d375b3c3 AG |
135 | continue; |
136 | } | |
137 | ||
ba6383f8 UH |
138 | /* Treat our buffer as stream, and find any valid packets. */ |
139 | for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) { | |
140 | rs_packet = (void *)(&buf[i]); | |
141 | if (!rs_22_812_packet_valid(rs_packet)) { | |
d375b3c3 AG |
142 | i++; |
143 | continue; | |
144 | } | |
145 | good_packets++; | |
146 | i += RS_22_812_PACKET_SIZE; | |
147 | } | |
148 | ||
ba6383f8 UH |
149 | /* If we dropped more than two packets, something is wrong. */ |
150 | dropped = len - (good_packets * RS_22_812_PACKET_SIZE); | |
151 | if (dropped > 2 * RS_22_812_PACKET_SIZE) | |
d375b3c3 AG |
152 | continue; |
153 | ||
ba6383f8 | 154 | /* Let's see if we have anything good. */ |
d375b3c3 AG |
155 | if (good_packets == 0) |
156 | continue; | |
157 | ||
ba6383f8 | 158 | sr_info("Found RadioShack 22-812 on port '%s'.", conn); |
be8dbf3a | 159 | |
dccbd0ed | 160 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack", |
d375b3c3 AG |
161 | "22-812", ""))) |
162 | return NULL; | |
ba6383f8 | 163 | |
d375b3c3 | 164 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
dccbd0ed | 165 | sr_err("Device context malloc failed."); |
d375b3c3 AG |
166 | return NULL; |
167 | } | |
168 | ||
401476da | 169 | devc->serial = serial; |
d375b3c3 AG |
170 | |
171 | sdi->priv = devc; | |
172 | sdi->driver = di; | |
173 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
174 | return NULL; | |
175 | sdi->probes = g_slist_append(sdi->probes, probe); | |
176 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
177 | devices = g_slist_append(devices, sdi); | |
178 | break; | |
d375b3c3 | 179 | } |
401476da | 180 | serial_close(serial); |
d375b3c3 AG |
181 | |
182 | return devices; | |
183 | } | |
184 | ||
ba6383f8 | 185 | static GSList *hw_scan(GSList * options) |
d375b3c3 AG |
186 | { |
187 | struct sr_hwopt *opt; | |
188 | GSList *l, *devices; | |
189 | const char *conn, *serialcomm; | |
190 | ||
191 | conn = serialcomm = NULL; | |
192 | for (l = options; l; l = l->next) { | |
193 | opt = l->data; | |
194 | switch (opt->hwopt) { | |
195 | case SR_HWOPT_CONN: | |
196 | conn = opt->value; | |
197 | break; | |
198 | case SR_HWOPT_SERIALCOMM: | |
199 | serialcomm = opt->value; | |
200 | break; | |
201 | } | |
202 | } | |
203 | if (!conn) | |
204 | return NULL; | |
205 | ||
401476da | 206 | if (serialcomm) |
d375b3c3 AG |
207 | /* Use the provided comm specs. */ |
208 | devices = rs_22_812_scan(conn, serialcomm); | |
401476da BV |
209 | else |
210 | /* Try the default. */ | |
211 | devices = rs_22_812_scan(conn, SERIALCOMM); | |
d375b3c3 AG |
212 | |
213 | return devices; | |
214 | } | |
215 | ||
216 | static GSList *hw_dev_list(void) | |
217 | { | |
218 | struct drv_context *drvc; | |
219 | ||
220 | drvc = di->priv; | |
221 | ||
222 | return drvc->instances; | |
223 | } | |
224 | ||
225 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
226 | { | |
227 | struct dev_context *devc; | |
228 | ||
229 | if (!(devc = sdi->priv)) { | |
dccbd0ed | 230 | sr_err("sdi->priv was NULL."); |
d375b3c3 AG |
231 | return SR_ERR_BUG; |
232 | } | |
233 | ||
401476da | 234 | if (serial_open(devc->serial, O_RDONLY) != SR_OK) |
d375b3c3 | 235 | return SR_ERR; |
401476da | 236 | |
d375b3c3 AG |
237 | sdi->status = SR_ST_ACTIVE; |
238 | ||
239 | return SR_OK; | |
240 | } | |
241 | ||
242 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
243 | { | |
244 | struct dev_context *devc; | |
245 | ||
246 | if (!(devc = sdi->priv)) { | |
dccbd0ed | 247 | sr_err("sdi->priv was NULL."); |
d375b3c3 AG |
248 | return SR_ERR_BUG; |
249 | } | |
250 | ||
251 | if (devc->serial && devc->serial->fd != -1) { | |
401476da | 252 | serial_close(devc->serial); |
d375b3c3 AG |
253 | sdi->status = SR_ST_INACTIVE; |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static int hw_cleanup(void) | |
260 | { | |
261 | clear_instances(); | |
262 | ||
263 | return SR_OK; | |
264 | } | |
265 | ||
266 | static int hw_info_get(int info_id, const void **data, | |
ba6383f8 | 267 | const struct sr_dev_inst *sdi) |
d375b3c3 | 268 | { |
ba6383f8 | 269 | (void)sdi; |
d375b3c3 AG |
270 | |
271 | switch (info_id) { | |
272 | case SR_DI_HWOPTS: | |
273 | *data = hwopts; | |
274 | break; | |
275 | case SR_DI_HWCAPS: | |
276 | *data = hwcaps; | |
277 | break; | |
278 | case SR_DI_NUM_PROBES: | |
279 | *data = GINT_TO_POINTER(1); | |
280 | break; | |
281 | case SR_DI_PROBE_NAMES: | |
282 | *data = probe_names; | |
283 | break; | |
284 | default: | |
ba6383f8 | 285 | sr_err("Unknown info_id: %d.", info_id); |
d375b3c3 AG |
286 | return SR_ERR_ARG; |
287 | } | |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 | ||
292 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
ba6383f8 | 293 | const void *value) |
d375b3c3 AG |
294 | { |
295 | struct dev_context *devc; | |
296 | ||
297 | if (sdi->status != SR_ST_ACTIVE) | |
298 | return SR_ERR; | |
299 | ||
300 | if (!(devc = sdi->priv)) { | |
dccbd0ed | 301 | sr_err("sdi->priv was NULL."); |
d375b3c3 AG |
302 | return SR_ERR_BUG; |
303 | } | |
304 | ||
305 | switch (hwcap) { | |
306 | case SR_HWCAP_LIMIT_SAMPLES: | |
307 | devc->limit_samples = *(const uint64_t *)value; | |
dccbd0ed | 308 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
d375b3c3 AG |
309 | devc->limit_samples); |
310 | break; | |
311 | default: | |
dccbd0ed | 312 | sr_err("Unknown capability: %d.", hwcap); |
ba6383f8 | 313 | return SR_ERR_ARG; |
d375b3c3 AG |
314 | break; |
315 | } | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
ba6383f8 | 321 | void *cb_data) |
d375b3c3 AG |
322 | { |
323 | struct sr_datafeed_packet packet; | |
324 | struct sr_datafeed_header header; | |
325 | struct sr_datafeed_meta_analog meta; | |
326 | struct dev_context *devc; | |
327 | ||
328 | if (!(devc = sdi->priv)) { | |
dccbd0ed | 329 | sr_err("sdi->priv was NULL."); |
d375b3c3 AG |
330 | return SR_ERR_BUG; |
331 | } | |
332 | ||
dccbd0ed | 333 | sr_dbg("Starting acquisition."); |
d375b3c3 AG |
334 | |
335 | devc->cb_data = cb_data; | |
336 | ||
ba6383f8 UH |
337 | /* |
338 | * Reset the number of samples to take. If we've already collected our | |
be8dbf3a | 339 | * quota, but we start a new session, and don't reset this, we'll just |
ba6383f8 UH |
340 | * quit without aquiring any new samples. |
341 | */ | |
be8dbf3a AG |
342 | devc->num_samples = 0; |
343 | ||
d375b3c3 | 344 | /* Send header packet to the session bus. */ |
dccbd0ed | 345 | sr_dbg("Sending SR_DF_HEADER."); |
d375b3c3 | 346 | packet.type = SR_DF_HEADER; |
ba6383f8 | 347 | packet.payload = (uint8_t *) & header; |
d375b3c3 AG |
348 | header.feed_version = 1; |
349 | gettimeofday(&header.starttime, NULL); | |
350 | sr_session_send(devc->cb_data, &packet); | |
351 | ||
352 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
dccbd0ed | 353 | sr_dbg("Sending SR_DF_META_ANALOG."); |
d375b3c3 AG |
354 | packet.type = SR_DF_META_ANALOG; |
355 | packet.payload = &meta; | |
356 | meta.num_probes = 1; | |
357 | sr_session_send(devc->cb_data, &packet); | |
358 | ||
ba6383f8 | 359 | /* Poll every 50ms, or whenever some data comes in. */ |
d375b3c3 | 360 | sr_source_add(devc->serial->fd, G_IO_IN, 50, |
ba6383f8 | 361 | radioshack_dmm_receive_data, (void *)sdi); |
d375b3c3 AG |
362 | |
363 | return SR_OK; | |
364 | } | |
365 | ||
69b07d14 | 366 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
d375b3c3 AG |
367 | { |
368 | struct sr_datafeed_packet packet; | |
369 | struct dev_context *devc; | |
370 | ||
371 | if (sdi->status != SR_ST_ACTIVE) | |
372 | return SR_ERR; | |
373 | ||
374 | if (!(devc = sdi->priv)) { | |
dccbd0ed | 375 | sr_err("sdi->priv was NULL."); |
d375b3c3 AG |
376 | return SR_ERR_BUG; |
377 | } | |
378 | ||
dccbd0ed | 379 | sr_dbg("Stopping acquisition."); |
d375b3c3 AG |
380 | |
381 | sr_source_remove(devc->serial->fd); | |
382 | hw_dev_close((struct sr_dev_inst *)sdi); | |
383 | ||
384 | /* Send end packet to the session bus. */ | |
dccbd0ed | 385 | sr_dbg("Sending SR_DF_END."); |
d375b3c3 AG |
386 | packet.type = SR_DF_END; |
387 | sr_session_send(cb_data, &packet); | |
388 | ||
389 | return SR_OK; | |
390 | } | |
391 | ||
392 | SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = { | |
393 | .name = "radioshack-dmm", | |
ba6383f8 | 394 | .longname = "RadioShack 22-812/22-039 DMMs", |
d375b3c3 AG |
395 | .api_version = 1, |
396 | .init = hw_init, | |
397 | .cleanup = hw_cleanup, | |
398 | .scan = hw_scan, | |
399 | .dev_list = hw_dev_list, | |
400 | .dev_clear = clear_instances, | |
401 | .dev_open = hw_dev_open, | |
402 | .dev_close = hw_dev_close, | |
403 | .info_get = hw_info_get, | |
404 | .dev_config_set = hw_dev_config_set, | |
405 | .dev_acquisition_start = hw_dev_acquisition_start, | |
406 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
407 | .priv = NULL, | |
408 | }; |