2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
5 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
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/>.
21 #include <libsigrok/libsigrok.h>
22 #include <libsigrok-internal.h>
28 static void send_frame_start(struct sr_dev_inst *sdi)
30 struct dev_context *devc;
31 struct lcr_parse_info *info;
34 struct sr_datafeed_packet packet;
37 info = &devc->parse_info;
39 /* Communicate changes of frequency or model before data values. */
40 freq = info->output_freq;
41 if (freq != devc->output_freq) {
42 devc->output_freq = freq;
43 sr_session_send_meta(sdi, SR_CONF_OUTPUT_FREQUENCY,
44 g_variant_new_double(freq));
46 model = info->circuit_model;
47 if (model && model != devc->circuit_model) {
48 devc->circuit_model = model;
49 sr_session_send_meta(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL,
50 g_variant_new_string(model));
53 /* Data is about to get sent. Start a new frame. */
54 packet.type = SR_DF_FRAME_BEGIN;
55 sr_session_send(sdi, &packet);
58 static void send_frame_end(struct sr_dev_inst *sdi)
60 struct sr_datafeed_packet packet;
62 packet.type = SR_DF_FRAME_END;
63 sr_session_send(sdi, &packet);
66 static int handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
68 struct dev_context *devc;
69 struct lcr_parse_info *info;
70 const struct lcr_info *lcr;
74 struct sr_datafeed_packet packet;
75 struct sr_datafeed_analog analog;
76 struct sr_analog_encoding encoding;
77 struct sr_analog_meaning meaning;
78 struct sr_analog_spec spec;
80 struct sr_channel *channel;
83 info = &devc->parse_info;
86 /* Note: digits/spec_digits will be overridden later. */
87 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
88 analog.num_samples = 1;
92 for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) {
93 channel = g_slist_nth_data(sdi->channels, ch_idx);
94 analog.meaning->channels = g_slist_append(NULL, channel);
95 info->ch_idx = ch_idx;
96 rc = lcr->packet_parse(pkt, &value, &analog, info);
97 if (sdi->session && rc == SR_OK && analog.meaning->mq && channel->enabled) {
99 send_frame_start(sdi);
102 packet.type = SR_DF_ANALOG;
103 packet.payload = &analog;
104 sr_session_send(sdi, &packet);
106 g_slist_free(analog.meaning->channels);
110 sr_sw_limits_update_frames_read(&devc->limits, 1);
116 static int handle_new_data(struct sr_dev_inst *sdi)
118 struct dev_context *devc;
119 struct sr_serial_dev_inst *serial;
121 const struct lcr_info *lcr;
128 /* Read another chunk of data into the buffer. */
129 rdsize = sizeof(devc->buf) - devc->buf_rxpos;
130 rdsize = serial_read_nonblocking(serial, &devc->buf[devc->buf_rxpos], rdsize);
133 devc->buf_rxpos += rdsize;
136 * Process as many packets as the buffer might contain. Assume
137 * that the stream is synchronized in the typical case. Re-sync
138 * in case of mismatch (skip individual bytes until data matches
139 * the expected packet layout again).
141 lcr = devc->lcr_info;
142 while (devc->buf_rxpos >= lcr->packet_size) {
144 if (!lcr->packet_valid(pkt)) {
145 copy_len = devc->buf_rxpos - 1;
146 memmove(&devc->buf[0], &devc->buf[1], copy_len);
150 (void)handle_packet(sdi, pkt);
151 copy_len = devc->buf_rxpos - lcr->packet_size;
152 memmove(&devc->buf[0], &devc->buf[lcr->packet_size], copy_len);
153 devc->buf_rxpos -= lcr->packet_size;
159 SR_PRIV int lcr_receive_data(int fd, int revents, void *cb_data)
161 struct sr_dev_inst *sdi;
162 struct dev_context *devc;
166 if (!(sdi = cb_data))
168 if (!(devc = sdi->priv))
171 if (revents == G_IO_IN)
172 handle_new_data(sdi);
174 if (sr_sw_limits_check(&devc->limits))
175 sr_dev_acquisition_stop(sdi);