]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 | #ifdef _WIN32 | |
21 | // Windows: Avoid boost/thread namespace pollution (which includes windows.h). | |
22 | #define NOGDI | |
23 | #define NORESOURCE | |
24 | #endif | |
25 | #include <boost/thread/locks.hpp> | |
26 | #include <boost/thread/shared_mutex.hpp> | |
27 | ||
28 | #include <QFileInfo> | |
29 | ||
30 | #include <cassert> | |
31 | #include <mutex> | |
32 | #include <stdexcept> | |
33 | ||
34 | #include <sys/stat.h> | |
35 | ||
36 | #include "session.hpp" | |
37 | #include "devicemanager.hpp" | |
38 | ||
39 | #include "data/analog.hpp" | |
40 | #include "data/analogsegment.hpp" | |
41 | #include "data/decoderstack.hpp" | |
42 | #include "data/logic.hpp" | |
43 | #include "data/logicsegment.hpp" | |
44 | #include "data/signalbase.hpp" | |
45 | #include "data/decode/decoder.hpp" | |
46 | ||
47 | #include "devices/hardwaredevice.hpp" | |
48 | #include "devices/inputfile.hpp" | |
49 | #include "devices/sessionfile.hpp" | |
50 | ||
51 | #include "toolbars/mainbar.hpp" | |
52 | ||
53 | #include "view/analogsignal.hpp" | |
54 | #include "view/decodetrace.hpp" | |
55 | #include "view/logicsignal.hpp" | |
56 | #include "view/signal.hpp" | |
57 | #include "view/view.hpp" | |
58 | ||
59 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
60 | ||
61 | #ifdef ENABLE_DECODE | |
62 | #include <libsigrokdecode/libsigrokdecode.h> | |
63 | #endif | |
64 | ||
65 | using boost::shared_lock; | |
66 | using boost::shared_mutex; | |
67 | using boost::unique_lock; | |
68 | ||
69 | using std::bad_alloc; | |
70 | using std::dynamic_pointer_cast; | |
71 | using std::find_if; | |
72 | using std::function; | |
73 | using std::lock_guard; | |
74 | using std::list; | |
75 | using std::make_pair; | |
76 | using std::make_shared; | |
77 | using std::map; | |
78 | using std::max; | |
79 | using std::move; | |
80 | using std::mutex; | |
81 | using std::pair; | |
82 | using std::recursive_mutex; | |
83 | using std::runtime_error; | |
84 | using std::set; | |
85 | using std::shared_ptr; | |
86 | using std::string; | |
87 | using std::unordered_set; | |
88 | using std::vector; | |
89 | ||
90 | using sigrok::Analog; | |
91 | using sigrok::Channel; | |
92 | using sigrok::ConfigKey; | |
93 | using sigrok::DatafeedCallbackFunction; | |
94 | using sigrok::Error; | |
95 | using sigrok::Header; | |
96 | using sigrok::InputFormat; | |
97 | using sigrok::Logic; | |
98 | using sigrok::Meta; | |
99 | using sigrok::OutputFormat; | |
100 | using sigrok::Packet; | |
101 | using sigrok::PacketPayload; | |
102 | using sigrok::Session; | |
103 | using sigrok::SessionDevice; | |
104 | ||
105 | using Glib::VariantBase; | |
106 | using Glib::Variant; | |
107 | ||
108 | namespace pv { | |
109 | Session::Session(DeviceManager &device_manager, QString name) : | |
110 | device_manager_(device_manager), | |
111 | default_name_(name), | |
112 | name_(name), | |
113 | capture_state_(Stopped), | |
114 | cur_samplerate_(0), | |
115 | data_saved_(true) | |
116 | { | |
117 | } | |
118 | ||
119 | Session::~Session() | |
120 | { | |
121 | // Stop and join to the thread | |
122 | stop_capture(); | |
123 | } | |
124 | ||
125 | DeviceManager& Session::device_manager() | |
126 | { | |
127 | return device_manager_; | |
128 | } | |
129 | ||
130 | const DeviceManager& Session::device_manager() const | |
131 | { | |
132 | return device_manager_; | |
133 | } | |
134 | ||
135 | shared_ptr<sigrok::Session> Session::session() const | |
136 | { | |
137 | if (!device_) | |
138 | return shared_ptr<sigrok::Session>(); | |
139 | return device_->session(); | |
140 | } | |
141 | ||
142 | shared_ptr<devices::Device> Session::device() const | |
143 | { | |
144 | return device_; | |
145 | } | |
146 | ||
147 | QString Session::name() const | |
148 | { | |
149 | return name_; | |
150 | } | |
151 | ||
152 | void Session::set_name(QString name) | |
153 | { | |
154 | if (default_name_.isEmpty()) | |
155 | default_name_ = name; | |
156 | ||
157 | name_ = name; | |
158 | ||
159 | name_changed(); | |
160 | } | |
161 | ||
162 | const list< shared_ptr<views::ViewBase> > Session::views() const | |
163 | { | |
164 | return views_; | |
165 | } | |
166 | ||
167 | shared_ptr<views::ViewBase> Session::main_view() const | |
168 | { | |
169 | return main_view_; | |
170 | } | |
171 | ||
172 | void Session::set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar) | |
173 | { | |
174 | main_bar_ = main_bar; | |
175 | } | |
176 | ||
177 | shared_ptr<pv::toolbars::MainBar> Session::main_bar() const | |
178 | { | |
179 | return main_bar_; | |
180 | } | |
181 | ||
182 | bool Session::data_saved() const | |
183 | { | |
184 | return data_saved_; | |
185 | } | |
186 | ||
187 | void Session::save_settings(QSettings &settings) const | |
188 | { | |
189 | map<string, string> dev_info; | |
190 | list<string> key_list; | |
191 | int stacks = 0, views = 0; | |
192 | ||
193 | if (device_) { | |
194 | shared_ptr<devices::HardwareDevice> hw_device = | |
195 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
196 | ||
197 | if (hw_device) { | |
198 | settings.setValue("device_type", "hardware"); | |
199 | settings.beginGroup("device"); | |
200 | ||
201 | key_list.emplace_back("vendor"); | |
202 | key_list.emplace_back("model"); | |
203 | key_list.emplace_back("version"); | |
204 | key_list.emplace_back("serial_num"); | |
205 | key_list.emplace_back("connection_id"); | |
206 | ||
207 | dev_info = device_manager_.get_device_info(device_); | |
208 | ||
209 | for (string key : key_list) { | |
210 | if (dev_info.count(key)) | |
211 | settings.setValue(QString::fromUtf8(key.c_str()), | |
212 | QString::fromUtf8(dev_info.at(key).c_str())); | |
213 | else | |
214 | settings.remove(QString::fromUtf8(key.c_str())); | |
215 | } | |
216 | ||
217 | settings.endGroup(); | |
218 | } | |
219 | ||
220 | shared_ptr<devices::SessionFile> sessionfile_device = | |
221 | dynamic_pointer_cast< devices::SessionFile >(device_); | |
222 | ||
223 | if (sessionfile_device) { | |
224 | settings.setValue("device_type", "sessionfile"); | |
225 | settings.beginGroup("device"); | |
226 | settings.setValue("filename", QString::fromStdString( | |
227 | sessionfile_device->full_name())); | |
228 | settings.endGroup(); | |
229 | } | |
230 | ||
231 | // Save channels and decoders | |
232 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
233 | #ifdef ENABLE_DECODE | |
234 | if (base->is_decode_signal()) { | |
235 | shared_ptr<pv::data::DecoderStack> decoder_stack = | |
236 | base->decoder_stack(); | |
237 | shared_ptr<data::decode::Decoder> top_decoder = | |
238 | decoder_stack->stack().front(); | |
239 | ||
240 | settings.beginGroup("decoder_stack" + QString::number(stacks++)); | |
241 | settings.setValue("id", top_decoder->decoder()->id); | |
242 | settings.setValue("name", top_decoder->decoder()->name); | |
243 | settings.endGroup(); | |
244 | } else | |
245 | #endif | |
246 | { | |
247 | settings.beginGroup(base->internal_name()); | |
248 | base->save_settings(settings); | |
249 | settings.endGroup(); | |
250 | } | |
251 | } | |
252 | ||
253 | settings.setValue("decoder_stacks", stacks); | |
254 | ||
255 | // Save view states and their signal settings | |
256 | // Note: main_view must be saved as view0 | |
257 | settings.beginGroup("view" + QString::number(views++)); | |
258 | main_view_->save_settings(settings); | |
259 | settings.endGroup(); | |
260 | ||
261 | for (shared_ptr<views::ViewBase> view : views_) { | |
262 | if (view != main_view_) { | |
263 | settings.beginGroup("view" + QString::number(views++)); | |
264 | view->save_settings(settings); | |
265 | settings.endGroup(); | |
266 | } | |
267 | } | |
268 | ||
269 | settings.setValue("views", views); | |
270 | } | |
271 | } | |
272 | ||
273 | void Session::restore_settings(QSettings &settings) | |
274 | { | |
275 | shared_ptr<devices::Device> device; | |
276 | ||
277 | QString device_type = settings.value("device_type").toString(); | |
278 | ||
279 | if (device_type == "hardware") { | |
280 | map<string, string> dev_info; | |
281 | list<string> key_list; | |
282 | ||
283 | // Re-select last used device if possible but only if it's not demo | |
284 | settings.beginGroup("device"); | |
285 | key_list.emplace_back("vendor"); | |
286 | key_list.emplace_back("model"); | |
287 | key_list.emplace_back("version"); | |
288 | key_list.emplace_back("serial_num"); | |
289 | key_list.emplace_back("connection_id"); | |
290 | ||
291 | for (string key : key_list) { | |
292 | const QString k = QString::fromStdString(key); | |
293 | if (!settings.contains(k)) | |
294 | continue; | |
295 | ||
296 | const string value = settings.value(k).toString().toStdString(); | |
297 | if (!value.empty()) | |
298 | dev_info.insert(make_pair(key, value)); | |
299 | } | |
300 | ||
301 | if (dev_info.count("model") > 0) | |
302 | device = device_manager_.find_device_from_info(dev_info); | |
303 | ||
304 | if (device) | |
305 | set_device(device); | |
306 | ||
307 | settings.endGroup(); | |
308 | } | |
309 | ||
310 | if (device_type == "sessionfile") { | |
311 | settings.beginGroup("device"); | |
312 | QString filename = settings.value("filename").toString(); | |
313 | settings.endGroup(); | |
314 | ||
315 | if (QFileInfo(filename).isReadable()) { | |
316 | device = make_shared<devices::SessionFile>(device_manager_.context(), | |
317 | filename.toStdString()); | |
318 | set_device(device); | |
319 | ||
320 | // TODO Perform error handling | |
321 | start_capture([](QString infoMessage) { (void)infoMessage; }); | |
322 | ||
323 | set_name(QFileInfo(filename).fileName()); | |
324 | } | |
325 | } | |
326 | ||
327 | if (device) { | |
328 | // Restore channels | |
329 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
330 | settings.beginGroup(base->internal_name()); | |
331 | base->restore_settings(settings); | |
332 | settings.endGroup(); | |
333 | } | |
334 | ||
335 | // Restore decoders | |
336 | #ifdef ENABLE_DECODE | |
337 | int stacks = settings.value("decoder_stacks").toInt(); | |
338 | ||
339 | for (int i = 0; i < stacks; i++) { | |
340 | settings.beginGroup("decoder_stack" + QString::number(i++)); | |
341 | ||
342 | QString id = settings.value("id").toString(); | |
343 | add_decoder(srd_decoder_get_by_id(id.toStdString().c_str())); | |
344 | ||
345 | settings.endGroup(); | |
346 | } | |
347 | #endif | |
348 | ||
349 | // Restore views | |
350 | int views = settings.value("views").toInt(); | |
351 | ||
352 | for (int i = 0; i < views; i++) { | |
353 | settings.beginGroup("view" + QString::number(i)); | |
354 | ||
355 | if (i > 0) { | |
356 | views::ViewType type = (views::ViewType)settings.value("type").toInt(); | |
357 | add_view(name_, type, this); | |
358 | views_.back()->restore_settings(settings); | |
359 | } else | |
360 | main_view_->restore_settings(settings); | |
361 | ||
362 | settings.endGroup(); | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | void Session::select_device(shared_ptr<devices::Device> device) | |
368 | { | |
369 | try { | |
370 | if (device) | |
371 | set_device(device); | |
372 | else | |
373 | set_default_device(); | |
374 | } catch (const QString &e) { | |
375 | main_bar_->session_error(tr("Failed to Select Device"), | |
376 | tr("Failed to Select Device")); | |
377 | } | |
378 | } | |
379 | ||
380 | void Session::set_device(shared_ptr<devices::Device> device) | |
381 | { | |
382 | assert(device); | |
383 | ||
384 | // Ensure we are not capturing before setting the device | |
385 | stop_capture(); | |
386 | ||
387 | if (device_) | |
388 | device_->close(); | |
389 | ||
390 | device_.reset(); | |
391 | ||
392 | // Revert name back to default name (e.g. "Session 1") as the data is gone | |
393 | name_ = default_name_; | |
394 | name_changed(); | |
395 | ||
396 | // Remove all stored data | |
397 | for (shared_ptr<views::ViewBase> view : views_) { | |
398 | view->clear_signals(); | |
399 | #ifdef ENABLE_DECODE | |
400 | view->clear_decode_signals(); | |
401 | #endif | |
402 | } | |
403 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
404 | d->clear(); | |
405 | all_signal_data_.clear(); | |
406 | signalbases_.clear(); | |
407 | cur_logic_segment_.reset(); | |
408 | ||
409 | for (auto entry : cur_analog_segments_) { | |
410 | shared_ptr<sigrok::Channel>(entry.first).reset(); | |
411 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
412 | } | |
413 | ||
414 | logic_data_.reset(); | |
415 | ||
416 | signals_changed(); | |
417 | ||
418 | device_ = move(device); | |
419 | ||
420 | try { | |
421 | device_->open(); | |
422 | } catch (const QString &e) { | |
423 | device_.reset(); | |
424 | } | |
425 | ||
426 | if (device_) { | |
427 | device_->session()->add_datafeed_callback([=] | |
428 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
429 | data_feed_in(device, packet); | |
430 | }); | |
431 | ||
432 | update_signals(); | |
433 | } | |
434 | ||
435 | device_changed(); | |
436 | } | |
437 | ||
438 | void Session::set_default_device() | |
439 | { | |
440 | const list< shared_ptr<devices::HardwareDevice> > &devices = | |
441 | device_manager_.devices(); | |
442 | ||
443 | if (devices.empty()) | |
444 | return; | |
445 | ||
446 | // Try and find the demo device and select that by default | |
447 | const auto iter = find_if(devices.begin(), devices.end(), | |
448 | [] (const shared_ptr<devices::HardwareDevice> &d) { | |
449 | return d->hardware_device()->driver()->name() == "demo"; }); | |
450 | set_device((iter == devices.end()) ? devices.front() : *iter); | |
451 | } | |
452 | ||
453 | void Session::load_init_file(const string &file_name, const string &format) | |
454 | { | |
455 | shared_ptr<InputFormat> input_format; | |
456 | ||
457 | if (!format.empty()) { | |
458 | const map<string, shared_ptr<InputFormat> > formats = | |
459 | device_manager_.context()->input_formats(); | |
460 | const auto iter = find_if(formats.begin(), formats.end(), | |
461 | [&](const pair<string, shared_ptr<InputFormat> > f) { | |
462 | return f.first == format; }); | |
463 | if (iter == formats.end()) { | |
464 | main_bar_->session_error(tr("Error"), | |
465 | tr("Unexpected input format: %s").arg(QString::fromStdString(format))); | |
466 | return; | |
467 | } | |
468 | ||
469 | input_format = (*iter).second; | |
470 | } | |
471 | ||
472 | load_file(QString::fromStdString(file_name), input_format); | |
473 | } | |
474 | ||
475 | void Session::load_file(QString file_name, | |
476 | shared_ptr<sigrok::InputFormat> format, | |
477 | const map<string, Glib::VariantBase> &options) | |
478 | { | |
479 | const QString errorMessage( | |
480 | QString("Failed to load file %1").arg(file_name)); | |
481 | ||
482 | try { | |
483 | if (format) | |
484 | set_device(shared_ptr<devices::Device>( | |
485 | new devices::InputFile( | |
486 | device_manager_.context(), | |
487 | file_name.toStdString(), | |
488 | format, options))); | |
489 | else | |
490 | set_device(shared_ptr<devices::Device>( | |
491 | new devices::SessionFile( | |
492 | device_manager_.context(), | |
493 | file_name.toStdString()))); | |
494 | } catch (Error e) { | |
495 | main_bar_->session_error(tr("Failed to load ") + file_name, e.what()); | |
496 | set_default_device(); | |
497 | main_bar_->update_device_list(); | |
498 | return; | |
499 | } | |
500 | ||
501 | main_bar_->update_device_list(); | |
502 | ||
503 | start_capture([&, errorMessage](QString infoMessage) { | |
504 | main_bar_->session_error(errorMessage, infoMessage); }); | |
505 | ||
506 | set_name(QFileInfo(file_name).fileName()); | |
507 | } | |
508 | ||
509 | Session::capture_state Session::get_capture_state() const | |
510 | { | |
511 | lock_guard<mutex> lock(sampling_mutex_); | |
512 | return capture_state_; | |
513 | } | |
514 | ||
515 | void Session::start_capture(function<void (const QString)> error_handler) | |
516 | { | |
517 | if (!device_) { | |
518 | error_handler(tr("No active device set, can't start acquisition.")); | |
519 | return; | |
520 | } | |
521 | ||
522 | stop_capture(); | |
523 | ||
524 | // Check that at least one channel is enabled | |
525 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
526 | if (sr_dev) { | |
527 | const auto channels = sr_dev->channels(); | |
528 | if (!any_of(channels.begin(), channels.end(), | |
529 | [](shared_ptr<Channel> channel) { | |
530 | return channel->enabled(); })) { | |
531 | error_handler(tr("No channels enabled.")); | |
532 | return; | |
533 | } | |
534 | } | |
535 | ||
536 | // Clear signal data | |
537 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
538 | d->clear(); | |
539 | ||
540 | // Revert name back to default name (e.g. "Session 1") for real devices | |
541 | // as the (possibly saved) data is gone. File devices keep their name. | |
542 | shared_ptr<devices::HardwareDevice> hw_device = | |
543 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
544 | ||
545 | if (hw_device) { | |
546 | name_ = default_name_; | |
547 | name_changed(); | |
548 | } | |
549 | ||
550 | // Begin the session | |
551 | sampling_thread_ = std::thread( | |
552 | &Session::sample_thread_proc, this, error_handler); | |
553 | } | |
554 | ||
555 | void Session::stop_capture() | |
556 | { | |
557 | if (get_capture_state() != Stopped) | |
558 | device_->stop(); | |
559 | ||
560 | // Check that sampling stopped | |
561 | if (sampling_thread_.joinable()) | |
562 | sampling_thread_.join(); | |
563 | } | |
564 | ||
565 | void Session::register_view(shared_ptr<views::ViewBase> view) | |
566 | { | |
567 | if (views_.empty()) { | |
568 | main_view_ = view; | |
569 | } | |
570 | ||
571 | views_.push_back(view); | |
572 | ||
573 | update_signals(); | |
574 | } | |
575 | ||
576 | void Session::deregister_view(shared_ptr<views::ViewBase> view) | |
577 | { | |
578 | views_.remove_if([&](shared_ptr<views::ViewBase> v) { return v == view; }); | |
579 | ||
580 | if (views_.empty()) { | |
581 | main_view_.reset(); | |
582 | ||
583 | // Without a view there can be no main bar | |
584 | main_bar_.reset(); | |
585 | } | |
586 | } | |
587 | ||
588 | bool Session::has_view(shared_ptr<views::ViewBase> view) | |
589 | { | |
590 | for (shared_ptr<views::ViewBase> v : views_) | |
591 | if (v == view) | |
592 | return true; | |
593 | ||
594 | return false; | |
595 | } | |
596 | ||
597 | double Session::get_samplerate() const | |
598 | { | |
599 | double samplerate = 0.0; | |
600 | ||
601 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
602 | assert(d); | |
603 | const vector< shared_ptr<pv::data::Segment> > segments = | |
604 | d->segments(); | |
605 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
606 | samplerate = max(samplerate, s->samplerate()); | |
607 | } | |
608 | // If there is no sample rate given we use samples as unit | |
609 | if (samplerate == 0.0) | |
610 | samplerate = 1.0; | |
611 | ||
612 | return samplerate; | |
613 | } | |
614 | ||
615 | const unordered_set< shared_ptr<data::SignalBase> > Session::signalbases() const | |
616 | { | |
617 | return signalbases_; | |
618 | } | |
619 | ||
620 | #ifdef ENABLE_DECODE | |
621 | bool Session::add_decoder(srd_decoder *const dec) | |
622 | { | |
623 | if (!dec) | |
624 | return false; | |
625 | ||
626 | map<const srd_channel*, shared_ptr<data::SignalBase> > channels; | |
627 | shared_ptr<data::DecoderStack> decoder_stack; | |
628 | ||
629 | try { | |
630 | // Create the decoder | |
631 | decoder_stack = make_shared<data::DecoderStack>(*this, dec); | |
632 | ||
633 | // Make a list of all the channels | |
634 | vector<const srd_channel*> all_channels; | |
635 | for (const GSList *i = dec->channels; i; i = i->next) | |
636 | all_channels.push_back((const srd_channel*)i->data); | |
637 | for (const GSList *i = dec->opt_channels; i; i = i->next) | |
638 | all_channels.push_back((const srd_channel*)i->data); | |
639 | ||
640 | // Auto select the initial channels | |
641 | for (const srd_channel *pdch : all_channels) | |
642 | for (shared_ptr<data::SignalBase> b : signalbases_) { | |
643 | if (b->logic_data()) { | |
644 | if (QString::fromUtf8(pdch->name).toLower(). | |
645 | contains(b->name().toLower())) | |
646 | channels[pdch] = b; | |
647 | } | |
648 | } | |
649 | ||
650 | assert(decoder_stack); | |
651 | assert(!decoder_stack->stack().empty()); | |
652 | assert(decoder_stack->stack().front()); | |
653 | decoder_stack->stack().front()->set_channels(channels); | |
654 | ||
655 | // Create the decode signal | |
656 | shared_ptr<data::SignalBase> signalbase = | |
657 | make_shared<data::SignalBase>(nullptr, data::SignalBase::DecodeChannel); | |
658 | ||
659 | signalbase->set_decoder_stack(decoder_stack); | |
660 | signalbases_.insert(signalbase); | |
661 | ||
662 | for (shared_ptr<views::ViewBase> view : views_) | |
663 | view->add_decode_signal(signalbase); | |
664 | } catch (runtime_error e) { | |
665 | return false; | |
666 | } | |
667 | ||
668 | signals_changed(); | |
669 | ||
670 | // Do an initial decode | |
671 | decoder_stack->begin_decode(); | |
672 | ||
673 | return true; | |
674 | } | |
675 | ||
676 | void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase) | |
677 | { | |
678 | signalbases_.erase(signalbase); | |
679 | ||
680 | for (shared_ptr<views::ViewBase> view : views_) | |
681 | view->remove_decode_signal(signalbase); | |
682 | ||
683 | signals_changed(); | |
684 | } | |
685 | #endif | |
686 | ||
687 | void Session::set_capture_state(capture_state state) | |
688 | { | |
689 | bool changed; | |
690 | ||
691 | { | |
692 | lock_guard<mutex> lock(sampling_mutex_); | |
693 | changed = capture_state_ != state; | |
694 | capture_state_ = state; | |
695 | } | |
696 | ||
697 | if (changed) | |
698 | capture_state_changed(state); | |
699 | } | |
700 | ||
701 | void Session::update_signals() | |
702 | { | |
703 | if (!device_) { | |
704 | signalbases_.clear(); | |
705 | logic_data_.reset(); | |
706 | for (shared_ptr<views::ViewBase> view : views_) { | |
707 | view->clear_signals(); | |
708 | #ifdef ENABLE_DECODE | |
709 | view->clear_decode_signals(); | |
710 | #endif | |
711 | } | |
712 | return; | |
713 | } | |
714 | ||
715 | lock_guard<recursive_mutex> lock(data_mutex_); | |
716 | ||
717 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
718 | if (!sr_dev) { | |
719 | signalbases_.clear(); | |
720 | logic_data_.reset(); | |
721 | for (shared_ptr<views::ViewBase> view : views_) { | |
722 | view->clear_signals(); | |
723 | #ifdef ENABLE_DECODE | |
724 | view->clear_decode_signals(); | |
725 | #endif | |
726 | } | |
727 | return; | |
728 | } | |
729 | ||
730 | // Detect what data types we will receive | |
731 | auto channels = sr_dev->channels(); | |
732 | unsigned int logic_channel_count = count_if( | |
733 | channels.begin(), channels.end(), | |
734 | [] (shared_ptr<Channel> channel) { | |
735 | return channel->type() == sigrok::ChannelType::LOGIC; }); | |
736 | ||
737 | // Create data containers for the logic data segments | |
738 | { | |
739 | lock_guard<recursive_mutex> data_lock(data_mutex_); | |
740 | ||
741 | if (logic_channel_count == 0) { | |
742 | logic_data_.reset(); | |
743 | } else if (!logic_data_ || | |
744 | logic_data_->num_channels() != logic_channel_count) { | |
745 | logic_data_.reset(new data::Logic( | |
746 | logic_channel_count)); | |
747 | assert(logic_data_); | |
748 | } | |
749 | } | |
750 | ||
751 | // Make the signals list | |
752 | for (shared_ptr<views::ViewBase> viewbase : views_) { | |
753 | views::TraceView::View *trace_view = | |
754 | qobject_cast<views::TraceView::View*>(viewbase.get()); | |
755 | ||
756 | if (trace_view) { | |
757 | unordered_set< shared_ptr<views::TraceView::Signal> > | |
758 | prev_sigs(trace_view->signals()); | |
759 | trace_view->clear_signals(); | |
760 | ||
761 | for (auto channel : sr_dev->channels()) { | |
762 | shared_ptr<data::SignalBase> signalbase; | |
763 | shared_ptr<views::TraceView::Signal> signal; | |
764 | ||
765 | // Find the channel in the old signals | |
766 | const auto iter = find_if( | |
767 | prev_sigs.cbegin(), prev_sigs.cend(), | |
768 | [&](const shared_ptr<views::TraceView::Signal> &s) { | |
769 | return s->base()->channel() == channel; | |
770 | }); | |
771 | if (iter != prev_sigs.end()) { | |
772 | // Copy the signal from the old set to the new | |
773 | signal = *iter; | |
774 | trace_view->add_signal(signal); | |
775 | } else { | |
776 | // Find the signalbase for this channel if possible | |
777 | signalbase.reset(); | |
778 | for (const shared_ptr<data::SignalBase> b : signalbases_) | |
779 | if (b->channel() == channel) | |
780 | signalbase = b; | |
781 | ||
782 | switch(channel->type()->id()) { | |
783 | case SR_CHANNEL_LOGIC: | |
784 | if (!signalbase) { | |
785 | signalbase = make_shared<data::SignalBase>(channel, | |
786 | data::SignalBase::LogicChannel); | |
787 | signalbases_.insert(signalbase); | |
788 | ||
789 | all_signal_data_.insert(logic_data_); | |
790 | signalbase->set_data(logic_data_); | |
791 | } | |
792 | ||
793 | signal = shared_ptr<views::TraceView::Signal>( | |
794 | new views::TraceView::LogicSignal(*this, | |
795 | device_, signalbase)); | |
796 | trace_view->add_signal(signal); | |
797 | break; | |
798 | ||
799 | case SR_CHANNEL_ANALOG: | |
800 | { | |
801 | if (!signalbase) { | |
802 | signalbase = make_shared<data::SignalBase>(channel, | |
803 | data::SignalBase::AnalogChannel); | |
804 | signalbases_.insert(signalbase); | |
805 | ||
806 | shared_ptr<data::Analog> data(new data::Analog()); | |
807 | all_signal_data_.insert(data); | |
808 | signalbase->set_data(data); | |
809 | } | |
810 | ||
811 | signal = shared_ptr<views::TraceView::Signal>( | |
812 | new views::TraceView::AnalogSignal( | |
813 | *this, signalbase)); | |
814 | trace_view->add_signal(signal); | |
815 | break; | |
816 | } | |
817 | ||
818 | default: | |
819 | assert(0); | |
820 | break; | |
821 | } | |
822 | } | |
823 | } | |
824 | } | |
825 | } | |
826 | ||
827 | signals_changed(); | |
828 | } | |
829 | ||
830 | shared_ptr<data::SignalBase> Session::signalbase_from_channel( | |
831 | shared_ptr<sigrok::Channel> channel) const | |
832 | { | |
833 | for (shared_ptr<data::SignalBase> sig : signalbases_) { | |
834 | assert(sig); | |
835 | if (sig->channel() == channel) | |
836 | return sig; | |
837 | } | |
838 | return shared_ptr<data::SignalBase>(); | |
839 | } | |
840 | ||
841 | void Session::sample_thread_proc(function<void (const QString)> error_handler) | |
842 | { | |
843 | assert(error_handler); | |
844 | ||
845 | if (!device_) | |
846 | return; | |
847 | ||
848 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
849 | ||
850 | out_of_memory_ = false; | |
851 | ||
852 | try { | |
853 | device_->start(); | |
854 | } catch (Error e) { | |
855 | error_handler(e.what()); | |
856 | return; | |
857 | } | |
858 | ||
859 | set_capture_state(device_->session()->trigger() ? | |
860 | AwaitingTrigger : Running); | |
861 | ||
862 | device_->run(); | |
863 | set_capture_state(Stopped); | |
864 | ||
865 | // Confirm that SR_DF_END was received | |
866 | if (cur_logic_segment_) { | |
867 | qDebug("SR_DF_END was not received."); | |
868 | assert(0); | |
869 | } | |
870 | ||
871 | // Optimize memory usage | |
872 | free_unused_memory(); | |
873 | ||
874 | // We now have unsaved data unless we just "captured" from a file | |
875 | shared_ptr<devices::File> file_device = | |
876 | dynamic_pointer_cast<devices::File>(device_); | |
877 | ||
878 | if (!file_device) | |
879 | data_saved_ = false; | |
880 | ||
881 | if (out_of_memory_) | |
882 | error_handler(tr("Out of memory, acquisition stopped.")); | |
883 | } | |
884 | ||
885 | void Session::free_unused_memory() | |
886 | { | |
887 | for (shared_ptr<data::SignalData> data : all_signal_data_) { | |
888 | const vector< shared_ptr<data::Segment> > segments = data->segments(); | |
889 | ||
890 | for (shared_ptr<data::Segment> segment : segments) { | |
891 | segment->free_unused_memory(); | |
892 | } | |
893 | } | |
894 | } | |
895 | ||
896 | void Session::feed_in_header() | |
897 | { | |
898 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
899 | } | |
900 | ||
901 | void Session::feed_in_meta(shared_ptr<Meta> meta) | |
902 | { | |
903 | for (auto entry : meta->config()) { | |
904 | switch (entry.first->id()) { | |
905 | case SR_CONF_SAMPLERATE: | |
906 | // We can't rely on the header to always contain the sample rate, | |
907 | // so in case it's supplied via a meta packet, we use it. | |
908 | if (!cur_samplerate_) | |
909 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); | |
910 | ||
911 | /// @todo handle samplerate changes | |
912 | break; | |
913 | default: | |
914 | // Unknown metadata is not an error. | |
915 | break; | |
916 | } | |
917 | } | |
918 | ||
919 | signals_changed(); | |
920 | } | |
921 | ||
922 | void Session::feed_in_trigger() | |
923 | { | |
924 | // The channel containing most samples should be most accurate | |
925 | uint64_t sample_count = 0; | |
926 | ||
927 | { | |
928 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
929 | assert(d); | |
930 | uint64_t temp_count = 0; | |
931 | ||
932 | const vector< shared_ptr<pv::data::Segment> > segments = | |
933 | d->segments(); | |
934 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
935 | temp_count += s->get_sample_count(); | |
936 | ||
937 | if (temp_count > sample_count) | |
938 | sample_count = temp_count; | |
939 | } | |
940 | } | |
941 | ||
942 | trigger_event(sample_count / get_samplerate()); | |
943 | } | |
944 | ||
945 | void Session::feed_in_frame_begin() | |
946 | { | |
947 | if (cur_logic_segment_ || !cur_analog_segments_.empty()) | |
948 | frame_began(); | |
949 | } | |
950 | ||
951 | void Session::feed_in_logic(shared_ptr<Logic> logic) | |
952 | { | |
953 | lock_guard<recursive_mutex> lock(data_mutex_); | |
954 | ||
955 | if (!logic_data_) { | |
956 | // The only reason logic_data_ would not have been created is | |
957 | // if it was not possible to determine the signals when the | |
958 | // device was created. | |
959 | update_signals(); | |
960 | } | |
961 | ||
962 | if (!cur_logic_segment_) { | |
963 | // This could be the first packet after a trigger | |
964 | set_capture_state(Running); | |
965 | ||
966 | // Create a new data segment | |
967 | cur_logic_segment_ = make_shared<data::LogicSegment>( | |
968 | *logic_data_, logic->unit_size(), cur_samplerate_); | |
969 | logic_data_->push_segment(cur_logic_segment_); | |
970 | ||
971 | // @todo Putting this here means that only listeners querying | |
972 | // for logic will be notified. Currently the only user of | |
973 | // frame_began is DecoderStack, but in future we need to signal | |
974 | // this after both analog and logic sweeps have begun. | |
975 | frame_began(); | |
976 | } | |
977 | ||
978 | cur_logic_segment_->append_payload(logic); | |
979 | ||
980 | data_received(); | |
981 | } | |
982 | ||
983 | void Session::feed_in_analog(shared_ptr<Analog> analog) | |
984 | { | |
985 | lock_guard<recursive_mutex> lock(data_mutex_); | |
986 | ||
987 | const vector<shared_ptr<Channel>> channels = analog->channels(); | |
988 | const unsigned int channel_count = channels.size(); | |
989 | const size_t sample_count = analog->num_samples() / channel_count; | |
990 | const float *data = static_cast<const float *>(analog->data_pointer()); | |
991 | bool sweep_beginning = false; | |
992 | ||
993 | if (signalbases_.empty()) | |
994 | update_signals(); | |
995 | ||
996 | for (auto channel : channels) { | |
997 | shared_ptr<data::AnalogSegment> segment; | |
998 | ||
999 | // Try to get the segment of the channel | |
1000 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
1001 | iterator iter = cur_analog_segments_.find(channel); | |
1002 | if (iter != cur_analog_segments_.end()) | |
1003 | segment = (*iter).second; | |
1004 | else { | |
1005 | // If no segment was found, this means we haven't | |
1006 | // created one yet. i.e. this is the first packet | |
1007 | // in the sweep containing this segment. | |
1008 | sweep_beginning = true; | |
1009 | ||
1010 | // Find the analog data associated with the channel | |
1011 | shared_ptr<data::SignalBase> base = signalbase_from_channel(channel); | |
1012 | assert(base); | |
1013 | ||
1014 | shared_ptr<data::Analog> data(base->analog_data()); | |
1015 | assert(data); | |
1016 | ||
1017 | // Create a segment, keep it in the maps of channels | |
1018 | segment = make_shared<data::AnalogSegment>( | |
1019 | *data, cur_samplerate_); | |
1020 | cur_analog_segments_[channel] = segment; | |
1021 | ||
1022 | // Push the segment into the analog data. | |
1023 | data->push_segment(segment); | |
1024 | } | |
1025 | ||
1026 | assert(segment); | |
1027 | ||
1028 | // Append the samples in the segment | |
1029 | segment->append_interleaved_samples(data++, sample_count, | |
1030 | channel_count); | |
1031 | } | |
1032 | ||
1033 | if (sweep_beginning) { | |
1034 | // This could be the first packet after a trigger | |
1035 | set_capture_state(Running); | |
1036 | } | |
1037 | ||
1038 | data_received(); | |
1039 | } | |
1040 | ||
1041 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, | |
1042 | shared_ptr<Packet> packet) | |
1043 | { | |
1044 | static bool frame_began=false; | |
1045 | ||
1046 | (void)device; | |
1047 | ||
1048 | assert(device); | |
1049 | assert(device == device_->device()); | |
1050 | assert(packet); | |
1051 | ||
1052 | switch (packet->type()->id()) { | |
1053 | case SR_DF_HEADER: | |
1054 | feed_in_header(); | |
1055 | break; | |
1056 | ||
1057 | case SR_DF_META: | |
1058 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); | |
1059 | break; | |
1060 | ||
1061 | case SR_DF_TRIGGER: | |
1062 | feed_in_trigger(); | |
1063 | break; | |
1064 | ||
1065 | case SR_DF_FRAME_BEGIN: | |
1066 | feed_in_frame_begin(); | |
1067 | frame_began = true; | |
1068 | break; | |
1069 | ||
1070 | case SR_DF_LOGIC: | |
1071 | try { | |
1072 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
1073 | } catch (bad_alloc) { | |
1074 | out_of_memory_ = true; | |
1075 | device_->stop(); | |
1076 | } | |
1077 | break; | |
1078 | ||
1079 | case SR_DF_ANALOG: | |
1080 | try { | |
1081 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
1082 | } catch (bad_alloc) { | |
1083 | out_of_memory_ = true; | |
1084 | device_->stop(); | |
1085 | } | |
1086 | break; | |
1087 | ||
1088 | case SR_DF_FRAME_END: | |
1089 | case SR_DF_END: | |
1090 | { | |
1091 | { | |
1092 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1093 | cur_logic_segment_.reset(); | |
1094 | cur_analog_segments_.clear(); | |
1095 | } | |
1096 | if (frame_began) { | |
1097 | frame_began = false; | |
1098 | frame_ended(); | |
1099 | } | |
1100 | break; | |
1101 | } | |
1102 | default: | |
1103 | break; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | void Session::on_data_saved() | |
1108 | { | |
1109 | data_saved_ = true; | |
1110 | } | |
1111 | ||
1112 | } // namespace pv |