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/thread/thread.hpp>
31 #include <pv/data/logic.h>
32 #include <pv/data/logicsnapshot.h>
33 #include <pv/view/logicsignal.h>
34 #include <pv/view/decode/annotation.h>
36 using namespace boost;
42 const double Decoder::DecodeMargin = 1.0;
43 const double Decoder::DecodeThreshold = 0.2;
44 const int64_t Decoder::DecodeChunkLength = 4096;
46 mutex Decoder::_global_decode_mutex;
48 Decoder::Decoder(const srd_decoder *const dec,
49 std::map<const srd_probe*,
50 boost::shared_ptr<pv::view::LogicSignal> > probes,
51 GHashTable *options) :
63 _decode_thread.interrupt();
64 _decode_thread.join();
66 g_hash_table_destroy(_options);
69 const srd_decoder* Decoder::get_decoder() const
74 const vector< shared_ptr<view::decode::Annotation> >
75 Decoder::annotations() const
77 lock_guard<mutex> lock(_mutex);
81 QString Decoder::error_message()
83 lock_guard<mutex> lock(_mutex);
84 return _error_message;
87 void Decoder::begin_decode()
89 _decode_thread.interrupt();
90 _decode_thread.join();
95 // We get the logic data of the first probe in the list.
96 // This works because we are currently assuming all
97 // LogicSignals have the same data/snapshot
98 shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second;
100 shared_ptr<data::Logic> data = sig->data();
102 _decode_thread = boost::thread(&Decoder::decode_proc, this,
106 void Decoder::clear_snapshots()
110 void Decoder::init_decoder()
112 if (!_probes.empty())
114 shared_ptr<pv::view::LogicSignal> logic_signal =
115 dynamic_pointer_cast<pv::view::LogicSignal>(
116 (*_probes.begin()).second);
118 shared_ptr<pv::data::Logic> data(
119 logic_signal->data());
121 _start_time = data->get_start_time();
122 _samplerate = data->get_samplerate();
123 if (_samplerate == 0.0)
130 void Decoder::decode_proc(shared_ptr<data::Logic> data)
132 srd_session *session;
133 uint8_t chunk[DecodeChunkLength];
137 _annotations.clear();
139 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
140 data->get_snapshots();
141 if (snapshots.empty())
144 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
146 const int64_t sample_count = snapshot->get_sample_count() - 1;
148 // Create the session
149 srd_session_new(&session);
152 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
153 g_variant_new_uint64((uint64_t)_samplerate));
155 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
156 Decoder::annotation_callback, this);
158 // Create the decoder instance
159 srd_decoder_inst *const decoder_inst = srd_inst_new(
160 session, _decoder->id, _options);
162 _error_message = tr("Failed to initialise decoder");
167 GHashTable *const probes = g_hash_table_new_full(g_str_hash,
168 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
170 for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
171 const_iterator i = _probes.begin();
172 i != _probes.end(); i++)
174 shared_ptr<view::Signal> signal((*i).second);
175 GVariant *const gvar = g_variant_new_int32(
176 signal->probe()->index);
177 g_variant_ref_sink(gvar);
178 g_hash_table_insert(probes, (*i).first->id, gvar);
181 srd_inst_probe_set_all(decoder_inst, probes);
184 srd_session_start(session);
187 !this_thread::interruption_requested() && i < sample_count;
188 i += DecodeChunkLength)
190 lock_guard<mutex> decode_lock(_global_decode_mutex);
192 const int64_t chunk_end = min(
193 i + DecodeChunkLength, sample_count);
194 snapshot->get_samples(chunk, i, chunk_end);
196 if (srd_session_send(session, i, i + sample_count,
197 chunk, chunk_end - i) != SRD_OK) {
198 _error_message = tr("Failed to initialise decoder");
203 // Destroy the session
204 srd_session_destroy(session);
207 void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder)
209 using namespace pv::view::decode;
214 Decoder *const d = (Decoder*)decoder;
216 shared_ptr<Annotation> a(new Annotation(pdata));
217 lock_guard<mutex> lock(d->_mutex);
218 d->_annotations.push_back(a);
220 d->new_decode_data();