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