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