19a060f2bccdaa37360982fe54677ceb7b7a8608
[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 struct srd_decoder;
42 struct srd_decoder_annotation_row;
43 struct srd_channel;
44 struct srd_proto_data;
45 struct srd_session;
46
47 namespace DecoderStackTest {
48 struct TwoDecoderStack;
49 }
50
51 namespace pv {
52
53 class Session;
54
55 namespace view {
56 class LogicSignal;
57 }
58
59 namespace data {
60
61 class LogicSegment;
62
63 namespace decode {
64 class Annotation;
65 class Decoder;
66 }
67
68 class Logic;
69
70 class DecoderStack : public QObject
71 {
72         Q_OBJECT
73
74 private:
75         static const double DecodeMargin;
76         static const double DecodeThreshold;
77         static const int64_t DecodeChunkLength;
78         static const unsigned int DecodeNotifyPeriod;
79
80 public:
81         DecoderStack(pv::Session &session, const srd_decoder *const dec);
82
83         virtual ~DecoderStack();
84
85         const std::list< std::shared_ptr<decode::Decoder> >& stack() const;
86         void push(std::shared_ptr<decode::Decoder> decoder);
87         void remove(int index);
88
89         double samplerate() const;
90
91         const pv::util::Timestamp& start_time() const;
92
93         int64_t samples_decoded() const;
94
95         std::vector<decode::Row> get_visible_rows() const;
96
97         /**
98          * Extracts sorted annotations between two period into a vector.
99          */
100         void get_annotation_subset(
101                 std::vector<pv::data::decode::Annotation> &dest,
102                 const decode::Row &row, uint64_t start_sample,
103                 uint64_t end_sample) const;
104
105         QString error_message();
106
107         void clear();
108
109         uint64_t max_sample_count() const;
110
111         void begin_decode();
112
113 private:
114         boost::optional<int64_t> wait_for_data() const;
115
116         void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
117                 const unsigned int unit_size, srd_session *const session);
118
119         void decode_proc();
120
121         static void annotation_callback(srd_proto_data *pdata,
122                 void *decoder);
123
124 private Q_SLOTS:
125         void on_new_frame();
126
127         void on_data_received();
128
129         void on_frame_ended();
130
131 Q_SIGNALS:
132         void new_decode_data();
133
134 private:
135         pv::Session &session_;
136
137         pv::util::Timestamp start_time_;
138         double samplerate_;
139
140         /**
141          * This mutex prevents more than one thread from accessing
142          * libsigrokdecode concurrently.
143          * @todo A proper solution should be implemented to allow multiple
144          * decode operations in parallel.
145          */
146         static std::mutex global_srd_mutex_;
147
148         std::list< std::shared_ptr<decode::Decoder> > stack_;
149
150         std::shared_ptr<pv::data::LogicSegment> segment_;
151
152         mutable std::mutex input_mutex_;
153         mutable std::condition_variable input_cond_;
154         int64_t sample_count_;
155         bool frame_complete_;
156
157         mutable std::mutex output_mutex_;
158         int64_t samples_decoded_;
159
160         std::map<const decode::Row, decode::RowData> rows_;
161
162         std::map<std::pair<const srd_decoder*, int>, decode::Row> class_rows_;
163
164         QString error_message_;
165
166         std::thread decode_thread_;
167         std::atomic<bool> interrupt_;
168
169         friend struct DecoderStackTest::TwoDecoderStack;
170 };
171
172 } // namespace data
173 } // namespace pv
174
175 #endif // PULSEVIEW_PV_DATA_DECODERSTACK_HPP