]>
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; |
ad908057 | 39 | using std::shared_ptr; |
47747218 SA |
40 | using std::unique_lock; |
41 | using pv::data::decode::Annotation; | |
ad908057 | 42 | using pv::data::decode::Decoder; |
ecd07c20 | 43 | using pv::data::decode::Row; |
ad908057 SA |
44 | |
45 | namespace pv { | |
46 | namespace data { | |
47 | ||
47747218 SA |
48 | const double DecodeSignal::DecodeMargin = 1.0; |
49 | const double DecodeSignal::DecodeThreshold = 0.2; | |
3fbddf7f | 50 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; |
ad908057 | 51 | |
ad908057 | 52 | |
47747218 SA |
53 | DecodeSignal::DecodeSignal(pv::Session &session) : |
54 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
55 | session_(session), | |
e91883bb | 56 | srd_session_(nullptr), |
47747218 SA |
57 | logic_mux_data_invalid_(false), |
58 | start_time_(0), | |
59 | samplerate_(0), | |
47747218 SA |
60 | samples_decoded_(0), |
61 | frame_complete_(false) | |
ad908057 | 62 | { |
47747218 SA |
63 | connect(&session_, SIGNAL(capture_state_changed(int)), |
64 | this, SLOT(on_capture_state_changed(int))); | |
ad908057 SA |
65 | } |
66 | ||
47747218 | 67 | DecodeSignal::~DecodeSignal() |
ad908057 | 68 | { |
1b56c646 | 69 | reset_decode(); |
ad908057 SA |
70 | } |
71 | ||
47747218 | 72 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const |
ecd07c20 | 73 | { |
47747218 | 74 | return stack_; |
ecd07c20 SA |
75 | } |
76 | ||
c8e60bdf | 77 | void DecodeSignal::stack_decoder(const srd_decoder *decoder) |
ad908057 SA |
78 | { |
79 | assert(decoder); | |
762ab7a4 SA |
80 | const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder); |
81 | ||
82 | stack_.push_back(dec); | |
47747218 SA |
83 | |
84 | // Set name if this decoder is the first in the list | |
85 | if (stack_.size() == 1) | |
86 | set_name(QString::fromUtf8(decoder->name)); | |
132a5c6d | 87 | |
27a3f09b | 88 | // Include the newly created decode channels in the channel lists |
9f97b357 | 89 | update_channel_list(); |
132a5c6d | 90 | |
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 | |
9f97b357 | 110 | update_channel_list(); |
47747218 | 111 | begin_decode(); |
ad908057 SA |
112 | } |
113 | ||
114 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
115 | { | |
47747218 | 116 | auto iter = stack_.cbegin(); |
ad908057 | 117 | for (int i = 0; i < index; i++, iter++) |
47747218 | 118 | assert(iter != stack_.end()); |
ad908057 SA |
119 | |
120 | shared_ptr<Decoder> dec = *iter; | |
121 | ||
122 | // Toggle decoder visibility | |
123 | bool state = false; | |
124 | if (dec) { | |
125 | state = !dec->shown(); | |
126 | dec->show(state); | |
127 | } | |
128 | ||
129 | return state; | |
130 | } | |
131 | ||
47747218 SA |
132 | void DecodeSignal::reset_decode() |
133 | { | |
1b56c646 SA |
134 | if (decode_thread_.joinable()) { |
135 | decode_interrupt_ = true; | |
136 | decode_input_cond_.notify_one(); | |
137 | decode_thread_.join(); | |
138 | } | |
139 | ||
140 | if (logic_mux_thread_.joinable()) { | |
141 | logic_mux_interrupt_ = true; | |
142 | logic_mux_cond_.notify_one(); | |
143 | logic_mux_thread_.join(); | |
144 | } | |
145 | ||
e91883bb SA |
146 | stop_srd_session(); |
147 | ||
47747218 SA |
148 | frame_complete_ = false; |
149 | samples_decoded_ = 0; | |
150 | error_message_ = QString(); | |
1b56c646 | 151 | |
47747218 | 152 | rows_.clear(); |
4913560f | 153 | current_rows_= nullptr; |
47747218 | 154 | class_rows_.clear(); |
1b56c646 SA |
155 | |
156 | logic_mux_data_.reset(); | |
157 | logic_mux_data_invalid_ = true; | |
eee3eab9 SA |
158 | |
159 | decode_reset(); | |
47747218 SA |
160 | } |
161 | ||
946b52e1 SA |
162 | void DecodeSignal::begin_decode() |
163 | { | |
47747218 SA |
164 | if (decode_thread_.joinable()) { |
165 | decode_interrupt_ = true; | |
166 | decode_input_cond_.notify_one(); | |
167 | decode_thread_.join(); | |
168 | } | |
169 | ||
170 | if (logic_mux_thread_.joinable()) { | |
171 | logic_mux_interrupt_ = true; | |
172 | logic_mux_cond_.notify_one(); | |
173 | logic_mux_thread_.join(); | |
174 | } | |
175 | ||
176 | reset_decode(); | |
177 | ||
27a3f09b SA |
178 | if (stack_.size() == 0) { |
179 | error_message_ = tr("No decoders"); | |
180 | return; | |
181 | } | |
182 | ||
183 | assert(channels_.size() > 0); | |
184 | ||
185 | if (get_assigned_signal_count() == 0) { | |
186 | error_message_ = tr("There are no channels assigned to this decoder"); | |
187 | return; | |
188 | } | |
189 | ||
04627946 SA |
190 | // Make sure that all assigned channels still provide logic data |
191 | // (can happen when a converted signal was assigned but the | |
192 | // conversion removed in the meanwhile) | |
193 | for (data::DecodeChannel &ch : channels_) | |
194 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) | |
195 | ch.assigned_signal = nullptr; | |
196 | ||
47747218 SA |
197 | // Check that all decoders have the required channels |
198 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
199 | if (!dec->have_required_channels()) { | |
200 | error_message_ = tr("One or more required channels " | |
201 | "have not been specified"); | |
202 | return; | |
203 | } | |
204 | ||
4913560f | 205 | // Map out all the annotation classes |
47747218 SA |
206 | for (const shared_ptr<decode::Decoder> &dec : stack_) { |
207 | assert(dec); | |
208 | const srd_decoder *const decc = dec->decoder(); | |
209 | assert(dec->decoder()); | |
210 | ||
47747218 SA |
211 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { |
212 | const srd_decoder_annotation_row *const ann_row = | |
213 | (srd_decoder_annotation_row *)l->data; | |
214 | assert(ann_row); | |
215 | ||
216 | const Row row(decc, ann_row); | |
217 | ||
47747218 SA |
218 | for (const GSList *ll = ann_row->ann_classes; |
219 | ll; ll = ll->next) | |
220 | class_rows_[make_pair(decc, | |
221 | GPOINTER_TO_INT(ll->data))] = row; | |
222 | } | |
223 | } | |
224 | ||
4913560f SA |
225 | prepare_annotation_segment(); |
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_) { | |
232 | const int64_t ch_count = get_assigned_signal_count(); | |
233 | const int64_t unit_size = (ch_count + 7) / 8; | |
234 | logic_mux_data_ = make_shared<Logic>(ch_count); | |
83b9c07b SA |
235 | logic_mux_segment_ = make_shared<LogicSegment>(*logic_mux_data_, unit_size, samplerate_); |
236 | logic_mux_data_->push_segment(logic_mux_segment_); | |
27a3f09b | 237 | } |
47747218 | 238 | |
27a3f09b SA |
239 | // Make sure the logic output data is complete and up-to-date |
240 | logic_mux_interrupt_ = false; | |
241 | logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this); | |
242 | ||
243 | // Decode the muxed logic data | |
47747218 SA |
244 | decode_interrupt_ = false; |
245 | decode_thread_ = std::thread(&DecodeSignal::decode_proc, this); | |
a8a9222d SA |
246 | |
247 | // Receive notifications when new sample data is available | |
248 | connect_input_notifiers(); | |
946b52e1 SA |
249 | } |
250 | ||
ecd07c20 SA |
251 | QString DecodeSignal::error_message() const |
252 | { | |
47747218 SA |
253 | lock_guard<mutex> lock(output_mutex_); |
254 | return error_message_; | |
ecd07c20 SA |
255 | } |
256 | ||
47747218 | 257 | const vector<data::DecodeChannel> DecodeSignal::get_channels() const |
9f97b357 SA |
258 | { |
259 | return channels_; | |
260 | } | |
261 | ||
762ab7a4 | 262 | void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec) |
132a5c6d | 263 | { |
47747218 SA |
264 | bool new_assignment = false; |
265 | ||
132a5c6d SA |
266 | // Try to auto-select channels that don't have signals assigned yet |
267 | for (data::DecodeChannel &ch : channels_) { | |
762ab7a4 SA |
268 | // If a decoder is given, auto-assign only its channels |
269 | if (dec && (ch.decoder_ != dec)) | |
270 | continue; | |
271 | ||
132a5c6d SA |
272 | if (ch.assigned_signal) |
273 | continue; | |
274 | ||
692f6093 SA |
275 | for (shared_ptr<data::SignalBase> s : session_.signalbases()) { |
276 | const QString ch_name = ch.name.toLower(); | |
277 | const QString s_name = s->name().toLower(); | |
278 | ||
279 | if (s->logic_data() && | |
280 | ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) { | |
132a5c6d | 281 | ch.assigned_signal = s.get(); |
47747218 SA |
282 | new_assignment = true; |
283 | } | |
692f6093 | 284 | } |
47747218 SA |
285 | } |
286 | ||
287 | if (new_assignment) { | |
288 | logic_mux_data_invalid_ = true; | |
27a3f09b | 289 | commit_decoder_channels(); |
47747218 | 290 | channels_updated(); |
132a5c6d SA |
291 | } |
292 | } | |
293 | ||
9f97b357 SA |
294 | void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal) |
295 | { | |
296 | for (data::DecodeChannel &ch : channels_) | |
47747218 | 297 | if (ch.id == channel_id) { |
9f97b357 | 298 | ch.assigned_signal = signal; |
47747218 SA |
299 | logic_mux_data_invalid_ = true; |
300 | } | |
9f97b357 | 301 | |
27a3f09b | 302 | commit_decoder_channels(); |
9f97b357 | 303 | channels_updated(); |
47747218 | 304 | begin_decode(); |
9f97b357 SA |
305 | } |
306 | ||
27a3f09b SA |
307 | int DecodeSignal::get_assigned_signal_count() const |
308 | { | |
309 | // Count all channels that have a signal assigned to them | |
310 | return count_if(channels_.begin(), channels_.end(), | |
311 | [](data::DecodeChannel ch) { return ch.assigned_signal; }); | |
312 | } | |
313 | ||
9f97b357 SA |
314 | void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) |
315 | { | |
316 | for (data::DecodeChannel &ch : channels_) | |
317 | if (ch.id == channel_id) | |
318 | ch.initial_pin_state = init_state; | |
319 | ||
320 | channels_updated(); | |
321 | ||
47747218 | 322 | begin_decode(); |
9f97b357 SA |
323 | } |
324 | ||
ff83d980 SA |
325 | double DecodeSignal::samplerate() const |
326 | { | |
47747218 | 327 | return samplerate_; |
ff83d980 SA |
328 | } |
329 | ||
330 | const pv::util::Timestamp& DecodeSignal::start_time() const | |
331 | { | |
47747218 | 332 | return start_time_; |
ff83d980 SA |
333 | } |
334 | ||
0c5fe73e SA |
335 | int64_t DecodeSignal::get_working_sample_count() const |
336 | { | |
337 | // The working sample count is the highest sample number for | |
338 | // which all used signals have data available, so go through | |
339 | // all channels and use the lowest overall sample count of the | |
340 | // current segment | |
341 | ||
342 | // TODO Currently, we assume only a single segment exists | |
343 | ||
344 | int64_t count = std::numeric_limits<int64_t>::max(); | |
345 | bool no_signals_assigned = true; | |
346 | ||
347 | for (const data::DecodeChannel &ch : channels_) | |
348 | if (ch.assigned_signal) { | |
349 | no_signals_assigned = false; | |
350 | ||
351 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
352 | if (!logic_data || logic_data->logic_segments().empty()) | |
353 | return 0; | |
354 | ||
355 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front(); | |
356 | count = min(count, (int64_t)segment->get_sample_count()); | |
357 | } | |
358 | ||
359 | return (no_signals_assigned ? 0 : count); | |
360 | } | |
361 | ||
362 | int64_t DecodeSignal::get_decoded_sample_count() const | |
ff83d980 | 363 | { |
47747218 SA |
364 | lock_guard<mutex> decode_lock(output_mutex_); |
365 | return samples_decoded_; | |
ff83d980 SA |
366 | } |
367 | ||
ecd07c20 SA |
368 | vector<Row> DecodeSignal::visible_rows() const |
369 | { | |
47747218 SA |
370 | lock_guard<mutex> lock(output_mutex_); |
371 | ||
372 | vector<Row> rows; | |
373 | ||
374 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
375 | assert(dec); | |
376 | if (!dec->shown()) | |
377 | continue; | |
378 | ||
379 | const srd_decoder *const decc = dec->decoder(); | |
380 | assert(dec->decoder()); | |
381 | ||
382 | // Add a row for the decoder if it doesn't have a row list | |
383 | if (!decc->annotation_rows) | |
384 | rows.emplace_back(decc); | |
385 | ||
386 | // Add the decoder rows | |
387 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
388 | const srd_decoder_annotation_row *const ann_row = | |
389 | (srd_decoder_annotation_row *)l->data; | |
390 | assert(ann_row); | |
391 | rows.emplace_back(decc, ann_row); | |
392 | } | |
393 | } | |
394 | ||
395 | return rows; | |
ecd07c20 SA |
396 | } |
397 | ||
398 | void DecodeSignal::get_annotation_subset( | |
399 | vector<pv::data::decode::Annotation> &dest, | |
400 | const decode::Row &row, uint64_t start_sample, | |
401 | uint64_t end_sample) const | |
402 | { | |
47747218 SA |
403 | lock_guard<mutex> lock(output_mutex_); |
404 | ||
4913560f SA |
405 | if (!current_rows_) |
406 | return; | |
407 | ||
408 | // TODO Instead of current_rows_, use rows_ and the ID of the segment | |
409 | ||
410 | const auto iter = current_rows_->find(row); | |
411 | if (iter != current_rows_->end()) | |
47747218 SA |
412 | (*iter).second.get_annotation_subset(dest, |
413 | start_sample, end_sample); | |
414 | } | |
415 | ||
416 | void DecodeSignal::save_settings(QSettings &settings) const | |
417 | { | |
418 | SignalBase::save_settings(settings); | |
419 | ||
c8e60bdf SA |
420 | settings.setValue("decoders", (int)(stack_.size())); |
421 | ||
8b5ea5a1 | 422 | // Save decoder stack |
c8e60bdf SA |
423 | int decoder_idx = 0; |
424 | for (shared_ptr<decode::Decoder> decoder : stack_) { | |
425 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); | |
426 | ||
427 | settings.setValue("id", decoder->decoder()->id); | |
428 | ||
d3feec23 SA |
429 | // Save decoder options |
430 | const map<string, GVariant*>& options = decoder->options(); | |
431 | ||
432 | settings.setValue("options", (int)options.size()); | |
433 | ||
434 | // Note: decode::Decoder::options() returns only the options | |
435 | // that differ from the default. See binding::Decoder::getter() | |
436 | int i = 0; | |
437 | for (auto option : options) { | |
438 | settings.beginGroup("option" + QString::number(i)); | |
439 | settings.setValue("name", QString::fromStdString(option.first)); | |
440 | GlobalSettings::store_gvariant(settings, option.second); | |
441 | settings.endGroup(); | |
442 | i++; | |
443 | } | |
444 | ||
c8e60bdf SA |
445 | settings.endGroup(); |
446 | } | |
447 | ||
8b5ea5a1 SA |
448 | // Save channel mapping |
449 | settings.setValue("channels", (int)channels_.size()); | |
450 | ||
451 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
452 | auto channel = find_if(channels_.begin(), channels_.end(), | |
453 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
454 | ||
455 | if (channel == channels_.end()) { | |
456 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
457 | continue; | |
458 | } | |
459 | ||
460 | settings.beginGroup("channel" + QString::number(channel_id)); | |
461 | ||
462 | settings.setValue("name", channel->name); // Useful for debugging | |
463 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
464 | ||
465 | if (channel->assigned_signal) | |
466 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
467 | ||
468 | settings.endGroup(); | |
469 | } | |
47747218 SA |
470 | } |
471 | ||
472 | void DecodeSignal::restore_settings(QSettings &settings) | |
473 | { | |
474 | SignalBase::restore_settings(settings); | |
475 | ||
8b5ea5a1 | 476 | // Restore decoder stack |
c8e60bdf SA |
477 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); |
478 | ||
479 | int decoders = settings.value("decoders").toInt(); | |
480 | ||
481 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
482 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
483 | ||
484 | QString id = settings.value("id").toString(); | |
485 | ||
486 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
487 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
488 | if (!dec) | |
489 | continue; | |
490 | ||
491 | if (QString::fromUtf8(dec->id) == id) { | |
d3feec23 SA |
492 | shared_ptr<decode::Decoder> decoder = |
493 | make_shared<decode::Decoder>(dec); | |
494 | ||
495 | stack_.push_back(decoder); | |
496 | ||
497 | // Restore decoder options that differ from their default | |
498 | int options = settings.value("options").toInt(); | |
499 | ||
500 | for (int i = 0; i < options; i++) { | |
501 | settings.beginGroup("option" + QString::number(i)); | |
502 | QString name = settings.value("name").toString(); | |
503 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
504 | decoder->set_option(name.toUtf8(), value); | |
505 | settings.endGroup(); | |
506 | } | |
c8e60bdf SA |
507 | |
508 | // Include the newly created decode channels in the channel lists | |
509 | update_channel_list(); | |
510 | break; | |
511 | } | |
512 | } | |
513 | ||
514 | settings.endGroup(); | |
4ca06ec3 | 515 | channels_updated(); |
c8e60bdf SA |
516 | } |
517 | ||
8b5ea5a1 SA |
518 | // Restore channel mapping |
519 | unsigned int channels = settings.value("channels").toInt(); | |
520 | ||
521 | const unordered_set< shared_ptr<data::SignalBase> > signalbases = | |
522 | session_.signalbases(); | |
523 | ||
524 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
525 | auto channel = find_if(channels_.begin(), channels_.end(), | |
526 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
527 | ||
528 | if (channel == channels_.end()) { | |
529 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
530 | continue; | |
531 | } | |
532 | ||
533 | settings.beginGroup("channel" + QString::number(channel_id)); | |
534 | ||
535 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
536 | ||
537 | for (shared_ptr<data::SignalBase> signal : signalbases) | |
538 | if (signal->name() == assigned_signal_name) | |
539 | channel->assigned_signal = signal.get(); | |
540 | ||
541 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
542 | ||
543 | settings.endGroup(); | |
544 | } | |
545 | ||
aa71890d SA |
546 | // Update the internal structures |
547 | update_channel_list(); | |
548 | commit_decoder_channels(); | |
549 | ||
8b5ea5a1 | 550 | begin_decode(); |
47747218 SA |
551 | } |
552 | ||
9f97b357 SA |
553 | void DecodeSignal::update_channel_list() |
554 | { | |
47747218 | 555 | vector<data::DecodeChannel> prev_channels = channels_; |
9f97b357 SA |
556 | channels_.clear(); |
557 | ||
558 | uint16_t id = 0; | |
559 | ||
560 | // Copy existing entries, create new as needed | |
47747218 | 561 | for (shared_ptr<Decoder> decoder : stack_) { |
9f97b357 SA |
562 | const srd_decoder* srd_d = decoder->decoder(); |
563 | const GSList *l; | |
564 | ||
565 | // Mandatory channels | |
566 | for (l = srd_d->channels; l; l = l->next) { | |
567 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
568 | bool ch_added = false; | |
569 | ||
570 | // Copy but update ID if this channel was in the list before | |
47747218 | 571 | for (data::DecodeChannel &ch : prev_channels) |
9f97b357 SA |
572 | if (ch.pdch_ == pdch) { |
573 | ch.id = id++; | |
574 | channels_.push_back(ch); | |
575 | ch_added = true; | |
576 | break; | |
577 | } | |
578 | ||
579 | if (!ch_added) { | |
580 | // Create new entry without a mapped signal | |
6e7a4a00 | 581 | data::DecodeChannel ch = {id++, 0, false, nullptr, |
9f97b357 SA |
582 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
583 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
584 | channels_.push_back(ch); | |
585 | } | |
586 | } | |
587 | ||
588 | // Optional channels | |
589 | for (l = srd_d->opt_channels; l; l = l->next) { | |
590 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
591 | bool ch_added = false; | |
592 | ||
593 | // Copy but update ID if this channel was in the list before | |
47747218 | 594 | for (data::DecodeChannel &ch : prev_channels) |
9f97b357 SA |
595 | if (ch.pdch_ == pdch) { |
596 | ch.id = id++; | |
597 | channels_.push_back(ch); | |
598 | ch_added = true; | |
599 | break; | |
600 | } | |
601 | ||
602 | if (!ch_added) { | |
603 | // Create new entry without a mapped signal | |
6e7a4a00 | 604 | data::DecodeChannel ch = {id++, 0, true, nullptr, |
9f97b357 SA |
605 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
606 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
607 | channels_.push_back(ch); | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
47747218 SA |
612 | // Invalidate the logic output data if the channel assignment changed |
613 | if (prev_channels.size() != channels_.size()) { | |
614 | // The number of channels changed, there's definitely a difference | |
615 | logic_mux_data_invalid_ = true; | |
616 | } else { | |
617 | // Same number but assignment may still differ, so compare all channels | |
618 | for (size_t i = 0; i < channels_.size(); i++) { | |
619 | const data::DecodeChannel &p_ch = prev_channels[i]; | |
620 | const data::DecodeChannel &ch = channels_[i]; | |
621 | ||
622 | if ((p_ch.pdch_ != ch.pdch_) || | |
623 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
624 | logic_mux_data_invalid_ = true; | |
625 | break; | |
626 | } | |
627 | } | |
628 | ||
629 | } | |
630 | ||
9f97b357 SA |
631 | channels_updated(); |
632 | } | |
633 | ||
27a3f09b | 634 | void DecodeSignal::commit_decoder_channels() |
ad908057 | 635 | { |
27a3f09b SA |
636 | // Submit channel list to every decoder, containing only the relevant channels |
637 | for (shared_ptr<decode::Decoder> dec : stack_) { | |
638 | vector<data::DecodeChannel*> channel_list; | |
639 | ||
640 | for (data::DecodeChannel &ch : channels_) | |
641 | if (ch.decoder_ == dec) | |
642 | channel_list.push_back(&ch); | |
47747218 | 643 | |
27a3f09b SA |
644 | dec->set_channels(channel_list); |
645 | } | |
aa71890d SA |
646 | |
647 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
648 | int id = 0; | |
649 | for (data::DecodeChannel &ch : channels_) | |
650 | if (ch.assigned_signal) | |
651 | ch.bit_id = id++; | |
47747218 SA |
652 | } |
653 | ||
27a3f09b | 654 | void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end) |
47747218 | 655 | { |
27a3f09b SA |
656 | // Enforce end to be greater than start |
657 | if (end <= start) | |
658 | return; | |
659 | ||
660 | // Fetch all segments and their data | |
661 | // TODO Currently, we assume only a single segment exists | |
662 | vector<shared_ptr<LogicSegment> > segments; | |
663 | vector<const uint8_t*> signal_data; | |
664 | vector<uint8_t> signal_in_bytepos; | |
665 | vector<uint8_t> signal_in_bitpos; | |
666 | ||
667 | for (data::DecodeChannel &ch : channels_) | |
668 | if (ch.assigned_signal) { | |
669 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
670 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front(); | |
671 | segments.push_back(segment); | |
b82243f7 SA |
672 | |
673 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
674 | segment->get_samples(start, end, data); | |
675 | signal_data.push_back(data); | |
27a3f09b SA |
676 | |
677 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
678 | signal_in_bytepos.push_back(bitpos / 8); | |
679 | signal_in_bitpos.push_back(bitpos % 8); | |
680 | } | |
681 | ||
682 | // Perform the muxing of signal data into the output data | |
83b9c07b | 683 | uint8_t* output = new uint8_t[(end - start) * logic_mux_segment_->unit_size()]; |
27a3f09b SA |
684 | unsigned int signal_count = signal_data.size(); |
685 | ||
686 | for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) { | |
687 | int bitpos = 0; | |
688 | uint8_t bytepos = 0; | |
689 | ||
83b9c07b SA |
690 | const int out_sample_pos = sample_cnt * logic_mux_segment_->unit_size(); |
691 | for (unsigned int i = 0; i < logic_mux_segment_->unit_size(); i++) | |
27a3f09b | 692 | output[out_sample_pos + i] = 0; |
47747218 | 693 | |
27a3f09b SA |
694 | for (unsigned int i = 0; i < signal_count; i++) { |
695 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
696 | const uint8_t in_sample = 1 & | |
697 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
47747218 | 698 | |
27a3f09b SA |
699 | const uint8_t out_sample = output[out_sample_pos + bytepos]; |
700 | ||
701 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
702 | ||
703 | bitpos++; | |
704 | if (bitpos > 7) { | |
705 | bitpos = 0; | |
706 | bytepos++; | |
707 | } | |
708 | } | |
47747218 SA |
709 | } |
710 | ||
83b9c07b | 711 | logic_mux_segment_->append_payload(output, (end - start) * logic_mux_segment_->unit_size()); |
27a3f09b SA |
712 | delete[] output; |
713 | ||
714 | for (const uint8_t* data : signal_data) | |
715 | delete[] data; | |
716 | } | |
717 | ||
718 | void DecodeSignal::logic_mux_proc() | |
719 | { | |
720 | do { | |
27a3f09b | 721 | const uint64_t input_sample_count = get_working_sample_count(); |
83b9c07b | 722 | const uint64_t output_sample_count = logic_mux_segment_->get_sample_count(); |
27a3f09b SA |
723 | |
724 | const uint64_t samples_to_process = | |
725 | (input_sample_count > output_sample_count) ? | |
726 | (input_sample_count - output_sample_count) : 0; | |
727 | ||
728 | // Process the samples if necessary... | |
729 | if (samples_to_process > 0) { | |
83b9c07b | 730 | const uint64_t unit_size = logic_mux_segment_->unit_size(); |
27a3f09b SA |
731 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; |
732 | ||
733 | uint64_t processed_samples = 0; | |
734 | do { | |
735 | const uint64_t start_sample = output_sample_count + processed_samples; | |
736 | const uint64_t sample_count = | |
737 | min(samples_to_process - processed_samples, chunk_sample_count); | |
738 | ||
739 | mux_logic_samples(start_sample, start_sample + sample_count); | |
740 | processed_samples += sample_count; | |
741 | ||
742 | // ...and process the newly muxed logic data | |
743 | decode_input_cond_.notify_one(); | |
744 | } while (processed_samples < samples_to_process); | |
745 | } | |
746 | ||
1ec191ed | 747 | if (samples_to_process == 0) { |
27a3f09b SA |
748 | // Wait for more input |
749 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
750 | logic_mux_cond_.wait(logic_mux_lock); | |
751 | } | |
1ec191ed | 752 | } while (!logic_mux_interrupt_); |
47747218 SA |
753 | } |
754 | ||
a3ebd556 SA |
755 | void DecodeSignal::query_input_metadata() |
756 | { | |
757 | // Update the samplerate and start time because we cannot start | |
758 | // the libsrd session without the current samplerate | |
759 | ||
760 | // TODO Currently we assume all channels have the same sample rate | |
761 | // and start time | |
762 | bool samplerate_valid = false; | |
8ebf8079 SA |
763 | data::DecodeChannel *any_channel; |
764 | shared_ptr<Logic> logic_data; | |
a3ebd556 | 765 | |
8ebf8079 SA |
766 | do { |
767 | any_channel = &(*find_if(channels_.begin(), channels_.end(), | |
768 | [](data::DecodeChannel ch) { return ch.assigned_signal; })); | |
a3ebd556 | 769 | |
8ebf8079 SA |
770 | logic_data = any_channel->assigned_signal->logic_data(); |
771 | ||
772 | if (!logic_data) { | |
773 | // Wait until input data is available or an interrupt was requested | |
774 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
775 | decode_input_cond_.wait(input_wait_lock); | |
776 | } | |
777 | } while (!logic_data && !decode_interrupt_); | |
778 | ||
779 | if (decode_interrupt_) | |
780 | return; | |
a3ebd556 SA |
781 | |
782 | do { | |
783 | if (!logic_data->logic_segments().empty()) { | |
784 | shared_ptr<LogicSegment> first_segment = | |
785 | any_channel->assigned_signal->logic_data()->logic_segments().front(); | |
786 | start_time_ = first_segment->start_time(); | |
787 | samplerate_ = first_segment->samplerate(); | |
788 | if (samplerate_ > 0) | |
789 | samplerate_valid = true; | |
790 | } | |
791 | ||
1ec191ed SA |
792 | if (!samplerate_valid) { |
793 | // Wait until input data is available or an interrupt was requested | |
794 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
795 | decode_input_cond_.wait(input_wait_lock); | |
796 | } | |
a3ebd556 SA |
797 | } while (!samplerate_valid && !decode_interrupt_); |
798 | } | |
799 | ||
47747218 | 800 | void DecodeSignal::decode_data( |
e91883bb | 801 | const int64_t abs_start_samplenum, const int64_t sample_count) |
47747218 | 802 | { |
83b9c07b | 803 | const int64_t unit_size = logic_mux_segment_->unit_size(); |
27a3f09b | 804 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; |
47747218 SA |
805 | |
806 | for (int64_t i = abs_start_samplenum; | |
807 | !decode_interrupt_ && (i < (abs_start_samplenum + sample_count)); | |
808 | i += chunk_sample_count) { | |
809 | ||
810 | const int64_t chunk_end = min(i + chunk_sample_count, | |
811 | abs_start_samplenum + sample_count); | |
812 | ||
b82243f7 SA |
813 | int64_t data_size = (chunk_end - i) * unit_size; |
814 | uint8_t* chunk = new uint8_t[data_size]; | |
83b9c07b | 815 | logic_mux_segment_->get_samples(i, chunk_end, chunk); |
47747218 | 816 | |
e91883bb | 817 | if (srd_session_send(srd_session_, i, chunk_end, chunk, |
b82243f7 | 818 | data_size, unit_size) != SRD_OK) { |
47747218 SA |
819 | error_message_ = tr("Decoder reported an error"); |
820 | delete[] chunk; | |
821 | break; | |
822 | } | |
a3ebd556 | 823 | |
47747218 SA |
824 | delete[] chunk; |
825 | ||
826 | { | |
827 | lock_guard<mutex> lock(output_mutex_); | |
828 | samples_decoded_ = chunk_end; | |
829 | } | |
1b56c646 SA |
830 | |
831 | // Notify the frontend that we processed some data and | |
832 | // possibly have new annotations as well | |
833 | new_annotations(); | |
47747218 SA |
834 | } |
835 | } | |
836 | ||
837 | void DecodeSignal::decode_proc() | |
838 | { | |
a3ebd556 SA |
839 | query_input_metadata(); |
840 | ||
841 | if (decode_interrupt_) | |
842 | return; | |
843 | ||
e91883bb | 844 | start_srd_session(); |
47747218 | 845 | |
27a3f09b SA |
846 | uint64_t sample_count; |
847 | uint64_t abs_start_samplenum = 0; | |
47747218 | 848 | do { |
27a3f09b SA |
849 | // Keep processing new samples until we exhaust the input data |
850 | do { | |
767bf4e7 | 851 | lock_guard<mutex> input_lock(input_mutex_); |
83b9c07b | 852 | sample_count = logic_mux_segment_->get_sample_count() - abs_start_samplenum; |
27a3f09b SA |
853 | |
854 | if (sample_count > 0) { | |
e91883bb | 855 | decode_data(abs_start_samplenum, sample_count); |
27a3f09b SA |
856 | abs_start_samplenum += sample_count; |
857 | } | |
1b56c646 SA |
858 | } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_); |
859 | ||
860 | if (error_message_.isEmpty() && !decode_interrupt_) { | |
861 | if (sample_count == 0) | |
862 | decode_finished(); | |
27a3f09b | 863 | |
a3ebd556 | 864 | // Wait for new input data or an interrupt was requested |
27a3f09b SA |
865 | unique_lock<mutex> input_wait_lock(input_mutex_); |
866 | decode_input_cond_.wait(input_wait_lock); | |
867 | } | |
868 | } while (error_message_.isEmpty() && !decode_interrupt_); | |
e91883bb SA |
869 | } |
870 | ||
871 | void DecodeSignal::start_srd_session() | |
872 | { | |
a3ebd556 SA |
873 | if (srd_session_) |
874 | stop_srd_session(); | |
e91883bb | 875 | |
a3ebd556 SA |
876 | // Create the session |
877 | srd_session_new(&srd_session_); | |
878 | assert(srd_session_); | |
47747218 | 879 | |
a3ebd556 SA |
880 | // Create the decoders |
881 | srd_decoder_inst *prev_di = nullptr; | |
882 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
883 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); | |
e91883bb | 884 | |
a3ebd556 SA |
885 | if (!di) { |
886 | error_message_ = tr("Failed to create decoder instance"); | |
887 | srd_session_destroy(srd_session_); | |
888 | return; | |
889 | } | |
e91883bb | 890 | |
a3ebd556 SA |
891 | if (prev_di) |
892 | srd_inst_stack(srd_session_, prev_di, di); | |
e91883bb | 893 | |
a3ebd556 | 894 | prev_di = di; |
e91883bb | 895 | } |
a3ebd556 SA |
896 | |
897 | // Start the session | |
898 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
899 | g_variant_new_uint64(samplerate_)); | |
900 | ||
901 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
902 | DecodeSignal::annotation_callback, this); | |
903 | ||
904 | srd_session_start(srd_session_); | |
e91883bb | 905 | } |
47747218 | 906 | |
e91883bb SA |
907 | void DecodeSignal::stop_srd_session() |
908 | { | |
909 | if (srd_session_) { | |
910 | // Destroy the session | |
911 | srd_session_destroy(srd_session_); | |
912 | srd_session_ = nullptr; | |
913 | } | |
47747218 SA |
914 | } |
915 | ||
a8a9222d SA |
916 | void DecodeSignal::connect_input_notifiers() |
917 | { | |
918 | // Disconnect the notification slot from the previous set of signals | |
1b56c646 | 919 | disconnect(this, SLOT(on_data_cleared())); |
a8a9222d SA |
920 | disconnect(this, SLOT(on_data_received())); |
921 | ||
922 | // Connect the currently used signals to our slot | |
923 | for (data::DecodeChannel &ch : channels_) { | |
924 | if (!ch.assigned_signal) | |
925 | continue; | |
926 | ||
1b56c646 SA |
927 | const data::SignalBase *signal = ch.assigned_signal; |
928 | connect(signal, SIGNAL(samples_cleared()), | |
929 | this, SLOT(on_data_cleared())); | |
930 | connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), | |
a8a9222d SA |
931 | this, SLOT(on_data_received())); |
932 | } | |
933 | } | |
934 | ||
4913560f SA |
935 | void DecodeSignal::prepare_annotation_segment() |
936 | { | |
937 | // TODO Won't work for multiple segments | |
938 | rows_.emplace_back(map<const decode::Row, decode::RowData>()); | |
939 | current_rows_ = &(rows_.back()); | |
940 | ||
941 | // Add annotation classes | |
942 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
943 | assert(dec); | |
944 | const srd_decoder *const decc = dec->decoder(); | |
945 | assert(dec->decoder()); | |
946 | ||
947 | // Add a row for the decoder if it doesn't have a row list | |
948 | if (!decc->annotation_rows) | |
949 | (*current_rows_)[Row(decc)] = decode::RowData(); | |
950 | ||
951 | // Add the decoder rows | |
952 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
953 | const srd_decoder_annotation_row *const ann_row = | |
954 | (srd_decoder_annotation_row *)l->data; | |
955 | assert(ann_row); | |
956 | ||
957 | const Row row(decc, ann_row); | |
958 | ||
959 | // Add a new empty row data object | |
960 | (*current_rows_)[row] = decode::RowData(); | |
961 | } | |
962 | } | |
963 | } | |
964 | ||
47747218 SA |
965 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) |
966 | { | |
967 | assert(pdata); | |
b21501d6 | 968 | assert(decode_signal); |
47747218 SA |
969 | |
970 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
971 | assert(ds); | |
972 | ||
973 | lock_guard<mutex> lock(ds->output_mutex_); | |
974 | ||
47747218 SA |
975 | // Find the row |
976 | assert(pdata->pdo); | |
977 | assert(pdata->pdo->di); | |
978 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
979 | assert(decc); | |
4913560f | 980 | assert(ds->current_rows_); |
47747218 | 981 | |
1becee23 UH |
982 | const srd_proto_data_annotation *const pda = |
983 | (const srd_proto_data_annotation*)pdata->data; | |
984 | assert(pda); | |
985 | ||
4913560f | 986 | auto row_iter = ds->current_rows_->end(); |
47747218 SA |
987 | |
988 | // Try looking up the sub-row of this class | |
1becee23 UH |
989 | const auto format = pda->ann_class; |
990 | const auto r = ds->class_rows_.find(make_pair(decc, format)); | |
47747218 | 991 | if (r != ds->class_rows_.end()) |
4913560f | 992 | row_iter = ds->current_rows_->find((*r).second); |
47747218 SA |
993 | else { |
994 | // Failing that, use the decoder as a key | |
4913560f | 995 | row_iter = ds->current_rows_->find(Row(decc)); |
47747218 SA |
996 | } |
997 | ||
4913560f | 998 | if (row_iter == ds->current_rows_->end()) { |
47747218 | 999 | qDebug() << "Unexpected annotation: decoder = " << decc << |
1becee23 | 1000 | ", format = " << format; |
47747218 SA |
1001 | assert(false); |
1002 | return; | |
1003 | } | |
1004 | ||
1005 | // Add the annotation | |
1becee23 | 1006 | (*row_iter).second.emplace_annotation(pdata); |
47747218 SA |
1007 | } |
1008 | ||
1009 | void DecodeSignal::on_capture_state_changed(int state) | |
1010 | { | |
1011 | // If a new acquisition was started, we need to start decoding from scratch | |
1012 | if (state == Session::Running) | |
1013 | begin_decode(); | |
1014 | } | |
1015 | ||
1b56c646 SA |
1016 | void DecodeSignal::on_data_cleared() |
1017 | { | |
1018 | reset_decode(); | |
1019 | } | |
1020 | ||
47747218 SA |
1021 | void DecodeSignal::on_data_received() |
1022 | { | |
1b56c646 SA |
1023 | if (!logic_mux_thread_.joinable()) |
1024 | begin_decode(); | |
1025 | else | |
1026 | logic_mux_cond_.notify_one(); | |
47747218 SA |
1027 | } |
1028 | ||
ad908057 SA |
1029 | } // namespace data |
1030 | } // namespace pv |