2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <libsigrokdecode/libsigrokdecode.h>
23 #include <boost/foreach.hpp>
24 #include <boost/thread/thread.hpp>
30 #include "decoderstack.h"
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>
38 using boost::lock_guard;
40 using boost::shared_ptr;
49 const double DecoderStack::DecodeMargin = 1.0;
50 const double DecoderStack::DecodeThreshold = 0.2;
51 const int64_t DecoderStack::DecodeChunkLength = 4096;
53 mutex DecoderStack::_global_decode_mutex;
55 DecoderStack::DecoderStack(const srd_decoder *const dec) :
58 _stack.push_back(shared_ptr<decode::Decoder>(
59 new decode::Decoder(dec)));
62 DecoderStack::~DecoderStack()
64 _decode_thread.interrupt();
65 _decode_thread.join();
68 const std::list< boost::shared_ptr<decode::Decoder> >&
69 DecoderStack::stack() const
74 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
77 _stack.push_back(decoder);
80 void DecoderStack::remove(int index)
82 using pv::data::decode::Decoder;
85 assert(index < (int)_stack.size());
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());
96 int64_t DecoderStack::samples_decoded() const
98 lock_guard<mutex> decode_lock(_mutex);
99 return _samples_decoded;
102 const vector<decode::Annotation> DecoderStack::annotations() const
104 lock_guard<mutex> lock(_mutex);
108 QString DecoderStack::error_message()
110 lock_guard<mutex> lock(_mutex);
111 return _error_message;
114 void DecoderStack::begin_decode()
116 shared_ptr<pv::view::LogicSignal> logic_signal;
117 shared_ptr<pv::data::Logic> data;
119 _decode_thread.interrupt();
120 _decode_thread.join();
122 _samples_decoded = 0;
124 _annotations.clear();
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())))
138 // Get the samplerate and start time
139 _start_time = data->get_start_time();
140 _samplerate = data->samplerate();
141 if (_samplerate == 0.0)
144 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
148 void DecoderStack::clear()
150 _annotations.clear();
153 uint64_t DecoderStack::get_max_sample_count() const
155 if (_annotations.empty())
157 return _annotations.back().end_sample();
160 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
162 srd_session *session;
163 uint8_t chunk[DecodeChunkLength];
164 srd_decoder_inst *prev_di = NULL;
168 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
169 data->get_snapshots();
170 if (snapshots.empty())
173 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
175 const int64_t sample_count = snapshot->get_sample_count() - 1;
176 const unsigned int chunk_sample_count =
177 DecodeChunkLength / snapshot->unit_size();
179 // Create the session
180 srd_session_new(&session);
183 // Create the decoders
184 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
186 srd_decoder_inst *const di = dec->create_decoder_inst(session);
190 _error_message = tr("Failed to initialise decoder");
191 srd_session_destroy(session);
196 srd_inst_stack (session, prev_di, di);
202 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
203 g_variant_new_uint64((uint64_t)_samplerate));
205 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
206 DecoderStack::annotation_callback, this);
208 srd_session_start(session);
211 !boost::this_thread::interruption_requested() &&
213 i += chunk_sample_count)
215 lock_guard<mutex> decode_lock(_global_decode_mutex);
217 const int64_t chunk_end = min(
218 i + chunk_sample_count, sample_count);
219 snapshot->get_samples(chunk, i, chunk_end);
221 if (srd_session_send(session, i, i + sample_count,
222 chunk, chunk_end - i) != SRD_OK) {
223 _error_message = tr("Failed to initialise decoder");
228 lock_guard<mutex> lock(_mutex);
229 _samples_decoded = chunk_end;
233 // Destroy the session
234 srd_session_destroy(session);
237 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
239 using pv::data::decode::Annotation;
244 DecoderStack *const d = (DecoderStack*)decoder;
246 lock_guard<mutex> lock(d->_mutex);
247 d->_annotations.push_back(Annotation(pdata));
249 d->new_decode_data();