2d41e5003e57c391d492e80430b380cb22df2fc7
[pulseview.git] / pv / data / decodesignal.hpp
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/row.hpp>
34 #include <pv/data/decode/rowdata.hpp>
35 #include <pv/data/signalbase.hpp>
36 #include <pv/util.hpp>
37
38 using std::atomic;
39 using std::condition_variable;
40 using std::map;
41 using std::mutex;
42 using std::pair;
43 using std::vector;
44 using std::shared_ptr;
45
46 namespace pv {
47 class Session;
48
49 namespace data {
50
51 namespace decode {
52 class Annotation;
53 class Decoder;
54 class Row;
55 }
56
57 class Logic;
58 class LogicSegment;
59 class SignalBase;
60 class SignalData;
61
62 struct DecodeChannel
63 {
64         uint16_t id;     ///< Global numerical ID for the decode channels in the stack
65         uint16_t bit_id; ///< Tells which bit within a sample represents this channel
66         const bool is_optional;
67         const pv::data::SignalBase *assigned_signal;
68         const QString name, desc;
69         int initial_pin_state;
70         const shared_ptr<pv::data::decode::Decoder> decoder_;
71         const srd_channel *pdch_;
72 };
73
74 struct DecodeSegment
75 {
76         map<const decode::Row, decode::RowData> annotation_rows;
77         pv::util::Timestamp start_time;
78         double samplerate;
79         int64_t samples_decoded;
80 };
81
82 class DecodeSignal : public SignalBase
83 {
84         Q_OBJECT
85
86 private:
87         static const double DecodeMargin;
88         static const double DecodeThreshold;
89         static const int64_t DecodeChunkLength;
90
91 public:
92         DecodeSignal(pv::Session &session);
93         virtual ~DecodeSignal();
94
95         bool is_decode_signal() const;
96         const vector< shared_ptr<data::decode::Decoder> >& decoder_stack() const;
97
98         void stack_decoder(const srd_decoder *decoder);
99         void remove_decoder(int index);
100         bool toggle_decoder_visibility(int index);
101
102         void reset_decode(bool shutting_down = false);
103         void begin_decode();
104         QString error_message() const;
105
106         const vector<data::DecodeChannel> get_channels() const;
107         void auto_assign_signals(const shared_ptr<pv::data::decode::Decoder> dec);
108         void assign_signal(const uint16_t channel_id, const SignalBase *signal);
109         int get_assigned_signal_count() const;
110
111         void set_initial_pin_state(const uint16_t channel_id, const int init_state);
112
113         double samplerate() const;
114         const pv::util::Timestamp start_time() const;
115
116         /**
117          * Returns the number of samples that can be worked on,
118          * i.e. the number of samples where samples are available
119          * for all connected channels.
120          */
121         int64_t get_working_sample_count(uint32_t segment_id) const;
122
123         int64_t get_decoded_sample_count(uint32_t segment_id) const;
124
125         vector<decode::Row> visible_rows() const;
126
127         /**
128          * Extracts sorted annotations between two period into a vector.
129          */
130         void get_annotation_subset(
131                 vector<pv::data::decode::Annotation> &dest,
132                 const decode::Row &row, uint32_t segment_id, uint64_t start_sample,
133                 uint64_t end_sample) const;
134
135         virtual void save_settings(QSettings &settings) const;
136
137         virtual void restore_settings(QSettings &settings);
138
139 private:
140         void set_error_message(QString msg);
141
142         uint32_t get_input_segment_count() const;
143
144         uint32_t get_input_samplerate(uint32_t segment_id) const;
145
146         void update_channel_list();
147
148         void commit_decoder_channels();
149
150         void mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end);
151
152         void logic_mux_proc();
153
154         void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
155                 const shared_ptr<LogicSegment> input_segment);
156
157         void decode_proc();
158
159         void start_srd_session();
160         void terminate_srd_session();
161         void stop_srd_session();
162
163         void connect_input_notifiers();
164
165         void create_decode_segment();
166
167         static void annotation_callback(srd_proto_data *pdata, void *decode_signal);
168
169 Q_SIGNALS:
170         void new_annotations(); // TODO Supply segment for which they belong to
171         void decode_reset();
172         void decode_finished();
173         void channels_updated();
174
175 private Q_SLOTS:
176         void on_capture_state_changed(int state);
177         void on_data_cleared();
178         void on_data_received();
179
180 private:
181         pv::Session &session_;
182
183         vector<data::DecodeChannel> channels_;
184
185         struct srd_session *srd_session_;
186
187         shared_ptr<Logic> logic_mux_data_;
188         uint32_t logic_mux_unit_size_;
189         bool logic_mux_data_invalid_;
190
191         vector< shared_ptr<decode::Decoder> > stack_;
192         bool stack_config_changed_;
193         map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
194
195         vector<DecodeSegment> segments_;
196         uint32_t current_segment_id_;
197
198         mutable mutex input_mutex_, output_mutex_, logic_mux_mutex_;
199         mutable condition_variable decode_input_cond_, logic_mux_cond_;
200
201         std::thread decode_thread_, logic_mux_thread_;
202         atomic<bool> decode_interrupt_, logic_mux_interrupt_;
203
204         QString error_message_;
205 };
206
207 } // namespace data
208 } // namespace pv
209
210 #endif // PULSEVIEW_PV_DATA_DECODESIGNAL_HPP