]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.hpp
SignalBase: Also emit data related signals
[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          * Extracts sorted annotations between two period into a vector.
108          */
109         void get_annotation_subset(
110                 vector<pv::data::decode::Annotation> &dest,
111                 const decode::Row &row, uint64_t start_sample,
112                 uint64_t end_sample) const;
113
114         QString error_message();
115
116         void clear();
117
118         uint64_t max_sample_count() const;
119
120         void begin_decode();
121
122 private:
123         boost::optional<int64_t> wait_for_data() const;
124
125         void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
126                 const unsigned int unit_size, srd_session *const session);
127
128         void decode_proc();
129
130         static void annotation_callback(srd_proto_data *pdata, void *decoder);
131
132 private Q_SLOTS:
133         void on_new_frame();
134
135         void on_data_received();
136
137         void on_frame_ended();
138
139 Q_SIGNALS:
140         void new_decode_data();
141
142 private:
143         pv::Session &session_;
144
145         pv::util::Timestamp start_time_;
146         double samplerate_;
147
148         /**
149          * This mutex prevents more than one thread from accessing
150          * libsigrokdecode concurrently.
151          * @todo A proper solution should be implemented to allow multiple
152          * decode operations in parallel.
153          */
154         static mutex global_srd_mutex_;
155
156         list< shared_ptr<decode::Decoder> > stack_;
157
158         shared_ptr<pv::data::LogicSegment> segment_;
159
160         mutable mutex input_mutex_;
161         mutable condition_variable input_cond_;
162         int64_t sample_count_;
163         bool frame_complete_;
164
165         mutable mutex output_mutex_;
166         int64_t samples_decoded_;
167
168         map<const decode::Row, decode::RowData> rows_;
169
170         map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
171
172         QString error_message_;
173
174         std::thread decode_thread_;
175         atomic<bool> interrupt_;
176
177         friend struct DecoderStackTest::TwoDecoderStack;
178 };
179
180 } // namespace data
181 } // namespace pv
182
183 #endif // PULSEVIEW_PV_DATA_DECODERSTACK_HPP