]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decoderstack.cpp
Renamed pv::data::Decoder to DecoderStack
[pulseview.git] / pv / data / decoderstack.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <libsigrokdecode/libsigrokdecode.h>
22
23#include <boost/thread/thread.hpp>
24
25#include <stdexcept>
26
27#include <QDebug>
28
29#include "decoderstack.h"
30
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>
35
36using namespace boost;
37using namespace std;
38
39namespace pv {
40namespace data {
41
42const double DecoderStack::DecodeMargin = 1.0;
43const double DecoderStack::DecodeThreshold = 0.2;
44const int64_t DecoderStack::DecodeChunkLength = 4096;
45
46mutex DecoderStack::_global_decode_mutex;
47
48DecoderStack::DecoderStack(const srd_decoder *const dec) :
49 _decoder(dec),
50 _options(g_hash_table_new_full(g_str_hash,
51 g_str_equal, g_free, (GDestroyNotify)g_variant_unref))
52{
53}
54
55DecoderStack::~DecoderStack()
56{
57 _decode_thread.interrupt();
58 _decode_thread.join();
59
60 g_hash_table_destroy(_options);
61}
62
63const srd_decoder* DecoderStack::decoder() const
64{
65 return _decoder;
66}
67
68const map<const srd_probe*, shared_ptr<view::LogicSignal> >&
69DecoderStack::probes() const
70{
71 return _probes;
72}
73
74void DecoderStack::set_probes(std::map<const srd_probe*,
75 boost::shared_ptr<view::LogicSignal> > probes)
76{
77 _probes = probes;
78 begin_decode();
79}
80
81const GHashTable* DecoderStack::options() const
82{
83 return _options;
84}
85
86void DecoderStack::set_option(const char *id, GVariant *value)
87{
88 g_variant_ref(value);
89 g_hash_table_replace(_options, (void*)g_strdup(id), value);
90 begin_decode();
91}
92
93const vector< shared_ptr<view::decode::Annotation> >
94 DecoderStack::annotations() const
95{
96 lock_guard<mutex> lock(_mutex);
97 return _annotations;
98}
99
100QString DecoderStack::error_message()
101{
102 lock_guard<mutex> lock(_mutex);
103 return _error_message;
104}
105
106void DecoderStack::begin_decode()
107{
108 _decode_thread.interrupt();
109 _decode_thread.join();
110
111 _annotations.clear();
112
113 if (_probes.empty())
114 return;
115
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);
120 if (logic_signal) {
121 shared_ptr<pv::data::Logic> data(
122 logic_signal->data());
123 if (data) {
124 _start_time = data->get_start_time();
125 _samplerate = data->get_samplerate();
126 if (_samplerate == 0.0)
127 _samplerate = 1.0;
128 }
129 }
130
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;
135 assert(sig);
136 shared_ptr<data::Logic> data = sig->data();
137
138 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
139 data);
140}
141
142void DecoderStack::clear_snapshots()
143{
144}
145
146void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
147{
148 srd_session *session;
149 uint8_t chunk[DecodeChunkLength];
150
151 assert(data);
152
153 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
154 data->get_snapshots();
155 if (snapshots.empty())
156 return;
157
158 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
159 snapshots.front();
160 const int64_t sample_count = snapshot->get_sample_count() - 1;
161
162 // Create the session
163 srd_session_new(&session);
164 assert(session);
165
166 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
167 g_variant_new_uint64((uint64_t)_samplerate));
168
169 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
170 DecoderStack::annotation_callback, this);
171
172 // Create the decoder instance
173 srd_decoder_inst *const decoder_inst = srd_inst_new(
174 session, _decoder->id, _options);
175 if(!decoder_inst) {
176 _error_message = tr("Failed to initialise decoder");
177 return;
178 }
179
180 // Setup the probes
181 GHashTable *const probes = g_hash_table_new_full(g_str_hash,
182 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
183
184 for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
185 const_iterator i = _probes.begin();
186 i != _probes.end(); i++)
187 {
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);
193 }
194
195 srd_inst_probe_set_all(decoder_inst, probes);
196
197 // Start the session
198 srd_session_start(session);
199
200 for (int64_t i = 0;
201 !this_thread::interruption_requested() && i < sample_count;
202 i += DecodeChunkLength)
203 {
204 lock_guard<mutex> decode_lock(_global_decode_mutex);
205
206 const int64_t chunk_end = min(
207 i + DecodeChunkLength, sample_count);
208 snapshot->get_samples(chunk, i, chunk_end);
209
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");
213 break;
214 }
215 }
216
217 // Destroy the session
218 srd_session_destroy(session);
219}
220
221void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
222{
223 using namespace pv::view::decode;
224
225 assert(pdata);
226 assert(decoder);
227
228 DecoderStack *const d = (DecoderStack*)decoder;
229
230 shared_ptr<Annotation> a(new Annotation(pdata));
231 lock_guard<mutex> lock(d->_mutex);
232 d->_annotations.push_back(a);
233
234 d->new_decode_data();
235}
236
237} // namespace data
238} // namespace pv