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 analog;
48 struct sr_analog_encoding encoding;
49 struct sr_analog_meaning meaning;
50 struct sr_analog_spec spec;
51 struct dev_context *devc;
53 dmm = (struct dmm_info *)sdi->driver;
58 /* Note: digits/spec_digits will be overridden by the DMM parsers. */
59 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
61 analog.meaning->channels = sdi->channels;
62 analog.num_samples = 1;
63 analog.meaning->mq = 0;
65 dmm->packet_parse(buf, &floatval, &analog, info);
66 analog.data = &floatval;
68 /* If this DMM needs additional handling, call the resp. function. */
70 dmm->dmm_details(&analog, info);
72 if (analog.meaning->mq != 0) {
73 /* Got a measurement. */
74 packet.type = SR_DF_ANALOG;
75 packet.payload = &analog;
76 sr_session_send(sdi, &packet);
77 sr_sw_limits_update_samples_read(&devc->limits, 1);
81 /** Request packet, if required. */
82 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
85 struct dev_context *devc;
86 struct sr_serial_dev_inst *serial;
89 dmm = (struct dmm_info *)sdi->driver;
91 if (!dmm->packet_request)
97 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
98 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
99 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
103 ret = dmm->packet_request(serial);
105 sr_err("Failed to request packet: %d.", ret);
109 if (dmm->req_timeout_ms)
110 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
115 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
117 struct dmm_info *dmm;
118 struct dev_context *devc;
119 int len, i, offset = 0;
120 struct sr_serial_dev_inst *serial;
122 dmm = (struct dmm_info *)sdi->driver;
127 /* Try to get as much data as the buffer can hold. */
128 len = DMM_BUFSIZE - devc->buflen;
129 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
131 return; /* No new bytes, nothing to do. */
133 sr_err("Serial port read error: %d.", len);
138 /* Now look for packets in that data. */
139 while ((devc->buflen - offset) >= dmm->packet_size) {
140 if (dmm->packet_valid(devc->buf + offset)) {
141 handle_packet(devc->buf + offset, sdi, info);
142 offset += dmm->packet_size;
144 /* Request next packet, if required. */
145 if (!dmm->packet_request)
147 if (dmm->req_timeout_ms || dmm->req_delay_ms)
148 devc->req_next_at = g_get_monotonic_time() +
149 dmm->req_delay_ms * 1000;
156 /* If we have any data left, move it to the beginning of our buffer. */
157 for (i = 0; i < devc->buflen - offset; i++)
158 devc->buf[i] = devc->buf[offset + i];
159 devc->buflen -= offset;
162 int receive_data(int fd, int revents, void *cb_data)
164 struct sr_dev_inst *sdi;
165 struct dev_context *devc;
166 struct dmm_info *dmm;
171 if (!(sdi = cb_data))
174 if (!(devc = sdi->priv))
177 dmm = (struct dmm_info *)sdi->driver;
179 if (revents == G_IO_IN) {
180 /* Serial data arrived. */
181 info = g_malloc(dmm->info_size);
182 handle_new_data(sdi, info);
185 /* Timeout; send another packet request if DMM needs it. */
186 if (dmm->packet_request && (req_packet(sdi) < 0))
190 if (sr_sw_limits_check(&devc->limits))
191 sr_dev_acquisition_stop(sdi);