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 if (sr_log_loglevel_get() < SR_LOG_DBG)
37 text = sr_hexdump_new(buf, len);
38 sr_dbg("DMM packet: %s", text->str);
39 sr_hexdump_free(text);
42 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
47 struct sr_datafeed_packet packet;
48 struct sr_datafeed_analog analog;
49 struct sr_analog_encoding encoding;
50 struct sr_analog_meaning meaning;
51 struct sr_analog_spec spec;
52 struct dev_context *devc;
54 struct sr_channel *channel;
57 dmm = (struct dmm_info *)sdi->driver;
59 log_dmm_packet(buf, dmm->packet_size);
63 memset(info, 0, dmm->info_size);
64 for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) {
65 /* Note: digits/spec_digits will be overridden by the DMM parsers. */
66 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
68 channel = g_slist_nth_data(sdi->channels, ch_idx);
69 analog.meaning->channels = g_slist_append(NULL, channel);
70 analog.num_samples = 1;
71 analog.meaning->mq = 0;
73 dmm->packet_parse(buf, &floatval, &analog, info);
74 analog.data = &floatval;
76 /* If this DMM needs additional handling, call the resp. function. */
78 dmm->dmm_details(&analog, info);
80 if (analog.meaning->mq != 0 && channel->enabled) {
81 /* Got a measurement. */
82 packet.type = SR_DF_ANALOG;
83 packet.payload = &analog;
84 sr_session_send(sdi, &packet);
90 sr_sw_limits_update_samples_read(&devc->limits, 1);
94 /** Request packet, if required. */
95 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
98 struct dev_context *devc;
99 struct sr_serial_dev_inst *serial;
102 dmm = (struct dmm_info *)sdi->driver;
104 if (!dmm->packet_request)
110 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
111 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
112 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
116 ret = dmm->packet_request(serial);
118 sr_err("Failed to request packet: %d.", ret);
122 if (dmm->req_timeout_ms)
123 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
128 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
130 struct dmm_info *dmm;
131 struct dev_context *devc;
133 struct sr_serial_dev_inst *serial;
135 dmm = (struct dmm_info *)sdi->driver;
140 /* Try to get as much data as the buffer can hold. */
141 len = DMM_BUFSIZE - devc->buflen;
142 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
144 return; /* No new bytes, nothing to do. */
146 sr_err("Serial port read error: %d.", len);
151 /* Now look for packets in that data. */
153 while ((devc->buflen - offset) >= dmm->packet_size) {
154 if (dmm->packet_valid(devc->buf + offset)) {
155 handle_packet(devc->buf + offset, sdi, info);
156 offset += dmm->packet_size;
158 /* Request next packet, if required. */
159 if (!dmm->packet_request)
161 if (dmm->req_timeout_ms || dmm->req_delay_ms)
162 devc->req_next_at = g_get_monotonic_time() +
163 dmm->req_delay_ms * 1000;
170 /* If we have any data left, move it to the beginning of our buffer. */
171 if (devc->buflen > offset)
172 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
173 devc->buflen -= offset;
176 int receive_data(int fd, int revents, void *cb_data)
178 struct sr_dev_inst *sdi;
179 struct dev_context *devc;
180 struct dmm_info *dmm;
185 if (!(sdi = cb_data))
188 if (!(devc = sdi->priv))
191 dmm = (struct dmm_info *)sdi->driver;
193 if (revents == G_IO_IN) {
194 /* Serial data arrived. */
195 info = g_malloc(dmm->info_size);
196 handle_new_data(sdi, info);
199 /* Timeout; send another packet request if DMM needs it. */
200 if (dmm->packet_request && (req_packet(sdi) < 0))
204 if (sr_sw_limits_check(&devc->limits))
205 sr_dev_acquisition_stop(sdi);