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 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
37 analog.num_samples = 1;
38 analog.meaning->mq = 0;
40 if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK)
42 analog.data = &floatval;
44 analog.meaning->channels = sdi->channels;
46 if (analog.meaning->mq != 0) {
47 /* Got a measurement. */
48 packet.type = SR_DF_ANALOG;
49 packet.payload = &analog;
50 sr_session_send(sdi, &packet);
51 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
55 static void handle_new_data(struct sr_dev_inst *sdi)
57 struct dev_context *devc;
58 int len, status, offset = 0;
59 struct sr_serial_dev_inst *serial;
64 /* Try to get as much data as the buffer can hold. */
65 len = DMM_BUFSIZE - devc->buflen;
66 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
68 sr_err("Serial port read error: %d.", len);
72 status = PACKET_INVALID_HEADER;
74 /* Now look for packets in that data. */
75 while (status != PACKET_NEED_MORE_DATA) {
76 /* We don't have a header, look for one. */
77 if (devc->next_packet_len == 0) {
78 len = devc->buflen - offset;
79 status = brymen_packet_length(devc->buf + offset, &len);
80 if (status == PACKET_HEADER_OK) {
81 /* We know how large the packet will be. */
82 devc->next_packet_len = len;
83 } else if (status == PACKET_NEED_MORE_DATA) {
84 /* We didn't yet receive the full header. */
85 devc->next_packet_len = 0;
88 /* Invalid header. Move on. */
89 devc->next_packet_len = 0;
95 /* We know how the packet size, but did we receive all of it? */
96 if (devc->buflen - offset < devc->next_packet_len)
99 /* We should have a full packet here, so we can check it. */
100 if (brymen_packet_is_valid(devc->buf + offset)) {
101 handle_packet(devc->buf + offset, sdi);
102 offset += devc->next_packet_len;
107 /* We are done with this packet. Look for a new one. */
108 devc->next_packet_len = 0;
111 /* If we have any data left, move it to the beginning of our buffer. */
112 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
113 devc->buflen -= offset;
116 SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
118 struct sr_dev_inst *sdi;
119 struct dev_context *devc;
120 struct sr_serial_dev_inst *serial;
125 if (!(sdi = cb_data))
128 if (!(devc = sdi->priv))
133 if (revents == G_IO_IN) {
134 /* Serial data arrived. */
135 handle_new_data(sdi);
137 /* Timeout, send another packet request. */
138 if ((ret = brymen_packet_request(serial)) < 0) {
139 sr_err("Failed to request packet: %d.", ret);
144 if (sr_sw_limits_check(&devc->sw_limits))
145 sdi->driver->dev_acquisition_stop(sdi);
151 * Try to find a valid packet in a serial data stream.
153 * @param serial Previously initialized serial port structure.
154 * @param buf Buffer containing the bytes to write.
155 * @param buflen Size of the buffer.
156 * @param get_packet_size Callback that assesses the size of incoming packets.
157 * @param is_valid Callback that assesses whether the packet is valid or not.
158 * @param timeout_ms The timeout after which, if no packet is detected, to
160 * @param baudrate The baudrate of the serial port. This parameter is not
161 * critical, but it helps fine tune the serial port polling
164 * @return SR_OK if a valid packet is found within the given timeout,
165 * SR_ERR upon failure.
167 SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
168 uint8_t *buf, size_t *buflen,
169 packet_length_t get_packet_size,
170 packet_valid_callback is_valid,
171 uint64_t timeout_ms, int baudrate)
173 int64_t start, time, byte_delay_us;
174 size_t ibuf, i, maxlen;
175 ssize_t len, stream_len;
181 sr_dbg("Detecting packets on %s (timeout = %" PRIu64
182 "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
184 /* Assume 8n1 transmission. That is 10 bits for every byte. */
185 byte_delay_us = 10 * ((1000 * 1000) / baudrate);
186 start = g_get_monotonic_time();
188 packet_len = i = ibuf = len = 0;
189 while (ibuf < maxlen) {
190 len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf);
193 sr_spew("Read %zd bytes.", len);
196 time = g_get_monotonic_time() - start;
199 stream_len = ibuf - i;
200 if (stream_len > 0 && packet_len == 0) {
201 /* How large of a packet are we expecting? */
202 packet_len = stream_len;
203 status = get_packet_size(&buf[i], &packet_len);
205 case PACKET_HEADER_OK:
206 /* We know how much data we need to wait for. */
208 case PACKET_NEED_MORE_DATA:
209 /* We did not receive the full header. */
212 case PACKET_INVALID_HEADER:
215 * We had enough data, but here was an error in
216 * parsing the header. Restart parsing from the
225 if ((stream_len >= packet_len) && (packet_len != 0)) {
226 /* We have at least a packet's worth of data. */
227 if (is_valid(&buf[i])) {
228 sr_spew("Found valid %d-byte packet after "
229 "%" PRIu64 "ms.", packet_len, time);
233 sr_spew("Got %d bytes, but not a valid "
234 "packet.", packet_len);
238 /* Not a valid packet. Continue searching. */
243 if (time >= (int64_t)timeout_ms) {
245 sr_dbg("Detection timed out after %" PRIi64 "ms.", time);
248 g_usleep(byte_delay_us);
252 sr_err("Didn't find a valid packet (read %zu bytes).", ibuf);