]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.hpp
DecoderStack: Make decoder sessions terminate after running
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifndef PULSEVIEW_PV_DATA_DECODERSTACK_HPP
22 #define PULSEVIEW_PV_DATA_DECODERSTACK_HPP
23
24 #include "signaldata.hpp"
25
26 #include <atomic>
27 #include <condition_variable>
28 #include <list>
29 #include <map>
30 #include <memory>
31 #include <thread>
32
33 #include <boost/optional.hpp>
34
35 #include <QObject>
36 #include <QString>
37
38 #include <pv/data/decode/row.hpp>
39 #include <pv/data/decode/rowdata.hpp>
40 #include <pv/util.hpp>
41
42 struct srd_decoder;
43 struct srd_decoder_annotation_row;
44 struct srd_channel;
45 struct srd_proto_data;
46 struct srd_session;
47
48 namespace DecoderStackTest {
49 struct TwoDecoderStack;
50 }
51
52 namespace pv {
53
54 class Session;
55
56 namespace view {
57 class LogicSignal;
58 }
59
60 namespace data {
61
62 class LogicSegment;
63
64 namespace decode {
65 class Annotation;
66 class Decoder;
67 }
68
69 class Logic;
70
71 class DecoderStack : public QObject
72 {
73         Q_OBJECT
74
75 private:
76         static const double DecodeMargin;
77         static const double DecodeThreshold;
78         static const int64_t DecodeChunkLength;
79         static const unsigned int DecodeNotifyPeriod;
80
81 public:
82         DecoderStack(pv::Session &session_,
83                 const srd_decoder *const decoder);
84
85         virtual ~DecoderStack();
86
87         const std::list< std::shared_ptr<decode::Decoder> >& stack() const;
88         void push(std::shared_ptr<decode::Decoder> decoder);
89         void remove(int index);
90
91         double samplerate() const;
92
93         const pv::util::Timestamp& start_time() const;
94
95         int64_t samples_decoded() const;
96
97         std::vector<decode::Row> get_visible_rows() const;
98
99         /**
100          * Extracts sorted annotations between two period into a vector.
101          */
102         void get_annotation_subset(
103                 std::vector<pv::data::decode::Annotation> &dest,
104                 const decode::Row &row, uint64_t start_sample,
105                 uint64_t end_sample) const;
106
107         QString error_message();
108
109         void clear();
110
111         uint64_t max_sample_count() const;
112
113         void begin_decode();
114
115 private:
116         boost::optional<int64_t> wait_for_data() const;
117
118         void decode_data(const int64_t sample_count,
119                 const unsigned int unit_size, srd_session *const session);
120
121         void decode_proc();
122
123         static void annotation_callback(srd_proto_data *pdata,
124                 void *decoder);
125
126 private Q_SLOTS:
127         void on_new_frame();
128
129         void on_data_received();
130
131         void on_frame_ended();
132
133 Q_SIGNALS:
134         void new_decode_data();
135
136 private:
137         pv::Session &session_;
138
139         pv::util::Timestamp start_time_;
140         double samplerate_;
141
142         /**
143          * This mutex prevents more than one decode operation occuring
144          * concurrently.
145          * @todo A proper solution should be implemented to allow multiple
146          * decode operations.
147          */
148         static std::mutex global_decode_mutex_;
149
150         std::list< std::shared_ptr<decode::Decoder> > stack_;
151
152         std::shared_ptr<pv::data::LogicSegment> segment_;
153
154         mutable std::mutex input_mutex_;
155         mutable std::condition_variable input_cond_;
156         int64_t sample_count_;
157         bool frame_complete_;
158
159         mutable std::mutex output_mutex_;
160         int64_t samples_decoded_;
161
162         std::map<const decode::Row, decode::RowData> rows_;
163
164         std::map<std::pair<const srd_decoder*, int>, decode::Row> class_rows_;
165
166         QString error_message_;
167
168         std::thread decode_thread_;
169         std::atomic<bool> interrupt_;
170
171         friend struct DecoderStackTest::TwoDecoderStack;
172 };
173
174 } // namespace data
175 } // namespace pv
176
177 #endif // PULSEVIEW_PV_DATA_DECODERSTACK_HPP