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