]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decodesignal.hpp
DecodeSignal: Fix race condition
[pulseview.git] / pv / data / decodesignal.hpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5 *
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 2 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
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/>.
18 */
19
20#ifndef PULSEVIEW_PV_DATA_DECODESIGNAL_HPP
21#define PULSEVIEW_PV_DATA_DECODESIGNAL_HPP
22
23#include <atomic>
24#include <condition_variable>
25#include <unordered_set>
26#include <vector>
27
28#include <QSettings>
29#include <QString>
30
31#include <libsigrokdecode/libsigrokdecode.h>
32
33#include <pv/data/decode/decoder.hpp>
34#include <pv/data/decode/row.hpp>
35#include <pv/data/decode/rowdata.hpp>
36#include <pv/data/signalbase.hpp>
37#include <pv/util.hpp>
38
39using std::atomic;
40using std::condition_variable;
41using std::map;
42using std::mutex;
43using std::pair;
44using std::vector;
45using std::shared_ptr;
46
47namespace pv {
48class Session;
49
50namespace data {
51
52namespace decode {
53class Annotation;
54class Decoder;
55class Row;
56}
57
58class Logic;
59class LogicSegment;
60class SignalBase;
61class SignalData;
62
63struct DecodeBinaryData
64{
65 vector<uint8_t> data;
66 uint64_t sample; ///< Number of the sample where this data was provided by the PD
67};
68
69struct DecodeSegment
70{
71 map<const decode::Row, decode::RowData> annotation_rows;
72 pv::util::Timestamp start_time;
73 double samplerate;
74 int64_t samples_decoded_incl, samples_decoded_excl;
75 vector<DecodeBinaryData> binary_data;
76};
77
78class DecodeSignal : public SignalBase
79{
80 Q_OBJECT
81
82private:
83 static const double DecodeMargin;
84 static const double DecodeThreshold;
85 static const int64_t DecodeChunkLength;
86
87public:
88 DecodeSignal(pv::Session &session);
89 virtual ~DecodeSignal();
90
91 bool is_decode_signal() const;
92 const vector< shared_ptr<data::decode::Decoder> >& decoder_stack() const;
93
94 void stack_decoder(const srd_decoder *decoder);
95 void remove_decoder(int index);
96 bool toggle_decoder_visibility(int index);
97
98 void reset_decode(bool shutting_down = false);
99 void begin_decode();
100 void pause_decode();
101 void resume_decode();
102 bool is_paused() const;
103 QString error_message() const;
104
105 const vector<data::decode::DecodeChannel> get_channels() const;
106 void auto_assign_signals(const shared_ptr<pv::data::decode::Decoder> dec);
107 void assign_signal(const uint16_t channel_id, const SignalBase *signal);
108 int get_assigned_signal_count() const;
109
110 void set_initial_pin_state(const uint16_t channel_id, const int init_state);
111
112 double samplerate() const;
113 const pv::util::Timestamp start_time() const;
114
115 /**
116 * Returns the number of samples that can be worked on,
117 * i.e. the number of samples where samples are available
118 * for all connected channels.
119 */
120 int64_t get_working_sample_count(uint32_t segment_id) const;
121
122 /**
123 * Returns the number of processed samples. Newly generated annotations will
124 * have sample numbers greater than this.
125 *
126 * If include_processing is true, this number will include the ones being
127 * currently processed (in case the decoder stack is running). In this case,
128 * newly generated annotations will have sample numbers smaller than this.
129 */
130 int64_t get_decoded_sample_count(uint32_t segment_id,
131 bool include_processing) const;
132
133 vector<decode::Row> get_rows(bool visible_only) const;
134
135 /**
136 * Extracts annotations from a single row into a vector.
137 * Note: The annotations may be unsorted and only annotations that fully
138 * fit into the sample range are considered.
139 */
140 void get_annotation_subset(
141 vector<pv::data::decode::Annotation> &dest,
142 const decode::Row &row, uint32_t segment_id, uint64_t start_sample,
143 uint64_t end_sample) const;
144
145 /**
146 * Extracts annotations from all rows into a vector.
147 * Note: The annotations may be unsorted and only annotations that fully
148 * fit into the sample range are considered.
149 */
150 void get_annotation_subset(
151 vector<pv::data::decode::Annotation> &dest,
152 uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const;
153
154 uint32_t get_binary_data_chunk_count(uint32_t segment_id) const;
155 void get_binary_data_chunk(uint32_t segment_id, uint32_t chunk_id,
156 const vector<uint8_t> **dest, uint64_t *size);
157 void get_binary_data_chunks_merged(uint32_t segment_id,
158 uint64_t start_sample, uint64_t end_sample, vector<uint8_t> *dest) const;
159
160 virtual void save_settings(QSettings &settings) const;
161
162 virtual void restore_settings(QSettings &settings);
163
164private:
165 void set_error_message(QString msg);
166
167 uint32_t get_input_segment_count() const;
168
169 uint32_t get_input_samplerate(uint32_t segment_id) const;
170
171 void update_channel_list();
172
173 void commit_decoder_channels();
174
175 void mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end);
176
177 void logic_mux_proc();
178
179 void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
180 const shared_ptr<LogicSegment> input_segment);
181
182 void decode_proc();
183
184 void start_srd_session();
185 void terminate_srd_session();
186 void stop_srd_session();
187
188 void connect_input_notifiers();
189
190 void create_decode_segment();
191
192 static void annotation_callback(srd_proto_data *pdata, void *decode_signal);
193 static void binary_callback(srd_proto_data *pdata, void *decode_signal);
194
195Q_SIGNALS:
196 void new_annotations(); // TODO Supply segment for which they belong to
197 void new_binary_data(unsigned int segment_id);
198 void decode_reset();
199 void decode_finished();
200 void channels_updated();
201
202private Q_SLOTS:
203 void on_capture_state_changed(int state);
204 void on_data_cleared();
205 void on_data_received();
206
207private:
208 pv::Session &session_;
209
210 vector<data::decode::DecodeChannel> channels_;
211
212 struct srd_session *srd_session_;
213
214 shared_ptr<Logic> logic_mux_data_;
215 uint32_t logic_mux_unit_size_;
216 bool logic_mux_data_invalid_;
217
218 vector< shared_ptr<decode::Decoder> > stack_;
219 bool stack_config_changed_;
220 map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
221
222 vector<DecodeSegment> segments_;
223 uint32_t current_segment_id_;
224
225 mutable mutex input_mutex_, output_mutex_, decode_pause_mutex_, logic_mux_mutex_;
226 mutable condition_variable decode_input_cond_, decode_pause_cond_,
227 logic_mux_cond_;
228
229 std::thread decode_thread_, logic_mux_thread_;
230 atomic<bool> decode_interrupt_, logic_mux_interrupt_;
231
232 bool decode_paused_;
233
234 QString error_message_;
235};
236
237} // namespace data
238} // namespace pv
239
240#endif // PULSEVIEW_PV_DATA_DECODESIGNAL_HPP