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(const srd_decoder *const dec) :
64 _stack.push_back(shared_ptr<decode::Decoder>(
65 new decode::Decoder(dec)));
68 DecoderStack::~DecoderStack()
70 _decode_thread.interrupt();
71 _decode_thread.join();
74 const std::list< boost::shared_ptr<decode::Decoder> >&
75 DecoderStack::stack() const
80 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
83 _stack.push_back(decoder);
86 void DecoderStack::remove(int index)
89 assert(index < (int)_stack.size());
91 // Find the decoder in the stack
92 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
93 for(int i = 0; i < index; i++, iter++)
94 assert(iter != _stack.end());
100 int64_t DecoderStack::samples_decoded() const
102 lock_guard<mutex> decode_lock(_mutex);
103 return _samples_decoded;
106 std::vector<Row> DecoderStack::get_rows() const
108 lock_guard<mutex> lock(_mutex);
112 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
115 const srd_decoder *const decc = dec->decoder();
116 assert(dec->decoder());
118 // Add a row for the decoder if it doesn't have a row list
119 if (!decc->annotation_rows)
120 rows.push_back(Row(decc));
122 // Add the decoder rows
123 for (const GSList *l = decc->annotation_rows; l; l = l->next)
125 const srd_decoder_annotation_row *const ann_row =
126 (srd_decoder_annotation_row *)l->data;
128 rows.push_back(Row(decc, ann_row));
135 void DecoderStack::get_annotation_subset(
136 std::vector<pv::data::decode::Annotation> &dest,
137 const Row &row, uint64_t start_sample,
138 uint64_t end_sample) const
140 lock_guard<mutex> lock(_mutex);
142 std::map<const Row, decode::RowData>::const_iterator iter =
144 if (iter != _rows.end())
145 (*iter).second.get_annotation_subset(dest,
146 start_sample, end_sample);
149 QString DecoderStack::error_message()
151 lock_guard<mutex> lock(_mutex);
152 return _error_message;
155 void DecoderStack::clear()
157 _samples_decoded = 0;
158 _error_message = QString();
163 void DecoderStack::begin_decode()
165 shared_ptr<pv::view::LogicSignal> logic_signal;
166 shared_ptr<pv::data::Logic> data;
168 _decode_thread.interrupt();
169 _decode_thread.join();
174 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
177 const srd_decoder *const decc = dec->decoder();
178 assert(dec->decoder());
180 // Add a row for the decoder if it doesn't have a row list
181 if (!decc->annotation_rows)
182 _rows[Row(decc)] = decode::RowData();
184 // Add the decoder rows
185 for (const GSList *l = decc->annotation_rows; l; l = l->next)
187 const srd_decoder_annotation_row *const ann_row =
188 (srd_decoder_annotation_row *)l->data;
191 const Row row(decc, ann_row);
193 // Add a new empty row data object
194 _rows[row] = decode::RowData();
196 // Map out all the classes
197 for (const GSList *ll = ann_row->ann_classes;
199 _class_rows[make_pair(decc,
200 GPOINTER_TO_INT(ll->data))] = row;
204 // We get the logic data of the first probe in the list.
205 // This works because we are currently assuming all
206 // LogicSignals have the same data/snapshot
207 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
208 if (dec && !dec->probes().empty() &&
209 ((logic_signal = (*dec->probes().begin()).second)) &&
210 ((data = logic_signal->logic_data())))
216 // Get the samplerate and start time
217 _start_time = data->get_start_time();
218 _samplerate = data->samplerate();
219 if (_samplerate == 0.0)
222 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
226 uint64_t DecoderStack::get_max_sample_count() const
228 uint64_t max_sample_count = 0;
230 for (map<const Row, RowData>::const_iterator i = _rows.begin();
231 i != _rows.end(); i++)
232 max_sample_count = max(max_sample_count,
233 (*i).second.get_max_sample());
235 return max_sample_count;
238 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
240 srd_session *session;
241 uint8_t chunk[DecodeChunkLength];
242 srd_decoder_inst *prev_di = NULL;
246 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
247 data->get_snapshots();
248 if (snapshots.empty())
251 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
253 const int64_t sample_count = snapshot->get_sample_count();
254 const unsigned int chunk_sample_count =
255 DecodeChunkLength / snapshot->unit_size();
257 // Create the session
258 srd_session_new(&session);
261 // Create the decoders
262 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
264 srd_decoder_inst *const di = dec->create_decoder_inst(session);
268 _error_message = tr("Failed to create decoder instance");
269 srd_session_destroy(session);
274 srd_inst_stack (session, prev_di, di);
280 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
281 g_variant_new_uint64((uint64_t)_samplerate));
283 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
284 DecoderStack::annotation_callback, this);
286 srd_session_start(session);
289 !boost::this_thread::interruption_requested() &&
291 i += chunk_sample_count)
293 lock_guard<mutex> decode_lock(_global_decode_mutex);
295 const int64_t chunk_end = min(
296 i + chunk_sample_count, sample_count);
297 snapshot->get_samples(chunk, i, chunk_end);
299 if (srd_session_send(session, i, i + sample_count,
300 chunk, chunk_end - i) != SRD_OK) {
301 _error_message = tr("Decoder reported an error");
306 lock_guard<mutex> lock(_mutex);
307 _samples_decoded = chunk_end;
311 // Destroy the session
312 srd_session_destroy(session);
315 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
320 DecoderStack *const d = (DecoderStack*)decoder;
323 lock_guard<mutex> lock(d->_mutex);
325 const Annotation a(pdata);
329 assert(pdata->pdo->di);
330 const srd_decoder *const decc = pdata->pdo->di->decoder;
333 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
335 // Try looking up the sub-row of this class
336 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
337 d->_class_rows.find(make_pair(decc, a.format()));
338 if (r != d->_class_rows.end())
339 row_iter = d->_rows.find((*r).second);
342 // Failing that, use the decoder as a key
343 row_iter = d->_rows.find(Row(decc));
346 assert(row_iter != d->_rows.end());
347 if (row_iter == d->_rows.end()) {
348 qDebug() << "Unexpected annotation: decoder = " << decc <<
349 ", format = " << a.format();
354 // Add the annotation
355 (*row_iter).second.push_annotation(a);
357 d->new_decode_data();