]> sigrok.org Git - pulseview.git/blame - pv/data/decoder.cpp
Implemented a global decode lock to prevent concurrent decode
[pulseview.git] / pv / data / decoder.cpp
CommitLineData
119aff65
JH
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
e332f6d3
JH
21#include <libsigrokdecode/libsigrokdecode.h>
22
e0fc5810
JH
23#include <boost/thread/thread.hpp>
24
e92cd4e4
JH
25#include <stdexcept>
26
b213ef09
JH
27#include <QDebug>
28
119aff65
JH
29#include "decoder.h"
30
e0fc5810
JH
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>
708c5523 35
e332f6d3
JH
36using namespace boost;
37using namespace std;
38
119aff65
JH
39namespace pv {
40namespace data {
41
e0fc5810
JH
42const double Decoder::DecodeMargin = 1.0;
43const double Decoder::DecodeThreshold = 0.2;
44const int64_t Decoder::DecodeChunkLength = 4096;
45
fe89c961
JH
46mutex Decoder::_global_decode_mutex;
47
708c5523
JH
48Decoder::Decoder(const srd_decoder *const dec,
49 std::map<const srd_probe*,
3045c869 50 boost::shared_ptr<pv::view::LogicSignal> > probes,
67fe5e9c 51 GHashTable *options) :
708c5523 52 _decoder(dec),
e332f6d3 53 _probes(probes),
b6b267bb 54 _options(options)
119aff65 55{
b6b267bb 56 init_decoder();
e92cd4e4 57
e0fc5810 58 begin_decode();
119aff65
JH
59}
60
640d69ce
JH
61Decoder::~Decoder()
62{
63 _decode_thread.interrupt();
64 _decode_thread.join();
67fe5e9c
JH
65
66 g_hash_table_destroy(_options);
640d69ce
JH
67}
68
119aff65
JH
69const srd_decoder* Decoder::get_decoder() const
70{
71 return _decoder;
72}
73
e0fc5810
JH
74const vector< shared_ptr<view::decode::Annotation> >
75 Decoder::annotations() const
76{
b6b267bb 77 lock_guard<mutex> lock(_mutex);
e0fc5810
JH
78 return _annotations;
79}
80
b6b267bb
JH
81QString Decoder::error_message()
82{
83 lock_guard<mutex> lock(_mutex);
84 return _error_message;
85}
86
640d69ce
JH
87void Decoder::begin_decode()
88{
89 _decode_thread.interrupt();
90 _decode_thread.join();
91
e0fc5810
JH
92 if (_probes.empty())
93 return;
94
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
3045c869 98 shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second;
e0fc5810 99 assert(sig);
3045c869 100 shared_ptr<data::Logic> data = sig->data();
e0fc5810 101
640d69ce 102 _decode_thread = boost::thread(&Decoder::decode_proc, this,
e0fc5810 103 data);
640d69ce
JH
104}
105
106void Decoder::clear_snapshots()
107{
108}
109
b6b267bb 110void Decoder::init_decoder()
e332f6d3 111{
e0fc5810
JH
112 if (!_probes.empty())
113 {
114 shared_ptr<pv::view::LogicSignal> logic_signal =
115 dynamic_pointer_cast<pv::view::LogicSignal>(
116 (*_probes.begin()).second);
117 if (logic_signal) {
118 shared_ptr<pv::data::Logic> data(
119 logic_signal->data());
120 if (data) {
e0fc5810 121 _start_time = data->get_start_time();
b6b267bb
JH
122 _samplerate = data->get_samplerate();
123 if (_samplerate == 0.0)
124 _samplerate = 1.0;
e0fc5810
JH
125 }
126 }
127 }
e332f6d3
JH
128}
129
640d69ce 130void Decoder::decode_proc(shared_ptr<data::Logic> data)
119aff65 131{
b6b267bb 132 srd_session *session;
e0fc5810
JH
133 uint8_t chunk[DecodeChunkLength];
134
135 assert(data);
136
137 _annotations.clear();
138
139 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
140 data->get_snapshots();
141 if (snapshots.empty())
142 return;
143
144 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
145 snapshots.front();
146 const int64_t sample_count = snapshot->get_sample_count() - 1;
e0fc5810 147
b6b267bb
JH
148 // Create the session
149 srd_session_new(&session);
150 assert(session);
e0fc5810 151
b6b267bb 152 srd_session_config_set(session, SRD_CONF_NUM_PROBES,
9d20a741 153 g_variant_new_uint64(_probes.size()));
b6b267bb 154 srd_session_config_set(session, SRD_CONF_UNITSIZE,
9d20a741 155 g_variant_new_uint64(snapshot->unit_size()));
b6b267bb
JH
156 srd_session_config_set(session, SRD_CONF_SAMPLERATE,
157 g_variant_new_uint64((uint64_t)_samplerate));
e0fc5810 158
b6b267bb 159 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
e0fc5810
JH
160 Decoder::annotation_callback, this);
161
b6b267bb
JH
162 // Create the decoder instance
163 srd_decoder_inst *const decoder_inst = srd_inst_new(
164 session, _decoder->id, _options);
165 if(!decoder_inst) {
166 _error_message = tr("Failed to initialise decoder");
167 return;
168 }
169
170 decoder_inst->data_samplerate = _samplerate;
171
172 // Setup the probes
173 GHashTable *const probes = g_hash_table_new_full(g_str_hash,
174 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
175
176 for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
177 const_iterator i = _probes.begin();
178 i != _probes.end(); i++)
179 {
180 shared_ptr<view::Signal> signal((*i).second);
181 GVariant *const gvar = g_variant_new_int32(
182 signal->probe()->index);
183 g_variant_ref_sink(gvar);
184 g_hash_table_insert(probes, (*i).first->id, gvar);
185 }
186
187 srd_inst_probe_set_all(decoder_inst, probes);
188
189 // Start the session
190 srd_session_start(session);
191
e0fc5810
JH
192 for (int64_t i = 0;
193 !this_thread::interruption_requested() && i < sample_count;
194 i += DecodeChunkLength)
195 {
fe89c961
JH
196 lock_guard<mutex> decode_lock(_global_decode_mutex);
197
e0fc5810
JH
198 const int64_t chunk_end = min(
199 i + DecodeChunkLength, sample_count);
200 snapshot->get_samples(chunk, i, chunk_end);
201
b6b267bb
JH
202 if (srd_session_send(session, i, chunk, chunk_end - i) !=
203 SRD_OK) {
204 _error_message = tr("Failed to initialise decoder");
e0fc5810 205 break;
b6b267bb 206 }
e0fc5810 207 }
b6b267bb
JH
208
209 // Destroy the session
210 srd_session_destroy(session);
e0fc5810
JH
211}
212
213void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder)
214{
215 using namespace pv::view::decode;
216
217 assert(pdata);
218 assert(decoder);
219
220 Decoder *const d = (Decoder*)decoder;
221
222 shared_ptr<Annotation> a(new Annotation(pdata));
b6b267bb 223 lock_guard<mutex> lock(d->_mutex);
e0fc5810 224 d->_annotations.push_back(a);
9cef9567
JH
225
226 d->new_decode_data();
119aff65
JH
227}
228
229} // namespace data
230} // namespace pv