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/view/logicsignal.h>
36 #include <pv/view/decode/annotation.h>
38 using namespace boost;
44 const double DecoderStack::DecodeMargin = 1.0;
45 const double DecoderStack::DecodeThreshold = 0.2;
46 const int64_t DecoderStack::DecodeChunkLength = 4096;
48 mutex DecoderStack::_global_decode_mutex;
50 DecoderStack::DecoderStack(const srd_decoder *const dec)
52 _stack.push_back(shared_ptr<decode::Decoder>(
53 new decode::Decoder(dec)));
56 DecoderStack::~DecoderStack()
58 _decode_thread.interrupt();
59 _decode_thread.join();
62 const std::list< boost::shared_ptr<decode::Decoder> >&
63 DecoderStack::stack() const
68 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
71 _stack.push_back(decoder);
74 const vector< shared_ptr<view::decode::Annotation> >
75 DecoderStack::annotations() const
77 lock_guard<mutex> lock(_mutex);
81 QString DecoderStack::error_message()
83 lock_guard<mutex> lock(_mutex);
84 return _error_message;
87 void DecoderStack::begin_decode()
89 shared_ptr<pv::view::LogicSignal> logic_signal;
90 shared_ptr<pv::data::Logic> data;
92 _decode_thread.interrupt();
93 _decode_thread.join();
97 // We get the logic data of the first probe in the list.
98 // This works because we are currently assuming all
99 // LogicSignals have the same data/snapshot
100 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
101 if (dec && !dec->probes().empty() &&
102 ((logic_signal = (*dec->probes().begin()).second)) &&
103 ((data = logic_signal->data())))
109 // Get the samplerate and start time
110 _start_time = data->get_start_time();
111 _samplerate = data->get_samplerate();
112 if (_samplerate == 0.0)
115 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
119 void DecoderStack::clear_snapshots()
123 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
125 srd_session *session;
126 uint8_t chunk[DecodeChunkLength];
127 srd_decoder_inst *prev_di = NULL;
131 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
132 data->get_snapshots();
133 if (snapshots.empty())
136 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
138 const int64_t sample_count = snapshot->get_sample_count() - 1;
140 // Create the session
141 srd_session_new(&session);
145 // Create the decoders
146 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
148 srd_decoder_inst *const di = dec->create_decoder_inst(session);
152 _error_message = tr("Failed to initialise decoder");
153 srd_session_destroy(session);
158 srd_inst_stack (session, prev_di, di);
164 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
165 g_variant_new_uint64((uint64_t)_samplerate));
167 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
168 DecoderStack::annotation_callback, this);
170 srd_session_start(session);
173 !this_thread::interruption_requested() && i < sample_count;
174 i += DecodeChunkLength)
176 lock_guard<mutex> decode_lock(_global_decode_mutex);
178 const int64_t chunk_end = min(
179 i + DecodeChunkLength, sample_count);
180 snapshot->get_samples(chunk, i, chunk_end);
182 if (srd_session_send(session, i, i + sample_count,
183 chunk, chunk_end - i) != SRD_OK) {
184 _error_message = tr("Failed to initialise decoder");
189 // Destroy the session
190 srd_session_destroy(session);
193 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
195 using namespace pv::view::decode;
200 DecoderStack *const d = (DecoderStack*)decoder;
202 shared_ptr<Annotation> a(new Annotation(pdata));
203 lock_guard<mutex> lock(d->_mutex);
204 d->_annotations.push_back(a);
206 d->new_decode_data();