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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 static void log_dmm_packet(const uint8_t *buf)
31 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
32 "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
33 "%02x %02x %02x %02x %02x %02x %02x",
34 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
35 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
36 buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
40 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
45 struct sr_datafeed_packet packet;
46 struct sr_datafeed_analog analog;
47 struct dev_context *devc;
49 dmm = (struct dmm_info *)sdi->driver;
54 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
56 analog.channels = sdi->channels;
57 analog.num_samples = 1;
60 dmm->packet_parse(buf, &floatval, &analog, info);
61 analog.data = &floatval;
63 /* If this DMM needs additional handling, call the resp. function. */
65 dmm->dmm_details(&analog, info);
67 if (analog.mq != -1) {
68 /* Got a measurement. */
69 packet.type = SR_DF_ANALOG;
70 packet.payload = &analog;
71 sr_session_send(devc->cb_data, &packet);
76 /** Request packet, if required. */
77 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
80 struct dev_context *devc;
81 struct sr_serial_dev_inst *serial;
84 dmm = (struct dmm_info *)sdi->driver;
86 if (!dmm->packet_request)
92 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
93 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
94 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
98 ret = dmm->packet_request(serial);
100 sr_err("Failed to request packet: %d.", ret);
104 if (dmm->req_timeout_ms)
105 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
110 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
112 struct dmm_info *dmm;
113 struct dev_context *devc;
114 int len, i, offset = 0;
115 struct sr_serial_dev_inst *serial;
117 dmm = (struct dmm_info *)sdi->driver;
122 /* Try to get as much data as the buffer can hold. */
123 len = DMM_BUFSIZE - devc->buflen;
124 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
126 return; /* No new bytes, nothing to do. */
128 sr_err("Serial port read error: %d.", len);
133 /* Now look for packets in that data. */
134 while ((devc->buflen - offset) >= dmm->packet_size) {
135 if (dmm->packet_valid(devc->buf + offset)) {
136 handle_packet(devc->buf + offset, sdi, info);
137 offset += dmm->packet_size;
139 /* Request next packet, if required. */
140 if (!dmm->packet_request)
142 if (dmm->req_timeout_ms || dmm->req_delay_ms)
143 devc->req_next_at = g_get_monotonic_time() +
144 dmm->req_delay_ms * 1000;
151 /* If we have any data left, move it to the beginning of our buffer. */
152 for (i = 0; i < devc->buflen - offset; i++)
153 devc->buf[i] = devc->buf[offset + i];
154 devc->buflen -= offset;
157 int receive_data(int fd, int revents, void *cb_data)
159 struct sr_dev_inst *sdi;
160 struct dev_context *devc;
161 struct dmm_info *dmm;
167 if (!(sdi = cb_data))
170 if (!(devc = sdi->priv))
173 dmm = (struct dmm_info *)sdi->driver;
175 if (revents == G_IO_IN) {
176 /* Serial data arrived. */
177 info = g_malloc(dmm->info_size);
178 handle_new_data(sdi, info);
181 /* Timeout; send another packet request if DMM needs it. */
182 if (dmm->packet_request && (req_packet(sdi) < 0))
186 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
187 sr_info("Requested number of samples reached.");
188 sdi->driver->dev_acquisition_stop(sdi, cb_data);
192 if (devc->limit_msec) {
193 time = (g_get_monotonic_time() - devc->starttime) / 1000;
194 if (time > (int64_t)devc->limit_msec) {
195 sr_info("Requested time limit reached.");
196 sdi->driver->dev_acquisition_stop(sdi, cb_data);