2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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/>.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 static void log_dmm_packet(const uint8_t *buf, size_t len)
34 text = sr_hexdump_new(buf, len);
35 sr_dbg("DMM packet: %s", text->str);
36 sr_hexdump_free(text);
39 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
44 struct sr_datafeed_packet packet;
45 struct sr_datafeed_analog analog;
46 struct sr_analog_encoding encoding;
47 struct sr_analog_meaning meaning;
48 struct sr_analog_spec spec;
49 struct dev_context *devc;
51 struct sr_channel *channel;
54 dmm = (struct dmm_info *)sdi->driver;
56 log_dmm_packet(buf, dmm->packet_size);
60 memset(info, 0, dmm->info_size);
61 for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) {
62 /* Note: digits/spec_digits will be overridden by the DMM parsers. */
63 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
65 channel = g_slist_nth_data(sdi->channels, ch_idx);
66 analog.meaning->channels = g_slist_append(NULL, channel);
67 analog.num_samples = 1;
68 analog.meaning->mq = 0;
70 dmm->packet_parse(buf, &floatval, &analog, info);
71 analog.data = &floatval;
73 /* If this DMM needs additional handling, call the resp. function. */
75 dmm->dmm_details(&analog, info);
77 if (analog.meaning->mq != 0 && channel->enabled) {
78 /* Got a measurement. */
79 packet.type = SR_DF_ANALOG;
80 packet.payload = &analog;
81 sr_session_send(sdi, &packet);
87 sr_sw_limits_update_samples_read(&devc->limits, 1);
91 /** Request packet, if required. */
92 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
95 struct dev_context *devc;
96 struct sr_serial_dev_inst *serial;
99 dmm = (struct dmm_info *)sdi->driver;
101 if (!dmm->packet_request)
107 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
108 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
109 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
113 ret = dmm->packet_request(serial);
115 sr_err("Failed to request packet: %d.", ret);
119 if (dmm->req_timeout_ms)
120 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
125 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
127 struct dmm_info *dmm;
128 struct dev_context *devc;
130 struct sr_serial_dev_inst *serial;
132 dmm = (struct dmm_info *)sdi->driver;
137 /* Try to get as much data as the buffer can hold. */
138 len = DMM_BUFSIZE - devc->buflen;
139 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
141 return; /* No new bytes, nothing to do. */
143 sr_err("Serial port read error: %d.", len);
148 /* Now look for packets in that data. */
150 while ((devc->buflen - offset) >= dmm->packet_size) {
151 if (dmm->packet_valid(devc->buf + offset)) {
152 handle_packet(devc->buf + offset, sdi, info);
153 offset += dmm->packet_size;
155 /* Request next packet, if required. */
156 if (!dmm->packet_request)
158 if (dmm->req_timeout_ms || dmm->req_delay_ms)
159 devc->req_next_at = g_get_monotonic_time() +
160 dmm->req_delay_ms * 1000;
167 /* If we have any data left, move it to the beginning of our buffer. */
168 if (devc->buflen > offset)
169 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
170 devc->buflen -= offset;
173 int receive_data(int fd, int revents, void *cb_data)
175 struct sr_dev_inst *sdi;
176 struct dev_context *devc;
177 struct dmm_info *dmm;
182 if (!(sdi = cb_data))
185 if (!(devc = sdi->priv))
188 dmm = (struct dmm_info *)sdi->driver;
190 if (revents == G_IO_IN) {
191 /* Serial data arrived. */
192 info = g_malloc(dmm->info_size);
193 handle_new_data(sdi, info);
196 /* Timeout; send another packet request if DMM needs it. */
197 if (dmm->packet_request && (req_packet(sdi) < 0))
201 if (sr_sw_limits_check(&devc->limits))
202 sr_dev_acquisition_stop(sdi);