]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
Continue work on SRD_OUTPUT_LOGIC
[pulseview.git] / pv / data / decodesignal.cpp
CommitLineData
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
c0c3e62a 20#include <cstring>
5a914348 21#include <forward_list>
0c5fe73e
SA
22#include <limits>
23
47747218
SA
24#include <QDebug>
25
ad908057
SA
26#include "logic.hpp"
27#include "logicsegment.hpp"
28#include "decodesignal.hpp"
29#include "signaldata.hpp"
30
ad908057 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
04b04675 36using std::dynamic_pointer_cast;
47747218 37using std::lock_guard;
ad908057 38using std::make_shared;
0c5fe73e 39using std::min;
5ecf957f 40using std::out_of_range;
ad908057 41using std::shared_ptr;
47747218 42using std::unique_lock;
6a26fc44 43using pv::data::decode::AnnotationClass;
a82325d1 44using pv::data::decode::DecodeChannel;
ad908057
SA
45
46namespace pv {
47namespace data {
48
47747218
SA
49const double DecodeSignal::DecodeMargin = 1.0;
50const double DecodeSignal::DecodeThreshold = 0.2;
3fbddf7f 51const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
ad908057 52
ad908057 53
47747218
SA
54DecodeSignal::DecodeSignal(pv::Session &session) :
55 SignalBase(nullptr, SignalBase::DecodeChannel),
56 session_(session),
e91883bb 57 srd_session_(nullptr),
47747218 58 logic_mux_data_invalid_(false),
e3202557 59 stack_config_changed_(true),
f6a93932 60 current_segment_id_(0)
ad908057 61{
47747218
SA
62 connect(&session_, SIGNAL(capture_state_changed(int)),
63 this, SLOT(on_capture_state_changed(int)));
ad908057
SA
64}
65
47747218 66DecodeSignal::~DecodeSignal()
ad908057 67{
8ce0e732 68 reset_decode(true);
ad908057
SA
69}
70
47747218 71const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
ecd07c20 72{
47747218 73 return stack_;
ecd07c20
SA
74}
75
ed75426f 76void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode)
ad908057
SA
77{
78 assert(decoder);
762ab7a4 79
b9591f3a 80 // Set name if this decoder is the first in the list or the name is unchanged
6a26fc44
SA
81 const srd_decoder* prev_dec = stack_.empty() ? nullptr : stack_.back()->get_srd_decoder();
82 const QString prev_dec_name = prev_dec ? QString::fromUtf8(prev_dec->name) : QString();
47747218 83
b9591f3a 84 if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name)))
47747218 85 set_name(QString::fromUtf8(decoder->name));
132a5c6d 86
f54e68b0 87 const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder, stack_.size());
b9591f3a
SA
88 stack_.push_back(dec);
89
02078aa1
SA
90 connect(dec.get(), SIGNAL(annotation_visibility_changed()),
91 this, SLOT(on_annotation_visibility_changed()));
92
27a3f09b 93 // Include the newly created decode channels in the channel lists
9f97b357 94 update_channel_list();
132a5c6d 95
e3202557 96 stack_config_changed_ = true;
762ab7a4 97 auto_assign_signals(dec);
27a3f09b 98 commit_decoder_channels();
04b04675 99 update_output_signals();
e77de61f
SA
100
101 decoder_stacked((void*)dec.get());
102
ed75426f
SA
103 if (restart_decode)
104 begin_decode();
ad908057
SA
105}
106
107void DecodeSignal::remove_decoder(int index)
108{
47747218
SA
109 assert(index >= 0);
110 assert(index < (int)stack_.size());
111
112 // Find the decoder in the stack
f9a0fd83
SA
113 auto iter = stack_.begin() + index;
114 assert(iter != stack_.end());
47747218 115
f9a0fd83
SA
116 shared_ptr<Decoder> dec = *iter;
117
118 decoder_removed(dec.get());
e77de61f 119
47747218
SA
120 // Delete the element
121 stack_.erase(iter);
122
123 // Update channels and decoded data
e3202557 124 stack_config_changed_ = true;
9f97b357 125 update_channel_list();
47747218 126 begin_decode();
ad908057
SA
127}
128
129bool DecodeSignal::toggle_decoder_visibility(int index)
130{
47747218 131 auto iter = stack_.cbegin();
ad908057 132 for (int i = 0; i < index; i++, iter++)
47747218 133 assert(iter != stack_.end());
ad908057
SA
134
135 shared_ptr<Decoder> dec = *iter;
136
137 // Toggle decoder visibility
138 bool state = false;
139 if (dec) {
5d3ca591
SA
140 state = !dec->visible();
141 dec->set_visible(state);
ad908057
SA
142 }
143
144 return state;
145}
146
8ce0e732 147void DecodeSignal::reset_decode(bool shutting_down)
47747218 148{
8b24af15
SA
149 resume_decode(); // Make sure the decode thread isn't blocked by pausing
150
8ce0e732 151 if (stack_config_changed_ || shutting_down)
e3202557
SA
152 stop_srd_session();
153 else
154 terminate_srd_session();
ce0b6a40 155
1b56c646
SA
156 if (decode_thread_.joinable()) {
157 decode_interrupt_ = true;
158 decode_input_cond_.notify_one();
159 decode_thread_.join();
160 }
161
162 if (logic_mux_thread_.joinable()) {
163 logic_mux_interrupt_ = true;
164 logic_mux_cond_.notify_one();
165 logic_mux_thread_.join();
166 }
167
8a603e13 168 current_segment_id_ = 0;
72435789 169 segments_.clear();
1b56c646 170
88304160
SA
171 for (const shared_ptr<decode::Decoder>& dec : stack_)
172 if (dec->has_logic_output())
173 output_logic_[dec->get_srd_decoder()]->clear();
174
1b56c646
SA
175 logic_mux_data_.reset();
176 logic_mux_data_invalid_ = true;
eee3eab9 177
403c3e87 178 if (!error_message_.isEmpty()) {
66b6f41b 179 error_message_.clear();
fe060a48
SA
180 // TODO Emulate noquote()
181 qDebug().nospace() << name() << ": Error cleared";
403c3e87 182 }
72435789 183
eee3eab9 184 decode_reset();
47747218
SA
185}
186
946b52e1
SA
187void DecodeSignal::begin_decode()
188{
47747218
SA
189 if (decode_thread_.joinable()) {
190 decode_interrupt_ = true;
191 decode_input_cond_.notify_one();
192 decode_thread_.join();
193 }
194
195 if (logic_mux_thread_.joinable()) {
196 logic_mux_interrupt_ = true;
197 logic_mux_cond_.notify_one();
198 logic_mux_thread_.join();
199 }
200
201 reset_decode();
202
27a3f09b 203 if (stack_.size() == 0) {
403c3e87 204 set_error_message(tr("No decoders"));
27a3f09b
SA
205 return;
206 }
207
208 assert(channels_.size() > 0);
209
210 if (get_assigned_signal_count() == 0) {
403c3e87 211 set_error_message(tr("There are no channels assigned to this decoder"));
27a3f09b
SA
212 return;
213 }
214
04627946
SA
215 // Make sure that all assigned channels still provide logic data
216 // (can happen when a converted signal was assigned but the
217 // conversion removed in the meanwhile)
a82325d1 218 for (decode::DecodeChannel& ch : channels_)
04627946
SA
219 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
220 ch.assigned_signal = nullptr;
221
47747218 222 // Check that all decoders have the required channels
6a26fc44 223 for (const shared_ptr<Decoder>& dec : stack_)
47747218 224 if (!dec->have_required_channels()) {
403c3e87
SA
225 set_error_message(tr("One or more required channels "
226 "have not been specified"));
47747218
SA
227 return;
228 }
229
27a3f09b
SA
230 // Free the logic data and its segment(s) if it needs to be updated
231 if (logic_mux_data_invalid_)
232 logic_mux_data_.reset();
233
234 if (!logic_mux_data_) {
72435789 235 const uint32_t ch_count = get_assigned_signal_count();
ed535cd7 236 logic_mux_unit_size_ = (ch_count + 7) / 8;
27a3f09b 237 logic_mux_data_ = make_shared<Logic>(ch_count);
27a3f09b 238 }
47747218 239
144e72c9 240 if (get_input_segment_count() == 0)
403c3e87 241 set_error_message(tr("No input data"));
ed535cd7 242
27a3f09b
SA
243 // Make sure the logic output data is complete and up-to-date
244 logic_mux_interrupt_ = false;
245 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
246
247 // Decode the muxed logic data
47747218
SA
248 decode_interrupt_ = false;
249 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
946b52e1
SA
250}
251
556259d2
SA
252void DecodeSignal::pause_decode()
253{
254 decode_paused_ = true;
255}
256
257void DecodeSignal::resume_decode()
258{
259 // Manual unlocking is done before notifying, to avoid waking up the
260 // waiting thread only to block again (see notify_one for details)
261 decode_pause_mutex_.unlock();
262 decode_pause_cond_.notify_one();
263 decode_paused_ = false;
264}
265
266bool DecodeSignal::is_paused() const
267{
268 return decode_paused_;
269}
270
a82325d1 271const vector<decode::DecodeChannel> DecodeSignal::get_channels() const
9f97b357
SA
272{
273 return channels_;
274}
275
762ab7a4 276void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
132a5c6d 277{
47747218
SA
278 bool new_assignment = false;
279
db51130d
SA
280 // Disconnect all input signal notifications so we don't have duplicate connections
281 disconnect_input_notifiers();
282
132a5c6d 283 // Try to auto-select channels that don't have signals assigned yet
a82325d1 284 for (decode::DecodeChannel& ch : channels_) {
762ab7a4
SA
285 // If a decoder is given, auto-assign only its channels
286 if (dec && (ch.decoder_ != dec))
287 continue;
288
132a5c6d
SA
289 if (ch.assigned_signal)
290 continue;
291
c526a8fd
SA
292 QString ch_name = ch.name.toLower();
293 ch_name = ch_name.replace(QRegExp("[-_.]"), " ");
a8030bb0
SA
294
295 shared_ptr<data::SignalBase> match;
f4ab4b5c 296 for (const shared_ptr<data::SignalBase>& s : session_.signalbases()) {
f9ebf303
SA
297 if (!s->enabled())
298 continue;
299
c526a8fd
SA
300 QString s_name = s->name().toLower();
301 s_name = s_name.replace(QRegExp("[-_.]"), " ");
692f6093
SA
302
303 if (s->logic_data() &&
304 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
a8030bb0
SA
305 if (!match)
306 match = s;
307 else {
308 // Only replace an existing match if it matches more characters
309 int old_unmatched = ch_name.length() - match->name().length();
310 int new_unmatched = ch_name.length() - s->name().length();
311 if (abs(new_unmatched) < abs(old_unmatched))
312 match = s;
313 }
47747218 314 }
692f6093 315 }
a8030bb0 316
8ff61402
SA
317 // Prevent using a signal more than once as D1 would match e.g. D1 and D10
318 bool signal_not_already_used = true;
319 for (decode::DecodeChannel& ch : channels_)
320 if (ch.assigned_signal && (ch.assigned_signal == match))
321 signal_not_already_used = false;
322
323 if (match && signal_not_already_used) {
cf1541a1 324 ch.assigned_signal = match;
a8030bb0
SA
325 new_assignment = true;
326 }
47747218
SA
327 }
328
329 if (new_assignment) {
db51130d
SA
330 // Receive notifications when new sample data is available
331 connect_input_notifiers();
332
47747218 333 logic_mux_data_invalid_ = true;
e3202557 334 stack_config_changed_ = true;
27a3f09b 335 commit_decoder_channels();
47747218 336 channels_updated();
132a5c6d
SA
337 }
338}
339
cf1541a1 340void DecodeSignal::assign_signal(const uint16_t channel_id, shared_ptr<const SignalBase> signal)
9f97b357 341{
db51130d
SA
342 // Disconnect all input signal notifications so we don't have duplicate connections
343 disconnect_input_notifiers();
344
a82325d1 345 for (decode::DecodeChannel& ch : channels_)
47747218 346 if (ch.id == channel_id) {
9f97b357 347 ch.assigned_signal = signal;
47747218
SA
348 logic_mux_data_invalid_ = true;
349 }
9f97b357 350
db51130d
SA
351 // Receive notifications when new sample data is available
352 connect_input_notifiers();
353
e3202557 354 stack_config_changed_ = true;
27a3f09b 355 commit_decoder_channels();
9f97b357 356 channels_updated();
47747218 357 begin_decode();
9f97b357
SA
358}
359
27a3f09b
SA
360int DecodeSignal::get_assigned_signal_count() const
361{
362 // Count all channels that have a signal assigned to them
363 return count_if(channels_.begin(), channels_.end(),
cf1541a1 364 [](decode::DecodeChannel ch) { return ch.assigned_signal.get(); });
27a3f09b
SA
365}
366
04b04675
SA
367void DecodeSignal::update_output_signals()
368{
369 for (const shared_ptr<decode::Decoder>& dec : stack_) {
370 assert(dec);
371
372 if (dec->has_logic_output()) {
373 const vector<decode::DecoderLogicOutputChannel> logic_channels =
374 dec->logic_output_channels();
375
376 // All signals of a decoder share the same LogicSegment, so it's
30cdef99 377 // sufficient to check for only the first channel
04b04675
SA
378 const decode::DecoderLogicOutputChannel& first_ch = logic_channels[0];
379
380 bool ch_exists = false;
381 for (const shared_ptr<SignalBase>& signal : output_signals_)
382 if (signal->internal_name() == first_ch.id)
383 ch_exists = true;
384
385 if (!ch_exists) {
386 shared_ptr<Logic> logic_data = make_shared<Logic>(logic_channels.size());
88304160 387 logic_data->set_samplerate(get_samplerate());
04b04675
SA
388 output_logic_[dec->get_srd_decoder()] = logic_data;
389
390 shared_ptr<LogicSegment> logic_segment = make_shared<data::LogicSegment>(
88304160 391 *logic_data, 0, (logic_data->num_channels() + 7) / 8, get_samplerate());
04b04675
SA
392 logic_data->push_segment(logic_segment);
393
394 uint index = 0;
395 for (const decode::DecoderLogicOutputChannel& logic_ch : logic_channels) {
396 shared_ptr<data::SignalBase> signal =
397 make_shared<data::SignalBase>(nullptr, LogicChannel);
398 signal->set_internal_name(logic_ch.id);
399 signal->set_name(logic_ch.id);
400 signal->set_index(index);
401 signal->set_data(logic_data);
402 output_signals_.push_back(signal);
403 session_.add_generated_signal(signal);
404 index++;
405 }
88304160
SA
406 } else {
407 shared_ptr<Logic> logic_data = output_logic_[dec->get_srd_decoder()];
408 logic_data->set_samplerate(get_samplerate());
409 for (shared_ptr<LogicSegment>& segment : logic_data->logic_segments())
410 segment->set_samplerate(get_samplerate());
04b04675
SA
411 }
412 }
413 }
414
415 // TODO Delete signals that no longer have a corresponding decoder (also from session)
88304160 416 // TODO Assert that all sample rates are the same as the session's
04b04675
SA
417 // TODO Set colors to the same as the decoder's background color
418}
419
9f97b357
SA
420void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
421{
a82325d1 422 for (decode::DecodeChannel& ch : channels_)
9f97b357
SA
423 if (ch.id == channel_id)
424 ch.initial_pin_state = init_state;
425
e3202557 426 stack_config_changed_ = true;
9f97b357 427 channels_updated();
47747218 428 begin_decode();
9f97b357
SA
429}
430
85125b0f 431double DecodeSignal::get_samplerate() const
ff83d980 432{
72435789
SA
433 double result = 0;
434
435 // TODO For now, we simply return the first samplerate that we have
8a603e13
SA
436 if (segments_.size() > 0)
437 result = segments_.front().samplerate;
72435789
SA
438
439 return result;
ff83d980
SA
440}
441
72435789 442const pv::util::Timestamp DecodeSignal::start_time() const
ff83d980 443{
72435789
SA
444 pv::util::Timestamp result;
445
446 // TODO For now, we simply return the first start time that we have
8a603e13
SA
447 if (segments_.size() > 0)
448 result = segments_.front().start_time;
72435789
SA
449
450 return result;
ff83d980
SA
451}
452
5ecf957f 453int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
0c5fe73e
SA
454{
455 // The working sample count is the highest sample number for
ed535cd7
SA
456 // which all used signals have data available, so go through all
457 // channels and use the lowest overall sample count of the segment
0c5fe73e 458
0c5fe73e
SA
459 int64_t count = std::numeric_limits<int64_t>::max();
460 bool no_signals_assigned = true;
461
a82325d1 462 for (const decode::DecodeChannel& ch : channels_)
0c5fe73e 463 if (ch.assigned_signal) {
1774d669
SA
464 if (!ch.assigned_signal->logic_data())
465 return 0;
466
0c5fe73e
SA
467 no_signals_assigned = false;
468
469 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
1774d669 470 if (logic_data->logic_segments().empty())
0c5fe73e
SA
471 return 0;
472
1774d669 473 if (segment_id >= logic_data->logic_segments().size())
5ecf957f 474 return 0;
1774d669 475
cf1541a1
SA
476 const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr();
477 if (segment)
478 count = min(count, (int64_t)segment->get_sample_count());
0c5fe73e
SA
479 }
480
481 return (no_signals_assigned ? 0 : count);
482}
483
b82908ab
SA
484int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id,
485 bool include_processing) const
ff83d980 486{
47747218 487 lock_guard<mutex> decode_lock(output_mutex_);
5ecf957f
SA
488
489 int64_t result = 0;
490
c8a6db0e
SA
491 if (segment_id >= segments_.size())
492 return result;
493
494 if (include_processing)
495 result = segments_[segment_id].samples_decoded_incl;
496 else
497 result = segments_[segment_id].samples_decoded_excl;
5ecf957f
SA
498
499 return result;
ff83d980
SA
500}
501
6a26fc44 502vector<Row*> DecodeSignal::get_rows(bool visible_only)
ecd07c20 503{
6a26fc44 504 vector<Row*> rows;
47747218 505
6a26fc44 506 for (const shared_ptr<Decoder>& dec : stack_) {
47747218 507 assert(dec);
5d3ca591 508 if (visible_only && !dec->visible())
cbb2e4da
SA
509 continue;
510
6a26fc44
SA
511 for (Row* row : dec->get_rows())
512 rows.push_back(row);
513 }
47747218 514
6a26fc44
SA
515 return rows;
516}
47747218 517
6a26fc44
SA
518vector<const Row*> DecodeSignal::get_rows(bool visible_only) const
519{
520 vector<const Row*> rows;
521
522 for (const shared_ptr<Decoder>& dec : stack_) {
523 assert(dec);
5d3ca591 524 if (visible_only && !dec->visible())
6a26fc44
SA
525 continue;
526
527 for (const Row* row : dec->get_rows())
528 rows.push_back(row);
47747218
SA
529 }
530
531 return rows;
ecd07c20
SA
532}
533
6a26fc44 534uint64_t DecodeSignal::get_annotation_count(const Row* row, uint32_t segment_id) const
f994f496
SA
535{
536 if (segment_id >= segments_.size())
537 return 0;
538
6a26fc44 539 const DecodeSegment* segment = &(segments_.at(segment_id));
f994f496 540
6a26fc44 541 auto row_it = segment->annotation_rows.find(row);
f994f496 542
6a26fc44 543 const RowData* rd;
5a9146ac 544 if (row_it == segment->annotation_rows.end())
6a26fc44 545 return 0;
5a9146ac 546 else
6a26fc44
SA
547 rd = &(row_it->second);
548
549 return rd->get_annotation_count();
f994f496
SA
550}
551
462941e2 552void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
6a26fc44 553 const Row* row, uint32_t segment_id, uint64_t start_sample,
ecd07c20
SA
554 uint64_t end_sample) const
555{
47747218
SA
556 lock_guard<mutex> lock(output_mutex_);
557
c8a6db0e
SA
558 if (segment_id >= segments_.size())
559 return;
72435789 560
6a26fc44
SA
561 const DecodeSegment* segment = &(segments_.at(segment_id));
562
563 auto row_it = segment->annotation_rows.find(row);
c8a6db0e 564
6a26fc44 565 const RowData* rd;
5a9146ac 566 if (row_it == segment->annotation_rows.end())
6a26fc44 567 return;
5a9146ac 568 else
6a26fc44
SA
569 rd = &(row_it->second);
570
571 rd->get_annotation_subset(dest, start_sample, end_sample);
47747218
SA
572}
573
462941e2 574void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
5a914348
SA
575 uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const
576{
462941e2
SA
577 for (const Row* row : get_rows())
578 get_annotation_subset(dest, row, segment_id, start_sample, end_sample);
5a914348
SA
579}
580
e77de61f 581uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id,
ac9494ef 582 const Decoder* dec, uint32_t bin_class_id) const
c0c3e62a 583{
1774d669 584 if ((segments_.size() == 0) || (segment_id >= segments_.size()))
c8a6db0e
SA
585 return 0;
586
1774d669 587 const DecodeSegment *segment = &(segments_[segment_id]);
e77de61f 588
1774d669
SA
589 for (const DecodeBinaryClass& bc : segment->binary_classes)
590 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
591 return bc.chunks.size();
c0c3e62a 592
e77de61f 593 return 0;
c0c3e62a
SA
594}
595
e77de61f 596void DecodeSignal::get_binary_data_chunk(uint32_t segment_id,
ac9494ef 597 const Decoder* dec, uint32_t bin_class_id, uint32_t chunk_id,
c0c3e62a
SA
598 const vector<uint8_t> **dest, uint64_t *size)
599{
1774d669
SA
600 if (segment_id >= segments_.size())
601 return;
e77de61f 602
1774d669
SA
603 const DecodeSegment *segment = &(segments_[segment_id]);
604
605 for (const DecodeBinaryClass& bc : segment->binary_classes)
606 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) {
607 if (dest) *dest = &(bc.chunks.at(chunk_id).data);
608 if (size) *size = bc.chunks.at(chunk_id).data.size();
609 return;
610 }
c0c3e62a
SA
611}
612
13b726cd 613void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id,
ac9494ef 614 const Decoder* dec, uint32_t bin_class_id, uint64_t start_sample,
e77de61f 615 uint64_t end_sample, vector<uint8_t> *dest) const
c0c3e62a
SA
616{
617 assert(dest != nullptr);
618
1774d669
SA
619 if (segment_id >= segments_.size())
620 return;
621
622 const DecodeSegment *segment = &(segments_[segment_id]);
623
624 const DecodeBinaryClass* bin_class = nullptr;
625 for (const DecodeBinaryClass& bc : segment->binary_classes)
626 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
627 bin_class = &bc;
628
629 // Determine overall size before copying to resize dest vector only once
630 uint64_t size = 0;
631 uint64_t matches = 0;
632 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
633 if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
634 size += chunk.data.size();
635 matches++;
636 }
637 dest->resize(size);
638
639 uint64_t offset = 0;
640 uint64_t matches2 = 0;
641 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
642 if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
643 memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size());
644 offset += chunk.data.size();
645 matches2++;
646
647 // Make sure we don't overwrite memory if the array grew in the meanwhile
648 if (matches2 == matches)
649 break;
650 }
c0c3e62a
SA
651}
652
13b726cd 653void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id,
6a26fc44
SA
654 const Decoder* dec, uint32_t bin_class_id, uint64_t start, uint64_t end,
655 vector<uint8_t> *dest) const
13b726cd
SA
656{
657 assert(dest != nullptr);
658
1774d669
SA
659 if (segment_id >= segments_.size())
660 return;
13b726cd 661
1774d669
SA
662 const DecodeSegment *segment = &(segments_[segment_id]);
663
664 const DecodeBinaryClass* bin_class = nullptr;
665 for (const DecodeBinaryClass& bc : segment->binary_classes)
666 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
667 bin_class = &bc;
668
669 // Determine overall size before copying to resize dest vector only once
670 uint64_t size = 0;
671 uint64_t offset = 0;
672 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
673 if (offset >= start)
674 size += chunk.data.size();
675 offset += chunk.data.size();
676 if (offset >= end)
677 break;
678 }
679 dest->resize(size);
680
681 offset = 0;
682 uint64_t dest_offset = 0;
683 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
684 if (offset >= start) {
685 memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size());
686 dest_offset += chunk.data.size();
13b726cd 687 }
1774d669
SA
688 offset += chunk.data.size();
689 if (offset >= end)
690 break;
13b726cd
SA
691 }
692}
693
628b45cc 694const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id,
6a26fc44 695 const Decoder* dec, uint32_t bin_class_id) const
628b45cc 696{
1774d669
SA
697 if (segment_id >= segments_.size())
698 return nullptr;
628b45cc 699
1774d669
SA
700 const DecodeSegment *segment = &(segments_[segment_id]);
701
702 for (const DecodeBinaryClass& bc : segment->binary_classes)
703 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
704 return &bc;
628b45cc
SA
705
706 return nullptr;
707}
c0c3e62a 708
f54e68b0
SA
709const deque<const Annotation*>* DecodeSignal::get_all_annotations_by_segment(
710 uint32_t segment_id) const
711{
1774d669
SA
712 if (segment_id >= segments_.size())
713 return nullptr;
f54e68b0 714
1774d669
SA
715 const DecodeSegment *segment = &(segments_[segment_id]);
716
717 return &(segment->all_annotations);
f54e68b0
SA
718}
719
47747218
SA
720void DecodeSignal::save_settings(QSettings &settings) const
721{
722 SignalBase::save_settings(settings);
723
c8e60bdf
SA
724 settings.setValue("decoders", (int)(stack_.size()));
725
8b5ea5a1 726 // Save decoder stack
c8e60bdf 727 int decoder_idx = 0;
6a26fc44 728 for (const shared_ptr<Decoder>& decoder : stack_) {
c8e60bdf
SA
729 settings.beginGroup("decoder" + QString::number(decoder_idx++));
730
6a26fc44 731 settings.setValue("id", decoder->get_srd_decoder()->id);
5d3ca591 732 settings.setValue("visible", decoder->visible());
c8e60bdf 733
d3feec23
SA
734 // Save decoder options
735 const map<string, GVariant*>& options = decoder->options();
736
737 settings.setValue("options", (int)options.size());
738
6a26fc44 739 // Note: Decoder::options() returns only the options
d3feec23
SA
740 // that differ from the default. See binding::Decoder::getter()
741 int i = 0;
f4ab4b5c 742 for (auto& option : options) {
d3feec23
SA
743 settings.beginGroup("option" + QString::number(i));
744 settings.setValue("name", QString::fromStdString(option.first));
745 GlobalSettings::store_gvariant(settings, option.second);
746 settings.endGroup();
747 i++;
748 }
749
e4cb8bcf
SA
750 // Save row properties
751 i = 0;
752 for (const Row* row : decoder->get_rows()) {
753 settings.beginGroup("row" + QString::number(i));
754 settings.setValue("visible", row->visible());
755 settings.endGroup();
756 i++;
757 }
758
759 // Save class properties
760 i = 0;
761 for (const AnnotationClass* ann_class : decoder->ann_classes()) {
762 settings.beginGroup("ann_class" + QString::number(i));
02078aa1 763 settings.setValue("visible", ann_class->visible());
e4cb8bcf
SA
764 settings.endGroup();
765 i++;
766 }
767
c8e60bdf
SA
768 settings.endGroup();
769 }
770
8b5ea5a1
SA
771 // Save channel mapping
772 settings.setValue("channels", (int)channels_.size());
773
774 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
775 auto channel = find_if(channels_.begin(), channels_.end(),
a82325d1 776 [&](decode::DecodeChannel ch) { return ch.id == channel_id; });
8b5ea5a1
SA
777
778 if (channel == channels_.end()) {
779 qDebug() << "ERROR: Gap in channel index:" << channel_id;
780 continue;
781 }
782
783 settings.beginGroup("channel" + QString::number(channel_id));
784
785 settings.setValue("name", channel->name); // Useful for debugging
786 settings.setValue("initial_pin_state", channel->initial_pin_state);
787
788 if (channel->assigned_signal)
789 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
790
791 settings.endGroup();
792 }
04b04675
SA
793
794 // TODO Save logic output signal settings
47747218
SA
795}
796
797void DecodeSignal::restore_settings(QSettings &settings)
798{
799 SignalBase::restore_settings(settings);
800
8b5ea5a1 801 // Restore decoder stack
c8e60bdf
SA
802 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
803
804 int decoders = settings.value("decoders").toInt();
805
806 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
807 settings.beginGroup("decoder" + QString::number(decoder_idx));
808
809 QString id = settings.value("id").toString();
810
811 for (GSList *entry = dec_list; entry; entry = entry->next) {
812 const srd_decoder *dec = (srd_decoder*)entry->data;
813 if (!dec)
814 continue;
815
816 if (QString::fromUtf8(dec->id) == id) {
f54e68b0 817 shared_ptr<Decoder> decoder = make_shared<Decoder>(dec, stack_.size());
d3feec23 818
02078aa1
SA
819 connect(decoder.get(), SIGNAL(annotation_visibility_changed()),
820 this, SLOT(on_annotation_visibility_changed()));
821
d3feec23 822 stack_.push_back(decoder);
5d3ca591 823 decoder->set_visible(settings.value("visible", true).toBool());
d3feec23
SA
824
825 // Restore decoder options that differ from their default
826 int options = settings.value("options").toInt();
827
828 for (int i = 0; i < options; i++) {
829 settings.beginGroup("option" + QString::number(i));
830 QString name = settings.value("name").toString();
831 GVariant *value = GlobalSettings::restore_gvariant(settings);
832 decoder->set_option(name.toUtf8(), value);
833 settings.endGroup();
834 }
c8e60bdf
SA
835
836 // Include the newly created decode channels in the channel lists
837 update_channel_list();
e4cb8bcf
SA
838
839 // Restore row properties
840 int i = 0;
841 for (Row* row : decoder->get_rows()) {
842 settings.beginGroup("row" + QString::number(i));
843 row->set_visible(settings.value("visible", true).toBool());
844 settings.endGroup();
845 i++;
846 }
847
848 // Restore class properties
849 i = 0;
850 for (AnnotationClass* ann_class : decoder->ann_classes()) {
851 settings.beginGroup("ann_class" + QString::number(i));
02078aa1 852 ann_class->set_visible(settings.value("visible", true).toBool());
e4cb8bcf
SA
853 settings.endGroup();
854 i++;
855 }
856
c8e60bdf
SA
857 break;
858 }
859 }
860
861 settings.endGroup();
4ca06ec3 862 channels_updated();
c8e60bdf
SA
863 }
864
8b5ea5a1
SA
865 // Restore channel mapping
866 unsigned int channels = settings.value("channels").toInt();
867
56c4de9a 868 const vector< shared_ptr<data::SignalBase> > signalbases =
8b5ea5a1
SA
869 session_.signalbases();
870
871 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
872 auto channel = find_if(channels_.begin(), channels_.end(),
a82325d1 873 [&](decode::DecodeChannel ch) { return ch.id == channel_id; });
8b5ea5a1
SA
874
875 if (channel == channels_.end()) {
876 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
877 continue;
878 }
879
880 settings.beginGroup("channel" + QString::number(channel_id));
881
882 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
883
f4ab4b5c 884 for (const shared_ptr<data::SignalBase>& signal : signalbases)
2cc02460 885 if ((signal->name() == assigned_signal_name) && (signal->type() != SignalBase::DecodeChannel))
cf1541a1 886 channel->assigned_signal = signal;
8b5ea5a1
SA
887
888 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
889
890 settings.endGroup();
891 }
892
d023660f
SA
893 connect_input_notifiers();
894
aa71890d 895 // Update the internal structures
e3202557 896 stack_config_changed_ = true;
aa71890d
SA
897 update_channel_list();
898 commit_decoder_channels();
04b04675
SA
899 update_output_signals();
900
901 // TODO Restore logic output signal settings
aa71890d 902
8b5ea5a1 903 begin_decode();
47747218
SA
904}
905
144e72c9
SA
906bool DecodeSignal::all_input_segments_complete(uint32_t segment_id) const
907{
908 bool all_complete = true;
909
910 for (const decode::DecodeChannel& ch : channels_)
911 if (ch.assigned_signal) {
912 if (!ch.assigned_signal->logic_data())
913 continue;
914
915 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
916 if (logic_data->logic_segments().empty())
917 return false;
918
919 if (segment_id >= logic_data->logic_segments().size())
920 return false;
921
cf1541a1
SA
922 const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr();
923 if (segment && !segment->is_complete())
144e72c9
SA
924 all_complete = false;
925 }
926
927 return all_complete;
928}
929
8a603e13
SA
930uint32_t DecodeSignal::get_input_segment_count() const
931{
932 uint64_t count = std::numeric_limits<uint64_t>::max();
933 bool no_signals_assigned = true;
934
a82325d1 935 for (const decode::DecodeChannel& ch : channels_)
8a603e13
SA
936 if (ch.assigned_signal) {
937 no_signals_assigned = false;
938
939 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
940 if (!logic_data || logic_data->logic_segments().empty())
941 return 0;
942
943 // Find the min value of all segment counts
944 if ((uint64_t)(logic_data->logic_segments().size()) < count)
945 count = logic_data->logic_segments().size();
946 }
947
948 return (no_signals_assigned ? 0 : count);
949}
950
2a12830c 951double DecodeSignal::get_input_samplerate(uint32_t segment_id) const
8a603e13
SA
952{
953 double samplerate = 0;
954
a82325d1 955 for (const decode::DecodeChannel& ch : channels_)
8a603e13
SA
956 if (ch.assigned_signal) {
957 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
958 if (!logic_data || logic_data->logic_segments().empty())
959 continue;
960
961 try {
cf1541a1
SA
962 const shared_ptr<const LogicSegment> segment =
963 logic_data->logic_segments().at(segment_id)->get_shared_ptr();
964 if (segment)
965 samplerate = segment->samplerate();
30677c13 966 } catch (out_of_range&) {
8a603e13
SA
967 // Do nothing
968 }
969 break;
970 }
971
972 return samplerate;
973}
974
6a26fc44
SA
975Decoder* DecodeSignal::get_decoder_by_instance(const srd_decoder *const srd_dec)
976{
977 for (shared_ptr<Decoder>& d : stack_)
978 if (d->get_srd_decoder() == srd_dec)
979 return d.get();
980
981 return nullptr;
982}
983
9f97b357
SA
984void DecodeSignal::update_channel_list()
985{
a82325d1 986 vector<decode::DecodeChannel> prev_channels = channels_;
9f97b357
SA
987 channels_.clear();
988
989 uint16_t id = 0;
990
991 // Copy existing entries, create new as needed
f4ab4b5c 992 for (shared_ptr<Decoder>& decoder : stack_) {
6a26fc44 993 const srd_decoder* srd_dec = decoder->get_srd_decoder();
9f97b357
SA
994 const GSList *l;
995
996 // Mandatory channels
6a26fc44 997 for (l = srd_dec->channels; l; l = l->next) {
9f97b357
SA
998 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
999 bool ch_added = false;
1000
1001 // Copy but update ID if this channel was in the list before
a82325d1 1002 for (decode::DecodeChannel& ch : prev_channels)
9f97b357
SA
1003 if (ch.pdch_ == pdch) {
1004 ch.id = id++;
1005 channels_.push_back(ch);
1006 ch_added = true;
1007 break;
1008 }
1009
1010 if (!ch_added) {
1011 // Create new entry without a mapped signal
a82325d1 1012 decode::DecodeChannel ch = {id++, 0, false, nullptr,
9f97b357
SA
1013 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
1014 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
1015 channels_.push_back(ch);
1016 }
1017 }
1018
1019 // Optional channels
6a26fc44 1020 for (l = srd_dec->opt_channels; l; l = l->next) {
9f97b357
SA
1021 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
1022 bool ch_added = false;
1023
1024 // Copy but update ID if this channel was in the list before
a82325d1 1025 for (decode::DecodeChannel& ch : prev_channels)
9f97b357
SA
1026 if (ch.pdch_ == pdch) {
1027 ch.id = id++;
1028 channels_.push_back(ch);
1029 ch_added = true;
1030 break;
1031 }
1032
1033 if (!ch_added) {
1034 // Create new entry without a mapped signal
a82325d1 1035 decode::DecodeChannel ch = {id++, 0, true, nullptr,
9f97b357
SA
1036 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
1037 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
1038 channels_.push_back(ch);
1039 }
1040 }
1041 }
1042
47747218
SA
1043 // Invalidate the logic output data if the channel assignment changed
1044 if (prev_channels.size() != channels_.size()) {
1045 // The number of channels changed, there's definitely a difference
1046 logic_mux_data_invalid_ = true;
1047 } else {
1048 // Same number but assignment may still differ, so compare all channels
1049 for (size_t i = 0; i < channels_.size(); i++) {
a82325d1
SA
1050 const decode::DecodeChannel& p_ch = prev_channels[i];
1051 const decode::DecodeChannel& ch = channels_[i];
47747218
SA
1052
1053 if ((p_ch.pdch_ != ch.pdch_) ||
1054 (p_ch.assigned_signal != ch.assigned_signal)) {
1055 logic_mux_data_invalid_ = true;
1056 break;
1057 }
1058 }
1059
1060 }
1061
9f97b357
SA
1062 channels_updated();
1063}
1064
27a3f09b 1065void DecodeSignal::commit_decoder_channels()
ad908057 1066{
27a3f09b 1067 // Submit channel list to every decoder, containing only the relevant channels
6a26fc44 1068 for (shared_ptr<Decoder> dec : stack_) {
a82325d1 1069 vector<decode::DecodeChannel*> channel_list;
27a3f09b 1070
a82325d1 1071 for (decode::DecodeChannel& ch : channels_)
27a3f09b
SA
1072 if (ch.decoder_ == dec)
1073 channel_list.push_back(&ch);
47747218 1074
27a3f09b
SA
1075 dec->set_channels(channel_list);
1076 }
aa71890d
SA
1077
1078 // Channel bit IDs must be in sync with the channel's apperance in channels_
1079 int id = 0;
a82325d1 1080 for (decode::DecodeChannel& ch : channels_)
aa71890d
SA
1081 if (ch.assigned_signal)
1082 ch.bit_id = id++;
47747218
SA
1083}
1084
ed535cd7 1085void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end)
47747218 1086{
27a3f09b
SA
1087 // Enforce end to be greater than start
1088 if (end <= start)
1089 return;
1090
ed535cd7 1091 // Fetch the channel segments and their data
cf1541a1 1092 vector<shared_ptr<const LogicSegment> > segments;
27a3f09b
SA
1093 vector<const uint8_t*> signal_data;
1094 vector<uint8_t> signal_in_bytepos;
1095 vector<uint8_t> signal_in_bitpos;
1096
a82325d1 1097 for (decode::DecodeChannel& ch : channels_)
27a3f09b
SA
1098 if (ch.assigned_signal) {
1099 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
ed535cd7 1100
cf1541a1 1101 shared_ptr<const LogicSegment> segment;
f5a26d5e 1102 if (segment_id < logic_data->logic_segments().size()) {
cf1541a1 1103 segment = logic_data->logic_segments().at(segment_id)->get_shared_ptr();
f5a26d5e 1104 } else {
ed535cd7
SA
1105 qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \
1106 << "has no logic segment" << segment_id;
cf1541a1 1107 logic_mux_interrupt_ = true;
ed535cd7
SA
1108 return;
1109 }
cf1541a1 1110
f5a26d5e
SA
1111 if (!segment)
1112 return;
cf1541a1 1113
27a3f09b 1114 segments.push_back(segment);
b82243f7
SA
1115
1116 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
1117 segment->get_samples(start, end, data);
1118 signal_data.push_back(data);
27a3f09b
SA
1119
1120 const int bitpos = ch.assigned_signal->logic_bit_index();
1121 signal_in_bytepos.push_back(bitpos / 8);
1122 signal_in_bitpos.push_back(bitpos % 8);
1123 }
1124
ed535cd7
SA
1125 shared_ptr<LogicSegment> output_segment;
1126 try {
1127 output_segment = logic_mux_data_->logic_segments().at(segment_id);
30677c13 1128 } catch (out_of_range&) {
ed535cd7
SA
1129 qDebug() << "Muxer error for" << name() << ": no logic mux segment" \
1130 << segment_id << "in mux_logic_samples(), mux segments size is" \
1131 << logic_mux_data_->logic_segments().size();
cf1541a1 1132 logic_mux_interrupt_ = true;
ed535cd7
SA
1133 return;
1134 }
1135
27a3f09b 1136 // Perform the muxing of signal data into the output data
8a603e13 1137 uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()];
27a3f09b
SA
1138 unsigned int signal_count = signal_data.size();
1139
3b5282a2
SA
1140 for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start));
1141 sample_cnt++) {
1142
27a3f09b
SA
1143 int bitpos = 0;
1144 uint8_t bytepos = 0;
1145
8a603e13
SA
1146 const int out_sample_pos = sample_cnt * output_segment->unit_size();
1147 for (unsigned int i = 0; i < output_segment->unit_size(); i++)
27a3f09b 1148 output[out_sample_pos + i] = 0;
47747218 1149
27a3f09b
SA
1150 for (unsigned int i = 0; i < signal_count; i++) {
1151 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
1152 const uint8_t in_sample = 1 &
1153 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
47747218 1154
27a3f09b
SA
1155 const uint8_t out_sample = output[out_sample_pos + bytepos];
1156
1157 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
1158
1159 bitpos++;
1160 if (bitpos > 7) {
1161 bitpos = 0;
1162 bytepos++;
1163 }
1164 }
47747218
SA
1165 }
1166
8a603e13 1167 output_segment->append_payload(output, (end - start) * output_segment->unit_size());
27a3f09b
SA
1168 delete[] output;
1169
1170 for (const uint8_t* data : signal_data)
1171 delete[] data;
1172}
1173
1174void DecodeSignal::logic_mux_proc()
1175{
144e72c9
SA
1176 uint32_t input_segment_count;
1177 do {
1178 input_segment_count = get_input_segment_count();
1179 if (input_segment_count == 0) {
1180 // Wait for input data
1181 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1182 logic_mux_cond_.wait(logic_mux_lock);
1183 }
1184 } while ((!logic_mux_interrupt_) && (input_segment_count == 0));
1185
1186 if (logic_mux_interrupt_)
1187 return;
ed535cd7 1188
8a603e13
SA
1189 assert(logic_mux_data_);
1190
144e72c9
SA
1191 uint32_t segment_id = 0;
1192
65efd025
SA
1193 // Create initial logic mux segment
1194 shared_ptr<LogicSegment> output_segment =
cf1541a1 1195 make_shared<LogicSegment>(*logic_mux_data_, segment_id, logic_mux_unit_size_, 0);
65efd025 1196 logic_mux_data_->push_segment(output_segment);
8a603e13
SA
1197
1198 output_segment->set_samplerate(get_input_samplerate(0));
ed535cd7 1199
144e72c9
SA
1200 // Logic mux data is being updated
1201 logic_mux_data_invalid_ = false;
1202
1203 uint64_t samples_to_process;
27a3f09b 1204 do {
144e72c9
SA
1205 do {
1206 const uint64_t input_sample_count = get_working_sample_count(segment_id);
1207 const uint64_t output_sample_count = output_segment->get_sample_count();
27a3f09b 1208
144e72c9
SA
1209 samples_to_process =
1210 (input_sample_count > output_sample_count) ?
1211 (input_sample_count - output_sample_count) : 0;
ed535cd7 1212
144e72c9
SA
1213 if (samples_to_process > 0) {
1214 const uint64_t unit_size = output_segment->unit_size();
1215 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
ba5f2186 1216
144e72c9
SA
1217 uint64_t processed_samples = 0;
1218 do {
1219 const uint64_t start_sample = output_sample_count + processed_samples;
1220 const uint64_t sample_count =
1221 min(samples_to_process - processed_samples, chunk_sample_count);
1222
1223 mux_logic_samples(segment_id, start_sample, start_sample + sample_count);
1224 processed_samples += sample_count;
1225
1226 // ...and process the newly muxed logic data
1227 decode_input_cond_.notify_one();
1228 } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process));
1229 }
1230 } while (!logic_mux_interrupt_ && (samples_to_process > 0));
1231
1232 if (!logic_mux_interrupt_) {
1233 // samples_to_process is now 0, we've exhausted the currently available input data
1234
1235 // If the input segments are complete, we've completed this segment
1236 if (all_input_segments_complete(segment_id)) {
1237 if (!output_segment->is_complete())
1238 output_segment->set_complete();
1239
1240 if (segment_id < get_input_segment_count() - 1) {
db51130d 1241
144e72c9
SA
1242 // Process next segment
1243 segment_id++;
ed535cd7 1244
144e72c9
SA
1245 output_segment =
1246 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
1247 logic_mux_unit_size_, 0);
1248 logic_mux_data_->push_segment(output_segment);
1249
1250 output_segment->set_samplerate(get_input_samplerate(segment_id));
9431e2d3
SA
1251 } else {
1252 // Wait for more input data if we're processing the currently last segment
1253 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1254 logic_mux_cond_.wait(logic_mux_lock);
144e72c9 1255 }
db51130d
SA
1256 } else {
1257 // Input segments aren't all complete yet but samples_to_process is 0, wait for more input data
1258 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1259 logic_mux_cond_.wait(logic_mux_lock);
69e95096 1260 }
27a3f09b 1261 }
1ec191ed 1262 } while (!logic_mux_interrupt_);
47747218
SA
1263}
1264
1265void DecodeSignal::decode_data(
8a603e13 1266 const int64_t abs_start_samplenum, const int64_t sample_count,
cf1541a1 1267 const shared_ptr<const LogicSegment> input_segment)
47747218 1268{
8a603e13 1269 const int64_t unit_size = input_segment->unit_size();
27a3f09b 1270 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
47747218
SA
1271
1272 for (int64_t i = abs_start_samplenum;
69e95096 1273 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
47747218
SA
1274 i += chunk_sample_count) {
1275
1276 const int64_t chunk_end = min(i + chunk_sample_count,
1277 abs_start_samplenum + sample_count);
1278
e06cf18d
SA
1279 {
1280 lock_guard<mutex> lock(output_mutex_);
b82908ab
SA
1281 // Update the sample count showing the samples including currently processed ones
1282 segments_.at(current_segment_id_).samples_decoded_incl = chunk_end;
e06cf18d
SA
1283 }
1284
b82243f7
SA
1285 int64_t data_size = (chunk_end - i) * unit_size;
1286 uint8_t* chunk = new uint8_t[data_size];
8a603e13 1287 input_segment->get_samples(i, chunk_end, chunk);
47747218 1288
e91883bb 1289 if (srd_session_send(srd_session_, i, chunk_end, chunk,
144e72c9 1290 data_size, unit_size) != SRD_OK) {
403c3e87 1291 set_error_message(tr("Decoder reported an error"));
144e72c9
SA
1292 decode_interrupt_ = true;
1293 }
a3ebd556 1294
47747218
SA
1295 delete[] chunk;
1296
b82908ab
SA
1297 {
1298 lock_guard<mutex> lock(output_mutex_);
1299 // Now that all samples are processed, the exclusive sample count catches up
1300 segments_.at(current_segment_id_).samples_decoded_excl = chunk_end;
1301 }
1302
1b56c646
SA
1303 // Notify the frontend that we processed some data and
1304 // possibly have new annotations as well
1305 new_annotations();
556259d2
SA
1306
1307 if (decode_paused_) {
1308 unique_lock<mutex> pause_wait_lock(decode_pause_mutex_);
1309 decode_pause_cond_.wait(pause_wait_lock);
1310 }
47747218
SA
1311 }
1312}
1313
1314void DecodeSignal::decode_proc()
1315{
65efd025
SA
1316 current_segment_id_ = 0;
1317
1318 // If there is no input data available yet, wait until it is or we're interrupted
144e72c9
SA
1319 do {
1320 if (logic_mux_data_->logic_segments().size() == 0) {
1321 // Wait for input data
1322 unique_lock<mutex> input_wait_lock(input_mutex_);
1323 decode_input_cond_.wait(input_wait_lock);
1324 }
1325 } while ((!decode_interrupt_) && (logic_mux_data_->logic_segments().size() == 0));
a3ebd556
SA
1326
1327 if (decode_interrupt_)
1328 return;
1329
cf1541a1
SA
1330 shared_ptr<const LogicSegment> input_segment = logic_mux_data_->logic_segments().front()->get_shared_ptr();
1331 if (!input_segment)
1332 return;
65efd025
SA
1333
1334 // Create the initial segment and set its sample rate so that we can pass it to SRD
1335 create_decode_segment();
8a603e13 1336 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
65efd025 1337 segments_.at(current_segment_id_).start_time = input_segment->start_time();
8a603e13 1338
65efd025
SA
1339 start_srd_session();
1340
144e72c9 1341 uint64_t samples_to_process = 0;
27a3f09b 1342 uint64_t abs_start_samplenum = 0;
47747218 1343 do {
27a3f09b
SA
1344 // Keep processing new samples until we exhaust the input data
1345 do {
144e72c9 1346 samples_to_process = input_segment->get_sample_count() - abs_start_samplenum;
27a3f09b 1347
144e72c9
SA
1348 if (samples_to_process > 0) {
1349 decode_data(abs_start_samplenum, samples_to_process, input_segment);
1350 abs_start_samplenum += samples_to_process;
27a3f09b 1351 }
144e72c9
SA
1352 } while (!decode_interrupt_ && (samples_to_process > 0));
1353
1354 if (!decode_interrupt_) {
1355 // samples_to_process is now 0, we've exhausted the currently available input data
1356
1357 // If the input segment is complete, we've exhausted this segment
1358 if (input_segment->is_complete()) {
69e95096 1359 if (current_segment_id_ < (logic_mux_data_->logic_segments().size() - 1)) {
144e72c9
SA
1360 // Process next segment
1361 current_segment_id_++;
1362
1363 try {
1364 input_segment = logic_mux_data_->logic_segments().at(current_segment_id_);
1365 } catch (out_of_range&) {
1366 qDebug() << "Decode error for" << name() << ": no logic mux segment" \
1367 << current_segment_id_ << "in decode_proc(), mux segments size is" \
1368 << logic_mux_data_->logic_segments().size();
1369 decode_interrupt_ = true;
1370 return;
1371 }
1372 abs_start_samplenum = 0;
1373
1374 // Create the next segment and set its metadata
1375 create_decode_segment();
1376 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
1377 segments_.at(current_segment_id_).start_time = input_segment->start_time();
1378
1379 // Reset decoder state but keep the decoder stack intact
1380 terminate_srd_session();
1381 } else {
1382 // All segments have been processed
f5a26d5e
SA
1383 if (!decode_interrupt_)
1384 decode_finished();
9431e2d3
SA
1385
1386 // Wait for more input data
1387 unique_lock<mutex> input_wait_lock(input_mutex_);
1388 decode_input_cond_.wait(input_wait_lock);
8a603e13 1389 }
db51130d
SA
1390 } else {
1391 // Input segment isn't complete yet but samples_to_process is 0, wait for more input data
1392 unique_lock<mutex> input_wait_lock(input_mutex_);
1393 decode_input_cond_.wait(input_wait_lock);
69e95096
SA
1394 }
1395
27a3f09b 1396 }
144e72c9 1397 } while (!decode_interrupt_);
e91883bb
SA
1398}
1399
1400void DecodeSignal::start_srd_session()
1401{
8ce0e732
SA
1402 // If there were stack changes, the session has been destroyed by now, so if
1403 // it hasn't been destroyed, we can just reset and re-use it
ce0b6a40
GS
1404 if (srd_session_) {
1405 // When a decoder stack was created before, re-use it
1406 // for the next stream of input data, after terminating
1407 // potentially still executing operations, and resetting
1408 // internal state. Skip the rather expensive (teardown
1409 // and) construction of another decoder stack.
1410
1411 // TODO Reduce redundancy, use a common code path for
72486b78 1412 // the meta/start sequence?
ce0b6a40 1413 terminate_srd_session();
72486b78
SA
1414
1415 // Metadata is cleared also, so re-set it
ba5b52e1 1416 uint64_t samplerate = 0;
7dd518b8 1417 if (segments_.size() > 0)
ba5b52e1
GS
1418 samplerate = segments_.at(current_segment_id_).samplerate;
1419 if (samplerate)
7dd518b8 1420 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
ba5b52e1 1421 g_variant_new_uint64(samplerate));
6a26fc44 1422 for (const shared_ptr<Decoder>& dec : stack_)
72486b78 1423 dec->apply_all_options();
ce0b6a40 1424 srd_session_start(srd_session_);
72486b78 1425
ce0b6a40
GS
1426 return;
1427 }
e91883bb 1428
88304160
SA
1429 // Update the samplerates for the output logic channels
1430 update_output_signals();
1431
a3ebd556
SA
1432 // Create the session
1433 srd_session_new(&srd_session_);
1434 assert(srd_session_);
47747218 1435
a3ebd556
SA
1436 // Create the decoders
1437 srd_decoder_inst *prev_di = nullptr;
6a26fc44 1438 for (const shared_ptr<Decoder>& dec : stack_) {
a3ebd556 1439 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
e91883bb 1440
a3ebd556 1441 if (!di) {
403c3e87 1442 set_error_message(tr("Failed to create decoder instance"));
a3ebd556 1443 srd_session_destroy(srd_session_);
c5f9553c 1444 srd_session_ = nullptr;
a3ebd556
SA
1445 return;
1446 }
e91883bb 1447
a3ebd556
SA
1448 if (prev_di)
1449 srd_inst_stack(srd_session_, prev_di, di);
e91883bb 1450
a3ebd556 1451 prev_di = di;
e91883bb 1452 }
a3ebd556
SA
1453
1454 // Start the session
7dd518b8
SA
1455 if (segments_.size() > 0)
1456 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1457 g_variant_new_uint64(segments_.at(current_segment_id_).samplerate));
a3ebd556
SA
1458
1459 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
1460 DecodeSignal::annotation_callback, this);
1461
c0c3e62a
SA
1462 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY,
1463 DecodeSignal::binary_callback, this);
1464
04b04675
SA
1465 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_LOGIC,
1466 DecodeSignal::logic_output_callback, this);
1467
a3ebd556 1468 srd_session_start(srd_session_);
e3202557
SA
1469
1470 // We just recreated the srd session, so all stack changes are applied now
1471 stack_config_changed_ = false;
e91883bb 1472}
47747218 1473
ce0b6a40
GS
1474void DecodeSignal::terminate_srd_session()
1475{
1476 // Call the "terminate and reset" routine for the decoder stack
1477 // (if available). This does not harm those stacks which already
1478 // have completed their operation, and reduces response time for
1479 // those stacks which still are processing data while the
1480 // application no longer wants them to.
72486b78 1481 if (srd_session_) {
ce0b6a40 1482 srd_session_terminate_reset(srd_session_);
72486b78
SA
1483
1484 // Metadata is cleared also, so re-set it
ba5b52e1 1485 uint64_t samplerate = 0;
7dd518b8 1486 if (segments_.size() > 0)
ba5b52e1
GS
1487 samplerate = segments_.at(current_segment_id_).samplerate;
1488 if (samplerate)
7dd518b8 1489 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
ba5b52e1 1490 g_variant_new_uint64(samplerate));
6a26fc44 1491 for (const shared_ptr<Decoder>& dec : stack_)
72486b78
SA
1492 dec->apply_all_options();
1493 }
ce0b6a40
GS
1494}
1495
e91883bb
SA
1496void DecodeSignal::stop_srd_session()
1497{
1498 if (srd_session_) {
1499 // Destroy the session
1500 srd_session_destroy(srd_session_);
1501 srd_session_ = nullptr;
8ce0e732
SA
1502
1503 // Mark the decoder instances as non-existant since they were deleted
6a26fc44 1504 for (const shared_ptr<Decoder>& dec : stack_)
8ce0e732 1505 dec->invalidate_decoder_inst();
e91883bb 1506 }
47747218
SA
1507}
1508
a8a9222d
SA
1509void DecodeSignal::connect_input_notifiers()
1510{
a8a9222d 1511 // Connect the currently used signals to our slot
a82325d1 1512 for (decode::DecodeChannel& ch : channels_) {
a8a9222d
SA
1513 if (!ch.assigned_signal)
1514 continue;
db51130d 1515 const data::SignalBase *signal = ch.assigned_signal.get();
1c552f40 1516
db51130d 1517 connect(signal, SIGNAL(samples_cleared()),
d023660f 1518 this, SLOT(on_data_cleared()), Qt::UniqueConnection);
db51130d 1519 connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)),
d023660f
SA
1520 this, SLOT(on_data_received()), Qt::UniqueConnection);
1521
1522 if (signal->logic_data())
1523 connect(signal->logic_data().get(), SIGNAL(segment_completed()),
1524 this, SLOT(on_input_segment_completed()), Qt::UniqueConnection);
db51130d
SA
1525 }
1526}
a8a9222d 1527
db51130d
SA
1528void DecodeSignal::disconnect_input_notifiers()
1529{
1530 // Disconnect the notification slot from the previous set of signals
1531 for (decode::DecodeChannel& ch : channels_) {
1532 if (!ch.assigned_signal)
1533 continue;
cf1541a1 1534 const data::SignalBase *signal = ch.assigned_signal.get();
db51130d
SA
1535 disconnect(signal, nullptr, this, SLOT(on_data_cleared()));
1536 disconnect(signal, nullptr, this, SLOT(on_data_received()));
db51130d 1537
d023660f
SA
1538 if (signal->logic_data())
1539 disconnect(signal->logic_data().get(), nullptr, this, SLOT(on_input_segment_completed()));
1540 }
db51130d
SA
1541}
1542
65efd025 1543void DecodeSignal::create_decode_segment()
4913560f 1544{
65efd025 1545 // Create annotation segment
f54e68b0 1546 segments_.emplace_back();
72435789 1547
65efd025 1548 // Add annotation classes
feda6c6b 1549 for (const shared_ptr<Decoder>& dec : stack_)
6a26fc44
SA
1550 for (Row* row : dec->get_rows())
1551 segments_.back().annotation_rows.emplace(row, RowData(row));
e77de61f
SA
1552
1553 // Prepare our binary output classes
6a26fc44 1554 for (const shared_ptr<Decoder>& dec : stack_) {
ac9494ef 1555 uint32_t n = dec->get_binary_class_count();
e77de61f 1556
ac9494ef 1557 for (uint32_t i = 0; i < n; i++)
e77de61f 1558 segments_.back().binary_classes.push_back(
c8a6db0e 1559 {dec.get(), dec->get_binary_class(i), deque<DecodeBinaryDataChunk>()});
e77de61f 1560 }
4913560f
SA
1561}
1562
47747218
SA
1563void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
1564{
1565 assert(pdata);
b21501d6 1566 assert(decode_signal);
47747218
SA
1567
1568 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1569 assert(ds);
1570
3b5282a2
SA
1571 if (ds->decode_interrupt_)
1572 return;
1573
02c87df7
SA
1574 if (ds->segments_.empty())
1575 return;
1576
47747218
SA
1577 lock_guard<mutex> lock(ds->output_mutex_);
1578
c0c3e62a 1579 // Get the decoder and the annotation data
47747218
SA
1580 assert(pdata->pdo);
1581 assert(pdata->pdo->di);
6a26fc44
SA
1582 const srd_decoder *const srd_dec = pdata->pdo->di->decoder;
1583 assert(srd_dec);
47747218 1584
c0c3e62a 1585 const srd_proto_data_annotation *const pda = (const srd_proto_data_annotation*)pdata->data;
1becee23
UH
1586 assert(pda);
1587
c0c3e62a 1588 // Find the row
6a26fc44
SA
1589 Decoder* dec = ds->get_decoder_by_instance(srd_dec);
1590 assert(dec);
47747218 1591
6a26fc44 1592 AnnotationClass* ann_class = dec->get_ann_class_by_id(pda->ann_class);
4526de63
SA
1593 if (!ann_class) {
1594 qWarning() << "Decoder" << ds->display_name() << "wanted to add annotation" <<
1595 "with class ID" << pda->ann_class << "but there are only" <<
1596 dec->ann_classes().size() << "known classes";
1597 return;
1598 }
6a26fc44
SA
1599
1600 const Row* row = ann_class->row;
1601
1602 if (!row)
1603 row = dec->get_row_by_id(0);
47747218 1604
f54e68b0
SA
1605 RowData& row_data = ds->segments_[ds->current_segment_id_].annotation_rows.at(row);
1606
1607 // Add the annotation to the row
1608 const Annotation* ann = row_data.emplace_annotation(pdata);
1609
ca9a5918
SA
1610 // We insert the annotation into the global annotation list in a way so that
1611 // the annotation list is sorted by start sample and length. Otherwise, we'd
1612 // have to sort the model, which is expensive
f54e68b0
SA
1613 deque<const Annotation*>& all_annotations =
1614 ds->segments_[ds->current_segment_id_].all_annotations;
ca9a5918
SA
1615
1616 if (all_annotations.empty()) {
1617 all_annotations.emplace_back(ann);
1618 } else {
1619 const uint64_t new_ann_len = (pdata->end_sample - pdata->start_sample);
1620 bool ann_has_earlier_start = (pdata->start_sample < all_annotations.back()->start_sample());
1621 bool ann_is_longer = (new_ann_len >
1622 (all_annotations.back()->end_sample() - all_annotations.back()->start_sample()));
1623
1624 if (ann_has_earlier_start && ann_is_longer) {
1625 bool ann_has_same_start;
1626 auto it = all_annotations.end();
1627
1628 do {
1629 it--;
1630 ann_has_earlier_start = (pdata->start_sample < (*it)->start_sample());
1631 ann_has_same_start = (pdata->start_sample == (*it)->start_sample());
d656b010 1632 ann_is_longer = (new_ann_len > (*it)->length());
ca9a5918
SA
1633 } while ((ann_has_earlier_start || (ann_has_same_start && ann_is_longer)) && (it != all_annotations.begin()));
1634
1635 // Allow inserting at the front
1636 if (it != all_annotations.begin())
1637 it++;
1638
1639 all_annotations.emplace(it, ann);
1640 } else
1641 all_annotations.emplace_back(ann);
1642 }
88a25978
SA
1643
1644 // When emplace_annotation() inserts instead of appends an annotation,
1645 // the pointers in all_annotations that follow the inserted annotation and
1646 // point to annotations for this row are off by one and must be updated
1647 if (&(row_data.annotations().back()) != ann) {
1648 // Search backwards until we find the annotation we just added
1649 auto row_it = row_data.annotations().end();
1650 auto all_it = all_annotations.end();
1651 do {
1652 all_it--;
1653 if ((*all_it)->row_data() == &row_data)
1654 row_it--;
1655 } while (&(*row_it) != ann);
1656
1657 // Update the annotation addresses for this row's annotations until the end
1658 do {
1659 if ((*all_it)->row_data() == &row_data) {
1660 *all_it = &(*row_it);
1661 row_it++;
1662 }
1663 all_it++;
1664 } while (all_it != all_annotations.end());
1665 }
47747218
SA
1666}
1667
c0c3e62a
SA
1668void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal)
1669{
1670 assert(pdata);
1671 assert(decode_signal);
1672
1673 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1674 assert(ds);
1675
1676 if (ds->decode_interrupt_)
1677 return;
1678
e77de61f
SA
1679 // Get the decoder and the binary data
1680 assert(pdata->pdo);
1681 assert(pdata->pdo->di);
6a26fc44
SA
1682 const srd_decoder *const srd_dec = pdata->pdo->di->decoder;
1683 assert(srd_dec);
e77de61f 1684
c0c3e62a
SA
1685 const srd_proto_data_binary *const pdb = (const srd_proto_data_binary*)pdata->data;
1686 assert(pdb);
1687
e77de61f 1688 // Find the matching DecodeBinaryClass
c0c3e62a
SA
1689 DecodeSegment* segment = &(ds->segments_.at(ds->current_segment_id_));
1690
e77de61f
SA
1691 DecodeBinaryClass* bin_class = nullptr;
1692 for (DecodeBinaryClass& bc : segment->binary_classes)
6a26fc44
SA
1693 if ((bc.decoder->get_srd_decoder() == srd_dec) &&
1694 (bc.info->bin_class_id == (uint32_t)pdb->bin_class))
e77de61f 1695 bin_class = &bc;
c0c3e62a 1696
e77de61f
SA
1697 if (!bin_class) {
1698 qWarning() << "Could not find valid DecodeBinaryClass in segment" <<
1699 ds->current_segment_id_ << "for binary class ID" << pdb->bin_class <<
1700 ", segment only knows" << segment->binary_classes.size() << "classes";
1701 return;
1702 }
c0c3e62a 1703
e77de61f
SA
1704 // Add the data chunk
1705 bin_class->chunks.emplace_back();
1706 DecodeBinaryDataChunk* chunk = &(bin_class->chunks.back());
1707
1708 chunk->sample = pdata->start_sample;
1709 chunk->data.resize(pdb->size);
1710 memcpy(chunk->data.data(), pdb->data, pdb->size);
1711
6a26fc44 1712 Decoder* dec = ds->get_decoder_by_instance(srd_dec);
b2b18d3a
SA
1713
1714 ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class);
c0c3e62a
SA
1715}
1716
04b04675
SA
1717void DecodeSignal::logic_output_callback(srd_proto_data *pdata, void *decode_signal)
1718{
1719 assert(pdata);
1720 assert(decode_signal);
1721
1722 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1723 assert(ds);
1724
1725 if (ds->decode_interrupt_)
1726 return;
1727
1728 lock_guard<mutex> lock(ds->output_mutex_);
1729
1730 assert(pdata->pdo);
1731 assert(pdata->pdo->di);
1732 const srd_decoder *const decc = pdata->pdo->di->decoder;
1733 assert(decc);
1734
1735 const srd_proto_data_logic *const pdl = (const srd_proto_data_logic*)pdata->data;
1736 assert(pdl);
1737
1738 shared_ptr<Logic> output_logic = ds->output_logic_.at(decc);
30cdef99
SA
1739
1740 vector< shared_ptr<Segment> > segments = output_logic->segments();
1741
1742 shared_ptr<LogicSegment> last_segment;
1743
1744 if (!segments.empty())
1745 last_segment = dynamic_pointer_cast<LogicSegment>(segments.back());
1746 else {
1747 // Happens when the data was cleared - all segments are gone then
88304160 1748 // segment_id is always 0 as it's the first segment
30cdef99
SA
1749 last_segment = make_shared<data::LogicSegment>(
1750 *output_logic, 0, (output_logic->num_channels() + 7) / 8, output_logic->get_samplerate());
1751 output_logic->push_segment(last_segment);
1752 }
04b04675 1753
88304160
SA
1754 vector<uint8_t> data;
1755 for (unsigned int i = pdata->start_sample; i < pdata->end_sample; i++)
1756 data.emplace_back(*((uint8_t*)pdl->data));
1757
1758 last_segment->append_subsignal_payload(pdl->logic_class, data.data(), data.size());
04b04675 1759
88304160
SA
1760 qInfo() << "Received logic output state change for class" << pdl->logic_class << "from decoder" \
1761 << QString::fromUtf8(decc->name) << "from" << pdata->start_sample << "to" << pdata->end_sample;
04b04675
SA
1762}
1763
47747218
SA
1764void DecodeSignal::on_capture_state_changed(int state)
1765{
1766 // If a new acquisition was started, we need to start decoding from scratch
ba5f2186
SA
1767 if (state == Session::Running) {
1768 logic_mux_data_invalid_ = true;
47747218 1769 begin_decode();
ba5f2186 1770 }
47747218
SA
1771}
1772
1b56c646
SA
1773void DecodeSignal::on_data_cleared()
1774{
1775 reset_decode();
1776}
1777
47747218
SA
1778void DecodeSignal::on_data_received()
1779{
5620539c 1780 // If we detected a lack of input data when trying to start decoding,
d023660f 1781 // we have set an error message. Bail out if we still don't have data
5620539c
SA
1782 // to work with
1783 if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0))
1784 return;
d023660f
SA
1785
1786 if (!error_message_.isEmpty()) {
69e95096
SA
1787 error_message_.clear();
1788 // TODO Emulate noquote()
d023660f 1789 qDebug().nospace() << name() << ": Input data available, error cleared";
69e95096 1790 }
5620539c 1791
1b56c646
SA
1792 if (!logic_mux_thread_.joinable())
1793 begin_decode();
1794 else
1795 logic_mux_cond_.notify_one();
47747218
SA
1796}
1797
db51130d
SA
1798void DecodeSignal::on_input_segment_completed()
1799{
1800 if (!logic_mux_thread_.joinable())
1801 logic_mux_cond_.notify_one();
1802}
1803
02078aa1
SA
1804void DecodeSignal::on_annotation_visibility_changed()
1805{
1806 annotation_visibility_changed();
1807}
1808
ad908057
SA
1809} // namespace data
1810} // namespace pv