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