]>
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> |
06e810f2 | 35 | #include <pv/data/decode/annotation.h> |
e0fc5810 | 36 | #include <pv/view/logicsignal.h> |
708c5523 | 37 | |
819f4c25 JH |
38 | using boost::lock_guard; |
39 | using boost::mutex; | |
40 | using boost::shared_ptr; | |
41 | using std::deque; | |
42 | using std::min; | |
43 | using std::list; | |
44 | using std::vector; | |
e332f6d3 | 45 | |
119aff65 JH |
46 | namespace pv { |
47 | namespace data { | |
48 | ||
6e89374a JH |
49 | const double DecoderStack::DecodeMargin = 1.0; |
50 | const double DecoderStack::DecodeThreshold = 0.2; | |
51 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 52 | |
6e89374a | 53 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 54 | |
5dfeb70f JH |
55 | DecoderStack::DecoderStack(const srd_decoder *const dec) : |
56 | _samples_decoded(0) | |
119aff65 | 57 | { |
7491a29f JH |
58 | _stack.push_back(shared_ptr<decode::Decoder>( |
59 | new decode::Decoder(dec))); | |
119aff65 JH |
60 | } |
61 | ||
6e89374a | 62 | DecoderStack::~DecoderStack() |
640d69ce JH |
63 | { |
64 | _decode_thread.interrupt(); | |
65 | _decode_thread.join(); | |
4e5a4405 JH |
66 | } |
67 | ||
7491a29f JH |
68 | const std::list< boost::shared_ptr<decode::Decoder> >& |
69 | DecoderStack::stack() const | |
4e5a4405 | 70 | { |
7491a29f | 71 | return _stack; |
4e5a4405 JH |
72 | } |
73 | ||
7491a29f | 74 | void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 75 | { |
7491a29f JH |
76 | assert(decoder); |
77 | _stack.push_back(decoder); | |
4e5a4405 JH |
78 | } |
79 | ||
613d097c JH |
80 | void DecoderStack::remove(int index) |
81 | { | |
82 | using pv::data::decode::Decoder; | |
83 | ||
84 | assert(index >= 0); | |
85 | assert(index < (int)_stack.size()); | |
86 | ||
87 | // Find the decoder in the stack | |
88 | list< shared_ptr<Decoder> >::iterator iter = _stack.begin(); | |
89 | for(int i = 0; i < index; i++, iter++) | |
90 | assert(iter != _stack.end()); | |
91 | ||
92 | // Delete the element | |
93 | _stack.erase(iter); | |
94 | } | |
95 | ||
5dfeb70f JH |
96 | int64_t DecoderStack::samples_decoded() const |
97 | { | |
98 | lock_guard<mutex> decode_lock(_mutex); | |
99 | return _samples_decoded; | |
100 | } | |
101 | ||
06e810f2 | 102 | const vector<decode::Annotation> DecoderStack::annotations() const |
e0fc5810 | 103 | { |
b6b267bb | 104 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
105 | return _annotations; |
106 | } | |
107 | ||
6e89374a | 108 | QString DecoderStack::error_message() |
b6b267bb JH |
109 | { |
110 | lock_guard<mutex> lock(_mutex); | |
111 | return _error_message; | |
112 | } | |
113 | ||
6e89374a | 114 | void DecoderStack::begin_decode() |
640d69ce | 115 | { |
7491a29f JH |
116 | shared_ptr<pv::view::LogicSignal> logic_signal; |
117 | shared_ptr<pv::data::Logic> data; | |
118 | ||
640d69ce JH |
119 | _decode_thread.interrupt(); |
120 | _decode_thread.join(); | |
121 | ||
5dfeb70f JH |
122 | _samples_decoded = 0; |
123 | ||
4e5a4405 JH |
124 | _annotations.clear(); |
125 | ||
e0fc5810 JH |
126 | // We get the logic data of the first probe in the list. |
127 | // This works because we are currently assuming all | |
128 | // LogicSignals have the same data/snapshot | |
7491a29f JH |
129 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) |
130 | if (dec && !dec->probes().empty() && | |
131 | ((logic_signal = (*dec->probes().begin()).second)) && | |
7aa09b00 | 132 | ((data = logic_signal->logic_data()))) |
7491a29f JH |
133 | break; |
134 | ||
135 | if (!data) | |
136 | return; | |
137 | ||
138 | // Get the samplerate and start time | |
139 | _start_time = data->get_start_time(); | |
9d28da5a | 140 | _samplerate = data->samplerate(); |
7491a29f JH |
141 | if (_samplerate == 0.0) |
142 | _samplerate = 1.0; | |
e0fc5810 | 143 | |
6e89374a | 144 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 145 | data); |
640d69ce JH |
146 | } |
147 | ||
caabb84c | 148 | void DecoderStack::clear() |
640d69ce | 149 | { |
dd6b5009 | 150 | _annotations.clear(); |
640d69ce JH |
151 | } |
152 | ||
a007f5ad JH |
153 | uint64_t DecoderStack::get_max_sample_count() const |
154 | { | |
155 | if (_annotations.empty()) | |
156 | return 0; | |
db62bbfd | 157 | return _annotations.back().end_sample(); |
a007f5ad JH |
158 | } |
159 | ||
6e89374a | 160 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 161 | { |
b6b267bb | 162 | srd_session *session; |
e0fc5810 | 163 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 164 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
165 | |
166 | assert(data); | |
167 | ||
e0fc5810 JH |
168 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
169 | data->get_snapshots(); | |
170 | if (snapshots.empty()) | |
171 | return; | |
172 | ||
173 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
174 | snapshots.front(); | |
c02f1e09 | 175 | const int64_t sample_count = snapshot->get_sample_count(); |
064a7f42 JH |
176 | const unsigned int chunk_sample_count = |
177 | DecodeChunkLength / snapshot->unit_size(); | |
e0fc5810 | 178 | |
375b6216 UH |
179 | // Clear error message upon every new session run |
180 | _error_message = QString(); | |
181 | ||
b6b267bb JH |
182 | // Create the session |
183 | srd_session_new(&session); | |
184 | assert(session); | |
e0fc5810 | 185 | |
7491a29f JH |
186 | // Create the decoders |
187 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
188 | { | |
189 | srd_decoder_inst *const di = dec->create_decoder_inst(session); | |
e0fc5810 | 190 | |
7491a29f JH |
191 | if (!di) |
192 | { | |
8a290daf | 193 | _error_message = tr("Failed to create decoder instance"); |
7491a29f JH |
194 | srd_session_destroy(session); |
195 | return; | |
196 | } | |
b6b267bb | 197 | |
7491a29f JH |
198 | if (prev_di) |
199 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 200 | |
7491a29f | 201 | prev_di = di; |
b6b267bb JH |
202 | } |
203 | ||
b6b267bb | 204 | // Start the session |
7491a29f JH |
205 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
206 | g_variant_new_uint64((uint64_t)_samplerate)); | |
207 | ||
208 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
209 | DecoderStack::annotation_callback, this); | |
210 | ||
b6b267bb JH |
211 | srd_session_start(session); |
212 | ||
e0fc5810 | 213 | for (int64_t i = 0; |
819f4c25 JH |
214 | !boost::this_thread::interruption_requested() && |
215 | i < sample_count; | |
064a7f42 | 216 | i += chunk_sample_count) |
e0fc5810 | 217 | { |
fe89c961 JH |
218 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
219 | ||
e0fc5810 | 220 | const int64_t chunk_end = min( |
064a7f42 | 221 | i + chunk_sample_count, sample_count); |
e0fc5810 JH |
222 | snapshot->get_samples(chunk, i, chunk_end); |
223 | ||
f00fc2a9 BV |
224 | if (srd_session_send(session, i, i + sample_count, |
225 | chunk, chunk_end - i) != SRD_OK) { | |
8a290daf | 226 | _error_message = tr("Decoder reported an error"); |
e0fc5810 | 227 | break; |
b6b267bb | 228 | } |
5dfeb70f JH |
229 | |
230 | { | |
231 | lock_guard<mutex> lock(_mutex); | |
232 | _samples_decoded = chunk_end; | |
233 | } | |
e0fc5810 | 234 | } |
b6b267bb JH |
235 | |
236 | // Destroy the session | |
237 | srd_session_destroy(session); | |
e0fc5810 JH |
238 | } |
239 | ||
6e89374a | 240 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 | 241 | { |
06e810f2 | 242 | using pv::data::decode::Annotation; |
e0fc5810 | 243 | |
6d8b0562 UH |
244 | GSList *l, *ll; |
245 | int row, ann_class; | |
246 | struct srd_decoder_annotation_row *ann_row; | |
247 | ||
e0fc5810 JH |
248 | assert(pdata); |
249 | assert(decoder); | |
250 | ||
6e89374a | 251 | DecoderStack *const d = (DecoderStack*)decoder; |
e0fc5810 | 252 | |
b6b267bb | 253 | lock_guard<mutex> lock(d->_mutex); |
6d8b0562 UH |
254 | |
255 | Annotation a = Annotation(pdata); | |
256 | ||
257 | const shared_ptr<decode::Decoder> &dec = *d->stack().begin(); | |
258 | ||
259 | for (l = dec->decoder()->annotation_rows, row = 0; l; | |
260 | l = l->next, row++) | |
261 | { | |
262 | ann_row = (struct srd_decoder_annotation_row *)l->data; | |
263 | ||
264 | for (ll = ann_row->ann_classes, ann_class = 0; ll; | |
265 | ll = ll->next, ann_class++) | |
266 | { | |
267 | if (GPOINTER_TO_INT(ll->data) == a.format()) | |
268 | a.set_row(row); | |
269 | } | |
270 | } | |
271 | ||
272 | d->_annotations.push_back(a); | |
9cef9567 JH |
273 | |
274 | d->new_decode_data(); | |
119aff65 JH |
275 | } |
276 | ||
277 | } // namespace data | |
278 | } // namespace pv |