2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi)
26 struct dev_context *devc;
27 struct sr_datafeed_packet packet;
28 struct sr_datafeed_analog analog;
29 struct sr_analog_encoding encoding;
30 struct sr_analog_meaning meaning;
31 struct sr_analog_spec spec;
35 /* TODO: Use proper 'digits' value for this device (and its modes). */
36 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
38 analog.num_samples = 1;
39 analog.meaning->mq = 0;
41 if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK)
43 analog.data = &floatval;
45 analog.meaning->channels = sdi->channels;
47 if (analog.meaning->mq != 0) {
48 /* Got a measurement. */
49 packet.type = SR_DF_ANALOG;
50 packet.payload = &analog;
51 sr_session_send(sdi, &packet);
52 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
56 static void handle_new_data(struct sr_dev_inst *sdi)
58 struct dev_context *devc;
59 int len, status, offset = 0;
60 struct sr_serial_dev_inst *serial;
65 /* Try to get as much data as the buffer can hold. */
66 len = DMM_BUFSIZE - devc->buflen;
67 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
69 sr_err("Serial port read error: %d.", len);
73 status = PACKET_INVALID_HEADER;
75 /* Now look for packets in that data. */
76 while (status != PACKET_NEED_MORE_DATA) {
77 /* We don't have a header, look for one. */
78 if (devc->next_packet_len == 0) {
79 len = devc->buflen - offset;
80 status = brymen_packet_length(devc->buf + offset, &len);
81 if (status == PACKET_HEADER_OK) {
82 /* We know how large the packet will be. */
83 devc->next_packet_len = len;
84 } else if (status == PACKET_NEED_MORE_DATA) {
85 /* We didn't yet receive the full header. */
86 devc->next_packet_len = 0;
89 /* Invalid header. Move on. */
90 devc->next_packet_len = 0;
96 /* We know how the packet size, but did we receive all of it? */
97 if (devc->buflen - offset < devc->next_packet_len)
100 /* We should have a full packet here, so we can check it. */
101 if (brymen_packet_is_valid(devc->buf + offset)) {
102 handle_packet(devc->buf + offset, sdi);
103 offset += devc->next_packet_len;
108 /* We are done with this packet. Look for a new one. */
109 devc->next_packet_len = 0;
112 /* If we have any data left, move it to the beginning of our buffer. */
113 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
114 devc->buflen -= offset;
117 SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
119 struct sr_dev_inst *sdi;
120 struct dev_context *devc;
121 struct sr_serial_dev_inst *serial;
126 if (!(sdi = cb_data))
129 if (!(devc = sdi->priv))
134 if (revents == G_IO_IN) {
135 /* Serial data arrived. */
136 handle_new_data(sdi);
138 /* Timeout, send another packet request. */
139 if ((ret = brymen_packet_request(serial)) < 0) {
140 sr_err("Failed to request packet: %d.", ret);
145 if (sr_sw_limits_check(&devc->sw_limits))
146 sr_dev_acquisition_stop(sdi);
152 * Try to find a valid packet in a serial data stream.
154 * @param serial Previously initialized serial port structure.
155 * @param buf Buffer containing the bytes to write.
156 * @param buflen Size of the buffer.
157 * @param get_packet_size Callback that assesses the size of incoming packets.
158 * @param is_valid Callback that assesses whether the packet is valid or not.
159 * @param timeout_ms The timeout after which, if no packet is detected, to
161 * @param baudrate The baudrate of the serial port. This parameter is not
162 * critical, but it helps fine tune the serial port polling
165 * @return SR_OK if a valid packet is found within the given timeout,
166 * SR_ERR upon failure.
168 SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
169 uint8_t *buf, size_t *buflen,
170 packet_length_t get_packet_size,
171 packet_valid_callback is_valid,
172 uint64_t timeout_ms, int baudrate)
174 int64_t start, time, byte_delay_us;
175 size_t ibuf, i, maxlen;
176 ssize_t len, stream_len;
182 sr_dbg("Detecting packets on %s (timeout = %" PRIu64
183 "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
185 /* Assume 8n1 transmission. That is 10 bits for every byte. */
186 byte_delay_us = 10 * ((1000 * 1000) / baudrate);
187 start = g_get_monotonic_time();
189 packet_len = i = ibuf = len = 0;
190 while (ibuf < maxlen) {
191 len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf);
194 sr_spew("Read %zd bytes.", len);
197 time = g_get_monotonic_time() - start;
200 stream_len = ibuf - i;
201 if (stream_len > 0 && packet_len == 0) {
202 /* How large of a packet are we expecting? */
203 packet_len = stream_len;
204 status = get_packet_size(&buf[i], &packet_len);
206 case PACKET_HEADER_OK:
207 /* We know how much data we need to wait for. */
209 case PACKET_NEED_MORE_DATA:
210 /* We did not receive the full header. */
213 case PACKET_INVALID_HEADER:
216 * We had enough data, but here was an error in
217 * parsing the header. Restart parsing from the
226 if ((stream_len >= packet_len) && (packet_len != 0)) {
227 /* We have at least a packet's worth of data. */
228 if (is_valid(&buf[i])) {
229 sr_spew("Found valid %d-byte packet after "
230 "%" PRIu64 "ms.", packet_len, time);
234 sr_spew("Got %d bytes, but not a valid "
235 "packet.", packet_len);
239 /* Not a valid packet. Continue searching. */
244 if (time >= (int64_t)timeout_ms) {
246 sr_dbg("Detection timed out after %" PRIi64 "ms.", time);
249 g_usleep(byte_delay_us);
253 sr_err("Didn't find a valid packet (read %zu bytes).", ibuf);