]>
Commit | Line | Data |
---|---|---|
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 | ||
7491a29f | 23 | #include <boost/foreach.hpp> |
e0fc5810 JH |
24 | #include <boost/thread/thread.hpp> |
25 | ||
e92cd4e4 JH |
26 | #include <stdexcept> |
27 | ||
b213ef09 JH |
28 | #include <QDebug> |
29 | ||
6e89374a | 30 | #include "decoderstack.h" |
119aff65 | 31 | |
e0fc5810 JH |
32 | #include <pv/data/logic.h> |
33 | #include <pv/data/logicsnapshot.h> | |
7491a29f | 34 | #include <pv/data/decode/decoder.h> |
e0fc5810 JH |
35 | #include <pv/view/logicsignal.h> |
36 | #include <pv/view/decode/annotation.h> | |
708c5523 | 37 | |
e332f6d3 JH |
38 | using namespace boost; |
39 | using namespace std; | |
40 | ||
119aff65 JH |
41 | namespace pv { |
42 | namespace data { | |
43 | ||
6e89374a JH |
44 | const double DecoderStack::DecodeMargin = 1.0; |
45 | const double DecoderStack::DecodeThreshold = 0.2; | |
46 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 47 | |
6e89374a | 48 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 49 | |
7491a29f | 50 | DecoderStack::DecoderStack(const srd_decoder *const dec) |
119aff65 | 51 | { |
7491a29f JH |
52 | _stack.push_back(shared_ptr<decode::Decoder>( |
53 | new decode::Decoder(dec))); | |
119aff65 JH |
54 | } |
55 | ||
6e89374a | 56 | DecoderStack::~DecoderStack() |
640d69ce JH |
57 | { |
58 | _decode_thread.interrupt(); | |
59 | _decode_thread.join(); | |
4e5a4405 JH |
60 | } |
61 | ||
7491a29f JH |
62 | const std::list< boost::shared_ptr<decode::Decoder> >& |
63 | DecoderStack::stack() const | |
4e5a4405 | 64 | { |
7491a29f | 65 | return _stack; |
4e5a4405 JH |
66 | } |
67 | ||
7491a29f | 68 | void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 69 | { |
7491a29f JH |
70 | assert(decoder); |
71 | _stack.push_back(decoder); | |
4e5a4405 JH |
72 | } |
73 | ||
e0fc5810 | 74 | const vector< shared_ptr<view::decode::Annotation> > |
6e89374a | 75 | DecoderStack::annotations() const |
e0fc5810 | 76 | { |
b6b267bb | 77 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
78 | return _annotations; |
79 | } | |
80 | ||
6e89374a | 81 | QString DecoderStack::error_message() |
b6b267bb JH |
82 | { |
83 | lock_guard<mutex> lock(_mutex); | |
84 | return _error_message; | |
85 | } | |
86 | ||
6e89374a | 87 | void DecoderStack::begin_decode() |
640d69ce | 88 | { |
7491a29f JH |
89 | shared_ptr<pv::view::LogicSignal> logic_signal; |
90 | shared_ptr<pv::data::Logic> data; | |
91 | ||
640d69ce JH |
92 | _decode_thread.interrupt(); |
93 | _decode_thread.join(); | |
94 | ||
4e5a4405 JH |
95 | _annotations.clear(); |
96 | ||
e0fc5810 JH |
97 | // We get the logic data of the first probe in the list. |
98 | // This works because we are currently assuming all | |
99 | // LogicSignals have the same data/snapshot | |
7491a29f JH |
100 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) |
101 | if (dec && !dec->probes().empty() && | |
102 | ((logic_signal = (*dec->probes().begin()).second)) && | |
103 | ((data = logic_signal->data()))) | |
104 | break; | |
105 | ||
106 | if (!data) | |
107 | return; | |
108 | ||
109 | // Get the samplerate and start time | |
110 | _start_time = data->get_start_time(); | |
111 | _samplerate = data->get_samplerate(); | |
112 | if (_samplerate == 0.0) | |
113 | _samplerate = 1.0; | |
e0fc5810 | 114 | |
6e89374a | 115 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 116 | data); |
640d69ce JH |
117 | } |
118 | ||
6e89374a | 119 | void DecoderStack::clear_snapshots() |
640d69ce JH |
120 | { |
121 | } | |
122 | ||
6e89374a | 123 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 124 | { |
b6b267bb | 125 | srd_session *session; |
e0fc5810 | 126 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 127 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
128 | |
129 | assert(data); | |
130 | ||
e0fc5810 JH |
131 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
132 | data->get_snapshots(); | |
133 | if (snapshots.empty()) | |
134 | return; | |
135 | ||
136 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
137 | snapshots.front(); | |
138 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
e0fc5810 | 139 | |
b6b267bb JH |
140 | // Create the session |
141 | srd_session_new(&session); | |
142 | assert(session); | |
e0fc5810 | 143 | |
e0fc5810 | 144 | |
7491a29f JH |
145 | // Create the decoders |
146 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
147 | { | |
148 | srd_decoder_inst *const di = dec->create_decoder_inst(session); | |
e0fc5810 | 149 | |
7491a29f JH |
150 | if (!di) |
151 | { | |
152 | _error_message = tr("Failed to initialise decoder"); | |
153 | srd_session_destroy(session); | |
154 | return; | |
155 | } | |
b6b267bb | 156 | |
7491a29f JH |
157 | if (prev_di) |
158 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 159 | |
7491a29f | 160 | prev_di = di; |
b6b267bb JH |
161 | } |
162 | ||
b6b267bb | 163 | // Start the session |
7491a29f JH |
164 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
165 | g_variant_new_uint64((uint64_t)_samplerate)); | |
166 | ||
167 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
168 | DecoderStack::annotation_callback, this); | |
169 | ||
b6b267bb JH |
170 | srd_session_start(session); |
171 | ||
e0fc5810 JH |
172 | for (int64_t i = 0; |
173 | !this_thread::interruption_requested() && i < sample_count; | |
174 | i += DecodeChunkLength) | |
175 | { | |
fe89c961 JH |
176 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
177 | ||
e0fc5810 JH |
178 | const int64_t chunk_end = min( |
179 | i + DecodeChunkLength, sample_count); | |
180 | snapshot->get_samples(chunk, i, chunk_end); | |
181 | ||
f00fc2a9 BV |
182 | if (srd_session_send(session, i, i + sample_count, |
183 | chunk, chunk_end - i) != SRD_OK) { | |
b6b267bb | 184 | _error_message = tr("Failed to initialise decoder"); |
e0fc5810 | 185 | break; |
b6b267bb | 186 | } |
e0fc5810 | 187 | } |
b6b267bb JH |
188 | |
189 | // Destroy the session | |
190 | srd_session_destroy(session); | |
e0fc5810 JH |
191 | } |
192 | ||
6e89374a | 193 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 JH |
194 | { |
195 | using namespace pv::view::decode; | |
196 | ||
197 | assert(pdata); | |
198 | assert(decoder); | |
199 | ||
6e89374a | 200 | DecoderStack *const d = (DecoderStack*)decoder; |
e0fc5810 JH |
201 | |
202 | shared_ptr<Annotation> a(new Annotation(pdata)); | |
b6b267bb | 203 | lock_guard<mutex> lock(d->_mutex); |
e0fc5810 | 204 | d->_annotations.push_back(a); |
9cef9567 JH |
205 | |
206 | d->new_decode_data(); | |
119aff65 JH |
207 | } |
208 | ||
209 | } // namespace data | |
210 | } // namespace pv |