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