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