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