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