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