]>
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; | |
f9101a91 JH |
42 | using std::make_pair; |
43 | using std::max; | |
819f4c25 JH |
44 | using std::min; |
45 | using std::list; | |
f9101a91 JH |
46 | using std::map; |
47 | using std::pair; | |
819f4c25 | 48 | using std::vector; |
e332f6d3 | 49 | |
f9101a91 JH |
50 | using namespace pv::data::decode; |
51 | ||
119aff65 JH |
52 | namespace pv { |
53 | namespace data { | |
54 | ||
6e89374a JH |
55 | const double DecoderStack::DecodeMargin = 1.0; |
56 | const double DecoderStack::DecodeThreshold = 0.2; | |
57 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 58 | |
6e89374a | 59 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 60 | |
bdc5c3b0 JH |
61 | DecoderStack::DecoderStack(pv::SigSession &session, |
62 | const srd_decoder *const dec) : | |
63 | _session(session), | |
5dfeb70f | 64 | _samples_decoded(0) |
119aff65 | 65 | { |
7491a29f JH |
66 | _stack.push_back(shared_ptr<decode::Decoder>( |
67 | new decode::Decoder(dec))); | |
119aff65 JH |
68 | } |
69 | ||
6e89374a | 70 | DecoderStack::~DecoderStack() |
640d69ce | 71 | { |
6d483b8b MC |
72 | if (_decode_thread.joinable()) { |
73 | _decode_thread.interrupt(); | |
74 | _decode_thread.join(); | |
75 | } | |
4e5a4405 JH |
76 | } |
77 | ||
7491a29f JH |
78 | const std::list< boost::shared_ptr<decode::Decoder> >& |
79 | DecoderStack::stack() const | |
4e5a4405 | 80 | { |
7491a29f | 81 | return _stack; |
4e5a4405 JH |
82 | } |
83 | ||
7491a29f | 84 | void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 85 | { |
7491a29f JH |
86 | assert(decoder); |
87 | _stack.push_back(decoder); | |
4e5a4405 JH |
88 | } |
89 | ||
613d097c JH |
90 | void DecoderStack::remove(int index) |
91 | { | |
613d097c JH |
92 | assert(index >= 0); |
93 | assert(index < (int)_stack.size()); | |
94 | ||
95 | // Find the decoder in the stack | |
96 | list< shared_ptr<Decoder> >::iterator iter = _stack.begin(); | |
97 | for(int i = 0; i < index; i++, iter++) | |
98 | assert(iter != _stack.end()); | |
99 | ||
100 | // Delete the element | |
101 | _stack.erase(iter); | |
102 | } | |
103 | ||
5dfeb70f JH |
104 | int64_t DecoderStack::samples_decoded() const |
105 | { | |
106 | lock_guard<mutex> decode_lock(_mutex); | |
107 | return _samples_decoded; | |
108 | } | |
109 | ||
dd048a7e | 110 | std::vector<Row> DecoderStack::get_visible_rows() const |
f9101a91 JH |
111 | { |
112 | lock_guard<mutex> lock(_mutex); | |
113 | ||
114 | vector<Row> rows; | |
115 | ||
116 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) | |
117 | { | |
118 | assert(dec); | |
dd048a7e JH |
119 | if (!dec->shown()) |
120 | continue; | |
121 | ||
f9101a91 JH |
122 | const srd_decoder *const decc = dec->decoder(); |
123 | assert(dec->decoder()); | |
124 | ||
125 | // Add a row for the decoder if it doesn't have a row list | |
126 | if (!decc->annotation_rows) | |
127 | rows.push_back(Row(decc)); | |
128 | ||
129 | // Add the decoder rows | |
130 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
131 | { | |
132 | const srd_decoder_annotation_row *const ann_row = | |
133 | (srd_decoder_annotation_row *)l->data; | |
134 | assert(ann_row); | |
135 | rows.push_back(Row(decc, ann_row)); | |
136 | } | |
137 | } | |
138 | ||
139 | return rows; | |
140 | } | |
141 | ||
92421299 JH |
142 | void DecoderStack::get_annotation_subset( |
143 | std::vector<pv::data::decode::Annotation> &dest, | |
f9101a91 JH |
144 | const Row &row, uint64_t start_sample, |
145 | uint64_t end_sample) const | |
e0fc5810 | 146 | { |
b6b267bb | 147 | lock_guard<mutex> lock(_mutex); |
92421299 | 148 | |
f9101a91 JH |
149 | std::map<const Row, decode::RowData>::const_iterator iter = |
150 | _rows.find(row); | |
151 | if (iter != _rows.end()) | |
152 | (*iter).second.get_annotation_subset(dest, | |
153 | start_sample, end_sample); | |
e0fc5810 JH |
154 | } |
155 | ||
6e89374a | 156 | QString DecoderStack::error_message() |
b6b267bb JH |
157 | { |
158 | lock_guard<mutex> lock(_mutex); | |
159 | return _error_message; | |
160 | } | |
161 | ||
f9101a91 JH |
162 | void DecoderStack::clear() |
163 | { | |
164 | _samples_decoded = 0; | |
165 | _error_message = QString(); | |
166 | _rows.clear(); | |
167 | _class_rows.clear(); | |
168 | } | |
169 | ||
6e89374a | 170 | void DecoderStack::begin_decode() |
640d69ce | 171 | { |
7491a29f JH |
172 | shared_ptr<pv::view::LogicSignal> logic_signal; |
173 | shared_ptr<pv::data::Logic> data; | |
174 | ||
6d483b8b MC |
175 | if (_decode_thread.joinable()) { |
176 | _decode_thread.interrupt(); | |
177 | _decode_thread.join(); | |
178 | } | |
640d69ce | 179 | |
92421299 | 180 | clear(); |
4e5a4405 | 181 | |
f9101a91 JH |
182 | // Add classes |
183 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) | |
184 | { | |
185 | assert(dec); | |
186 | const srd_decoder *const decc = dec->decoder(); | |
187 | assert(dec->decoder()); | |
188 | ||
189 | // Add a row for the decoder if it doesn't have a row list | |
190 | if (!decc->annotation_rows) | |
191 | _rows[Row(decc)] = decode::RowData(); | |
192 | ||
193 | // Add the decoder rows | |
194 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
195 | { | |
196 | const srd_decoder_annotation_row *const ann_row = | |
197 | (srd_decoder_annotation_row *)l->data; | |
198 | assert(ann_row); | |
199 | ||
200 | const Row row(decc, ann_row); | |
201 | ||
202 | // Add a new empty row data object | |
203 | _rows[row] = decode::RowData(); | |
204 | ||
205 | // Map out all the classes | |
206 | for (const GSList *ll = ann_row->ann_classes; | |
207 | ll; ll = ll->next) | |
208 | _class_rows[make_pair(decc, | |
209 | GPOINTER_TO_INT(ll->data))] = row; | |
210 | } | |
211 | } | |
212 | ||
e0fc5810 JH |
213 | // We get the logic data of the first probe in the list. |
214 | // This works because we are currently assuming all | |
215 | // LogicSignals have the same data/snapshot | |
7491a29f JH |
216 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) |
217 | if (dec && !dec->probes().empty() && | |
218 | ((logic_signal = (*dec->probes().begin()).second)) && | |
7aa09b00 | 219 | ((data = logic_signal->logic_data()))) |
7491a29f JH |
220 | break; |
221 | ||
222 | if (!data) | |
223 | return; | |
224 | ||
225 | // Get the samplerate and start time | |
226 | _start_time = data->get_start_time(); | |
9d28da5a | 227 | _samplerate = data->samplerate(); |
7491a29f JH |
228 | if (_samplerate == 0.0) |
229 | _samplerate = 1.0; | |
e0fc5810 | 230 | |
6e89374a | 231 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 232 | data); |
640d69ce JH |
233 | } |
234 | ||
a007f5ad JH |
235 | uint64_t DecoderStack::get_max_sample_count() const |
236 | { | |
f9101a91 JH |
237 | uint64_t max_sample_count = 0; |
238 | ||
239 | for (map<const Row, RowData>::const_iterator i = _rows.begin(); | |
240 | i != _rows.end(); i++) | |
241 | max_sample_count = max(max_sample_count, | |
242 | (*i).second.get_max_sample()); | |
243 | ||
244 | return max_sample_count; | |
a007f5ad JH |
245 | } |
246 | ||
6e89374a | 247 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 248 | { |
b6b267bb | 249 | srd_session *session; |
e0fc5810 | 250 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 251 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
252 | |
253 | assert(data); | |
254 | ||
a2d4b551 | 255 | // Check we have a snapshot of data |
e0fc5810 JH |
256 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
257 | data->get_snapshots(); | |
258 | if (snapshots.empty()) | |
259 | return; | |
260 | ||
a2d4b551 JH |
261 | // Check that all decoders have the required probes |
262 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
263 | if (!dec->have_required_probes()) | |
264 | return; | |
265 | ||
e0fc5810 JH |
266 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = |
267 | snapshots.front(); | |
c02f1e09 | 268 | const int64_t sample_count = snapshot->get_sample_count(); |
c2d4e9df | 269 | const unsigned int unit_size = snapshot->unit_size(); |
064a7f42 | 270 | const unsigned int chunk_sample_count = |
c2d4e9df | 271 | DecodeChunkLength / unit_size; |
e0fc5810 | 272 | |
b6b267bb JH |
273 | // Create the session |
274 | srd_session_new(&session); | |
275 | assert(session); | |
e0fc5810 | 276 | |
7491a29f JH |
277 | // Create the decoders |
278 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
279 | { | |
13a3528c | 280 | srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size); |
e0fc5810 | 281 | |
7491a29f JH |
282 | if (!di) |
283 | { | |
8a290daf | 284 | _error_message = tr("Failed to create decoder instance"); |
7491a29f JH |
285 | srd_session_destroy(session); |
286 | return; | |
287 | } | |
b6b267bb | 288 | |
7491a29f JH |
289 | if (prev_di) |
290 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 291 | |
7491a29f | 292 | prev_di = di; |
b6b267bb JH |
293 | } |
294 | ||
b6b267bb | 295 | // Start the session |
7491a29f JH |
296 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
297 | g_variant_new_uint64((uint64_t)_samplerate)); | |
298 | ||
299 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
300 | DecoderStack::annotation_callback, this); | |
301 | ||
b6b267bb JH |
302 | srd_session_start(session); |
303 | ||
e0fc5810 | 304 | for (int64_t i = 0; |
819f4c25 JH |
305 | !boost::this_thread::interruption_requested() && |
306 | i < sample_count; | |
064a7f42 | 307 | i += chunk_sample_count) |
e0fc5810 | 308 | { |
fe89c961 JH |
309 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
310 | ||
e0fc5810 | 311 | const int64_t chunk_end = min( |
064a7f42 | 312 | i + chunk_sample_count, sample_count); |
e0fc5810 JH |
313 | snapshot->get_samples(chunk, i, chunk_end); |
314 | ||
c2d4e9df DE |
315 | if (srd_session_send(session, i, i + sample_count, chunk, |
316 | (chunk_end - i) * unit_size) != SRD_OK) { | |
8a290daf | 317 | _error_message = tr("Decoder reported an error"); |
e0fc5810 | 318 | break; |
b6b267bb | 319 | } |
5dfeb70f JH |
320 | |
321 | { | |
322 | lock_guard<mutex> lock(_mutex); | |
323 | _samples_decoded = chunk_end; | |
324 | } | |
e0fc5810 | 325 | } |
b6b267bb JH |
326 | |
327 | // Destroy the session | |
328 | srd_session_destroy(session); | |
e0fc5810 JH |
329 | } |
330 | ||
6e89374a | 331 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 | 332 | { |
e0fc5810 JH |
333 | assert(pdata); |
334 | assert(decoder); | |
335 | ||
6e89374a | 336 | DecoderStack *const d = (DecoderStack*)decoder; |
f9101a91 | 337 | assert(d); |
e0fc5810 | 338 | |
b6b267bb | 339 | lock_guard<mutex> lock(d->_mutex); |
6d8b0562 | 340 | |
f9101a91 JH |
341 | const Annotation a(pdata); |
342 | ||
343 | // Find the row | |
344 | assert(pdata->pdo); | |
345 | assert(pdata->pdo->di); | |
346 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
347 | assert(decc); | |
348 | ||
349 | map<const Row, decode::RowData>::iterator row_iter = d->_rows.end(); | |
350 | ||
351 | // Try looking up the sub-row of this class | |
352 | const map<pair<const srd_decoder*, int>, Row>::const_iterator r = | |
353 | d->_class_rows.find(make_pair(decc, a.format())); | |
354 | if (r != d->_class_rows.end()) | |
355 | row_iter = d->_rows.find((*r).second); | |
356 | else | |
6d8b0562 | 357 | { |
f9101a91 JH |
358 | // Failing that, use the decoder as a key |
359 | row_iter = d->_rows.find(Row(decc)); | |
6d8b0562 UH |
360 | } |
361 | ||
f9101a91 JH |
362 | assert(row_iter != d->_rows.end()); |
363 | if (row_iter == d->_rows.end()) { | |
364 | qDebug() << "Unexpected annotation: decoder = " << decc << | |
365 | ", format = " << a.format(); | |
366 | assert(0); | |
367 | return; | |
368 | } | |
9cef9567 | 369 | |
f9101a91 JH |
370 | // Add the annotation |
371 | (*row_iter).second.push_annotation(a); | |
92421299 | 372 | |
9cef9567 | 373 | d->new_decode_data(); |
119aff65 JH |
374 | } |
375 | ||
376 | } // namespace data | |
377 | } // namespace pv |