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) :
50 _options(g_hash_table_new_full(g_str_hash,
51 g_str_equal, g_free, (GDestroyNotify)g_variant_unref))
57 _decode_thread.interrupt();
58 _decode_thread.join();
60 g_hash_table_destroy(_options);
63 const srd_decoder* Decoder::decoder() const
68 const map<const srd_probe*, shared_ptr<view::LogicSignal> >&
69 Decoder::probes() const
74 void Decoder::set_probes(std::map<const srd_probe*,
75 boost::shared_ptr<view::LogicSignal> > probes)
81 const GHashTable* Decoder::options() const
86 void Decoder::set_option(const char *id, GVariant *value)
89 g_hash_table_replace(_options, (void*)g_strdup(id), value);
93 const vector< shared_ptr<view::decode::Annotation> >
94 Decoder::annotations() const
96 lock_guard<mutex> lock(_mutex);
100 QString Decoder::error_message()
102 lock_guard<mutex> lock(_mutex);
103 return _error_message;
106 void Decoder::begin_decode()
108 _decode_thread.interrupt();
109 _decode_thread.join();
111 _annotations.clear();
116 // Get the samplerate and start time
117 shared_ptr<pv::view::LogicSignal> logic_signal =
118 dynamic_pointer_cast<pv::view::LogicSignal>(
119 (*_probes.begin()).second);
121 shared_ptr<pv::data::Logic> data(
122 logic_signal->data());
124 _start_time = data->get_start_time();
125 _samplerate = data->get_samplerate();
126 if (_samplerate == 0.0)
131 // We get the logic data of the first probe in the list.
132 // This works because we are currently assuming all
133 // LogicSignals have the same data/snapshot
134 shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second;
136 shared_ptr<data::Logic> data = sig->data();
138 _decode_thread = boost::thread(&Decoder::decode_proc, this,
142 void Decoder::clear_snapshots()
146 void Decoder::decode_proc(shared_ptr<data::Logic> data)
148 srd_session *session;
149 uint8_t chunk[DecodeChunkLength];
153 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
154 data->get_snapshots();
155 if (snapshots.empty())
158 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
160 const int64_t sample_count = snapshot->get_sample_count() - 1;
162 // Create the session
163 srd_session_new(&session);
166 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
167 g_variant_new_uint64((uint64_t)_samplerate));
169 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
170 Decoder::annotation_callback, this);
172 // Create the decoder instance
173 srd_decoder_inst *const decoder_inst = srd_inst_new(
174 session, _decoder->id, _options);
176 _error_message = tr("Failed to initialise decoder");
181 GHashTable *const probes = g_hash_table_new_full(g_str_hash,
182 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
184 for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
185 const_iterator i = _probes.begin();
186 i != _probes.end(); i++)
188 shared_ptr<view::Signal> signal((*i).second);
189 GVariant *const gvar = g_variant_new_int32(
190 signal->probe()->index);
191 g_variant_ref_sink(gvar);
192 g_hash_table_insert(probes, (*i).first->id, gvar);
195 srd_inst_probe_set_all(decoder_inst, probes);
198 srd_session_start(session);
201 !this_thread::interruption_requested() && i < sample_count;
202 i += DecodeChunkLength)
204 lock_guard<mutex> decode_lock(_global_decode_mutex);
206 const int64_t chunk_end = min(
207 i + DecodeChunkLength, sample_count);
208 snapshot->get_samples(chunk, i, chunk_end);
210 if (srd_session_send(session, i, i + sample_count,
211 chunk, chunk_end - i) != SRD_OK) {
212 _error_message = tr("Failed to initialise decoder");
217 // Destroy the session
218 srd_session_destroy(session);
221 void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder)
223 using namespace pv::view::decode;
228 Decoder *const d = (Decoder*)decoder;
230 shared_ptr<Annotation> a(new Annotation(pdata));
231 lock_guard<mutex> lock(d->_mutex);
232 d->_annotations.push_back(a);
234 d->new_decode_data();