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