]>
Commit | Line | Data |
---|---|---|
ad908057 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
5a914348 | 20 | #include <forward_list> |
0c5fe73e SA |
21 | #include <limits> |
22 | ||
47747218 SA |
23 | #include <QDebug> |
24 | ||
ad908057 SA |
25 | #include "logic.hpp" |
26 | #include "logicsegment.hpp" | |
27 | #include "decodesignal.hpp" | |
28 | #include "signaldata.hpp" | |
29 | ||
30 | #include <pv/binding/decoder.hpp> | |
31 | #include <pv/data/decode/decoder.hpp> | |
ecd07c20 | 32 | #include <pv/data/decode/row.hpp> |
d3feec23 | 33 | #include <pv/globalsettings.hpp> |
ad908057 SA |
34 | #include <pv/session.hpp> |
35 | ||
5a914348 | 36 | using std::forward_list; |
47747218 SA |
37 | using std::lock_guard; |
38 | using std::make_pair; | |
ad908057 | 39 | using std::make_shared; |
0c5fe73e | 40 | using std::min; |
5ecf957f | 41 | using std::out_of_range; |
ad908057 | 42 | using std::shared_ptr; |
47747218 SA |
43 | using std::unique_lock; |
44 | using pv::data::decode::Annotation; | |
ad908057 | 45 | using pv::data::decode::Decoder; |
ecd07c20 | 46 | using pv::data::decode::Row; |
ad908057 SA |
47 | |
48 | namespace pv { | |
49 | namespace data { | |
50 | ||
47747218 SA |
51 | const double DecodeSignal::DecodeMargin = 1.0; |
52 | const double DecodeSignal::DecodeThreshold = 0.2; | |
3fbddf7f | 53 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; |
ad908057 | 54 | |
ad908057 | 55 | |
47747218 SA |
56 | DecodeSignal::DecodeSignal(pv::Session &session) : |
57 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
58 | session_(session), | |
e91883bb | 59 | srd_session_(nullptr), |
47747218 | 60 | logic_mux_data_invalid_(false), |
e3202557 | 61 | stack_config_changed_(true), |
8a603e13 | 62 | current_segment_id_(0) |
ad908057 | 63 | { |
47747218 SA |
64 | connect(&session_, SIGNAL(capture_state_changed(int)), |
65 | this, SLOT(on_capture_state_changed(int))); | |
ad908057 SA |
66 | } |
67 | ||
47747218 | 68 | DecodeSignal::~DecodeSignal() |
ad908057 | 69 | { |
8ce0e732 | 70 | reset_decode(true); |
ad908057 SA |
71 | } |
72 | ||
47747218 | 73 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const |
ecd07c20 | 74 | { |
47747218 | 75 | return stack_; |
ecd07c20 SA |
76 | } |
77 | ||
c8e60bdf | 78 | void DecodeSignal::stack_decoder(const srd_decoder *decoder) |
ad908057 SA |
79 | { |
80 | assert(decoder); | |
762ab7a4 SA |
81 | const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder); |
82 | ||
83 | stack_.push_back(dec); | |
47747218 SA |
84 | |
85 | // Set name if this decoder is the first in the list | |
86 | if (stack_.size() == 1) | |
87 | set_name(QString::fromUtf8(decoder->name)); | |
132a5c6d | 88 | |
27a3f09b | 89 | // Include the newly created decode channels in the channel lists |
9f97b357 | 90 | update_channel_list(); |
132a5c6d | 91 | |
e3202557 | 92 | stack_config_changed_ = true; |
762ab7a4 | 93 | auto_assign_signals(dec); |
27a3f09b | 94 | commit_decoder_channels(); |
47747218 | 95 | begin_decode(); |
ad908057 SA |
96 | } |
97 | ||
98 | void DecodeSignal::remove_decoder(int index) | |
99 | { | |
47747218 SA |
100 | assert(index >= 0); |
101 | assert(index < (int)stack_.size()); | |
102 | ||
103 | // Find the decoder in the stack | |
104 | auto iter = stack_.begin(); | |
105 | for (int i = 0; i < index; i++, iter++) | |
106 | assert(iter != stack_.end()); | |
107 | ||
108 | // Delete the element | |
109 | stack_.erase(iter); | |
110 | ||
111 | // Update channels and decoded data | |
e3202557 | 112 | stack_config_changed_ = true; |
9f97b357 | 113 | update_channel_list(); |
47747218 | 114 | begin_decode(); |
ad908057 SA |
115 | } |
116 | ||
117 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
118 | { | |
47747218 | 119 | auto iter = stack_.cbegin(); |
ad908057 | 120 | for (int i = 0; i < index; i++, iter++) |
47747218 | 121 | assert(iter != stack_.end()); |
ad908057 SA |
122 | |
123 | shared_ptr<Decoder> dec = *iter; | |
124 | ||
125 | // Toggle decoder visibility | |
126 | bool state = false; | |
127 | if (dec) { | |
128 | state = !dec->shown(); | |
129 | dec->show(state); | |
130 | } | |
131 | ||
132 | return state; | |
133 | } | |
134 | ||
8ce0e732 | 135 | void DecodeSignal::reset_decode(bool shutting_down) |
47747218 | 136 | { |
8ce0e732 | 137 | if (stack_config_changed_ || shutting_down) |
e3202557 SA |
138 | stop_srd_session(); |
139 | else | |
140 | terminate_srd_session(); | |
ce0b6a40 | 141 | |
1b56c646 SA |
142 | if (decode_thread_.joinable()) { |
143 | decode_interrupt_ = true; | |
144 | decode_input_cond_.notify_one(); | |
145 | decode_thread_.join(); | |
146 | } | |
147 | ||
148 | if (logic_mux_thread_.joinable()) { | |
149 | logic_mux_interrupt_ = true; | |
150 | logic_mux_cond_.notify_one(); | |
151 | logic_mux_thread_.join(); | |
152 | } | |
153 | ||
556259d2 SA |
154 | decode_pause_mutex_.unlock(); |
155 | decode_paused_ = false; | |
156 | ||
47747218 | 157 | class_rows_.clear(); |
8a603e13 | 158 | current_segment_id_ = 0; |
72435789 | 159 | segments_.clear(); |
1b56c646 SA |
160 | |
161 | logic_mux_data_.reset(); | |
162 | logic_mux_data_invalid_ = true; | |
eee3eab9 | 163 | |
403c3e87 SA |
164 | if (!error_message_.isEmpty()) { |
165 | error_message_ = QString(); | |
fe060a48 SA |
166 | // TODO Emulate noquote() |
167 | qDebug().nospace() << name() << ": Error cleared"; | |
403c3e87 | 168 | } |
72435789 | 169 | |
eee3eab9 | 170 | decode_reset(); |
47747218 SA |
171 | } |
172 | ||
946b52e1 SA |
173 | void DecodeSignal::begin_decode() |
174 | { | |
47747218 SA |
175 | if (decode_thread_.joinable()) { |
176 | decode_interrupt_ = true; | |
177 | decode_input_cond_.notify_one(); | |
178 | decode_thread_.join(); | |
179 | } | |
180 | ||
181 | if (logic_mux_thread_.joinable()) { | |
182 | logic_mux_interrupt_ = true; | |
183 | logic_mux_cond_.notify_one(); | |
184 | logic_mux_thread_.join(); | |
185 | } | |
186 | ||
187 | reset_decode(); | |
188 | ||
27a3f09b | 189 | if (stack_.size() == 0) { |
403c3e87 | 190 | set_error_message(tr("No decoders")); |
27a3f09b SA |
191 | return; |
192 | } | |
193 | ||
194 | assert(channels_.size() > 0); | |
195 | ||
196 | if (get_assigned_signal_count() == 0) { | |
403c3e87 | 197 | set_error_message(tr("There are no channels assigned to this decoder")); |
27a3f09b SA |
198 | return; |
199 | } | |
200 | ||
04627946 SA |
201 | // Make sure that all assigned channels still provide logic data |
202 | // (can happen when a converted signal was assigned but the | |
203 | // conversion removed in the meanwhile) | |
204 | for (data::DecodeChannel &ch : channels_) | |
205 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) | |
206 | ch.assigned_signal = nullptr; | |
207 | ||
47747218 SA |
208 | // Check that all decoders have the required channels |
209 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
210 | if (!dec->have_required_channels()) { | |
403c3e87 SA |
211 | set_error_message(tr("One or more required channels " |
212 | "have not been specified")); | |
47747218 SA |
213 | return; |
214 | } | |
215 | ||
4913560f | 216 | // Map out all the annotation classes |
f9807084 | 217 | int row_index = 0; |
47747218 SA |
218 | for (const shared_ptr<decode::Decoder> &dec : stack_) { |
219 | assert(dec); | |
220 | const srd_decoder *const decc = dec->decoder(); | |
221 | assert(dec->decoder()); | |
222 | ||
47747218 SA |
223 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { |
224 | const srd_decoder_annotation_row *const ann_row = | |
225 | (srd_decoder_annotation_row *)l->data; | |
226 | assert(ann_row); | |
227 | ||
f9807084 | 228 | const Row row(row_index++, decc, ann_row); |
47747218 | 229 | |
47747218 SA |
230 | for (const GSList *ll = ann_row->ann_classes; |
231 | ll; ll = ll->next) | |
232 | class_rows_[make_pair(decc, | |
233 | GPOINTER_TO_INT(ll->data))] = row; | |
234 | } | |
235 | } | |
236 | ||
27a3f09b SA |
237 | // Free the logic data and its segment(s) if it needs to be updated |
238 | if (logic_mux_data_invalid_) | |
239 | logic_mux_data_.reset(); | |
240 | ||
241 | if (!logic_mux_data_) { | |
72435789 | 242 | const uint32_t ch_count = get_assigned_signal_count(); |
ed535cd7 | 243 | logic_mux_unit_size_ = (ch_count + 7) / 8; |
27a3f09b | 244 | logic_mux_data_ = make_shared<Logic>(ch_count); |
27a3f09b | 245 | } |
47747218 | 246 | |
ed535cd7 SA |
247 | // Receive notifications when new sample data is available |
248 | connect_input_notifiers(); | |
249 | ||
8a603e13 | 250 | if (get_input_segment_count() == 0) { |
403c3e87 | 251 | set_error_message(tr("No input data")); |
ed535cd7 SA |
252 | return; |
253 | } | |
254 | ||
27a3f09b SA |
255 | // Make sure the logic output data is complete and up-to-date |
256 | logic_mux_interrupt_ = false; | |
257 | logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this); | |
258 | ||
259 | // Decode the muxed logic data | |
47747218 SA |
260 | decode_interrupt_ = false; |
261 | decode_thread_ = std::thread(&DecodeSignal::decode_proc, this); | |
946b52e1 SA |
262 | } |
263 | ||
556259d2 SA |
264 | void DecodeSignal::pause_decode() |
265 | { | |
266 | decode_paused_ = true; | |
267 | } | |
268 | ||
269 | void DecodeSignal::resume_decode() | |
270 | { | |
271 | // Manual unlocking is done before notifying, to avoid waking up the | |
272 | // waiting thread only to block again (see notify_one for details) | |
273 | decode_pause_mutex_.unlock(); | |
274 | decode_pause_cond_.notify_one(); | |
275 | decode_paused_ = false; | |
276 | } | |
277 | ||
278 | bool DecodeSignal::is_paused() const | |
279 | { | |
280 | return decode_paused_; | |
281 | } | |
282 | ||
ecd07c20 SA |
283 | QString DecodeSignal::error_message() const |
284 | { | |
47747218 SA |
285 | lock_guard<mutex> lock(output_mutex_); |
286 | return error_message_; | |
ecd07c20 SA |
287 | } |
288 | ||
47747218 | 289 | const vector<data::DecodeChannel> DecodeSignal::get_channels() const |
9f97b357 SA |
290 | { |
291 | return channels_; | |
292 | } | |
293 | ||
762ab7a4 | 294 | void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec) |
132a5c6d | 295 | { |
47747218 SA |
296 | bool new_assignment = false; |
297 | ||
132a5c6d SA |
298 | // Try to auto-select channels that don't have signals assigned yet |
299 | for (data::DecodeChannel &ch : channels_) { | |
762ab7a4 SA |
300 | // If a decoder is given, auto-assign only its channels |
301 | if (dec && (ch.decoder_ != dec)) | |
302 | continue; | |
303 | ||
132a5c6d SA |
304 | if (ch.assigned_signal) |
305 | continue; | |
306 | ||
a8030bb0 SA |
307 | const QString ch_name = ch.name.toLower(); |
308 | ||
309 | shared_ptr<data::SignalBase> match; | |
692f6093 | 310 | for (shared_ptr<data::SignalBase> s : session_.signalbases()) { |
f9ebf303 SA |
311 | if (!s->enabled()) |
312 | continue; | |
313 | ||
692f6093 SA |
314 | const QString s_name = s->name().toLower(); |
315 | ||
316 | if (s->logic_data() && | |
317 | ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) { | |
a8030bb0 SA |
318 | if (!match) |
319 | match = s; | |
320 | else { | |
321 | // Only replace an existing match if it matches more characters | |
322 | int old_unmatched = ch_name.length() - match->name().length(); | |
323 | int new_unmatched = ch_name.length() - s->name().length(); | |
324 | if (abs(new_unmatched) < abs(old_unmatched)) | |
325 | match = s; | |
326 | } | |
47747218 | 327 | } |
692f6093 | 328 | } |
a8030bb0 SA |
329 | |
330 | if (match) { | |
331 | ch.assigned_signal = match.get(); | |
332 | new_assignment = true; | |
333 | } | |
47747218 SA |
334 | } |
335 | ||
336 | if (new_assignment) { | |
337 | logic_mux_data_invalid_ = true; | |
e3202557 | 338 | stack_config_changed_ = true; |
27a3f09b | 339 | commit_decoder_channels(); |
47747218 | 340 | channels_updated(); |
132a5c6d SA |
341 | } |
342 | } | |
343 | ||
9f97b357 SA |
344 | void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal) |
345 | { | |
346 | for (data::DecodeChannel &ch : channels_) | |
47747218 | 347 | if (ch.id == channel_id) { |
9f97b357 | 348 | ch.assigned_signal = signal; |
47747218 SA |
349 | logic_mux_data_invalid_ = true; |
350 | } | |
9f97b357 | 351 | |
e3202557 | 352 | stack_config_changed_ = true; |
27a3f09b | 353 | commit_decoder_channels(); |
9f97b357 | 354 | channels_updated(); |
47747218 | 355 | begin_decode(); |
9f97b357 SA |
356 | } |
357 | ||
27a3f09b SA |
358 | int DecodeSignal::get_assigned_signal_count() const |
359 | { | |
360 | // Count all channels that have a signal assigned to them | |
361 | return count_if(channels_.begin(), channels_.end(), | |
362 | [](data::DecodeChannel ch) { return ch.assigned_signal; }); | |
363 | } | |
364 | ||
9f97b357 SA |
365 | void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) |
366 | { | |
367 | for (data::DecodeChannel &ch : channels_) | |
368 | if (ch.id == channel_id) | |
369 | ch.initial_pin_state = init_state; | |
370 | ||
e3202557 | 371 | stack_config_changed_ = true; |
9f97b357 | 372 | channels_updated(); |
47747218 | 373 | begin_decode(); |
9f97b357 SA |
374 | } |
375 | ||
ff83d980 SA |
376 | double DecodeSignal::samplerate() const |
377 | { | |
72435789 SA |
378 | double result = 0; |
379 | ||
380 | // TODO For now, we simply return the first samplerate that we have | |
8a603e13 SA |
381 | if (segments_.size() > 0) |
382 | result = segments_.front().samplerate; | |
72435789 SA |
383 | |
384 | return result; | |
ff83d980 SA |
385 | } |
386 | ||
72435789 | 387 | const pv::util::Timestamp DecodeSignal::start_time() const |
ff83d980 | 388 | { |
72435789 SA |
389 | pv::util::Timestamp result; |
390 | ||
391 | // TODO For now, we simply return the first start time that we have | |
8a603e13 SA |
392 | if (segments_.size() > 0) |
393 | result = segments_.front().start_time; | |
72435789 SA |
394 | |
395 | return result; | |
ff83d980 SA |
396 | } |
397 | ||
5ecf957f | 398 | int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const |
0c5fe73e SA |
399 | { |
400 | // The working sample count is the highest sample number for | |
ed535cd7 SA |
401 | // which all used signals have data available, so go through all |
402 | // channels and use the lowest overall sample count of the segment | |
0c5fe73e | 403 | |
0c5fe73e SA |
404 | int64_t count = std::numeric_limits<int64_t>::max(); |
405 | bool no_signals_assigned = true; | |
406 | ||
407 | for (const data::DecodeChannel &ch : channels_) | |
408 | if (ch.assigned_signal) { | |
409 | no_signals_assigned = false; | |
410 | ||
411 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
412 | if (!logic_data || logic_data->logic_segments().empty()) | |
413 | return 0; | |
414 | ||
5ecf957f SA |
415 | try { |
416 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
417 | count = min(count, (int64_t)segment->get_sample_count()); | |
30677c13 | 418 | } catch (out_of_range&) { |
5ecf957f SA |
419 | return 0; |
420 | } | |
0c5fe73e SA |
421 | } |
422 | ||
423 | return (no_signals_assigned ? 0 : count); | |
424 | } | |
425 | ||
b82908ab SA |
426 | int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id, |
427 | bool include_processing) const | |
ff83d980 | 428 | { |
47747218 | 429 | lock_guard<mutex> decode_lock(output_mutex_); |
5ecf957f SA |
430 | |
431 | int64_t result = 0; | |
432 | ||
72435789 SA |
433 | try { |
434 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
b82908ab SA |
435 | if (include_processing) |
436 | result = segment->samples_decoded_incl; | |
437 | else | |
438 | result = segment->samples_decoded_excl; | |
30677c13 | 439 | } catch (out_of_range&) { |
72435789 SA |
440 | // Do nothing |
441 | } | |
5ecf957f SA |
442 | |
443 | return result; | |
ff83d980 SA |
444 | } |
445 | ||
ecd07c20 SA |
446 | vector<Row> DecodeSignal::visible_rows() const |
447 | { | |
47747218 SA |
448 | lock_guard<mutex> lock(output_mutex_); |
449 | ||
450 | vector<Row> rows; | |
451 | ||
452 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
453 | assert(dec); | |
454 | if (!dec->shown()) | |
455 | continue; | |
456 | ||
457 | const srd_decoder *const decc = dec->decoder(); | |
458 | assert(dec->decoder()); | |
459 | ||
f9807084 | 460 | int row_index = 0; |
47747218 SA |
461 | // Add a row for the decoder if it doesn't have a row list |
462 | if (!decc->annotation_rows) | |
f9807084 | 463 | rows.emplace_back(row_index++, decc); |
47747218 SA |
464 | |
465 | // Add the decoder rows | |
466 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
467 | const srd_decoder_annotation_row *const ann_row = | |
468 | (srd_decoder_annotation_row *)l->data; | |
469 | assert(ann_row); | |
f9807084 | 470 | rows.emplace_back(row_index++, decc, ann_row); |
47747218 SA |
471 | } |
472 | } | |
473 | ||
474 | return rows; | |
ecd07c20 SA |
475 | } |
476 | ||
477 | void DecodeSignal::get_annotation_subset( | |
478 | vector<pv::data::decode::Annotation> &dest, | |
72435789 | 479 | const decode::Row &row, uint32_t segment_id, uint64_t start_sample, |
ecd07c20 SA |
480 | uint64_t end_sample) const |
481 | { | |
47747218 SA |
482 | lock_guard<mutex> lock(output_mutex_); |
483 | ||
72435789 SA |
484 | try { |
485 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
486 | const map<const decode::Row, decode::RowData> *rows = | |
487 | &(segment->annotation_rows); | |
488 | ||
489 | const auto iter = rows->find(row); | |
490 | if (iter != rows->end()) | |
491 | (*iter).second.get_annotation_subset(dest, | |
492 | start_sample, end_sample); | |
30677c13 | 493 | } catch (out_of_range&) { |
72435789 SA |
494 | // Do nothing |
495 | } | |
47747218 SA |
496 | } |
497 | ||
5a914348 SA |
498 | void DecodeSignal::get_annotation_subset( |
499 | vector<pv::data::decode::Annotation> &dest, | |
500 | uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const | |
501 | { | |
502 | // Note: We put all vectors and lists on the heap, not the stack | |
503 | ||
504 | const vector<Row> rows = visible_rows(); | |
505 | ||
506 | // Use forward_lists for faster merging | |
507 | forward_list<Annotation> *all_ann_list = new forward_list<Annotation>(); | |
508 | ||
509 | for (const Row& row : rows) { | |
510 | vector<Annotation> *ann_vector = new vector<Annotation>(); | |
511 | get_annotation_subset(*ann_vector, row, segment_id, start_sample, end_sample); | |
512 | ||
513 | forward_list<Annotation> *ann_list = | |
514 | new forward_list<Annotation>(ann_vector->begin(), ann_vector->end()); | |
515 | delete ann_vector; | |
516 | ||
517 | all_ann_list->merge(*ann_list); | |
518 | delete ann_list; | |
519 | } | |
520 | ||
521 | move(all_ann_list->begin(), all_ann_list->end(), back_inserter(dest)); | |
522 | delete all_ann_list; | |
523 | } | |
524 | ||
47747218 SA |
525 | void DecodeSignal::save_settings(QSettings &settings) const |
526 | { | |
527 | SignalBase::save_settings(settings); | |
528 | ||
c8e60bdf SA |
529 | settings.setValue("decoders", (int)(stack_.size())); |
530 | ||
8b5ea5a1 | 531 | // Save decoder stack |
c8e60bdf SA |
532 | int decoder_idx = 0; |
533 | for (shared_ptr<decode::Decoder> decoder : stack_) { | |
534 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); | |
535 | ||
536 | settings.setValue("id", decoder->decoder()->id); | |
537 | ||
d3feec23 SA |
538 | // Save decoder options |
539 | const map<string, GVariant*>& options = decoder->options(); | |
540 | ||
541 | settings.setValue("options", (int)options.size()); | |
542 | ||
543 | // Note: decode::Decoder::options() returns only the options | |
544 | // that differ from the default. See binding::Decoder::getter() | |
545 | int i = 0; | |
546 | for (auto option : options) { | |
547 | settings.beginGroup("option" + QString::number(i)); | |
548 | settings.setValue("name", QString::fromStdString(option.first)); | |
549 | GlobalSettings::store_gvariant(settings, option.second); | |
550 | settings.endGroup(); | |
551 | i++; | |
552 | } | |
553 | ||
c8e60bdf SA |
554 | settings.endGroup(); |
555 | } | |
556 | ||
8b5ea5a1 SA |
557 | // Save channel mapping |
558 | settings.setValue("channels", (int)channels_.size()); | |
559 | ||
560 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
561 | auto channel = find_if(channels_.begin(), channels_.end(), | |
562 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
563 | ||
564 | if (channel == channels_.end()) { | |
565 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
566 | continue; | |
567 | } | |
568 | ||
569 | settings.beginGroup("channel" + QString::number(channel_id)); | |
570 | ||
571 | settings.setValue("name", channel->name); // Useful for debugging | |
572 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
573 | ||
574 | if (channel->assigned_signal) | |
575 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
576 | ||
577 | settings.endGroup(); | |
578 | } | |
47747218 SA |
579 | } |
580 | ||
581 | void DecodeSignal::restore_settings(QSettings &settings) | |
582 | { | |
583 | SignalBase::restore_settings(settings); | |
584 | ||
8b5ea5a1 | 585 | // Restore decoder stack |
c8e60bdf SA |
586 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); |
587 | ||
588 | int decoders = settings.value("decoders").toInt(); | |
589 | ||
590 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
591 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
592 | ||
593 | QString id = settings.value("id").toString(); | |
594 | ||
595 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
596 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
597 | if (!dec) | |
598 | continue; | |
599 | ||
600 | if (QString::fromUtf8(dec->id) == id) { | |
d3feec23 SA |
601 | shared_ptr<decode::Decoder> decoder = |
602 | make_shared<decode::Decoder>(dec); | |
603 | ||
604 | stack_.push_back(decoder); | |
605 | ||
606 | // Restore decoder options that differ from their default | |
607 | int options = settings.value("options").toInt(); | |
608 | ||
609 | for (int i = 0; i < options; i++) { | |
610 | settings.beginGroup("option" + QString::number(i)); | |
611 | QString name = settings.value("name").toString(); | |
612 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
613 | decoder->set_option(name.toUtf8(), value); | |
614 | settings.endGroup(); | |
615 | } | |
c8e60bdf SA |
616 | |
617 | // Include the newly created decode channels in the channel lists | |
618 | update_channel_list(); | |
619 | break; | |
620 | } | |
621 | } | |
622 | ||
623 | settings.endGroup(); | |
4ca06ec3 | 624 | channels_updated(); |
c8e60bdf SA |
625 | } |
626 | ||
8b5ea5a1 SA |
627 | // Restore channel mapping |
628 | unsigned int channels = settings.value("channels").toInt(); | |
629 | ||
630 | const unordered_set< shared_ptr<data::SignalBase> > signalbases = | |
631 | session_.signalbases(); | |
632 | ||
633 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
634 | auto channel = find_if(channels_.begin(), channels_.end(), | |
635 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
636 | ||
637 | if (channel == channels_.end()) { | |
638 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
639 | continue; | |
640 | } | |
641 | ||
642 | settings.beginGroup("channel" + QString::number(channel_id)); | |
643 | ||
644 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
645 | ||
646 | for (shared_ptr<data::SignalBase> signal : signalbases) | |
647 | if (signal->name() == assigned_signal_name) | |
648 | channel->assigned_signal = signal.get(); | |
649 | ||
650 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
651 | ||
652 | settings.endGroup(); | |
653 | } | |
654 | ||
aa71890d | 655 | // Update the internal structures |
e3202557 | 656 | stack_config_changed_ = true; |
aa71890d SA |
657 | update_channel_list(); |
658 | commit_decoder_channels(); | |
659 | ||
8b5ea5a1 | 660 | begin_decode(); |
47747218 SA |
661 | } |
662 | ||
403c3e87 SA |
663 | void DecodeSignal::set_error_message(QString msg) |
664 | { | |
665 | error_message_ = msg; | |
fe060a48 SA |
666 | // TODO Emulate noquote() |
667 | qDebug().nospace() << name() << ": " << msg; | |
403c3e87 SA |
668 | } |
669 | ||
8a603e13 SA |
670 | uint32_t DecodeSignal::get_input_segment_count() const |
671 | { | |
672 | uint64_t count = std::numeric_limits<uint64_t>::max(); | |
673 | bool no_signals_assigned = true; | |
674 | ||
675 | for (const data::DecodeChannel &ch : channels_) | |
676 | if (ch.assigned_signal) { | |
677 | no_signals_assigned = false; | |
678 | ||
679 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
680 | if (!logic_data || logic_data->logic_segments().empty()) | |
681 | return 0; | |
682 | ||
683 | // Find the min value of all segment counts | |
684 | if ((uint64_t)(logic_data->logic_segments().size()) < count) | |
685 | count = logic_data->logic_segments().size(); | |
686 | } | |
687 | ||
688 | return (no_signals_assigned ? 0 : count); | |
689 | } | |
690 | ||
691 | uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const | |
692 | { | |
693 | double samplerate = 0; | |
694 | ||
695 | for (const data::DecodeChannel &ch : channels_) | |
696 | if (ch.assigned_signal) { | |
697 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
698 | if (!logic_data || logic_data->logic_segments().empty()) | |
699 | continue; | |
700 | ||
701 | try { | |
702 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
703 | samplerate = segment->samplerate(); | |
30677c13 | 704 | } catch (out_of_range&) { |
8a603e13 SA |
705 | // Do nothing |
706 | } | |
707 | break; | |
708 | } | |
709 | ||
710 | return samplerate; | |
711 | } | |
712 | ||
9f97b357 SA |
713 | void DecodeSignal::update_channel_list() |
714 | { | |
47747218 | 715 | vector<data::DecodeChannel> prev_channels = channels_; |
9f97b357 SA |
716 | channels_.clear(); |
717 | ||
718 | uint16_t id = 0; | |
719 | ||
720 | // Copy existing entries, create new as needed | |
47747218 | 721 | for (shared_ptr<Decoder> decoder : stack_) { |
9f97b357 SA |
722 | const srd_decoder* srd_d = decoder->decoder(); |
723 | const GSList *l; | |
724 | ||
725 | // Mandatory channels | |
726 | for (l = srd_d->channels; l; l = l->next) { | |
727 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
728 | bool ch_added = false; | |
729 | ||
730 | // Copy but update ID if this channel was in the list before | |
47747218 | 731 | for (data::DecodeChannel &ch : prev_channels) |
9f97b357 SA |
732 | if (ch.pdch_ == pdch) { |
733 | ch.id = id++; | |
734 | channels_.push_back(ch); | |
735 | ch_added = true; | |
736 | break; | |
737 | } | |
738 | ||
739 | if (!ch_added) { | |
740 | // Create new entry without a mapped signal | |
6e7a4a00 | 741 | data::DecodeChannel ch = {id++, 0, false, nullptr, |
9f97b357 SA |
742 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
743 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
744 | channels_.push_back(ch); | |
745 | } | |
746 | } | |
747 | ||
748 | // Optional channels | |
749 | for (l = srd_d->opt_channels; l; l = l->next) { | |
750 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
751 | bool ch_added = false; | |
752 | ||
753 | // Copy but update ID if this channel was in the list before | |
47747218 | 754 | for (data::DecodeChannel &ch : prev_channels) |
9f97b357 SA |
755 | if (ch.pdch_ == pdch) { |
756 | ch.id = id++; | |
757 | channels_.push_back(ch); | |
758 | ch_added = true; | |
759 | break; | |
760 | } | |
761 | ||
762 | if (!ch_added) { | |
763 | // Create new entry without a mapped signal | |
6e7a4a00 | 764 | data::DecodeChannel ch = {id++, 0, true, nullptr, |
9f97b357 SA |
765 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
766 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
767 | channels_.push_back(ch); | |
768 | } | |
769 | } | |
770 | } | |
771 | ||
47747218 SA |
772 | // Invalidate the logic output data if the channel assignment changed |
773 | if (prev_channels.size() != channels_.size()) { | |
774 | // The number of channels changed, there's definitely a difference | |
775 | logic_mux_data_invalid_ = true; | |
776 | } else { | |
777 | // Same number but assignment may still differ, so compare all channels | |
778 | for (size_t i = 0; i < channels_.size(); i++) { | |
779 | const data::DecodeChannel &p_ch = prev_channels[i]; | |
780 | const data::DecodeChannel &ch = channels_[i]; | |
781 | ||
782 | if ((p_ch.pdch_ != ch.pdch_) || | |
783 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
784 | logic_mux_data_invalid_ = true; | |
785 | break; | |
786 | } | |
787 | } | |
788 | ||
789 | } | |
790 | ||
9f97b357 SA |
791 | channels_updated(); |
792 | } | |
793 | ||
27a3f09b | 794 | void DecodeSignal::commit_decoder_channels() |
ad908057 | 795 | { |
27a3f09b SA |
796 | // Submit channel list to every decoder, containing only the relevant channels |
797 | for (shared_ptr<decode::Decoder> dec : stack_) { | |
798 | vector<data::DecodeChannel*> channel_list; | |
799 | ||
800 | for (data::DecodeChannel &ch : channels_) | |
801 | if (ch.decoder_ == dec) | |
802 | channel_list.push_back(&ch); | |
47747218 | 803 | |
27a3f09b SA |
804 | dec->set_channels(channel_list); |
805 | } | |
aa71890d SA |
806 | |
807 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
808 | int id = 0; | |
809 | for (data::DecodeChannel &ch : channels_) | |
810 | if (ch.assigned_signal) | |
811 | ch.bit_id = id++; | |
47747218 SA |
812 | } |
813 | ||
ed535cd7 | 814 | void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end) |
47747218 | 815 | { |
27a3f09b SA |
816 | // Enforce end to be greater than start |
817 | if (end <= start) | |
818 | return; | |
819 | ||
ed535cd7 | 820 | // Fetch the channel segments and their data |
27a3f09b SA |
821 | vector<shared_ptr<LogicSegment> > segments; |
822 | vector<const uint8_t*> signal_data; | |
823 | vector<uint8_t> signal_in_bytepos; | |
824 | vector<uint8_t> signal_in_bitpos; | |
825 | ||
826 | for (data::DecodeChannel &ch : channels_) | |
827 | if (ch.assigned_signal) { | |
828 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
ed535cd7 SA |
829 | |
830 | shared_ptr<LogicSegment> segment; | |
831 | try { | |
832 | segment = logic_data->logic_segments().at(segment_id); | |
30677c13 | 833 | } catch (out_of_range&) { |
ed535cd7 SA |
834 | qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \ |
835 | << "has no logic segment" << segment_id; | |
836 | return; | |
837 | } | |
27a3f09b | 838 | segments.push_back(segment); |
b82243f7 SA |
839 | |
840 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
841 | segment->get_samples(start, end, data); | |
842 | signal_data.push_back(data); | |
27a3f09b SA |
843 | |
844 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
845 | signal_in_bytepos.push_back(bitpos / 8); | |
846 | signal_in_bitpos.push_back(bitpos % 8); | |
847 | } | |
848 | ||
ed535cd7 SA |
849 | |
850 | shared_ptr<LogicSegment> output_segment; | |
851 | try { | |
852 | output_segment = logic_mux_data_->logic_segments().at(segment_id); | |
30677c13 | 853 | } catch (out_of_range&) { |
ed535cd7 SA |
854 | qDebug() << "Muxer error for" << name() << ": no logic mux segment" \ |
855 | << segment_id << "in mux_logic_samples(), mux segments size is" \ | |
856 | << logic_mux_data_->logic_segments().size(); | |
857 | return; | |
858 | } | |
859 | ||
27a3f09b | 860 | // Perform the muxing of signal data into the output data |
8a603e13 | 861 | uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()]; |
27a3f09b SA |
862 | unsigned int signal_count = signal_data.size(); |
863 | ||
3b5282a2 SA |
864 | for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start)); |
865 | sample_cnt++) { | |
866 | ||
27a3f09b SA |
867 | int bitpos = 0; |
868 | uint8_t bytepos = 0; | |
869 | ||
8a603e13 SA |
870 | const int out_sample_pos = sample_cnt * output_segment->unit_size(); |
871 | for (unsigned int i = 0; i < output_segment->unit_size(); i++) | |
27a3f09b | 872 | output[out_sample_pos + i] = 0; |
47747218 | 873 | |
27a3f09b SA |
874 | for (unsigned int i = 0; i < signal_count; i++) { |
875 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
876 | const uint8_t in_sample = 1 & | |
877 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
47747218 | 878 | |
27a3f09b SA |
879 | const uint8_t out_sample = output[out_sample_pos + bytepos]; |
880 | ||
881 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
882 | ||
883 | bitpos++; | |
884 | if (bitpos > 7) { | |
885 | bitpos = 0; | |
886 | bytepos++; | |
887 | } | |
888 | } | |
47747218 SA |
889 | } |
890 | ||
8a603e13 | 891 | output_segment->append_payload(output, (end - start) * output_segment->unit_size()); |
27a3f09b SA |
892 | delete[] output; |
893 | ||
894 | for (const uint8_t* data : signal_data) | |
895 | delete[] data; | |
896 | } | |
897 | ||
898 | void DecodeSignal::logic_mux_proc() | |
899 | { | |
ed535cd7 SA |
900 | uint32_t segment_id = 0; |
901 | ||
8a603e13 SA |
902 | assert(logic_mux_data_); |
903 | ||
65efd025 SA |
904 | // Create initial logic mux segment |
905 | shared_ptr<LogicSegment> output_segment = | |
85a70280 SA |
906 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, |
907 | logic_mux_unit_size_, 0); | |
65efd025 | 908 | logic_mux_data_->push_segment(output_segment); |
8a603e13 SA |
909 | |
910 | output_segment->set_samplerate(get_input_samplerate(0)); | |
ed535cd7 | 911 | |
27a3f09b | 912 | do { |
ed535cd7 | 913 | const uint64_t input_sample_count = get_working_sample_count(segment_id); |
8a603e13 | 914 | const uint64_t output_sample_count = output_segment->get_sample_count(); |
27a3f09b SA |
915 | |
916 | const uint64_t samples_to_process = | |
917 | (input_sample_count > output_sample_count) ? | |
918 | (input_sample_count - output_sample_count) : 0; | |
919 | ||
920 | // Process the samples if necessary... | |
921 | if (samples_to_process > 0) { | |
8a603e13 | 922 | const uint64_t unit_size = output_segment->unit_size(); |
27a3f09b SA |
923 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; |
924 | ||
925 | uint64_t processed_samples = 0; | |
926 | do { | |
927 | const uint64_t start_sample = output_sample_count + processed_samples; | |
928 | const uint64_t sample_count = | |
929 | min(samples_to_process - processed_samples, chunk_sample_count); | |
930 | ||
ed535cd7 | 931 | mux_logic_samples(segment_id, start_sample, start_sample + sample_count); |
27a3f09b SA |
932 | processed_samples += sample_count; |
933 | ||
934 | // ...and process the newly muxed logic data | |
935 | decode_input_cond_.notify_one(); | |
3b5282a2 | 936 | } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process)); |
27a3f09b SA |
937 | } |
938 | ||
1ec191ed | 939 | if (samples_to_process == 0) { |
ed535cd7 SA |
940 | // TODO Optimize this by caching the input segment count and only |
941 | // querying it when the cached value was reached | |
942 | if (segment_id < get_input_segment_count() - 1) { | |
943 | // Process next segment | |
944 | segment_id++; | |
945 | ||
65efd025 | 946 | output_segment = |
85a70280 SA |
947 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, |
948 | logic_mux_unit_size_, 0); | |
65efd025 | 949 | logic_mux_data_->push_segment(output_segment); |
ba5f2186 | 950 | |
8a603e13 SA |
951 | output_segment->set_samplerate(get_input_samplerate(segment_id)); |
952 | ||
ed535cd7 SA |
953 | } else { |
954 | // All segments have been processed | |
955 | logic_mux_data_invalid_ = false; | |
956 | ||
957 | // Wait for more input | |
958 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
959 | logic_mux_cond_.wait(logic_mux_lock); | |
960 | } | |
27a3f09b | 961 | } |
1ae18301 | 962 | |
1ec191ed | 963 | } while (!logic_mux_interrupt_); |
47747218 SA |
964 | } |
965 | ||
966 | void DecodeSignal::decode_data( | |
8a603e13 SA |
967 | const int64_t abs_start_samplenum, const int64_t sample_count, |
968 | const shared_ptr<LogicSegment> input_segment) | |
47747218 | 969 | { |
8a603e13 | 970 | const int64_t unit_size = input_segment->unit_size(); |
27a3f09b | 971 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; |
47747218 SA |
972 | |
973 | for (int64_t i = abs_start_samplenum; | |
1ae18301 SA |
974 | error_message_.isEmpty() && !decode_interrupt_ && |
975 | (i < (abs_start_samplenum + sample_count)); | |
47747218 SA |
976 | i += chunk_sample_count) { |
977 | ||
978 | const int64_t chunk_end = min(i + chunk_sample_count, | |
979 | abs_start_samplenum + sample_count); | |
980 | ||
e06cf18d SA |
981 | { |
982 | lock_guard<mutex> lock(output_mutex_); | |
b82908ab SA |
983 | // Update the sample count showing the samples including currently processed ones |
984 | segments_.at(current_segment_id_).samples_decoded_incl = chunk_end; | |
e06cf18d SA |
985 | } |
986 | ||
b82243f7 SA |
987 | int64_t data_size = (chunk_end - i) * unit_size; |
988 | uint8_t* chunk = new uint8_t[data_size]; | |
8a603e13 | 989 | input_segment->get_samples(i, chunk_end, chunk); |
47747218 | 990 | |
e91883bb | 991 | if (srd_session_send(srd_session_, i, chunk_end, chunk, |
1ae18301 | 992 | data_size, unit_size) != SRD_OK) |
403c3e87 | 993 | set_error_message(tr("Decoder reported an error")); |
a3ebd556 | 994 | |
47747218 SA |
995 | delete[] chunk; |
996 | ||
b82908ab SA |
997 | { |
998 | lock_guard<mutex> lock(output_mutex_); | |
999 | // Now that all samples are processed, the exclusive sample count catches up | |
1000 | segments_.at(current_segment_id_).samples_decoded_excl = chunk_end; | |
1001 | } | |
1002 | ||
1b56c646 SA |
1003 | // Notify the frontend that we processed some data and |
1004 | // possibly have new annotations as well | |
1005 | new_annotations(); | |
556259d2 SA |
1006 | |
1007 | if (decode_paused_) { | |
1008 | unique_lock<mutex> pause_wait_lock(decode_pause_mutex_); | |
1009 | decode_pause_cond_.wait(pause_wait_lock); | |
1010 | } | |
47747218 SA |
1011 | } |
1012 | } | |
1013 | ||
1014 | void DecodeSignal::decode_proc() | |
1015 | { | |
65efd025 SA |
1016 | current_segment_id_ = 0; |
1017 | ||
1018 | // If there is no input data available yet, wait until it is or we're interrupted | |
1019 | if (logic_mux_data_->logic_segments().size() == 0) { | |
1020 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1021 | decode_input_cond_.wait(input_wait_lock); | |
1022 | } | |
a3ebd556 SA |
1023 | |
1024 | if (decode_interrupt_) | |
1025 | return; | |
1026 | ||
8a603e13 | 1027 | shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front(); |
8a603e13 | 1028 | assert(input_segment); |
65efd025 SA |
1029 | |
1030 | // Create the initial segment and set its sample rate so that we can pass it to SRD | |
1031 | create_decode_segment(); | |
8a603e13 | 1032 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); |
65efd025 | 1033 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); |
8a603e13 | 1034 | |
65efd025 SA |
1035 | start_srd_session(); |
1036 | ||
1037 | uint64_t sample_count = 0; | |
27a3f09b | 1038 | uint64_t abs_start_samplenum = 0; |
47747218 | 1039 | do { |
27a3f09b SA |
1040 | // Keep processing new samples until we exhaust the input data |
1041 | do { | |
767bf4e7 | 1042 | lock_guard<mutex> input_lock(input_mutex_); |
8a603e13 | 1043 | sample_count = input_segment->get_sample_count() - abs_start_samplenum; |
27a3f09b SA |
1044 | |
1045 | if (sample_count > 0) { | |
8a603e13 | 1046 | decode_data(abs_start_samplenum, sample_count, input_segment); |
27a3f09b SA |
1047 | abs_start_samplenum += sample_count; |
1048 | } | |
1b56c646 SA |
1049 | } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_); |
1050 | ||
8a603e13 SA |
1051 | if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) { |
1052 | if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) { | |
1053 | // Process next segment | |
1054 | current_segment_id_++; | |
1055 | ||
1056 | try { | |
1057 | input_segment = logic_mux_data_->logic_segments().at(current_segment_id_); | |
30677c13 | 1058 | } catch (out_of_range&) { |
8a603e13 SA |
1059 | qDebug() << "Decode error for" << name() << ": no logic mux segment" \ |
1060 | << current_segment_id_ << "in decode_proc(), mux segments size is" \ | |
1061 | << logic_mux_data_->logic_segments().size(); | |
1062 | return; | |
1063 | } | |
1064 | abs_start_samplenum = 0; | |
1065 | ||
65efd025 SA |
1066 | // Create the next segment and set its metadata |
1067 | create_decode_segment(); | |
8a603e13 | 1068 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); |
65efd025 | 1069 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); |
8a603e13 | 1070 | |
90ca4cc8 SA |
1071 | // Reset decoder state but keep the decoder stack intact |
1072 | terminate_srd_session(); | |
8a603e13 SA |
1073 | } else { |
1074 | // All segments have been processed | |
1b56c646 | 1075 | decode_finished(); |
27a3f09b | 1076 | |
8a603e13 SA |
1077 | // Wait for new input data or an interrupt was requested |
1078 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1079 | decode_input_cond_.wait(input_wait_lock); | |
1080 | } | |
27a3f09b SA |
1081 | } |
1082 | } while (error_message_.isEmpty() && !decode_interrupt_); | |
ce0b6a40 GS |
1083 | |
1084 | // Potentially reap decoders when the application no longer is | |
1085 | // interested in their (pending) results. | |
1086 | if (decode_interrupt_) | |
1087 | terminate_srd_session(); | |
e91883bb SA |
1088 | } |
1089 | ||
1090 | void DecodeSignal::start_srd_session() | |
1091 | { | |
8ce0e732 SA |
1092 | // If there were stack changes, the session has been destroyed by now, so if |
1093 | // it hasn't been destroyed, we can just reset and re-use it | |
ce0b6a40 GS |
1094 | if (srd_session_) { |
1095 | // When a decoder stack was created before, re-use it | |
1096 | // for the next stream of input data, after terminating | |
1097 | // potentially still executing operations, and resetting | |
1098 | // internal state. Skip the rather expensive (teardown | |
1099 | // and) construction of another decoder stack. | |
1100 | ||
1101 | // TODO Reduce redundancy, use a common code path for | |
72486b78 | 1102 | // the meta/start sequence? |
ce0b6a40 | 1103 | terminate_srd_session(); |
72486b78 SA |
1104 | |
1105 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1106 | uint64_t samplerate = 0; |
7dd518b8 | 1107 | if (segments_.size() > 0) |
ba5b52e1 GS |
1108 | samplerate = segments_.at(current_segment_id_).samplerate; |
1109 | if (samplerate) | |
7dd518b8 | 1110 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1111 | g_variant_new_uint64(samplerate)); |
72486b78 SA |
1112 | for (const shared_ptr<decode::Decoder> &dec : stack_) |
1113 | dec->apply_all_options(); | |
ce0b6a40 | 1114 | srd_session_start(srd_session_); |
72486b78 | 1115 | |
ce0b6a40 GS |
1116 | return; |
1117 | } | |
e91883bb | 1118 | |
a3ebd556 SA |
1119 | // Create the session |
1120 | srd_session_new(&srd_session_); | |
1121 | assert(srd_session_); | |
47747218 | 1122 | |
a3ebd556 SA |
1123 | // Create the decoders |
1124 | srd_decoder_inst *prev_di = nullptr; | |
1125 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
1126 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); | |
e91883bb | 1127 | |
a3ebd556 | 1128 | if (!di) { |
403c3e87 | 1129 | set_error_message(tr("Failed to create decoder instance")); |
a3ebd556 | 1130 | srd_session_destroy(srd_session_); |
c5f9553c | 1131 | srd_session_ = nullptr; |
a3ebd556 SA |
1132 | return; |
1133 | } | |
e91883bb | 1134 | |
a3ebd556 SA |
1135 | if (prev_di) |
1136 | srd_inst_stack(srd_session_, prev_di, di); | |
e91883bb | 1137 | |
a3ebd556 | 1138 | prev_di = di; |
e91883bb | 1139 | } |
a3ebd556 SA |
1140 | |
1141 | // Start the session | |
7dd518b8 SA |
1142 | if (segments_.size() > 0) |
1143 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1144 | g_variant_new_uint64(segments_.at(current_segment_id_).samplerate)); | |
a3ebd556 SA |
1145 | |
1146 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
1147 | DecodeSignal::annotation_callback, this); | |
1148 | ||
1149 | srd_session_start(srd_session_); | |
e3202557 SA |
1150 | |
1151 | // We just recreated the srd session, so all stack changes are applied now | |
1152 | stack_config_changed_ = false; | |
e91883bb | 1153 | } |
47747218 | 1154 | |
ce0b6a40 GS |
1155 | void DecodeSignal::terminate_srd_session() |
1156 | { | |
1157 | // Call the "terminate and reset" routine for the decoder stack | |
1158 | // (if available). This does not harm those stacks which already | |
1159 | // have completed their operation, and reduces response time for | |
1160 | // those stacks which still are processing data while the | |
1161 | // application no longer wants them to. | |
72486b78 | 1162 | if (srd_session_) { |
ce0b6a40 | 1163 | srd_session_terminate_reset(srd_session_); |
72486b78 SA |
1164 | |
1165 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1166 | uint64_t samplerate = 0; |
7dd518b8 | 1167 | if (segments_.size() > 0) |
ba5b52e1 GS |
1168 | samplerate = segments_.at(current_segment_id_).samplerate; |
1169 | if (samplerate) | |
7dd518b8 | 1170 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1171 | g_variant_new_uint64(samplerate)); |
72486b78 SA |
1172 | for (const shared_ptr<decode::Decoder> &dec : stack_) |
1173 | dec->apply_all_options(); | |
1174 | } | |
ce0b6a40 GS |
1175 | } |
1176 | ||
e91883bb SA |
1177 | void DecodeSignal::stop_srd_session() |
1178 | { | |
1179 | if (srd_session_) { | |
1180 | // Destroy the session | |
1181 | srd_session_destroy(srd_session_); | |
1182 | srd_session_ = nullptr; | |
8ce0e732 SA |
1183 | |
1184 | // Mark the decoder instances as non-existant since they were deleted | |
1185 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
1186 | dec->invalidate_decoder_inst(); | |
e91883bb | 1187 | } |
47747218 SA |
1188 | } |
1189 | ||
a8a9222d SA |
1190 | void DecodeSignal::connect_input_notifiers() |
1191 | { | |
1192 | // Disconnect the notification slot from the previous set of signals | |
1b56c646 | 1193 | disconnect(this, SLOT(on_data_cleared())); |
a8a9222d SA |
1194 | disconnect(this, SLOT(on_data_received())); |
1195 | ||
1196 | // Connect the currently used signals to our slot | |
1197 | for (data::DecodeChannel &ch : channels_) { | |
1198 | if (!ch.assigned_signal) | |
1199 | continue; | |
1200 | ||
1b56c646 SA |
1201 | const data::SignalBase *signal = ch.assigned_signal; |
1202 | connect(signal, SIGNAL(samples_cleared()), | |
1203 | this, SLOT(on_data_cleared())); | |
b10b58f4 | 1204 | connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)), |
a8a9222d SA |
1205 | this, SLOT(on_data_received())); |
1206 | } | |
1207 | } | |
1208 | ||
65efd025 | 1209 | void DecodeSignal::create_decode_segment() |
4913560f | 1210 | { |
65efd025 SA |
1211 | // Create annotation segment |
1212 | segments_.emplace_back(DecodeSegment()); | |
72435789 | 1213 | |
65efd025 SA |
1214 | // Add annotation classes |
1215 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
1216 | assert(dec); | |
1217 | const srd_decoder *const decc = dec->decoder(); | |
1218 | assert(dec->decoder()); | |
4913560f | 1219 | |
f9807084 | 1220 | int row_index = 0; |
65efd025 SA |
1221 | // Add a row for the decoder if it doesn't have a row list |
1222 | if (!decc->annotation_rows) | |
f9807084 | 1223 | (segments_.back().annotation_rows)[Row(row_index++, decc)] = |
65efd025 | 1224 | decode::RowData(); |
4913560f | 1225 | |
65efd025 SA |
1226 | // Add the decoder rows |
1227 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
1228 | const srd_decoder_annotation_row *const ann_row = | |
1229 | (srd_decoder_annotation_row *)l->data; | |
1230 | assert(ann_row); | |
4913560f | 1231 | |
f9807084 | 1232 | const Row row(row_index++, decc, ann_row); |
4913560f | 1233 | |
65efd025 SA |
1234 | // Add a new empty row data object |
1235 | (segments_.back().annotation_rows)[row] = | |
1236 | decode::RowData(); | |
4913560f SA |
1237 | } |
1238 | } | |
1239 | } | |
1240 | ||
47747218 SA |
1241 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) |
1242 | { | |
1243 | assert(pdata); | |
b21501d6 | 1244 | assert(decode_signal); |
47747218 SA |
1245 | |
1246 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1247 | assert(ds); | |
1248 | ||
3b5282a2 SA |
1249 | if (ds->decode_interrupt_) |
1250 | return; | |
1251 | ||
47747218 SA |
1252 | lock_guard<mutex> lock(ds->output_mutex_); |
1253 | ||
47747218 SA |
1254 | // Find the row |
1255 | assert(pdata->pdo); | |
1256 | assert(pdata->pdo->di); | |
1257 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
1258 | assert(decc); | |
1259 | ||
1becee23 UH |
1260 | const srd_proto_data_annotation *const pda = |
1261 | (const srd_proto_data_annotation*)pdata->data; | |
1262 | assert(pda); | |
1263 | ||
8a603e13 | 1264 | auto row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.end(); |
47747218 SA |
1265 | |
1266 | // Try looking up the sub-row of this class | |
1becee23 UH |
1267 | const auto format = pda->ann_class; |
1268 | const auto r = ds->class_rows_.find(make_pair(decc, format)); | |
47747218 | 1269 | if (r != ds->class_rows_.end()) |
8a603e13 | 1270 | row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find((*r).second); |
47747218 SA |
1271 | else { |
1272 | // Failing that, use the decoder as a key | |
f9807084 | 1273 | row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find(Row(0, decc)); |
47747218 SA |
1274 | } |
1275 | ||
8a603e13 | 1276 | if (row_iter == ds->segments_.at(ds->current_segment_id_).annotation_rows.end()) { |
47747218 | 1277 | qDebug() << "Unexpected annotation: decoder = " << decc << |
1becee23 | 1278 | ", format = " << format; |
47747218 SA |
1279 | assert(false); |
1280 | return; | |
1281 | } | |
1282 | ||
1283 | // Add the annotation | |
5a914348 | 1284 | (*row_iter).second.emplace_annotation(pdata, &((*row_iter).first)); |
47747218 SA |
1285 | } |
1286 | ||
1287 | void DecodeSignal::on_capture_state_changed(int state) | |
1288 | { | |
1289 | // If a new acquisition was started, we need to start decoding from scratch | |
ba5f2186 SA |
1290 | if (state == Session::Running) { |
1291 | logic_mux_data_invalid_ = true; | |
47747218 | 1292 | begin_decode(); |
ba5f2186 | 1293 | } |
47747218 SA |
1294 | } |
1295 | ||
1b56c646 SA |
1296 | void DecodeSignal::on_data_cleared() |
1297 | { | |
1298 | reset_decode(); | |
1299 | } | |
1300 | ||
47747218 SA |
1301 | void DecodeSignal::on_data_received() |
1302 | { | |
5620539c SA |
1303 | // If we detected a lack of input data when trying to start decoding, |
1304 | // we have set an error message. Only try again if we now have data | |
1305 | // to work with | |
1306 | if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0)) | |
1307 | return; | |
1308 | ||
1b56c646 SA |
1309 | if (!logic_mux_thread_.joinable()) |
1310 | begin_decode(); | |
1311 | else | |
1312 | logic_mux_cond_.notify_one(); | |
47747218 SA |
1313 | } |
1314 | ||
ad908057 SA |
1315 | } // namespace data |
1316 | } // namespace pv |