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)
32 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
33 "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
34 "%02x %02x %02x %02x %02x %02x %02x",
35 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
36 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
37 buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
41 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
46 struct sr_datafeed_packet packet;
47 struct sr_datafeed_analog_old analog;
48 struct dev_context *devc;
50 dmm = (struct dmm_info *)sdi->driver;
55 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
57 analog.channels = sdi->channels;
58 analog.num_samples = 1;
61 dmm->packet_parse(buf, &floatval, &analog, info);
62 analog.data = &floatval;
64 /* If this DMM needs additional handling, call the resp. function. */
66 dmm->dmm_details(&analog, info);
68 if (analog.mq != -1) {
69 /* Got a measurement. */
70 packet.type = SR_DF_ANALOG_OLD;
71 packet.payload = &analog;
72 sr_session_send(devc->cb_data, &packet);
77 /** Request packet, if required. */
78 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
81 struct dev_context *devc;
82 struct sr_serial_dev_inst *serial;
85 dmm = (struct dmm_info *)sdi->driver;
87 if (!dmm->packet_request)
93 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
94 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
95 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
99 ret = dmm->packet_request(serial);
101 sr_err("Failed to request packet: %d.", ret);
105 if (dmm->req_timeout_ms)
106 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
111 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
113 struct dmm_info *dmm;
114 struct dev_context *devc;
115 int len, i, offset = 0;
116 struct sr_serial_dev_inst *serial;
118 dmm = (struct dmm_info *)sdi->driver;
123 /* Try to get as much data as the buffer can hold. */
124 len = DMM_BUFSIZE - devc->buflen;
125 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
127 return; /* No new bytes, nothing to do. */
129 sr_err("Serial port read error: %d.", len);
134 /* Now look for packets in that data. */
135 while ((devc->buflen - offset) >= dmm->packet_size) {
136 if (dmm->packet_valid(devc->buf + offset)) {
137 handle_packet(devc->buf + offset, sdi, info);
138 offset += dmm->packet_size;
140 /* Request next packet, if required. */
141 if (!dmm->packet_request)
143 if (dmm->req_timeout_ms || dmm->req_delay_ms)
144 devc->req_next_at = g_get_monotonic_time() +
145 dmm->req_delay_ms * 1000;
152 /* If we have any data left, move it to the beginning of our buffer. */
153 for (i = 0; i < devc->buflen - offset; i++)
154 devc->buf[i] = devc->buf[offset + i];
155 devc->buflen -= offset;
158 int receive_data(int fd, int revents, void *cb_data)
160 struct sr_dev_inst *sdi;
161 struct dev_context *devc;
162 struct dmm_info *dmm;
168 if (!(sdi = cb_data))
171 if (!(devc = sdi->priv))
174 dmm = (struct dmm_info *)sdi->driver;
176 if (revents == G_IO_IN) {
177 /* Serial data arrived. */
178 info = g_malloc(dmm->info_size);
179 handle_new_data(sdi, info);
182 /* Timeout; send another packet request if DMM needs it. */
183 if (dmm->packet_request && (req_packet(sdi) < 0))
187 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
188 sr_info("Requested number of samples reached.");
189 sdi->driver->dev_acquisition_stop(sdi, cb_data);
193 if (devc->limit_msec) {
194 time = (g_get_monotonic_time() - devc->starttime) / 1000;
195 if (time > (int64_t)devc->limit_msec) {
196 sr_info("Requested time limit reached.");
197 sdi->driver->dev_acquisition_stop(sdi, cb_data);