]> sigrok.org Git - pulseview.git/blame - pv/data/decoder.cpp
Destroy the decode session after use
[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
708c5523
JH
46Decoder::Decoder(const srd_decoder *const dec,
47 std::map<const srd_probe*,
67fe5e9c
JH
48 boost::shared_ptr<pv::view::Signal> > probes,
49 GHashTable *options) :
708c5523 50 _decoder(dec),
e332f6d3 51 _probes(probes),
67fe5e9c 52 _options(options),
9d20a741 53 _session(NULL),
e332f6d3 54 _decoder_inst(NULL)
119aff65 55{
e92cd4e4
JH
56 if (!init_decoder())
57 throw runtime_error("Failed to initialise decoder.");
58
e0fc5810 59 begin_decode();
119aff65
JH
60}
61
640d69ce
JH
62Decoder::~Decoder()
63{
64 _decode_thread.interrupt();
65 _decode_thread.join();
67fe5e9c
JH
66
67 g_hash_table_destroy(_options);
baee87e3
JH
68
69 if (_session)
70 srd_session_destroy(_session);
640d69ce
JH
71}
72
119aff65
JH
73const srd_decoder* Decoder::get_decoder() const
74{
75 return _decoder;
76}
77
e0fc5810
JH
78const vector< shared_ptr<view::decode::Annotation> >
79 Decoder::annotations() const
80{
81 lock_guard<mutex> lock(_annotations_mutex);
82 return _annotations;
83}
84
640d69ce
JH
85void Decoder::begin_decode()
86{
87 _decode_thread.interrupt();
88 _decode_thread.join();
89
e0fc5810
JH
90 if (_probes.empty())
91 return;
92
93 // We get the logic data of the first probe in the list.
94 // This works because we are currently assuming all
95 // LogicSignals have the same data/snapshot
96 shared_ptr<pv::view::Signal> sig = (*_probes.begin()).second;
97 assert(sig);
98 const pv::view::LogicSignal *const l =
99 dynamic_cast<pv::view::LogicSignal*>(sig.get());
100 assert(l);
101 shared_ptr<data::Logic> data = l->data();
102
640d69ce 103 _decode_thread = boost::thread(&Decoder::decode_proc, this,
e0fc5810 104 data);
640d69ce
JH
105}
106
107void Decoder::clear_snapshots()
108{
109}
110
e92cd4e4 111bool Decoder::init_decoder()
e332f6d3 112{
e0fc5810
JH
113 if (!_probes.empty())
114 {
115 shared_ptr<pv::view::LogicSignal> logic_signal =
116 dynamic_pointer_cast<pv::view::LogicSignal>(
117 (*_probes.begin()).second);
118 if (logic_signal) {
119 shared_ptr<pv::data::Logic> data(
120 logic_signal->data());
121 if (data) {
122 _samplerate = data->get_samplerate();
123 _start_time = data->get_start_time();
124 }
125 }
126 }
127
9d20a741
JH
128 srd_session_new(&_session);
129 assert(_session);
130
131 _decoder_inst = srd_inst_new(_session, _decoder->id, _options);
52292f6c
JH
132 if(!_decoder_inst) {
133 qDebug() << "Failed to initialise decoder";
e92cd4e4 134 return false;
52292f6c 135 }
e332f6d3 136
e0fc5810
JH
137 _decoder_inst->data_samplerate = _samplerate;
138
e332f6d3
JH
139 GHashTable *probes = g_hash_table_new_full(g_str_hash,
140 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
141
142 for(map<const srd_probe*, shared_ptr<view::Signal> >::
143 const_iterator i = _probes.begin();
144 i != _probes.end(); i++)
145 {
146 shared_ptr<view::Signal> signal((*i).second);
147 GVariant *const gvar = g_variant_new_int32(
148 signal->probe()->index);
149 g_variant_ref_sink(gvar);
150 g_hash_table_insert(probes, (*i).first->id, gvar);
151 }
152
153 srd_inst_probe_set_all(_decoder_inst, probes);
e92cd4e4
JH
154
155 return true;
e332f6d3
JH
156}
157
640d69ce 158void Decoder::decode_proc(shared_ptr<data::Logic> data)
119aff65 159{
e0fc5810
JH
160 uint8_t chunk[DecodeChunkLength];
161
162 assert(data);
163
164 _annotations.clear();
165
166 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
167 data->get_snapshots();
168 if (snapshots.empty())
169 return;
170
171 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
172 snapshots.front();
173 const int64_t sample_count = snapshot->get_sample_count() - 1;
174 double samplerate = data->get_samplerate();
175
176 // Show sample rate as 1Hz when it is unknown
177 if (samplerate == 0.0)
178 samplerate = 1.0;
179
9d20a741
JH
180 srd_session_config_set(_session, SRD_CONF_NUM_PROBES,
181 g_variant_new_uint64(_probes.size()));
182 srd_session_config_set(_session, SRD_CONF_UNITSIZE,
183 g_variant_new_uint64(snapshot->unit_size()));
184 srd_session_config_set(_session, SRD_CONF_SAMPLERATE,
185 g_variant_new_uint64((uint64_t)samplerate));
186
187 srd_session_start(_session);
e0fc5810 188
9d20a741 189 srd_pd_output_callback_add(_session, SRD_OUTPUT_ANN,
e0fc5810
JH
190 Decoder::annotation_callback, this);
191
192 for (int64_t i = 0;
193 !this_thread::interruption_requested() && i < sample_count;
194 i += DecodeChunkLength)
195 {
196 const int64_t chunk_end = min(
197 i + DecodeChunkLength, sample_count);
198 snapshot->get_samples(chunk, i, chunk_end);
199
9d20a741
JH
200 if (srd_session_send(_session, i, chunk, chunk_end - i) !=
201 SRD_OK)
e0fc5810
JH
202 break;
203 }
204}
205
206void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder)
207{
208 using namespace pv::view::decode;
209
210 assert(pdata);
211 assert(decoder);
212
213 Decoder *const d = (Decoder*)decoder;
214
215 shared_ptr<Annotation> a(new Annotation(pdata));
216 lock_guard<mutex> lock(d->_annotations_mutex);
217 d->_annotations.push_back(a);
9cef9567
JH
218
219 d->new_decode_data();
119aff65
JH
220}
221
222} // namespace data
223} // namespace pv