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