]>
Commit | Line | Data |
---|---|---|
ad908057 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
0c5fe73e SA |
20 | #include <limits> |
21 | ||
47747218 SA |
22 | #include <QDebug> |
23 | ||
ad908057 SA |
24 | #include "logic.hpp" |
25 | #include "logicsegment.hpp" | |
26 | #include "decodesignal.hpp" | |
27 | #include "signaldata.hpp" | |
28 | ||
29 | #include <pv/binding/decoder.hpp> | |
30 | #include <pv/data/decode/decoder.hpp> | |
ecd07c20 | 31 | #include <pv/data/decode/row.hpp> |
d3feec23 | 32 | #include <pv/globalsettings.hpp> |
ad908057 SA |
33 | #include <pv/session.hpp> |
34 | ||
47747218 SA |
35 | using std::lock_guard; |
36 | using std::make_pair; | |
ad908057 | 37 | using std::make_shared; |
0c5fe73e | 38 | using std::min; |
5ecf957f | 39 | using std::out_of_range; |
ad908057 | 40 | using std::shared_ptr; |
47747218 SA |
41 | using std::unique_lock; |
42 | using pv::data::decode::Annotation; | |
ad908057 | 43 | using pv::data::decode::Decoder; |
ecd07c20 | 44 | using pv::data::decode::Row; |
ad908057 SA |
45 | |
46 | namespace pv { | |
47 | namespace data { | |
48 | ||
47747218 SA |
49 | const double DecodeSignal::DecodeMargin = 1.0; |
50 | const double DecodeSignal::DecodeThreshold = 0.2; | |
3fbddf7f | 51 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; |
ad908057 | 52 | |
ad908057 | 53 | |
47747218 SA |
54 | DecodeSignal::DecodeSignal(pv::Session &session) : |
55 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
56 | session_(session), | |
e91883bb | 57 | srd_session_(nullptr), |
47747218 SA |
58 | logic_mux_data_invalid_(false), |
59 | start_time_(0), | |
60 | samplerate_(0), | |
47747218 SA |
61 | samples_decoded_(0), |
62 | frame_complete_(false) | |
ad908057 | 63 | { |
47747218 SA |
64 | connect(&session_, SIGNAL(capture_state_changed(int)), |
65 | this, SLOT(on_capture_state_changed(int))); | |
ad908057 SA |
66 | } |
67 | ||
47747218 | 68 | DecodeSignal::~DecodeSignal() |
ad908057 | 69 | { |
1b56c646 | 70 | reset_decode(); |
ad908057 SA |
71 | } |
72 | ||
47747218 | 73 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const |
ecd07c20 | 74 | { |
47747218 | 75 | return stack_; |
ecd07c20 SA |
76 | } |
77 | ||
c8e60bdf | 78 | void DecodeSignal::stack_decoder(const srd_decoder *decoder) |
ad908057 SA |
79 | { |
80 | assert(decoder); | |
762ab7a4 SA |
81 | const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder); |
82 | ||
83 | stack_.push_back(dec); | |
47747218 SA |
84 | |
85 | // Set name if this decoder is the first in the list | |
86 | if (stack_.size() == 1) | |
87 | set_name(QString::fromUtf8(decoder->name)); | |
132a5c6d | 88 | |
27a3f09b | 89 | // Include the newly created decode channels in the channel lists |
9f97b357 | 90 | update_channel_list(); |
132a5c6d | 91 | |
762ab7a4 | 92 | auto_assign_signals(dec); |
27a3f09b | 93 | commit_decoder_channels(); |
47747218 | 94 | begin_decode(); |
ad908057 SA |
95 | } |
96 | ||
97 | void DecodeSignal::remove_decoder(int index) | |
98 | { | |
47747218 SA |
99 | assert(index >= 0); |
100 | assert(index < (int)stack_.size()); | |
101 | ||
102 | // Find the decoder in the stack | |
103 | auto iter = stack_.begin(); | |
104 | for (int i = 0; i < index; i++, iter++) | |
105 | assert(iter != stack_.end()); | |
106 | ||
107 | // Delete the element | |
108 | stack_.erase(iter); | |
109 | ||
110 | // Update channels and decoded data | |
9f97b357 | 111 | update_channel_list(); |
47747218 | 112 | begin_decode(); |
ad908057 SA |
113 | } |
114 | ||
115 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
116 | { | |
47747218 | 117 | auto iter = stack_.cbegin(); |
ad908057 | 118 | for (int i = 0; i < index; i++, iter++) |
47747218 | 119 | assert(iter != stack_.end()); |
ad908057 SA |
120 | |
121 | shared_ptr<Decoder> dec = *iter; | |
122 | ||
123 | // Toggle decoder visibility | |
124 | bool state = false; | |
125 | if (dec) { | |
126 | state = !dec->shown(); | |
127 | dec->show(state); | |
128 | } | |
129 | ||
130 | return state; | |
131 | } | |
132 | ||
47747218 SA |
133 | void DecodeSignal::reset_decode() |
134 | { | |
1b56c646 SA |
135 | if (decode_thread_.joinable()) { |
136 | decode_interrupt_ = true; | |
137 | decode_input_cond_.notify_one(); | |
138 | decode_thread_.join(); | |
139 | } | |
140 | ||
141 | if (logic_mux_thread_.joinable()) { | |
142 | logic_mux_interrupt_ = true; | |
143 | logic_mux_cond_.notify_one(); | |
144 | logic_mux_thread_.join(); | |
145 | } | |
146 | ||
e91883bb SA |
147 | stop_srd_session(); |
148 | ||
47747218 SA |
149 | frame_complete_ = false; |
150 | samples_decoded_ = 0; | |
5ecf957f | 151 | currently_processed_segment_ = 0; |
47747218 | 152 | error_message_ = QString(); |
1b56c646 | 153 | |
f2f4845f | 154 | segmented_rows_.clear(); |
4913560f | 155 | current_rows_= nullptr; |
47747218 | 156 | class_rows_.clear(); |
1b56c646 SA |
157 | |
158 | logic_mux_data_.reset(); | |
159 | logic_mux_data_invalid_ = true; | |
eee3eab9 SA |
160 | |
161 | decode_reset(); | |
47747218 SA |
162 | } |
163 | ||
946b52e1 SA |
164 | void DecodeSignal::begin_decode() |
165 | { | |
47747218 SA |
166 | if (decode_thread_.joinable()) { |
167 | decode_interrupt_ = true; | |
168 | decode_input_cond_.notify_one(); | |
169 | decode_thread_.join(); | |
170 | } | |
171 | ||
172 | if (logic_mux_thread_.joinable()) { | |
173 | logic_mux_interrupt_ = true; | |
174 | logic_mux_cond_.notify_one(); | |
175 | logic_mux_thread_.join(); | |
176 | } | |
177 | ||
178 | reset_decode(); | |
179 | ||
27a3f09b SA |
180 | if (stack_.size() == 0) { |
181 | error_message_ = tr("No decoders"); | |
182 | return; | |
183 | } | |
184 | ||
185 | assert(channels_.size() > 0); | |
186 | ||
187 | if (get_assigned_signal_count() == 0) { | |
188 | error_message_ = tr("There are no channels assigned to this decoder"); | |
189 | return; | |
190 | } | |
191 | ||
04627946 SA |
192 | // Make sure that all assigned channels still provide logic data |
193 | // (can happen when a converted signal was assigned but the | |
194 | // conversion removed in the meanwhile) | |
195 | for (data::DecodeChannel &ch : channels_) | |
196 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) | |
197 | ch.assigned_signal = nullptr; | |
198 | ||
47747218 SA |
199 | // Check that all decoders have the required channels |
200 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
201 | if (!dec->have_required_channels()) { | |
202 | error_message_ = tr("One or more required channels " | |
203 | "have not been specified"); | |
204 | return; | |
205 | } | |
206 | ||
4913560f | 207 | // Map out all the annotation classes |
47747218 SA |
208 | for (const shared_ptr<decode::Decoder> &dec : stack_) { |
209 | assert(dec); | |
210 | const srd_decoder *const decc = dec->decoder(); | |
211 | assert(dec->decoder()); | |
212 | ||
47747218 SA |
213 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { |
214 | const srd_decoder_annotation_row *const ann_row = | |
215 | (srd_decoder_annotation_row *)l->data; | |
216 | assert(ann_row); | |
217 | ||
218 | const Row row(decc, ann_row); | |
219 | ||
47747218 SA |
220 | for (const GSList *ll = ann_row->ann_classes; |
221 | ll; ll = ll->next) | |
222 | class_rows_[make_pair(decc, | |
223 | GPOINTER_TO_INT(ll->data))] = row; | |
224 | } | |
225 | } | |
226 | ||
f2f4845f SA |
227 | create_new_annotation_segment(); |
228 | ||
229 | // TODO Allow logic_mux_data and logic_mux_segment to work with multiple segments | |
4913560f | 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 | ||
5ecf957f | 339 | int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const |
0c5fe73e SA |
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 | ||
0c5fe73e SA |
346 | int64_t count = std::numeric_limits<int64_t>::max(); |
347 | bool no_signals_assigned = true; | |
348 | ||
349 | for (const data::DecodeChannel &ch : channels_) | |
350 | if (ch.assigned_signal) { | |
351 | no_signals_assigned = false; | |
352 | ||
353 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
354 | if (!logic_data || logic_data->logic_segments().empty()) | |
355 | return 0; | |
356 | ||
5ecf957f SA |
357 | try { |
358 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
359 | count = min(count, (int64_t)segment->get_sample_count()); | |
360 | } catch (out_of_range) { | |
361 | return 0; | |
362 | } | |
0c5fe73e SA |
363 | } |
364 | ||
365 | return (no_signals_assigned ? 0 : count); | |
366 | } | |
367 | ||
5ecf957f | 368 | int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id) const |
ff83d980 | 369 | { |
47747218 | 370 | lock_guard<mutex> decode_lock(output_mutex_); |
5ecf957f SA |
371 | |
372 | int64_t result = 0; | |
373 | ||
374 | if (segment_id == currently_processed_segment_) | |
375 | result = samples_decoded_; | |
376 | else | |
377 | if (segment_id < currently_processed_segment_) | |
378 | // Segment was already decoded fully | |
379 | result = get_working_sample_count(segment_id); | |
380 | else | |
381 | // Segment wasn't decoded at all yet | |
382 | result = 0; | |
383 | ||
384 | return result; | |
ff83d980 SA |
385 | } |
386 | ||
ecd07c20 SA |
387 | vector<Row> DecodeSignal::visible_rows() const |
388 | { | |
47747218 SA |
389 | lock_guard<mutex> lock(output_mutex_); |
390 | ||
391 | vector<Row> rows; | |
392 | ||
393 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
394 | assert(dec); | |
395 | if (!dec->shown()) | |
396 | continue; | |
397 | ||
398 | const srd_decoder *const decc = dec->decoder(); | |
399 | assert(dec->decoder()); | |
400 | ||
401 | // Add a row for the decoder if it doesn't have a row list | |
402 | if (!decc->annotation_rows) | |
403 | rows.emplace_back(decc); | |
404 | ||
405 | // Add the decoder rows | |
406 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
407 | const srd_decoder_annotation_row *const ann_row = | |
408 | (srd_decoder_annotation_row *)l->data; | |
409 | assert(ann_row); | |
410 | rows.emplace_back(decc, ann_row); | |
411 | } | |
412 | } | |
413 | ||
414 | return rows; | |
ecd07c20 SA |
415 | } |
416 | ||
417 | void DecodeSignal::get_annotation_subset( | |
418 | vector<pv::data::decode::Annotation> &dest, | |
419 | const decode::Row &row, uint64_t start_sample, | |
420 | uint64_t end_sample) const | |
421 | { | |
47747218 SA |
422 | lock_guard<mutex> lock(output_mutex_); |
423 | ||
4913560f SA |
424 | if (!current_rows_) |
425 | return; | |
426 | ||
f2f4845f | 427 | // TODO Instead of current_rows_, use segmented_rows_ and the ID of the segment |
4913560f SA |
428 | |
429 | const auto iter = current_rows_->find(row); | |
430 | if (iter != current_rows_->end()) | |
47747218 SA |
431 | (*iter).second.get_annotation_subset(dest, |
432 | start_sample, end_sample); | |
433 | } | |
434 | ||
435 | void DecodeSignal::save_settings(QSettings &settings) const | |
436 | { | |
437 | SignalBase::save_settings(settings); | |
438 | ||
c8e60bdf SA |
439 | settings.setValue("decoders", (int)(stack_.size())); |
440 | ||
8b5ea5a1 | 441 | // Save decoder stack |
c8e60bdf SA |
442 | int decoder_idx = 0; |
443 | for (shared_ptr<decode::Decoder> decoder : stack_) { | |
444 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); | |
445 | ||
446 | settings.setValue("id", decoder->decoder()->id); | |
447 | ||
d3feec23 SA |
448 | // Save decoder options |
449 | const map<string, GVariant*>& options = decoder->options(); | |
450 | ||
451 | settings.setValue("options", (int)options.size()); | |
452 | ||
453 | // Note: decode::Decoder::options() returns only the options | |
454 | // that differ from the default. See binding::Decoder::getter() | |
455 | int i = 0; | |
456 | for (auto option : options) { | |
457 | settings.beginGroup("option" + QString::number(i)); | |
458 | settings.setValue("name", QString::fromStdString(option.first)); | |
459 | GlobalSettings::store_gvariant(settings, option.second); | |
460 | settings.endGroup(); | |
461 | i++; | |
462 | } | |
463 | ||
c8e60bdf SA |
464 | settings.endGroup(); |
465 | } | |
466 | ||
8b5ea5a1 SA |
467 | // Save channel mapping |
468 | settings.setValue("channels", (int)channels_.size()); | |
469 | ||
470 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
471 | auto channel = find_if(channels_.begin(), channels_.end(), | |
472 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
473 | ||
474 | if (channel == channels_.end()) { | |
475 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
476 | continue; | |
477 | } | |
478 | ||
479 | settings.beginGroup("channel" + QString::number(channel_id)); | |
480 | ||
481 | settings.setValue("name", channel->name); // Useful for debugging | |
482 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
483 | ||
484 | if (channel->assigned_signal) | |
485 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
486 | ||
487 | settings.endGroup(); | |
488 | } | |
47747218 SA |
489 | } |
490 | ||
491 | void DecodeSignal::restore_settings(QSettings &settings) | |
492 | { | |
493 | SignalBase::restore_settings(settings); | |
494 | ||
8b5ea5a1 | 495 | // Restore decoder stack |
c8e60bdf SA |
496 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); |
497 | ||
498 | int decoders = settings.value("decoders").toInt(); | |
499 | ||
500 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
501 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
502 | ||
503 | QString id = settings.value("id").toString(); | |
504 | ||
505 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
506 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
507 | if (!dec) | |
508 | continue; | |
509 | ||
510 | if (QString::fromUtf8(dec->id) == id) { | |
d3feec23 SA |
511 | shared_ptr<decode::Decoder> decoder = |
512 | make_shared<decode::Decoder>(dec); | |
513 | ||
514 | stack_.push_back(decoder); | |
515 | ||
516 | // Restore decoder options that differ from their default | |
517 | int options = settings.value("options").toInt(); | |
518 | ||
519 | for (int i = 0; i < options; i++) { | |
520 | settings.beginGroup("option" + QString::number(i)); | |
521 | QString name = settings.value("name").toString(); | |
522 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
523 | decoder->set_option(name.toUtf8(), value); | |
524 | settings.endGroup(); | |
525 | } | |
c8e60bdf SA |
526 | |
527 | // Include the newly created decode channels in the channel lists | |
528 | update_channel_list(); | |
529 | break; | |
530 | } | |
531 | } | |
532 | ||
533 | settings.endGroup(); | |
4ca06ec3 | 534 | channels_updated(); |
c8e60bdf SA |
535 | } |
536 | ||
8b5ea5a1 SA |
537 | // Restore channel mapping |
538 | unsigned int channels = settings.value("channels").toInt(); | |
539 | ||
540 | const unordered_set< shared_ptr<data::SignalBase> > signalbases = | |
541 | session_.signalbases(); | |
542 | ||
543 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
544 | auto channel = find_if(channels_.begin(), channels_.end(), | |
545 | [&](data::DecodeChannel ch) { return ch.id == channel_id; }); | |
546 | ||
547 | if (channel == channels_.end()) { | |
548 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
549 | continue; | |
550 | } | |
551 | ||
552 | settings.beginGroup("channel" + QString::number(channel_id)); | |
553 | ||
554 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
555 | ||
556 | for (shared_ptr<data::SignalBase> signal : signalbases) | |
557 | if (signal->name() == assigned_signal_name) | |
558 | channel->assigned_signal = signal.get(); | |
559 | ||
560 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
561 | ||
562 | settings.endGroup(); | |
563 | } | |
564 | ||
aa71890d SA |
565 | // Update the internal structures |
566 | update_channel_list(); | |
567 | commit_decoder_channels(); | |
568 | ||
8b5ea5a1 | 569 | begin_decode(); |
47747218 SA |
570 | } |
571 | ||
9f97b357 SA |
572 | void DecodeSignal::update_channel_list() |
573 | { | |
47747218 | 574 | vector<data::DecodeChannel> prev_channels = channels_; |
9f97b357 SA |
575 | channels_.clear(); |
576 | ||
577 | uint16_t id = 0; | |
578 | ||
579 | // Copy existing entries, create new as needed | |
47747218 | 580 | for (shared_ptr<Decoder> decoder : stack_) { |
9f97b357 SA |
581 | const srd_decoder* srd_d = decoder->decoder(); |
582 | const GSList *l; | |
583 | ||
584 | // Mandatory channels | |
585 | for (l = srd_d->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, false, 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 | // Optional channels | |
608 | for (l = srd_d->opt_channels; l; l = l->next) { | |
609 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
610 | bool ch_added = false; | |
611 | ||
612 | // Copy but update ID if this channel was in the list before | |
47747218 | 613 | for (data::DecodeChannel &ch : prev_channels) |
9f97b357 SA |
614 | if (ch.pdch_ == pdch) { |
615 | ch.id = id++; | |
616 | channels_.push_back(ch); | |
617 | ch_added = true; | |
618 | break; | |
619 | } | |
620 | ||
621 | if (!ch_added) { | |
622 | // Create new entry without a mapped signal | |
6e7a4a00 | 623 | data::DecodeChannel ch = {id++, 0, true, nullptr, |
9f97b357 SA |
624 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
625 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
626 | channels_.push_back(ch); | |
627 | } | |
628 | } | |
629 | } | |
630 | ||
47747218 SA |
631 | // Invalidate the logic output data if the channel assignment changed |
632 | if (prev_channels.size() != channels_.size()) { | |
633 | // The number of channels changed, there's definitely a difference | |
634 | logic_mux_data_invalid_ = true; | |
635 | } else { | |
636 | // Same number but assignment may still differ, so compare all channels | |
637 | for (size_t i = 0; i < channels_.size(); i++) { | |
638 | const data::DecodeChannel &p_ch = prev_channels[i]; | |
639 | const data::DecodeChannel &ch = channels_[i]; | |
640 | ||
641 | if ((p_ch.pdch_ != ch.pdch_) || | |
642 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
643 | logic_mux_data_invalid_ = true; | |
644 | break; | |
645 | } | |
646 | } | |
647 | ||
648 | } | |
649 | ||
9f97b357 SA |
650 | channels_updated(); |
651 | } | |
652 | ||
27a3f09b | 653 | void DecodeSignal::commit_decoder_channels() |
ad908057 | 654 | { |
27a3f09b SA |
655 | // Submit channel list to every decoder, containing only the relevant channels |
656 | for (shared_ptr<decode::Decoder> dec : stack_) { | |
657 | vector<data::DecodeChannel*> channel_list; | |
658 | ||
659 | for (data::DecodeChannel &ch : channels_) | |
660 | if (ch.decoder_ == dec) | |
661 | channel_list.push_back(&ch); | |
47747218 | 662 | |
27a3f09b SA |
663 | dec->set_channels(channel_list); |
664 | } | |
aa71890d SA |
665 | |
666 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
667 | int id = 0; | |
668 | for (data::DecodeChannel &ch : channels_) | |
669 | if (ch.assigned_signal) | |
670 | ch.bit_id = id++; | |
47747218 SA |
671 | } |
672 | ||
27a3f09b | 673 | void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end) |
47747218 | 674 | { |
27a3f09b SA |
675 | // Enforce end to be greater than start |
676 | if (end <= start) | |
677 | return; | |
678 | ||
679 | // Fetch all segments and their data | |
680 | // TODO Currently, we assume only a single segment exists | |
681 | vector<shared_ptr<LogicSegment> > segments; | |
682 | vector<const uint8_t*> signal_data; | |
683 | vector<uint8_t> signal_in_bytepos; | |
684 | vector<uint8_t> signal_in_bitpos; | |
685 | ||
686 | for (data::DecodeChannel &ch : channels_) | |
687 | if (ch.assigned_signal) { | |
688 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
689 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front(); | |
690 | segments.push_back(segment); | |
b82243f7 SA |
691 | |
692 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
693 | segment->get_samples(start, end, data); | |
694 | signal_data.push_back(data); | |
27a3f09b SA |
695 | |
696 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
697 | signal_in_bytepos.push_back(bitpos / 8); | |
698 | signal_in_bitpos.push_back(bitpos % 8); | |
699 | } | |
700 | ||
701 | // Perform the muxing of signal data into the output data | |
83b9c07b | 702 | uint8_t* output = new uint8_t[(end - start) * logic_mux_segment_->unit_size()]; |
27a3f09b SA |
703 | unsigned int signal_count = signal_data.size(); |
704 | ||
705 | for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) { | |
706 | int bitpos = 0; | |
707 | uint8_t bytepos = 0; | |
708 | ||
83b9c07b SA |
709 | const int out_sample_pos = sample_cnt * logic_mux_segment_->unit_size(); |
710 | for (unsigned int i = 0; i < logic_mux_segment_->unit_size(); i++) | |
27a3f09b | 711 | output[out_sample_pos + i] = 0; |
47747218 | 712 | |
27a3f09b SA |
713 | for (unsigned int i = 0; i < signal_count; i++) { |
714 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
715 | const uint8_t in_sample = 1 & | |
716 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
47747218 | 717 | |
27a3f09b SA |
718 | const uint8_t out_sample = output[out_sample_pos + bytepos]; |
719 | ||
720 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
721 | ||
722 | bitpos++; | |
723 | if (bitpos > 7) { | |
724 | bitpos = 0; | |
725 | bytepos++; | |
726 | } | |
727 | } | |
47747218 SA |
728 | } |
729 | ||
83b9c07b | 730 | logic_mux_segment_->append_payload(output, (end - start) * logic_mux_segment_->unit_size()); |
27a3f09b SA |
731 | delete[] output; |
732 | ||
733 | for (const uint8_t* data : signal_data) | |
734 | delete[] data; | |
735 | } | |
736 | ||
737 | void DecodeSignal::logic_mux_proc() | |
738 | { | |
739 | do { | |
5ecf957f | 740 | const uint64_t input_sample_count = get_working_sample_count(currently_processed_segment_); |
83b9c07b | 741 | const uint64_t output_sample_count = logic_mux_segment_->get_sample_count(); |
27a3f09b SA |
742 | |
743 | const uint64_t samples_to_process = | |
744 | (input_sample_count > output_sample_count) ? | |
745 | (input_sample_count - output_sample_count) : 0; | |
746 | ||
747 | // Process the samples if necessary... | |
748 | if (samples_to_process > 0) { | |
83b9c07b | 749 | const uint64_t unit_size = logic_mux_segment_->unit_size(); |
27a3f09b SA |
750 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; |
751 | ||
752 | uint64_t processed_samples = 0; | |
753 | do { | |
754 | const uint64_t start_sample = output_sample_count + processed_samples; | |
755 | const uint64_t sample_count = | |
756 | min(samples_to_process - processed_samples, chunk_sample_count); | |
757 | ||
758 | mux_logic_samples(start_sample, start_sample + sample_count); | |
759 | processed_samples += sample_count; | |
760 | ||
761 | // ...and process the newly muxed logic data | |
762 | decode_input_cond_.notify_one(); | |
763 | } while (processed_samples < samples_to_process); | |
764 | } | |
765 | ||
1ec191ed | 766 | if (samples_to_process == 0) { |
ba5f2186 SA |
767 | logic_mux_data_invalid_ = false; |
768 | ||
27a3f09b SA |
769 | // Wait for more input |
770 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
771 | logic_mux_cond_.wait(logic_mux_lock); | |
772 | } | |
1ec191ed | 773 | } while (!logic_mux_interrupt_); |
47747218 SA |
774 | } |
775 | ||
a3ebd556 SA |
776 | void DecodeSignal::query_input_metadata() |
777 | { | |
778 | // Update the samplerate and start time because we cannot start | |
779 | // the libsrd session without the current samplerate | |
780 | ||
781 | // TODO Currently we assume all channels have the same sample rate | |
782 | // and start time | |
783 | bool samplerate_valid = false; | |
8ebf8079 SA |
784 | data::DecodeChannel *any_channel; |
785 | shared_ptr<Logic> logic_data; | |
a3ebd556 | 786 | |
8ebf8079 SA |
787 | do { |
788 | any_channel = &(*find_if(channels_.begin(), channels_.end(), | |
789 | [](data::DecodeChannel ch) { return ch.assigned_signal; })); | |
a3ebd556 | 790 | |
8ebf8079 SA |
791 | logic_data = any_channel->assigned_signal->logic_data(); |
792 | ||
793 | if (!logic_data) { | |
794 | // Wait until input data is available or an interrupt was requested | |
795 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
796 | decode_input_cond_.wait(input_wait_lock); | |
797 | } | |
798 | } while (!logic_data && !decode_interrupt_); | |
799 | ||
800 | if (decode_interrupt_) | |
801 | return; | |
a3ebd556 SA |
802 | |
803 | do { | |
804 | if (!logic_data->logic_segments().empty()) { | |
805 | shared_ptr<LogicSegment> first_segment = | |
806 | any_channel->assigned_signal->logic_data()->logic_segments().front(); | |
807 | start_time_ = first_segment->start_time(); | |
808 | samplerate_ = first_segment->samplerate(); | |
809 | if (samplerate_ > 0) | |
810 | samplerate_valid = true; | |
811 | } | |
812 | ||
1ec191ed SA |
813 | if (!samplerate_valid) { |
814 | // Wait until input data is available or an interrupt was requested | |
815 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
816 | decode_input_cond_.wait(input_wait_lock); | |
817 | } | |
a3ebd556 SA |
818 | } while (!samplerate_valid && !decode_interrupt_); |
819 | } | |
820 | ||
47747218 | 821 | void DecodeSignal::decode_data( |
e91883bb | 822 | const int64_t abs_start_samplenum, const int64_t sample_count) |
47747218 | 823 | { |
83b9c07b | 824 | const int64_t unit_size = logic_mux_segment_->unit_size(); |
27a3f09b | 825 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; |
47747218 SA |
826 | |
827 | for (int64_t i = abs_start_samplenum; | |
828 | !decode_interrupt_ && (i < (abs_start_samplenum + sample_count)); | |
829 | i += chunk_sample_count) { | |
830 | ||
831 | const int64_t chunk_end = min(i + chunk_sample_count, | |
832 | abs_start_samplenum + sample_count); | |
833 | ||
b82243f7 SA |
834 | int64_t data_size = (chunk_end - i) * unit_size; |
835 | uint8_t* chunk = new uint8_t[data_size]; | |
83b9c07b | 836 | logic_mux_segment_->get_samples(i, chunk_end, chunk); |
47747218 | 837 | |
e91883bb | 838 | if (srd_session_send(srd_session_, i, chunk_end, chunk, |
b82243f7 | 839 | data_size, unit_size) != SRD_OK) { |
47747218 SA |
840 | error_message_ = tr("Decoder reported an error"); |
841 | delete[] chunk; | |
842 | break; | |
843 | } | |
a3ebd556 | 844 | |
47747218 SA |
845 | delete[] chunk; |
846 | ||
847 | { | |
848 | lock_guard<mutex> lock(output_mutex_); | |
849 | samples_decoded_ = chunk_end; | |
850 | } | |
1b56c646 SA |
851 | |
852 | // Notify the frontend that we processed some data and | |
853 | // possibly have new annotations as well | |
854 | new_annotations(); | |
47747218 SA |
855 | } |
856 | } | |
857 | ||
858 | void DecodeSignal::decode_proc() | |
859 | { | |
a3ebd556 SA |
860 | query_input_metadata(); |
861 | ||
862 | if (decode_interrupt_) | |
863 | return; | |
864 | ||
e91883bb | 865 | start_srd_session(); |
47747218 | 866 | |
27a3f09b SA |
867 | uint64_t sample_count; |
868 | uint64_t abs_start_samplenum = 0; | |
47747218 | 869 | do { |
27a3f09b SA |
870 | // Keep processing new samples until we exhaust the input data |
871 | do { | |
767bf4e7 | 872 | lock_guard<mutex> input_lock(input_mutex_); |
83b9c07b | 873 | sample_count = logic_mux_segment_->get_sample_count() - abs_start_samplenum; |
27a3f09b SA |
874 | |
875 | if (sample_count > 0) { | |
e91883bb | 876 | decode_data(abs_start_samplenum, sample_count); |
27a3f09b SA |
877 | abs_start_samplenum += sample_count; |
878 | } | |
1b56c646 SA |
879 | } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_); |
880 | ||
881 | if (error_message_.isEmpty() && !decode_interrupt_) { | |
882 | if (sample_count == 0) | |
883 | decode_finished(); | |
27a3f09b | 884 | |
a3ebd556 | 885 | // Wait for new input data or an interrupt was requested |
27a3f09b SA |
886 | unique_lock<mutex> input_wait_lock(input_mutex_); |
887 | decode_input_cond_.wait(input_wait_lock); | |
888 | } | |
889 | } while (error_message_.isEmpty() && !decode_interrupt_); | |
e91883bb SA |
890 | } |
891 | ||
892 | void DecodeSignal::start_srd_session() | |
893 | { | |
a3ebd556 SA |
894 | if (srd_session_) |
895 | stop_srd_session(); | |
e91883bb | 896 | |
a3ebd556 SA |
897 | // Create the session |
898 | srd_session_new(&srd_session_); | |
899 | assert(srd_session_); | |
47747218 | 900 | |
a3ebd556 SA |
901 | // Create the decoders |
902 | srd_decoder_inst *prev_di = nullptr; | |
903 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
904 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); | |
e91883bb | 905 | |
a3ebd556 SA |
906 | if (!di) { |
907 | error_message_ = tr("Failed to create decoder instance"); | |
908 | srd_session_destroy(srd_session_); | |
909 | return; | |
910 | } | |
e91883bb | 911 | |
a3ebd556 SA |
912 | if (prev_di) |
913 | srd_inst_stack(srd_session_, prev_di, di); | |
e91883bb | 914 | |
a3ebd556 | 915 | prev_di = di; |
e91883bb | 916 | } |
a3ebd556 SA |
917 | |
918 | // Start the session | |
919 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
920 | g_variant_new_uint64(samplerate_)); | |
921 | ||
922 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
923 | DecodeSignal::annotation_callback, this); | |
924 | ||
925 | srd_session_start(srd_session_); | |
e91883bb | 926 | } |
47747218 | 927 | |
e91883bb SA |
928 | void DecodeSignal::stop_srd_session() |
929 | { | |
930 | if (srd_session_) { | |
931 | // Destroy the session | |
932 | srd_session_destroy(srd_session_); | |
933 | srd_session_ = nullptr; | |
934 | } | |
47747218 SA |
935 | } |
936 | ||
a8a9222d SA |
937 | void DecodeSignal::connect_input_notifiers() |
938 | { | |
939 | // Disconnect the notification slot from the previous set of signals | |
1b56c646 | 940 | disconnect(this, SLOT(on_data_cleared())); |
a8a9222d SA |
941 | disconnect(this, SLOT(on_data_received())); |
942 | ||
943 | // Connect the currently used signals to our slot | |
944 | for (data::DecodeChannel &ch : channels_) { | |
945 | if (!ch.assigned_signal) | |
946 | continue; | |
947 | ||
1b56c646 SA |
948 | const data::SignalBase *signal = ch.assigned_signal; |
949 | connect(signal, SIGNAL(samples_cleared()), | |
950 | this, SLOT(on_data_cleared())); | |
951 | connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), | |
a8a9222d SA |
952 | this, SLOT(on_data_received())); |
953 | } | |
954 | } | |
955 | ||
f2f4845f | 956 | void DecodeSignal::create_new_annotation_segment() |
4913560f | 957 | { |
f2f4845f SA |
958 | segmented_rows_.emplace_back(map<const decode::Row, decode::RowData>()); |
959 | current_rows_ = &(segmented_rows_.back()); | |
4913560f SA |
960 | |
961 | // Add annotation classes | |
962 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
963 | assert(dec); | |
964 | const srd_decoder *const decc = dec->decoder(); | |
965 | assert(dec->decoder()); | |
966 | ||
967 | // Add a row for the decoder if it doesn't have a row list | |
968 | if (!decc->annotation_rows) | |
969 | (*current_rows_)[Row(decc)] = decode::RowData(); | |
970 | ||
971 | // Add the decoder rows | |
972 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
973 | const srd_decoder_annotation_row *const ann_row = | |
974 | (srd_decoder_annotation_row *)l->data; | |
975 | assert(ann_row); | |
976 | ||
977 | const Row row(decc, ann_row); | |
978 | ||
979 | // Add a new empty row data object | |
980 | (*current_rows_)[row] = decode::RowData(); | |
981 | } | |
982 | } | |
983 | } | |
984 | ||
47747218 SA |
985 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) |
986 | { | |
987 | assert(pdata); | |
b21501d6 | 988 | assert(decode_signal); |
47747218 SA |
989 | |
990 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
991 | assert(ds); | |
992 | ||
993 | lock_guard<mutex> lock(ds->output_mutex_); | |
994 | ||
47747218 SA |
995 | // Find the row |
996 | assert(pdata->pdo); | |
997 | assert(pdata->pdo->di); | |
998 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
999 | assert(decc); | |
4913560f | 1000 | assert(ds->current_rows_); |
47747218 | 1001 | |
1becee23 UH |
1002 | const srd_proto_data_annotation *const pda = |
1003 | (const srd_proto_data_annotation*)pdata->data; | |
1004 | assert(pda); | |
1005 | ||
4913560f | 1006 | auto row_iter = ds->current_rows_->end(); |
47747218 SA |
1007 | |
1008 | // Try looking up the sub-row of this class | |
1becee23 UH |
1009 | const auto format = pda->ann_class; |
1010 | const auto r = ds->class_rows_.find(make_pair(decc, format)); | |
47747218 | 1011 | if (r != ds->class_rows_.end()) |
4913560f | 1012 | row_iter = ds->current_rows_->find((*r).second); |
47747218 SA |
1013 | else { |
1014 | // Failing that, use the decoder as a key | |
4913560f | 1015 | row_iter = ds->current_rows_->find(Row(decc)); |
47747218 SA |
1016 | } |
1017 | ||
4913560f | 1018 | if (row_iter == ds->current_rows_->end()) { |
47747218 | 1019 | qDebug() << "Unexpected annotation: decoder = " << decc << |
1becee23 | 1020 | ", format = " << format; |
47747218 SA |
1021 | assert(false); |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | // Add the annotation | |
1becee23 | 1026 | (*row_iter).second.emplace_annotation(pdata); |
47747218 SA |
1027 | } |
1028 | ||
1029 | void DecodeSignal::on_capture_state_changed(int state) | |
1030 | { | |
1031 | // If a new acquisition was started, we need to start decoding from scratch | |
ba5f2186 SA |
1032 | if (state == Session::Running) { |
1033 | logic_mux_data_invalid_ = true; | |
47747218 | 1034 | begin_decode(); |
ba5f2186 | 1035 | } |
47747218 SA |
1036 | } |
1037 | ||
1b56c646 SA |
1038 | void DecodeSignal::on_data_cleared() |
1039 | { | |
1040 | reset_decode(); | |
1041 | } | |
1042 | ||
47747218 SA |
1043 | void DecodeSignal::on_data_received() |
1044 | { | |
1b56c646 SA |
1045 | if (!logic_mux_thread_.joinable()) |
1046 | begin_decode(); | |
1047 | else | |
1048 | logic_mux_cond_.notify_one(); | |
47747218 SA |
1049 | } |
1050 | ||
ad908057 SA |
1051 | } // namespace data |
1052 | } // namespace pv |