2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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.
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.
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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
24 #include "radioshack-dmm.h"
25 #include <sys/types.h>
31 static const int hwopts[] = {
37 static const int hwcaps[] = {
39 SR_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
49 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
50 static struct sr_dev_driver *di = &radioshackdmm_driver_info;
52 static const struct radioshackdmm_profile supported_radioshackdmm[] = {
53 { RADIOSHACK_22_812, "22-812", 100 },
56 /* Properly close and free all devices. */
57 static int clear_instances(void)
59 struct sr_dev_inst *sdi;
60 struct drv_context *drvc;
61 struct dev_context *devc;
64 if (!(drvc = di->priv))
68 for (l = drvc->instances; l; l = l->next) {
71 if (!(devc = sdi->priv))
73 sr_serial_dev_inst_free(devc->serial);
74 sr_dev_inst_free(sdi);
76 g_slist_free(drvc->instances);
77 drvc->instances = NULL;
82 static int hw_init(void)
84 struct drv_context *drvc;
86 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
87 sr_err("Driver context malloc failed.");
96 static int serial_readline(int fd, char **buf, size_t *buflen,
103 start = g_get_monotonic_time();
108 len = maxlen - *buflen - 1;
111 len = serial_read(fd, *buf + *buflen, 1);
114 *(*buf + *buflen) = '\0';
115 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
116 /* Strip LF and terminate. */
117 *(*buf + --*buflen) = '\0';
121 if (g_get_monotonic_time() - start > timeout_ms)
130 static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
132 struct sr_dev_inst *sdi;
133 struct drv_context *drvc;
134 struct dev_context *devc;
135 struct sr_probe *probe;
141 if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) {
142 sr_err("Unable to open %s: %s.", conn, strerror(errno));
145 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
146 sr_err("Unable to set serial parameters.");
150 sr_info("Probing port %s readonly.", conn);
156 /* There's no way to get an ID from the multimeter. It just sends data
157 * periodically, so the best we can do is check if the packets match the
158 * expected format. */
159 while (!devices && retry < 3)
162 size_t good_packets = 0;
166 /* Let's get a bit of data and see if we can find a packet */
168 serial_readline(fd, &b, &len, 250);
169 if( (len == 0) || (len < RS_22_812_PACKET_SIZE) ) {
170 /* Not enough data received, is the DMM connected ? */
174 /* Let's treat our buffer like a stream, and find any
176 for( i = 0; i < len - RS_22_812_PACKET_SIZE + 1;
177 /* don't increment i here */ )
179 const rs_22_812_packet *packet = (void *)(&buf[i]);
180 if( !rs_22_812_is_packet_valid(packet) ){
185 i += RS_22_812_PACKET_SIZE;
188 /* If we dropped more than two packets worth of data, something
190 size_t dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
191 if(dropped > 2 * RS_22_812_PACKET_SIZE)
194 /* Let's see if we have anything good */
195 if (good_packets == 0)
198 sr_info("Found RS 22-812 on port %s.", conn);
200 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
203 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
204 sr_err("Device context malloc failed.");
208 /* devc->profile = RADIOSHACK_22_812; */
209 devc->serial = sr_serial_dev_inst_new(conn, -1);
210 devc->serialcomm = g_strdup(serialcomm);
214 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
216 sdi->probes = g_slist_append(sdi->probes, probe);
217 drvc->instances = g_slist_append(drvc->instances, sdi);
218 devices = g_slist_append(devices, sdi);
226 static GSList *hw_scan(GSList *options)
228 struct sr_hwopt *opt;
230 const char *conn, *serialcomm;
232 conn = serialcomm = NULL;
233 for (l = options; l; l = l->next) {
235 switch (opt->hwopt) {
239 case SR_HWOPT_SERIALCOMM:
240 serialcomm = opt->value;
248 /* Use the provided comm specs. */
249 devices = rs_22_812_scan(conn, serialcomm);
251 /* Then try the default 4800 8n1 */
252 devices = rs_22_812_scan(conn, "4800/8n1");
258 static GSList *hw_dev_list(void)
260 struct drv_context *drvc;
264 return drvc->instances;
267 static int hw_dev_open(struct sr_dev_inst *sdi)
269 struct dev_context *devc;
271 if (!(devc = sdi->priv)) {
272 sr_err("sdi->priv was NULL.");
276 devc->serial->fd = serial_open(devc->serial->port, O_RDONLY);
277 if (devc->serial->fd == -1) {
278 sr_err("Couldn't open serial port '%s'.",
282 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
283 sr_err("Unable to set serial parameters.");
286 sdi->status = SR_ST_ACTIVE;
291 static int hw_dev_close(struct sr_dev_inst *sdi)
293 struct dev_context *devc;
295 if (!(devc = sdi->priv)) {
296 sr_err("sdi->priv was NULL.");
300 if (devc->serial && devc->serial->fd != -1) {
301 serial_close(devc->serial->fd);
302 devc->serial->fd = -1;
303 sdi->status = SR_ST_INACTIVE;
309 static int hw_cleanup(void)
316 static int hw_info_get(int info_id, const void **data,
317 const struct sr_dev_inst *sdi)
319 (void)sdi; /* Does nothing. prevents "unused parameter" warning */
328 case SR_DI_NUM_PROBES:
329 *data = GINT_TO_POINTER(1);
331 case SR_DI_PROBE_NAMES:
341 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
344 struct dev_context *devc;
346 if (sdi->status != SR_ST_ACTIVE)
349 if (!(devc = sdi->priv)) {
350 sr_err("sdi->priv was NULL.");
355 case SR_HWCAP_LIMIT_SAMPLES:
356 devc->limit_samples = *(const uint64_t *)value;
357 sr_dbg("Setting sample limit to %" PRIu64 ".",
358 devc->limit_samples);
361 sr_err("Unknown capability: %d.", hwcap);
369 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
372 struct sr_datafeed_packet packet;
373 struct sr_datafeed_header header;
374 struct sr_datafeed_meta_analog meta;
375 struct dev_context *devc;
377 if (!(devc = sdi->priv)) {
378 sr_err("sdi->priv was NULL.");
382 sr_dbg("Starting acquisition.");
384 devc->cb_data = cb_data;
386 /* Reset the number of samples to take. If we've already collected our
387 * quota, but we start a new session, and don't reset this, we'll just
388 * quit without aquiring any new samples */
389 devc->num_samples = 0;
391 /* Send header packet to the session bus. */
392 sr_dbg("Sending SR_DF_HEADER.");
393 packet.type = SR_DF_HEADER;
394 packet.payload = (uint8_t *)&header;
395 header.feed_version = 1;
396 gettimeofday(&header.starttime, NULL);
397 sr_session_send(devc->cb_data, &packet);
399 /* Send metadata about the SR_DF_ANALOG packets to come. */
400 sr_dbg("Sending SR_DF_META_ANALOG.");
401 packet.type = SR_DF_META_ANALOG;
402 packet.payload = &meta;
404 sr_session_send(devc->cb_data, &packet);
406 /* Poll every 100ms, or whenever some data comes in. */
407 sr_source_add(devc->serial->fd, G_IO_IN, 50,
408 radioshack_receive_data, (void *)sdi );
413 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
416 struct sr_datafeed_packet packet;
417 struct dev_context *devc;
419 if (sdi->status != SR_ST_ACTIVE)
422 if (!(devc = sdi->priv)) {
423 sr_err("sdi->priv was NULL.");
427 sr_dbg("Stopping acquisition.");
429 sr_source_remove(devc->serial->fd);
430 hw_dev_close((struct sr_dev_inst *)sdi);
432 /* Send end packet to the session bus. */
433 sr_dbg("Sending SR_DF_END.");
434 packet.type = SR_DF_END;
435 sr_session_send(cb_data, &packet);
440 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = {
441 .name = "radioshack-dmm",
442 .longname = "Radioshack 22-812/22-039 DMMs",
445 .cleanup = hw_cleanup,
447 .dev_list = hw_dev_list,
448 .dev_clear = clear_instances,
449 .dev_open = hw_dev_open,
450 .dev_close = hw_dev_close,
451 .info_get = hw_info_get,
452 .dev_config_set = hw_dev_config_set,
453 .dev_acquisition_start = hw_dev_acquisition_start,
454 .dev_acquisition_stop = hw_dev_acquisition_stop,