]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
DecodeSignal: Use signals for new data notification, not session
[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
0c5fe73e
SA
20#include <limits>
21
47747218
SA
22#include <QDebug>
23
ad908057
SA
24#include "logic.hpp"
25#include "logicsegment.hpp"
26#include "decodesignal.hpp"
27#include "signaldata.hpp"
28
29#include <pv/binding/decoder.hpp>
30#include <pv/data/decode/decoder.hpp>
ecd07c20 31#include <pv/data/decode/row.hpp>
ad908057
SA
32#include <pv/session.hpp>
33
47747218
SA
34using std::lock_guard;
35using std::make_pair;
ad908057 36using std::make_shared;
0c5fe73e 37using std::min;
ad908057 38using std::shared_ptr;
47747218
SA
39using std::unique_lock;
40using pv::data::decode::Annotation;
ad908057 41using pv::data::decode::Decoder;
ecd07c20 42using pv::data::decode::Row;
ad908057
SA
43
44namespace pv {
45namespace data {
46
47747218
SA
47const double DecodeSignal::DecodeMargin = 1.0;
48const double DecodeSignal::DecodeThreshold = 0.2;
49const int64_t DecodeSignal::DecodeChunkLength = 10 * 1024 * 1024;
50const unsigned int DecodeSignal::DecodeNotifyPeriod = 1024;
ad908057 51
47747218 52mutex DecodeSignal::global_srd_mutex_;
9f97b357 53
ad908057 54
47747218
SA
55DecodeSignal::DecodeSignal(pv::Session &session) :
56 SignalBase(nullptr, SignalBase::DecodeChannel),
57 session_(session),
e91883bb 58 srd_session_(nullptr),
47747218
SA
59 logic_mux_data_invalid_(false),
60 start_time_(0),
61 samplerate_(0),
47747218
SA
62 annotation_count_(0),
63 samples_decoded_(0),
64 frame_complete_(false)
ad908057 65{
47747218
SA
66 connect(&session_, SIGNAL(capture_state_changed(int)),
67 this, SLOT(on_capture_state_changed(int)));
47747218
SA
68
69 set_name(tr("Empty decoder signal"));
ad908057
SA
70}
71
47747218 72DecodeSignal::~DecodeSignal()
ad908057 73{
47747218
SA
74 if (decode_thread_.joinable()) {
75 decode_interrupt_ = true;
76 decode_input_cond_.notify_one();
77 decode_thread_.join();
78 }
ad908057 79
47747218
SA
80 if (logic_mux_thread_.joinable()) {
81 logic_mux_interrupt_ = true;
82 logic_mux_cond_.notify_one();
83 logic_mux_thread_.join();
84 }
e91883bb
SA
85
86 stop_srd_session();
ad908057
SA
87}
88
47747218 89const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
ecd07c20 90{
47747218 91 return stack_;
ecd07c20
SA
92}
93
ad908057
SA
94void DecodeSignal::stack_decoder(srd_decoder *decoder)
95{
96 assert(decoder);
47747218
SA
97 stack_.push_back(make_shared<decode::Decoder>(decoder));
98
99 // Set name if this decoder is the first in the list
100 if (stack_.size() == 1)
101 set_name(QString::fromUtf8(decoder->name));
132a5c6d 102
27a3f09b 103 // Include the newly created decode channels in the channel lists
9f97b357 104 update_channel_list();
132a5c6d
SA
105
106 auto_assign_signals();
27a3f09b 107 commit_decoder_channels();
47747218 108 begin_decode();
ad908057
SA
109}
110
111void DecodeSignal::remove_decoder(int index)
112{
47747218
SA
113 assert(index >= 0);
114 assert(index < (int)stack_.size());
115
116 // Find the decoder in the stack
117 auto iter = stack_.begin();
118 for (int i = 0; i < index; i++, iter++)
119 assert(iter != stack_.end());
120
121 // Delete the element
122 stack_.erase(iter);
123
124 // Update channels and decoded data
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) {
140 state = !dec->shown();
141 dec->show(state);
142 }
143
144 return state;
145}
146
47747218
SA
147void DecodeSignal::reset_decode()
148{
e91883bb
SA
149 stop_srd_session();
150
47747218
SA
151 annotation_count_ = 0;
152 frame_complete_ = false;
153 samples_decoded_ = 0;
154 error_message_ = QString();
155 rows_.clear();
156 class_rows_.clear();
157}
158
946b52e1
SA
159void DecodeSignal::begin_decode()
160{
47747218
SA
161 if (decode_thread_.joinable()) {
162 decode_interrupt_ = true;
163 decode_input_cond_.notify_one();
164 decode_thread_.join();
165 }
166
167 if (logic_mux_thread_.joinable()) {
168 logic_mux_interrupt_ = true;
169 logic_mux_cond_.notify_one();
170 logic_mux_thread_.join();
171 }
172
173 reset_decode();
174
27a3f09b
SA
175 if (stack_.size() == 0) {
176 error_message_ = tr("No decoders");
177 return;
178 }
179
180 assert(channels_.size() > 0);
181
182 if (get_assigned_signal_count() == 0) {
183 error_message_ = tr("There are no channels assigned to this decoder");
184 return;
185 }
186
47747218
SA
187 // Check that all decoders have the required channels
188 for (const shared_ptr<decode::Decoder> &dec : stack_)
189 if (!dec->have_required_channels()) {
190 error_message_ = tr("One or more required channels "
191 "have not been specified");
192 return;
193 }
194
195 // Add annotation classes
196 for (const shared_ptr<decode::Decoder> &dec : stack_) {
197 assert(dec);
198 const srd_decoder *const decc = dec->decoder();
199 assert(dec->decoder());
200
201 // Add a row for the decoder if it doesn't have a row list
202 if (!decc->annotation_rows)
203 rows_[Row(decc)] = decode::RowData();
204
205 // Add the decoder rows
206 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
207 const srd_decoder_annotation_row *const ann_row =
208 (srd_decoder_annotation_row *)l->data;
209 assert(ann_row);
210
211 const Row row(decc, ann_row);
212
213 // Add a new empty row data object
214 rows_[row] = decode::RowData();
215
216 // Map out all the classes
217 for (const GSList *ll = ann_row->ann_classes;
218 ll; ll = ll->next)
219 class_rows_[make_pair(decc,
220 GPOINTER_TO_INT(ll->data))] = row;
221 }
222 }
223
27a3f09b
SA
224 // TODO Currently we assume all channels have the same sample rate
225 auto any_channel = find_if(channels_.begin(), channels_.end(),
226 [](data::DecodeChannel ch) { return ch.assigned_signal; });
227 shared_ptr<LogicSegment> first_segment =
228 any_channel->assigned_signal->logic_data()->logic_segments().front();
229 samplerate_ = first_segment->samplerate();
230
231 // Free the logic data and its segment(s) if it needs to be updated
232 if (logic_mux_data_invalid_)
233 logic_mux_data_.reset();
234
235 if (!logic_mux_data_) {
236 const int64_t ch_count = get_assigned_signal_count();
237 const int64_t unit_size = (ch_count + 7) / 8;
238 logic_mux_data_ = make_shared<Logic>(ch_count);
239 segment_ = make_shared<LogicSegment>(*logic_mux_data_, unit_size, samplerate_);
240 logic_mux_data_->push_segment(segment_);
241 }
47747218
SA
242
243 // Update the samplerate and start time
244 start_time_ = segment_->start_time();
245 samplerate_ = segment_->samplerate();
246 if (samplerate_ == 0.0)
247 samplerate_ = 1.0;
248
27a3f09b
SA
249 // Make sure the logic output data is complete and up-to-date
250 logic_mux_interrupt_ = false;
251 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
252
253 // Decode the muxed logic data
47747218
SA
254 decode_interrupt_ = false;
255 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
a8a9222d
SA
256
257 // Receive notifications when new sample data is available
258 connect_input_notifiers();
946b52e1
SA
259}
260
ecd07c20
SA
261QString DecodeSignal::error_message() const
262{
47747218
SA
263 lock_guard<mutex> lock(output_mutex_);
264 return error_message_;
ecd07c20
SA
265}
266
47747218 267const vector<data::DecodeChannel> DecodeSignal::get_channels() const
9f97b357
SA
268{
269 return channels_;
270}
271
132a5c6d
SA
272void DecodeSignal::auto_assign_signals()
273{
47747218
SA
274 bool new_assignment = false;
275
132a5c6d
SA
276 // Try to auto-select channels that don't have signals assigned yet
277 for (data::DecodeChannel &ch : channels_) {
278 if (ch.assigned_signal)
279 continue;
280
47747218
SA
281 for (shared_ptr<data::SignalBase> s : session_.signalbases())
282 if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower()))) {
132a5c6d 283 ch.assigned_signal = s.get();
47747218
SA
284 new_assignment = true;
285 }
286 }
287
288 if (new_assignment) {
289 logic_mux_data_invalid_ = true;
27a3f09b 290 commit_decoder_channels();
47747218 291 channels_updated();
132a5c6d
SA
292 }
293}
294
9f97b357
SA
295void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
296{
297 for (data::DecodeChannel &ch : channels_)
47747218 298 if (ch.id == channel_id) {
9f97b357 299 ch.assigned_signal = signal;
47747218
SA
300 logic_mux_data_invalid_ = true;
301 }
9f97b357 302
27a3f09b 303 commit_decoder_channels();
9f97b357 304 channels_updated();
47747218 305 begin_decode();
9f97b357
SA
306}
307
27a3f09b
SA
308int DecodeSignal::get_assigned_signal_count() const
309{
310 // Count all channels that have a signal assigned to them
311 return count_if(channels_.begin(), channels_.end(),
312 [](data::DecodeChannel ch) { return ch.assigned_signal; });
313}
314
9f97b357
SA
315void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
316{
317 for (data::DecodeChannel &ch : channels_)
318 if (ch.id == channel_id)
319 ch.initial_pin_state = init_state;
320
321 channels_updated();
322
47747218 323 begin_decode();
9f97b357
SA
324}
325
ff83d980
SA
326double DecodeSignal::samplerate() const
327{
47747218 328 return samplerate_;
ff83d980
SA
329}
330
331const pv::util::Timestamp& DecodeSignal::start_time() const
332{
47747218 333 return start_time_;
ff83d980
SA
334}
335
0c5fe73e
SA
336int64_t DecodeSignal::get_working_sample_count() const
337{
338 // The working sample count is the highest sample number for
339 // which all used signals have data available, so go through
340 // all channels and use the lowest overall sample count of the
341 // current segment
342
343 // TODO Currently, we assume only a single segment exists
344
345 int64_t count = std::numeric_limits<int64_t>::max();
346 bool no_signals_assigned = true;
347
348 for (const data::DecodeChannel &ch : channels_)
349 if (ch.assigned_signal) {
350 no_signals_assigned = false;
351
352 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
353 if (!logic_data || logic_data->logic_segments().empty())
354 return 0;
355
356 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
357 count = min(count, (int64_t)segment->get_sample_count());
358 }
359
360 return (no_signals_assigned ? 0 : count);
361}
362
363int64_t DecodeSignal::get_decoded_sample_count() const
ff83d980 364{
47747218
SA
365 lock_guard<mutex> decode_lock(output_mutex_);
366 return samples_decoded_;
ff83d980
SA
367}
368
ecd07c20
SA
369vector<Row> DecodeSignal::visible_rows() const
370{
47747218
SA
371 lock_guard<mutex> lock(output_mutex_);
372
373 vector<Row> rows;
374
375 for (const shared_ptr<decode::Decoder> &dec : stack_) {
376 assert(dec);
377 if (!dec->shown())
378 continue;
379
380 const srd_decoder *const decc = dec->decoder();
381 assert(dec->decoder());
382
383 // Add a row for the decoder if it doesn't have a row list
384 if (!decc->annotation_rows)
385 rows.emplace_back(decc);
386
387 // Add the decoder rows
388 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
389 const srd_decoder_annotation_row *const ann_row =
390 (srd_decoder_annotation_row *)l->data;
391 assert(ann_row);
392 rows.emplace_back(decc, ann_row);
393 }
394 }
395
396 return rows;
ecd07c20
SA
397}
398
399void DecodeSignal::get_annotation_subset(
400 vector<pv::data::decode::Annotation> &dest,
401 const decode::Row &row, uint64_t start_sample,
402 uint64_t end_sample) const
403{
47747218
SA
404 lock_guard<mutex> lock(output_mutex_);
405
406 const auto iter = rows_.find(row);
407 if (iter != rows_.end())
408 (*iter).second.get_annotation_subset(dest,
409 start_sample, end_sample);
410}
411
412void DecodeSignal::save_settings(QSettings &settings) const
413{
414 SignalBase::save_settings(settings);
415
416 // TODO Save decoder stack, channel mapping and decoder options
417}
418
419void DecodeSignal::restore_settings(QSettings &settings)
420{
421 SignalBase::restore_settings(settings);
422
423 // TODO Restore decoder stack, channel mapping and decoder options
424}
425
426uint64_t DecodeSignal::inc_annotation_count()
427{
428 return (annotation_count_++);
ecd07c20
SA
429}
430
9f97b357
SA
431void DecodeSignal::update_channel_list()
432{
47747218 433 vector<data::DecodeChannel> prev_channels = channels_;
9f97b357
SA
434 channels_.clear();
435
436 uint16_t id = 0;
437
438 // Copy existing entries, create new as needed
47747218 439 for (shared_ptr<Decoder> decoder : stack_) {
9f97b357
SA
440 const srd_decoder* srd_d = decoder->decoder();
441 const GSList *l;
442
443 // Mandatory channels
444 for (l = srd_d->channels; l; l = l->next) {
445 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
446 bool ch_added = false;
447
448 // Copy but update ID if this channel was in the list before
47747218 449 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
450 if (ch.pdch_ == pdch) {
451 ch.id = id++;
452 channels_.push_back(ch);
453 ch_added = true;
454 break;
455 }
456
457 if (!ch_added) {
458 // Create new entry without a mapped signal
459 data::DecodeChannel ch = {id++, false, nullptr,
460 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
461 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
462 channels_.push_back(ch);
463 }
464 }
465
466 // Optional channels
467 for (l = srd_d->opt_channels; l; l = l->next) {
468 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
469 bool ch_added = false;
470
471 // Copy but update ID if this channel was in the list before
47747218 472 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
473 if (ch.pdch_ == pdch) {
474 ch.id = id++;
475 channels_.push_back(ch);
476 ch_added = true;
477 break;
478 }
479
480 if (!ch_added) {
481 // Create new entry without a mapped signal
482 data::DecodeChannel ch = {id++, true, nullptr,
483 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
484 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
485 channels_.push_back(ch);
486 }
487 }
488 }
489
47747218
SA
490 // Invalidate the logic output data if the channel assignment changed
491 if (prev_channels.size() != channels_.size()) {
492 // The number of channels changed, there's definitely a difference
493 logic_mux_data_invalid_ = true;
494 } else {
495 // Same number but assignment may still differ, so compare all channels
496 for (size_t i = 0; i < channels_.size(); i++) {
497 const data::DecodeChannel &p_ch = prev_channels[i];
498 const data::DecodeChannel &ch = channels_[i];
499
500 if ((p_ch.pdch_ != ch.pdch_) ||
501 (p_ch.assigned_signal != ch.assigned_signal)) {
502 logic_mux_data_invalid_ = true;
503 break;
504 }
505 }
506
507 }
508
9f97b357
SA
509 channels_updated();
510}
511
27a3f09b 512void DecodeSignal::commit_decoder_channels()
ad908057 513{
27a3f09b
SA
514 // Submit channel list to every decoder, containing only the relevant channels
515 for (shared_ptr<decode::Decoder> dec : stack_) {
516 vector<data::DecodeChannel*> channel_list;
517
518 for (data::DecodeChannel &ch : channels_)
519 if (ch.decoder_ == dec)
520 channel_list.push_back(&ch);
47747218 521
27a3f09b
SA
522 dec->set_channels(channel_list);
523 }
47747218
SA
524}
525
27a3f09b 526void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
47747218 527{
27a3f09b
SA
528 // Enforce end to be greater than start
529 if (end <= start)
530 return;
531
532 // Fetch all segments and their data
533 // TODO Currently, we assume only a single segment exists
534 vector<shared_ptr<LogicSegment> > segments;
535 vector<const uint8_t*> signal_data;
536 vector<uint8_t> signal_in_bytepos;
537 vector<uint8_t> signal_in_bitpos;
538
539 for (data::DecodeChannel &ch : channels_)
540 if (ch.assigned_signal) {
541 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
542 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
543 segments.push_back(segment);
544 signal_data.push_back(segment->get_samples(start, end));
545
546 const int bitpos = ch.assigned_signal->logic_bit_index();
547 signal_in_bytepos.push_back(bitpos / 8);
548 signal_in_bitpos.push_back(bitpos % 8);
549 }
550
551 // Perform the muxing of signal data into the output data
552 uint8_t* output = new uint8_t[(end - start) * segment_->unit_size()];
553 unsigned int signal_count = signal_data.size();
554
555 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
556 int bitpos = 0;
557 uint8_t bytepos = 0;
558
559 const int out_sample_pos = sample_cnt * segment_->unit_size();
560 for (unsigned int i = 0; i < segment_->unit_size(); i++)
561 output[out_sample_pos + i] = 0;
47747218 562
27a3f09b
SA
563 for (unsigned int i = 0; i < signal_count; i++) {
564 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
565 const uint8_t in_sample = 1 &
566 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
47747218 567
27a3f09b
SA
568 const uint8_t out_sample = output[out_sample_pos + bytepos];
569
570 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
571
572 bitpos++;
573 if (bitpos > 7) {
574 bitpos = 0;
575 bytepos++;
576 }
577 }
47747218
SA
578 }
579
27a3f09b
SA
580 segment_->append_payload(output, (end - start) * segment_->unit_size());
581 delete[] output;
582
583 for (const uint8_t* data : signal_data)
584 delete[] data;
585}
586
587void DecodeSignal::logic_mux_proc()
588{
589 do {
27a3f09b
SA
590 const uint64_t input_sample_count = get_working_sample_count();
591 const uint64_t output_sample_count = segment_->get_sample_count();
592
593 const uint64_t samples_to_process =
594 (input_sample_count > output_sample_count) ?
595 (input_sample_count - output_sample_count) : 0;
596
597 // Process the samples if necessary...
598 if (samples_to_process > 0) {
599 const uint64_t unit_size = segment_->unit_size();
600 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
601
602 uint64_t processed_samples = 0;
603 do {
604 const uint64_t start_sample = output_sample_count + processed_samples;
605 const uint64_t sample_count =
606 min(samples_to_process - processed_samples, chunk_sample_count);
607
608 mux_logic_samples(start_sample, start_sample + sample_count);
609 processed_samples += sample_count;
610
611 // ...and process the newly muxed logic data
612 decode_input_cond_.notify_one();
613 } while (processed_samples < samples_to_process);
614 }
615
616 if (session_.get_capture_state() != Session::Stopped) {
617 // Wait for more input
618 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
619 logic_mux_cond_.wait(logic_mux_lock);
620 }
621 } while ((session_.get_capture_state() != Session::Stopped) && !logic_mux_interrupt_);
622
623 // No more input data and session is stopped, let the decode thread
624 // process any pending data, terminate and release the global SRD mutex
625 // in order to let other decoders run
626 decode_input_cond_.notify_one();
47747218
SA
627}
628
629void DecodeSignal::decode_data(
e91883bb 630 const int64_t abs_start_samplenum, const int64_t sample_count)
47747218 631{
27a3f09b
SA
632 const int64_t unit_size = segment_->unit_size();
633 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
47747218
SA
634
635 for (int64_t i = abs_start_samplenum;
636 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
637 i += chunk_sample_count) {
638
639 const int64_t chunk_end = min(i + chunk_sample_count,
640 abs_start_samplenum + sample_count);
641
642 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
643
e91883bb 644 if (srd_session_send(srd_session_, i, chunk_end, chunk,
47747218
SA
645 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
646 error_message_ = tr("Decoder reported an error");
647 delete[] chunk;
648 break;
649 }
650 delete[] chunk;
651
652 {
653 lock_guard<mutex> lock(output_mutex_);
654 samples_decoded_ = chunk_end;
655 }
656 }
657}
658
659void DecodeSignal::decode_proc()
660{
e91883bb 661 start_srd_session();
47747218 662
27a3f09b
SA
663 uint64_t sample_count;
664 uint64_t abs_start_samplenum = 0;
47747218 665 do {
27a3f09b
SA
666 // Keep processing new samples until we exhaust the input data
667 do {
e91883bb
SA
668 // Prevent any other decode threads from accessing libsigrokdecode
669 lock_guard<mutex> srd_lock(global_srd_mutex_);
670
27a3f09b
SA
671 {
672 lock_guard<mutex> input_lock(input_mutex_);
673 sample_count = segment_->get_sample_count() - abs_start_samplenum;
674 }
675
676 if (sample_count > 0) {
e91883bb 677 decode_data(abs_start_samplenum, sample_count);
27a3f09b
SA
678 abs_start_samplenum += sample_count;
679 }
680 } while (error_message_.isEmpty() && (sample_count > 0));
681
27a3f09b 682 if (error_message_.isEmpty()) {
e91883bb
SA
683 // Make sure all annotations are known to the frontend
684 new_annotations();
685
27a3f09b
SA
686 // Wait for new input data or an interrupt request
687 unique_lock<mutex> input_wait_lock(input_mutex_);
688 decode_input_cond_.wait(input_wait_lock);
689 }
690 } while (error_message_.isEmpty() && !decode_interrupt_);
e91883bb
SA
691}
692
693void DecodeSignal::start_srd_session()
694{
695 if (!srd_session_) {
696 // Create the session
697 srd_session_new(&srd_session_);
698 assert(srd_session_);
699
700 // Create the decoders
701 srd_decoder_inst *prev_di = nullptr;
702 for (const shared_ptr<decode::Decoder> &dec : stack_) {
703 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
704
705 if (!di) {
706 error_message_ = tr("Failed to create decoder instance");
707 srd_session_destroy(srd_session_);
708 return;
709 }
710
711 if (prev_di)
712 srd_inst_stack(srd_session_, prev_di, di);
47747218 713
e91883bb
SA
714 prev_di = di;
715 }
716
717 // Start the session
718 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
719 g_variant_new_uint64(samplerate_));
720
721 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
722 DecodeSignal::annotation_callback, this);
723
724 srd_session_start(srd_session_);
725 }
726}
47747218 727
e91883bb
SA
728void DecodeSignal::stop_srd_session()
729{
730 if (srd_session_) {
731 // Destroy the session
732 srd_session_destroy(srd_session_);
733 srd_session_ = nullptr;
734 }
47747218
SA
735}
736
a8a9222d
SA
737void DecodeSignal::connect_input_notifiers()
738{
739 // Disconnect the notification slot from the previous set of signals
740 disconnect(this, SLOT(on_data_received()));
741
742 // Connect the currently used signals to our slot
743 for (data::DecodeChannel &ch : channels_) {
744 if (!ch.assigned_signal)
745 continue;
746
747 shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
748 connect(logic_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
749 this, SLOT(on_data_received()));
750 }
751}
752
47747218
SA
753void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
754{
755 assert(pdata);
756 assert(decoder);
757
758 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
759 assert(ds);
760
761 lock_guard<mutex> lock(ds->output_mutex_);
762
763 const decode::Annotation a(pdata);
764
765 // Find the row
766 assert(pdata->pdo);
767 assert(pdata->pdo->di);
768 const srd_decoder *const decc = pdata->pdo->di->decoder;
769 assert(decc);
770
771 auto row_iter = ds->rows_.end();
772
773 // Try looking up the sub-row of this class
774 const auto r = ds->class_rows_.find(make_pair(decc, a.format()));
775 if (r != ds->class_rows_.end())
776 row_iter = ds->rows_.find((*r).second);
777 else {
778 // Failing that, use the decoder as a key
779 row_iter = ds->rows_.find(Row(decc));
780 }
781
782 assert(row_iter != ds->rows_.end());
783 if (row_iter == ds->rows_.end()) {
784 qDebug() << "Unexpected annotation: decoder = " << decc <<
785 ", format = " << a.format();
786 assert(false);
787 return;
788 }
789
790 // Add the annotation
791 (*row_iter).second.push_annotation(a);
792
793 // Notify the frontend every DecodeNotifyPeriod annotations
794 if (ds->inc_annotation_count() % DecodeNotifyPeriod == 0)
795 ds->new_annotations();
796}
797
798void DecodeSignal::on_capture_state_changed(int state)
799{
800 // If a new acquisition was started, we need to start decoding from scratch
801 if (state == Session::Running)
802 begin_decode();
803}
804
805void DecodeSignal::on_data_received()
806{
807 logic_mux_cond_.notify_one();
808}
809
ad908057
SA
810} // namespace data
811} // namespace pv