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