]> sigrok.org Git - libsigrok.git/blob - src/hardware/serial-lcr/protocol.c
scpi-pps: don't break SCPI devices when scanning for HP-IB devices
[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
35         devc = sdi->priv;
36         info = &devc->parse_info;
37
38         /* Communicate changes of frequency or model before data values. */
39         freq = info->output_freq;
40         if (freq != devc->output_freq) {
41                 devc->output_freq = freq;
42                 sr_session_send_meta(sdi, SR_CONF_OUTPUT_FREQUENCY,
43                         g_variant_new_double(freq));
44         }
45         model = info->circuit_model;
46         if (model && model != devc->circuit_model) {
47                 devc->circuit_model = model;
48                 sr_session_send_meta(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL,
49                         g_variant_new_string(model));
50         }
51
52         /* Data is about to get sent. Start a new frame. */
53         std_session_send_df_frame_begin(sdi);
54 }
55
56 static int handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
57 {
58         struct dev_context *devc;
59         struct lcr_parse_info *info;
60         const struct lcr_info *lcr;
61         size_t ch_idx;
62         int rc;
63         float value;
64         struct sr_datafeed_packet packet;
65         struct sr_datafeed_analog analog;
66         struct sr_analog_encoding encoding;
67         struct sr_analog_meaning meaning;
68         struct sr_analog_spec spec;
69         gboolean frame;
70         struct sr_channel *channel;
71
72         devc = sdi->priv;
73         info = &devc->parse_info;
74         lcr = devc->lcr_info;
75
76         /* Note: digits/spec_digits will be overridden later. */
77         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
78         analog.num_samples = 1;
79         analog.data = &value;
80
81         frame = FALSE;
82         for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) {
83                 channel = g_slist_nth_data(sdi->channels, ch_idx);
84                 analog.meaning->channels = g_slist_append(NULL, channel);
85                 info->ch_idx = ch_idx;
86                 rc = lcr->packet_parse(pkt, &value, &analog, info);
87                 if (sdi->session && rc == SR_OK && analog.meaning->mq && channel->enabled) {
88                         if (!frame) {
89                                 send_frame_start(sdi);
90                                 frame = TRUE;
91                         }
92                         packet.type = SR_DF_ANALOG;
93                         packet.payload = &analog;
94                         sr_session_send(sdi, &packet);
95                 }
96                 g_slist_free(analog.meaning->channels);
97         }
98         if (frame) {
99                 std_session_send_df_frame_end(sdi);
100                 sr_sw_limits_update_frames_read(&devc->limits, 1);
101         }
102
103         return SR_OK;
104 }
105
106 static int handle_new_data(struct sr_dev_inst *sdi)
107 {
108         struct dev_context *devc;
109         struct sr_serial_dev_inst *serial;
110         ssize_t rdsize;
111         const struct lcr_info *lcr;
112         uint8_t *pkt;
113         size_t copy_len;
114
115         devc = sdi->priv;
116         serial = sdi->conn;
117
118         /* Read another chunk of data into the buffer. */
119         rdsize = sizeof(devc->buf) - devc->buf_rxpos;
120         rdsize = serial_read_nonblocking(serial, &devc->buf[devc->buf_rxpos], rdsize);
121         if (rdsize < 0)
122                 return SR_ERR_IO;
123         devc->buf_rxpos += rdsize;
124
125         /*
126          * Process as many packets as the buffer might contain. Assume
127          * that the stream is synchronized in the typical case. Re-sync
128          * in case of mismatch (skip individual bytes until data matches
129          * the expected packet layout again).
130          */
131         lcr = devc->lcr_info;
132         while (devc->buf_rxpos >= lcr->packet_size) {
133                 pkt = &devc->buf[0];
134                 if (!lcr->packet_valid(pkt)) {
135                         copy_len = devc->buf_rxpos - 1;
136                         memmove(&devc->buf[0], &devc->buf[1], copy_len);
137                         devc->buf_rxpos--;
138                         continue;
139                 }
140                 (void)handle_packet(sdi, pkt);
141                 copy_len = devc->buf_rxpos - lcr->packet_size;
142                 memmove(&devc->buf[0], &devc->buf[lcr->packet_size], copy_len);
143                 devc->buf_rxpos -= lcr->packet_size;
144         }
145
146         return SR_OK;
147 }
148
149 static int handle_timeout(struct sr_dev_inst *sdi)
150 {
151         struct dev_context *devc;
152         const struct lcr_info *lcr;
153         struct sr_serial_dev_inst *serial;
154         int64_t now;
155         int ret;
156
157         devc = sdi->priv;
158         lcr = devc->lcr_info;
159
160         if (!lcr->packet_request)
161                 return SR_OK;
162
163         now = g_get_monotonic_time();
164         if (devc->req_next_at && now < devc->req_next_at)
165                 return SR_OK;
166
167         serial = sdi->conn;
168         ret = lcr->packet_request(serial);
169         if (ret < 0) {
170                 sr_err("Failed to request packet: %d.", ret);
171                 return ret;
172         }
173
174         if (lcr->req_timeout_ms)
175                 devc->req_next_at = now + lcr->req_timeout_ms * 1000;
176
177         return SR_OK;
178 }
179
180 SR_PRIV int lcr_receive_data(int fd, int revents, void *cb_data)
181 {
182         struct sr_dev_inst *sdi;
183         struct dev_context *devc;
184         int ret;
185
186         (void)fd;
187
188         if (!(sdi = cb_data))
189                 return TRUE;
190         if (!(devc = sdi->priv))
191                 return TRUE;
192
193         if (revents == G_IO_IN)
194                 ret = handle_new_data(sdi);
195         else
196                 ret = handle_timeout(sdi);
197         if (sr_sw_limits_check(&devc->limits))
198                 sr_dev_acquisition_stop(sdi);
199         if (ret != SR_OK)
200                 return FALSE;
201
202         return TRUE;
203 }