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