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