]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.cpp
Moved annotation painting code into DecodeTrace, and moved Annotation in pv::data...
[pulseview.git] / pv / data / decoderstack.cpp
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 #include <libsigrokdecode/libsigrokdecode.h>
22
23 #include <boost/foreach.hpp>
24 #include <boost/thread/thread.hpp>
25
26 #include <stdexcept>
27
28 #include <QDebug>
29
30 #include "decoderstack.h"
31
32 #include <pv/data/logic.h>
33 #include <pv/data/logicsnapshot.h>
34 #include <pv/data/decode/decoder.h>
35 #include <pv/data/decode/annotation.h>
36 #include <pv/view/logicsignal.h>
37
38 using namespace boost;
39 using namespace std;
40
41 namespace pv {
42 namespace data {
43
44 const double DecoderStack::DecodeMargin = 1.0;
45 const double DecoderStack::DecodeThreshold = 0.2;
46 const int64_t DecoderStack::DecodeChunkLength = 4096;
47
48 mutex DecoderStack::_global_decode_mutex;
49
50 DecoderStack::DecoderStack(const srd_decoder *const dec) :
51         _samples_decoded(0)
52 {
53         _stack.push_back(shared_ptr<decode::Decoder>(
54                 new decode::Decoder(dec)));
55 }
56
57 DecoderStack::~DecoderStack()
58 {
59         _decode_thread.interrupt();
60         _decode_thread.join();
61 }
62
63 const std::list< boost::shared_ptr<decode::Decoder> >&
64 DecoderStack::stack() const
65 {
66         return _stack;
67 }
68
69 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
70 {
71         assert(decoder);
72         _stack.push_back(decoder);
73 }
74
75 void DecoderStack::remove(int index)
76 {
77         using pv::data::decode::Decoder;
78
79         assert(index >= 0);
80         assert(index < (int)_stack.size());
81
82         // Find the decoder in the stack
83         list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
84         for(int i = 0; i < index; i++, iter++)
85                 assert(iter != _stack.end());
86
87         // Delete the element
88         _stack.erase(iter);
89 }
90
91 int64_t DecoderStack::samples_decoded() const
92 {
93         lock_guard<mutex> decode_lock(_mutex);
94         return _samples_decoded;
95 }
96
97 const vector<decode::Annotation> DecoderStack::annotations() const
98 {
99         lock_guard<mutex> lock(_mutex);
100         return _annotations;
101 }
102
103 QString DecoderStack::error_message()
104 {
105         lock_guard<mutex> lock(_mutex);
106         return _error_message;
107 }
108
109 void DecoderStack::begin_decode()
110 {
111         shared_ptr<pv::view::LogicSignal> logic_signal;
112         shared_ptr<pv::data::Logic> data;
113
114         _decode_thread.interrupt();
115         _decode_thread.join();
116
117         _samples_decoded = 0;
118
119         _annotations.clear();
120
121         // We get the logic data of the first probe in the list.
122         // This works because we are currently assuming all
123         // LogicSignals have the same data/snapshot
124         BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
125                 if (dec && !dec->probes().empty() &&
126                         ((logic_signal = (*dec->probes().begin()).second)) &&
127                         ((data = logic_signal->logic_data())))
128                         break;
129
130         if (!data)
131                 return;
132
133         // Get the samplerate and start time
134         _start_time = data->get_start_time();
135         _samplerate = data->samplerate();
136         if (_samplerate == 0.0)
137                 _samplerate = 1.0;
138
139         _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
140                 data);
141 }
142
143 void DecoderStack::clear()
144 {
145         _annotations.clear();
146 }
147
148 uint64_t DecoderStack::get_max_sample_count() const
149 {
150         if (_annotations.empty())
151                 return 0;
152         return _annotations.back().end_sample();
153 }
154
155 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
156 {
157         srd_session *session;
158         uint8_t chunk[DecodeChunkLength];
159         srd_decoder_inst *prev_di = NULL;
160
161         assert(data);
162
163         const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
164                 data->get_snapshots();
165         if (snapshots.empty())
166                 return;
167
168         const shared_ptr<pv::data::LogicSnapshot> &snapshot =
169                 snapshots.front();
170         const int64_t sample_count = snapshot->get_sample_count() - 1;
171
172         // Create the session
173         srd_session_new(&session);
174         assert(session);
175
176
177         // Create the decoders
178         BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
179         {
180                 srd_decoder_inst *const di = dec->create_decoder_inst(session);
181
182                 if (!di)
183                 {
184                         _error_message = tr("Failed to initialise decoder");
185                         srd_session_destroy(session);
186                         return;
187                 }
188
189                 if (prev_di)
190                         srd_inst_stack (session, prev_di, di);
191
192                 prev_di = di;
193         }
194
195         // Start the session
196         srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
197                 g_variant_new_uint64((uint64_t)_samplerate));
198
199         srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
200                 DecoderStack::annotation_callback, this);
201
202         srd_session_start(session);
203
204         for (int64_t i = 0;
205                 !this_thread::interruption_requested() && i < sample_count;
206                 i += DecodeChunkLength)
207         {
208                 lock_guard<mutex> decode_lock(_global_decode_mutex);
209
210                 const int64_t chunk_end = min(
211                         i + DecodeChunkLength, sample_count);
212                 snapshot->get_samples(chunk, i, chunk_end);
213
214                 if (srd_session_send(session, i, i + sample_count,
215                                 chunk, chunk_end - i) != SRD_OK) {
216                         _error_message = tr("Failed to initialise decoder");
217                         break;
218                 }
219
220                 {
221                         lock_guard<mutex> lock(_mutex);
222                         _samples_decoded = chunk_end;
223                 }
224         }
225
226         // Destroy the session
227         srd_session_destroy(session);
228 }
229
230 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
231 {
232         using pv::data::decode::Annotation;
233
234         assert(pdata);
235         assert(decoder);
236
237         DecoderStack *const d = (DecoderStack*)decoder;
238
239         lock_guard<mutex> lock(d->_mutex);
240         d->_annotations.push_back(Annotation(pdata));
241
242         d->new_decode_data();
243 }
244
245 } // namespace data
246 } // namespace pv