]> sigrok.org Git - libsigrok.git/blob - src/hardware/serial-lcr/protocol.c
serial-lcr: add support for packet request
[libsigrok.git] / src / hardware / serial-lcr / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
5  * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <libsigrok/libsigrok.h>
22 #include <libsigrok-internal.h>
23 #include "protocol.h"
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static void send_frame_start(struct sr_dev_inst *sdi)
29 {
30         struct dev_context *devc;
31         struct lcr_parse_info *info;
32         uint64_t freq;
33         const char *model;
34         struct sr_datafeed_packet packet;
35
36         devc = sdi->priv;
37         info = &devc->parse_info;
38
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));
45         }
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));
51         }
52
53         /* Data is about to get sent. Start a new frame. */
54         packet.type = SR_DF_FRAME_BEGIN;
55         sr_session_send(sdi, &packet);
56 }
57
58 static int handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
59 {
60         struct dev_context *devc;
61         struct lcr_parse_info *info;
62         const struct lcr_info *lcr;
63         size_t ch_idx;
64         int rc;
65         float value;
66         struct sr_datafeed_packet packet;
67         struct sr_datafeed_analog analog;
68         struct sr_analog_encoding encoding;
69         struct sr_analog_meaning meaning;
70         struct sr_analog_spec spec;
71         gboolean frame;
72         struct sr_channel *channel;
73
74         devc = sdi->priv;
75         info = &devc->parse_info;
76         lcr = devc->lcr_info;
77
78         /* Note: digits/spec_digits will be overridden later. */
79         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
80         analog.num_samples = 1;
81         analog.data = &value;
82
83         frame = FALSE;
84         for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) {
85                 channel = g_slist_nth_data(sdi->channels, ch_idx);
86                 analog.meaning->channels = g_slist_append(NULL, channel);
87                 info->ch_idx = ch_idx;
88                 rc = lcr->packet_parse(pkt, &value, &analog, info);
89                 if (sdi->session && rc == SR_OK && analog.meaning->mq && channel->enabled) {
90                         if (!frame) {
91                                 send_frame_start(sdi);
92                                 frame = TRUE;
93                         }
94                         packet.type = SR_DF_ANALOG;
95                         packet.payload = &analog;
96                         sr_session_send(sdi, &packet);
97                 }
98                 g_slist_free(analog.meaning->channels);
99         }
100         if (frame) {
101                 std_session_send_frame_end(sdi);
102                 sr_sw_limits_update_frames_read(&devc->limits, 1);
103         }
104
105         return SR_OK;
106 }
107
108 static int handle_new_data(struct sr_dev_inst *sdi)
109 {
110         struct dev_context *devc;
111         struct sr_serial_dev_inst *serial;
112         ssize_t rdsize;
113         const struct lcr_info *lcr;
114         uint8_t *pkt;
115         size_t copy_len;
116
117         devc = sdi->priv;
118         serial = sdi->conn;
119
120         /* Read another chunk of data into the buffer. */
121         rdsize = sizeof(devc->buf) - devc->buf_rxpos;
122         rdsize = serial_read_nonblocking(serial, &devc->buf[devc->buf_rxpos], rdsize);
123         if (rdsize < 0)
124                 return SR_ERR_IO;
125         devc->buf_rxpos += rdsize;
126
127         /*
128          * Process as many packets as the buffer might contain. Assume
129          * that the stream is synchronized in the typical case. Re-sync
130          * in case of mismatch (skip individual bytes until data matches
131          * the expected packet layout again).
132          */
133         lcr = devc->lcr_info;
134         while (devc->buf_rxpos >= lcr->packet_size) {
135                 pkt = &devc->buf[0];
136                 if (!lcr->packet_valid(pkt)) {
137                         copy_len = devc->buf_rxpos - 1;
138                         memmove(&devc->buf[0], &devc->buf[1], copy_len);
139                         devc->buf_rxpos--;
140                         continue;
141                 }
142                 (void)handle_packet(sdi, pkt);
143                 copy_len = devc->buf_rxpos - lcr->packet_size;
144                 memmove(&devc->buf[0], &devc->buf[lcr->packet_size], copy_len);
145                 devc->buf_rxpos -= lcr->packet_size;
146         }
147
148         return SR_OK;
149 }
150
151 static int handle_timeout(struct sr_dev_inst *sdi)
152 {
153         struct dev_context *devc;
154         const struct lcr_info *lcr;
155         struct sr_serial_dev_inst *serial;
156         int64_t now;
157         int ret;
158
159         devc = sdi->priv;
160         lcr = devc->lcr_info;
161
162         if (!lcr->packet_request)
163                 return SR_OK;
164
165         now = g_get_monotonic_time();
166         if (devc->req_next_at && now < devc->req_next_at)
167                 return SR_OK;
168
169         serial = sdi->conn;
170         ret = lcr->packet_request(serial);
171         if (ret < 0) {
172                 sr_err("Failed to request packet: %d.", ret);
173                 return ret;
174         }
175
176         if (lcr->req_timeout_ms)
177                 devc->req_next_at = now + lcr->req_timeout_ms * 1000;
178
179         return SR_OK;
180 }
181
182 SR_PRIV int lcr_receive_data(int fd, int revents, void *cb_data)
183 {
184         struct sr_dev_inst *sdi;
185         struct dev_context *devc;
186         int ret;
187
188         (void)fd;
189
190         if (!(sdi = cb_data))
191                 return TRUE;
192         if (!(devc = sdi->priv))
193                 return TRUE;
194
195         if (revents == G_IO_IN)
196                 ret = handle_new_data(sdi);
197         else
198                 ret = handle_timeout(sdi);
199         if (sr_sw_limits_check(&devc->limits))
200                 sr_dev_acquisition_stop(sdi);
201         if (ret != SR_OK)
202                 return FALSE;
203
204         return TRUE;
205 }