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;
50 using namespace pv::data::decode;
55 const double DecoderStack::DecodeMargin = 1.0;
56 const double DecoderStack::DecodeThreshold = 0.2;
57 const int64_t DecoderStack::DecodeChunkLength = 4096;
59 mutex DecoderStack::_global_decode_mutex;
61 DecoderStack::DecoderStack(pv::SigSession &session,
62 const srd_decoder *const dec) :
66 _stack.push_back(shared_ptr<decode::Decoder>(
67 new decode::Decoder(dec)));
70 DecoderStack::~DecoderStack()
72 if (_decode_thread.joinable()) {
73 _decode_thread.interrupt();
74 _decode_thread.join();
78 const std::list< boost::shared_ptr<decode::Decoder> >&
79 DecoderStack::stack() const
84 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
87 _stack.push_back(decoder);
90 void DecoderStack::remove(int index)
93 assert(index < (int)_stack.size());
95 // Find the decoder in the stack
96 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
97 for(int i = 0; i < index; i++, iter++)
98 assert(iter != _stack.end());
100 // Delete the element
104 int64_t DecoderStack::samples_decoded() const
106 lock_guard<mutex> decode_lock(_mutex);
107 return _samples_decoded;
110 std::vector<Row> DecoderStack::get_visible_rows() const
112 lock_guard<mutex> lock(_mutex);
116 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
122 const srd_decoder *const decc = dec->decoder();
123 assert(dec->decoder());
125 // Add a row for the decoder if it doesn't have a row list
126 if (!decc->annotation_rows)
127 rows.push_back(Row(decc));
129 // Add the decoder rows
130 for (const GSList *l = decc->annotation_rows; l; l = l->next)
132 const srd_decoder_annotation_row *const ann_row =
133 (srd_decoder_annotation_row *)l->data;
135 rows.push_back(Row(decc, ann_row));
142 void DecoderStack::get_annotation_subset(
143 std::vector<pv::data::decode::Annotation> &dest,
144 const Row &row, uint64_t start_sample,
145 uint64_t end_sample) const
147 lock_guard<mutex> lock(_mutex);
149 std::map<const Row, decode::RowData>::const_iterator iter =
151 if (iter != _rows.end())
152 (*iter).second.get_annotation_subset(dest,
153 start_sample, end_sample);
156 QString DecoderStack::error_message()
158 lock_guard<mutex> lock(_mutex);
159 return _error_message;
162 void DecoderStack::clear()
164 _samples_decoded = 0;
165 _error_message = QString();
170 void DecoderStack::begin_decode()
172 shared_ptr<pv::view::LogicSignal> logic_signal;
173 shared_ptr<pv::data::Logic> data;
175 if (_decode_thread.joinable()) {
176 _decode_thread.interrupt();
177 _decode_thread.join();
183 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
186 const srd_decoder *const decc = dec->decoder();
187 assert(dec->decoder());
189 // Add a row for the decoder if it doesn't have a row list
190 if (!decc->annotation_rows)
191 _rows[Row(decc)] = decode::RowData();
193 // Add the decoder rows
194 for (const GSList *l = decc->annotation_rows; l; l = l->next)
196 const srd_decoder_annotation_row *const ann_row =
197 (srd_decoder_annotation_row *)l->data;
200 const Row row(decc, ann_row);
202 // Add a new empty row data object
203 _rows[row] = decode::RowData();
205 // Map out all the classes
206 for (const GSList *ll = ann_row->ann_classes;
208 _class_rows[make_pair(decc,
209 GPOINTER_TO_INT(ll->data))] = row;
213 // We get the logic data of the first probe in the list.
214 // This works because we are currently assuming all
215 // LogicSignals have the same data/snapshot
216 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
217 if (dec && !dec->probes().empty() &&
218 ((logic_signal = (*dec->probes().begin()).second)) &&
219 ((data = logic_signal->logic_data())))
225 // Get the samplerate and start time
226 _start_time = data->get_start_time();
227 _samplerate = data->samplerate();
228 if (_samplerate == 0.0)
231 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
235 uint64_t DecoderStack::get_max_sample_count() const
237 uint64_t max_sample_count = 0;
239 for (map<const Row, RowData>::const_iterator i = _rows.begin();
240 i != _rows.end(); i++)
241 max_sample_count = max(max_sample_count,
242 (*i).second.get_max_sample());
244 return max_sample_count;
247 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
249 srd_session *session;
250 uint8_t chunk[DecodeChunkLength];
251 srd_decoder_inst *prev_di = NULL;
255 // Check we have a snapshot of data
256 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
257 data->get_snapshots();
258 if (snapshots.empty())
261 // Check that all decoders have the required probes
262 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
263 if (!dec->have_required_probes())
266 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
268 const int64_t sample_count = snapshot->get_sample_count();
269 const unsigned int unit_size = snapshot->unit_size();
270 const unsigned int chunk_sample_count =
271 DecodeChunkLength / unit_size;
273 // Create the session
274 srd_session_new(&session);
277 // Create the decoders
278 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
280 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
284 _error_message = tr("Failed to create decoder instance");
285 srd_session_destroy(session);
290 srd_inst_stack (session, prev_di, di);
296 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
297 g_variant_new_uint64((uint64_t)_samplerate));
299 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
300 DecoderStack::annotation_callback, this);
302 srd_session_start(session);
305 !boost::this_thread::interruption_requested() &&
307 i += chunk_sample_count)
309 lock_guard<mutex> decode_lock(_global_decode_mutex);
311 const int64_t chunk_end = min(
312 i + chunk_sample_count, sample_count);
313 snapshot->get_samples(chunk, i, chunk_end);
315 if (srd_session_send(session, i, i + sample_count, chunk,
316 (chunk_end - i) * unit_size) != SRD_OK) {
317 _error_message = tr("Decoder reported an error");
322 lock_guard<mutex> lock(_mutex);
323 _samples_decoded = chunk_end;
327 // Destroy the session
328 srd_session_destroy(session);
331 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
336 DecoderStack *const d = (DecoderStack*)decoder;
339 lock_guard<mutex> lock(d->_mutex);
341 const Annotation a(pdata);
345 assert(pdata->pdo->di);
346 const srd_decoder *const decc = pdata->pdo->di->decoder;
349 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
351 // Try looking up the sub-row of this class
352 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
353 d->_class_rows.find(make_pair(decc, a.format()));
354 if (r != d->_class_rows.end())
355 row_iter = d->_rows.find((*r).second);
358 // Failing that, use the decoder as a key
359 row_iter = d->_rows.find(Row(decc));
362 assert(row_iter != d->_rows.end());
363 if (row_iter == d->_rows.end()) {
364 qDebug() << "Unexpected annotation: decoder = " << decc <<
365 ", format = " << a.format();
370 // Add the annotation
371 (*row_iter).second.push_annotation(a);
373 d->new_decode_data();