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