]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.hpp
Introduce DecodeSignal class
[pulseview.git] / pv / data / decoderstack.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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_DECODERSTACK_HPP
21 #define PULSEVIEW_PV_DATA_DECODERSTACK_HPP
22
23 #include "signaldata.hpp"
24
25 #include <atomic>
26 #include <condition_variable>
27 #include <list>
28 #include <map>
29 #include <memory>
30 #include <thread>
31
32 #include <boost/optional.hpp>
33
34 #include <QObject>
35 #include <QString>
36
37 #include <pv/data/decode/row.hpp>
38 #include <pv/data/decode/rowdata.hpp>
39 #include <pv/util.hpp>
40
41 using std::atomic;
42 using std::condition_variable;
43 using std::list;
44 using std::map;
45 using std::mutex;
46 using std::pair;
47 using std::shared_ptr;
48 using std::vector;
49
50 struct srd_decoder;
51 struct srd_decoder_annotation_row;
52 struct srd_channel;
53 struct srd_proto_data;
54 struct srd_session;
55
56 namespace DecoderStackTest {
57 struct TwoDecoderStack;
58 }
59
60 namespace pv {
61
62 class Session;
63
64 namespace view {
65 class LogicSignal;
66 }
67
68 namespace data {
69
70 class LogicSegment;
71
72 namespace decode {
73 class Annotation;
74 class Decoder;
75 }
76
77 class Logic;
78
79 class DecoderStack : public QObject
80 {
81         Q_OBJECT
82
83 private:
84         static const double DecodeMargin;
85         static const double DecodeThreshold;
86         static const int64_t DecodeChunkLength;
87         static const unsigned int DecodeNotifyPeriod;
88
89 public:
90         DecoderStack(pv::Session &session, const srd_decoder *const dec);
91
92         virtual ~DecoderStack();
93
94         const list< shared_ptr<decode::Decoder> >& stack() const;
95         void push(shared_ptr<decode::Decoder> decoder);
96         void remove(int index);
97
98         double samplerate() const;
99
100         const pv::util::Timestamp& start_time() const;
101
102         int64_t samples_decoded() const;
103
104         vector<decode::Row> get_visible_rows() const;
105
106         /**
107          * Helper function for static annotation_callback(),
108          * must be public so the function can access it.
109          * Don't use from outside this class.
110          */
111         uint64_t inc_annotation_count();
112
113         /**
114          * Extracts sorted annotations between two period into a vector.
115          */
116         void get_annotation_subset(
117                 vector<pv::data::decode::Annotation> &dest,
118                 const decode::Row &row, uint64_t start_sample,
119                 uint64_t end_sample) const;
120
121         QString error_message();
122
123         void clear();
124
125         uint64_t max_sample_count() const;
126
127         void begin_decode();
128
129 private:
130         boost::optional<int64_t> wait_for_data() const;
131
132         void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
133                 const unsigned int unit_size, srd_session *const session);
134
135         void decode_proc();
136
137         static void annotation_callback(srd_proto_data *pdata, void *decoder_stack);
138
139 private Q_SLOTS:
140         void on_new_frame();
141
142         void on_data_received();
143
144         void on_frame_ended();
145
146 Q_SIGNALS:
147         void new_annotations();
148
149 private:
150         pv::Session &session_;
151
152         pv::util::Timestamp start_time_;
153         double samplerate_;
154
155         /**
156          * This mutex prevents more than one thread from accessing
157          * libsigrokdecode concurrently.
158          * @todo A proper solution should be implemented to allow multiple
159          * decode operations in parallel.
160          */
161         static mutex global_srd_mutex_;
162
163         list< shared_ptr<decode::Decoder> > stack_;
164
165         shared_ptr<pv::data::LogicSegment> segment_;
166
167         mutable mutex input_mutex_;
168         mutable condition_variable input_cond_;
169         int64_t sample_count_, annotation_count_;
170         bool frame_complete_;
171
172         mutable mutex output_mutex_;
173         int64_t samples_decoded_;
174
175         map<const decode::Row, decode::RowData> rows_;
176
177         map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
178
179         QString error_message_;
180
181         std::thread decode_thread_;
182         atomic<bool> interrupt_;
183
184         friend struct DecoderStackTest::TwoDecoderStack;
185 };
186
187 } // namespace data
188 } // namespace pv
189
190 #endif // PULSEVIEW_PV_DATA_DECODERSTACK_HPP