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