]>
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 | #include <QDebug> | |
21 | #include <QFileInfo> | |
22 | ||
23 | #include <cassert> | |
24 | #include <memory> | |
25 | #include <mutex> | |
26 | #include <stdexcept> | |
27 | ||
28 | #include <sys/stat.h> | |
29 | ||
30 | #include "devicemanager.hpp" | |
31 | #include "mainwindow.hpp" | |
32 | #include "session.hpp" | |
33 | ||
34 | #include "data/analog.hpp" | |
35 | #include "data/analogsegment.hpp" | |
36 | #include "data/decode/decoder.hpp" | |
37 | #include "data/logic.hpp" | |
38 | #include "data/logicsegment.hpp" | |
39 | #include "data/signalbase.hpp" | |
40 | ||
41 | #include "devices/hardwaredevice.hpp" | |
42 | #include "devices/inputfile.hpp" | |
43 | #include "devices/sessionfile.hpp" | |
44 | ||
45 | #include "toolbars/mainbar.hpp" | |
46 | ||
47 | #include "views/trace/analogsignal.hpp" | |
48 | #include "views/trace/decodetrace.hpp" | |
49 | #include "views/trace/logicsignal.hpp" | |
50 | #include "views/trace/signal.hpp" | |
51 | #include "views/trace/view.hpp" | |
52 | ||
53 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
54 | ||
55 | #ifdef ENABLE_DECODE | |
56 | #include <libsigrokdecode/libsigrokdecode.h> | |
57 | #include "data/decodesignal.hpp" | |
58 | #endif | |
59 | ||
60 | using std::bad_alloc; | |
61 | using std::dynamic_pointer_cast; | |
62 | using std::find_if; | |
63 | using std::function; | |
64 | using std::lock_guard; | |
65 | using std::list; | |
66 | using std::make_pair; | |
67 | using std::make_shared; | |
68 | using std::map; | |
69 | using std::max; | |
70 | using std::move; | |
71 | using std::mutex; | |
72 | using std::pair; | |
73 | using std::recursive_mutex; | |
74 | using std::runtime_error; | |
75 | using std::shared_ptr; | |
76 | using std::string; | |
77 | using std::unique_ptr; | |
78 | using std::unordered_set; | |
79 | using std::vector; | |
80 | ||
81 | using sigrok::Analog; | |
82 | using sigrok::Channel; | |
83 | using sigrok::ConfigKey; | |
84 | using sigrok::DatafeedCallbackFunction; | |
85 | using sigrok::Error; | |
86 | using sigrok::InputFormat; | |
87 | using sigrok::Logic; | |
88 | using sigrok::Meta; | |
89 | using sigrok::Packet; | |
90 | using sigrok::Session; | |
91 | ||
92 | using Glib::VariantBase; | |
93 | ||
94 | namespace pv { | |
95 | ||
96 | shared_ptr<sigrok::Context> Session::sr_context; | |
97 | ||
98 | Session::Session(DeviceManager &device_manager, QString name) : | |
99 | device_manager_(device_manager), | |
100 | default_name_(name), | |
101 | name_(name), | |
102 | capture_state_(Stopped), | |
103 | cur_samplerate_(0), | |
104 | data_saved_(true) | |
105 | { | |
106 | } | |
107 | ||
108 | Session::~Session() | |
109 | { | |
110 | // Stop and join to the thread | |
111 | stop_capture(); | |
112 | } | |
113 | ||
114 | DeviceManager& Session::device_manager() | |
115 | { | |
116 | return device_manager_; | |
117 | } | |
118 | ||
119 | const DeviceManager& Session::device_manager() const | |
120 | { | |
121 | return device_manager_; | |
122 | } | |
123 | ||
124 | shared_ptr<sigrok::Session> Session::session() const | |
125 | { | |
126 | if (!device_) | |
127 | return shared_ptr<sigrok::Session>(); | |
128 | return device_->session(); | |
129 | } | |
130 | ||
131 | shared_ptr<devices::Device> Session::device() const | |
132 | { | |
133 | return device_; | |
134 | } | |
135 | ||
136 | QString Session::name() const | |
137 | { | |
138 | return name_; | |
139 | } | |
140 | ||
141 | void Session::set_name(QString name) | |
142 | { | |
143 | if (default_name_.isEmpty()) | |
144 | default_name_ = name; | |
145 | ||
146 | name_ = name; | |
147 | ||
148 | name_changed(); | |
149 | } | |
150 | ||
151 | const list< shared_ptr<views::ViewBase> > Session::views() const | |
152 | { | |
153 | return views_; | |
154 | } | |
155 | ||
156 | shared_ptr<views::ViewBase> Session::main_view() const | |
157 | { | |
158 | return main_view_; | |
159 | } | |
160 | ||
161 | void Session::set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar) | |
162 | { | |
163 | main_bar_ = main_bar; | |
164 | } | |
165 | ||
166 | shared_ptr<pv::toolbars::MainBar> Session::main_bar() const | |
167 | { | |
168 | return main_bar_; | |
169 | } | |
170 | ||
171 | bool Session::data_saved() const | |
172 | { | |
173 | return data_saved_; | |
174 | } | |
175 | ||
176 | void Session::save_settings(QSettings &settings) const | |
177 | { | |
178 | map<string, string> dev_info; | |
179 | list<string> key_list; | |
180 | int decode_signals = 0, views = 0; | |
181 | ||
182 | if (device_) { | |
183 | shared_ptr<devices::HardwareDevice> hw_device = | |
184 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
185 | ||
186 | if (hw_device) { | |
187 | settings.setValue("device_type", "hardware"); | |
188 | settings.beginGroup("device"); | |
189 | ||
190 | key_list.emplace_back("vendor"); | |
191 | key_list.emplace_back("model"); | |
192 | key_list.emplace_back("version"); | |
193 | key_list.emplace_back("serial_num"); | |
194 | key_list.emplace_back("connection_id"); | |
195 | ||
196 | dev_info = device_manager_.get_device_info(device_); | |
197 | ||
198 | for (string& key : key_list) { | |
199 | if (dev_info.count(key)) | |
200 | settings.setValue(QString::fromUtf8(key.c_str()), | |
201 | QString::fromUtf8(dev_info.at(key).c_str())); | |
202 | else | |
203 | settings.remove(QString::fromUtf8(key.c_str())); | |
204 | } | |
205 | ||
206 | settings.endGroup(); | |
207 | } | |
208 | ||
209 | shared_ptr<devices::SessionFile> sessionfile_device = | |
210 | dynamic_pointer_cast<devices::SessionFile>(device_); | |
211 | ||
212 | if (sessionfile_device) { | |
213 | settings.setValue("device_type", "sessionfile"); | |
214 | settings.beginGroup("device"); | |
215 | settings.setValue("filename", QString::fromStdString( | |
216 | sessionfile_device->full_name())); | |
217 | settings.endGroup(); | |
218 | } | |
219 | ||
220 | shared_ptr<devices::InputFile> inputfile_device = | |
221 | dynamic_pointer_cast<devices::InputFile>(device_); | |
222 | ||
223 | if (inputfile_device) { | |
224 | settings.setValue("device_type", "inputfile"); | |
225 | settings.beginGroup("device"); | |
226 | inputfile_device->save_meta_to_settings(settings); | |
227 | settings.endGroup(); | |
228 | } | |
229 | ||
230 | // Save channels and decoders | |
231 | for (const shared_ptr<data::SignalBase>& base : signalbases_) { | |
232 | #ifdef ENABLE_DECODE | |
233 | if (base->is_decode_signal()) { | |
234 | settings.beginGroup("decode_signal" + QString::number(decode_signals++)); | |
235 | base->save_settings(settings); | |
236 | settings.endGroup(); | |
237 | } else | |
238 | #endif | |
239 | { | |
240 | settings.beginGroup(base->internal_name()); | |
241 | base->save_settings(settings); | |
242 | settings.endGroup(); | |
243 | } | |
244 | } | |
245 | ||
246 | settings.setValue("decode_signals", decode_signals); | |
247 | ||
248 | // Save view states and their signal settings | |
249 | // Note: main_view must be saved as view0 | |
250 | settings.beginGroup("view" + QString::number(views++)); | |
251 | main_view_->save_settings(settings); | |
252 | settings.endGroup(); | |
253 | ||
254 | for (const shared_ptr<views::ViewBase>& view : views_) { | |
255 | if (view != main_view_) { | |
256 | settings.beginGroup("view" + QString::number(views++)); | |
257 | view->save_settings(settings); | |
258 | settings.endGroup(); | |
259 | } | |
260 | } | |
261 | ||
262 | settings.setValue("views", views); | |
263 | } | |
264 | } | |
265 | ||
266 | void Session::restore_settings(QSettings &settings) | |
267 | { | |
268 | shared_ptr<devices::Device> device; | |
269 | ||
270 | QString device_type = settings.value("device_type").toString(); | |
271 | ||
272 | if (device_type == "hardware") { | |
273 | map<string, string> dev_info; | |
274 | list<string> key_list; | |
275 | ||
276 | // Re-select last used device if possible but only if it's not demo | |
277 | settings.beginGroup("device"); | |
278 | key_list.emplace_back("vendor"); | |
279 | key_list.emplace_back("model"); | |
280 | key_list.emplace_back("version"); | |
281 | key_list.emplace_back("serial_num"); | |
282 | key_list.emplace_back("connection_id"); | |
283 | ||
284 | for (string key : key_list) { | |
285 | const QString k = QString::fromStdString(key); | |
286 | if (!settings.contains(k)) | |
287 | continue; | |
288 | ||
289 | const string value = settings.value(k).toString().toStdString(); | |
290 | if (!value.empty()) | |
291 | dev_info.insert(make_pair(key, value)); | |
292 | } | |
293 | ||
294 | if (dev_info.count("model") > 0) | |
295 | device = device_manager_.find_device_from_info(dev_info); | |
296 | ||
297 | if (device) | |
298 | set_device(device); | |
299 | ||
300 | settings.endGroup(); | |
301 | } | |
302 | ||
303 | if ((device_type == "sessionfile") || (device_type == "inputfile")) { | |
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 = make_shared<devices::SessionFile>(device_manager_.context(), | |
311 | filename.toStdString()); | |
312 | } | |
313 | } | |
314 | ||
315 | if (device_type == "inputfile") { | |
316 | settings.beginGroup("device"); | |
317 | device = make_shared<devices::InputFile>(device_manager_.context(), | |
318 | settings); | |
319 | settings.endGroup(); | |
320 | } | |
321 | ||
322 | if (device) { | |
323 | set_device(device); | |
324 | ||
325 | start_capture([](QString infoMessage) { | |
326 | // TODO Emulate noquote() | |
327 | qDebug() << "Session error:" << infoMessage; }); | |
328 | ||
329 | set_name(QString::fromStdString( | |
330 | dynamic_pointer_cast<devices::File>(device)->display_name(device_manager_))); | |
331 | } | |
332 | } | |
333 | ||
334 | if (device) { | |
335 | // Restore channels | |
336 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
337 | settings.beginGroup(base->internal_name()); | |
338 | base->restore_settings(settings); | |
339 | settings.endGroup(); | |
340 | } | |
341 | ||
342 | // Restore decoders | |
343 | #ifdef ENABLE_DECODE | |
344 | int decode_signals = settings.value("decode_signals").toInt(); | |
345 | ||
346 | for (int i = 0; i < decode_signals; i++) { | |
347 | settings.beginGroup("decode_signal" + QString::number(i)); | |
348 | shared_ptr<data::DecodeSignal> signal = add_decode_signal(); | |
349 | signal->restore_settings(settings); | |
350 | settings.endGroup(); | |
351 | } | |
352 | #endif | |
353 | ||
354 | // Restore views | |
355 | int views = settings.value("views").toInt(); | |
356 | ||
357 | for (int i = 0; i < views; i++) { | |
358 | settings.beginGroup("view" + QString::number(i)); | |
359 | ||
360 | if (i > 0) { | |
361 | views::ViewType type = (views::ViewType)settings.value("type").toInt(); | |
362 | add_view(name_, type, this); | |
363 | views_.back()->restore_settings(settings); | |
364 | } else | |
365 | main_view_->restore_settings(settings); | |
366 | ||
367 | settings.endGroup(); | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | void Session::select_device(shared_ptr<devices::Device> device) | |
373 | { | |
374 | try { | |
375 | if (device) | |
376 | set_device(device); | |
377 | else | |
378 | set_default_device(); | |
379 | } catch (const QString &e) { | |
380 | MainWindow::show_session_error(tr("Failed to select device"), e); | |
381 | } | |
382 | } | |
383 | ||
384 | void Session::set_device(shared_ptr<devices::Device> device) | |
385 | { | |
386 | assert(device); | |
387 | ||
388 | // Ensure we are not capturing before setting the device | |
389 | stop_capture(); | |
390 | ||
391 | if (device_) | |
392 | device_->close(); | |
393 | ||
394 | device_.reset(); | |
395 | ||
396 | // Revert name back to default name (e.g. "Session 1") as the data is gone | |
397 | name_ = default_name_; | |
398 | name_changed(); | |
399 | ||
400 | // Remove all stored data and reset all views | |
401 | for (shared_ptr<views::ViewBase> view : views_) { | |
402 | view->clear_signals(); | |
403 | #ifdef ENABLE_DECODE | |
404 | view->clear_decode_signals(); | |
405 | #endif | |
406 | view->reset_view_state(); | |
407 | } | |
408 | for (const shared_ptr<data::SignalData>& d : all_signal_data_) | |
409 | d->clear(); | |
410 | all_signal_data_.clear(); | |
411 | signalbases_.clear(); | |
412 | cur_logic_segment_.reset(); | |
413 | ||
414 | for (auto& entry : cur_analog_segments_) { | |
415 | shared_ptr<sigrok::Channel>(entry.first).reset(); | |
416 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
417 | } | |
418 | ||
419 | logic_data_.reset(); | |
420 | ||
421 | signals_changed(); | |
422 | ||
423 | device_ = move(device); | |
424 | ||
425 | try { | |
426 | device_->open(); | |
427 | } catch (const QString &e) { | |
428 | device_.reset(); | |
429 | MainWindow::show_session_error(tr("Failed to open device"), e); | |
430 | } | |
431 | ||
432 | if (device_) { | |
433 | device_->session()->add_datafeed_callback([=] | |
434 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
435 | data_feed_in(device, packet); | |
436 | }); | |
437 | ||
438 | update_signals(); | |
439 | } | |
440 | ||
441 | device_changed(); | |
442 | } | |
443 | ||
444 | void Session::set_default_device() | |
445 | { | |
446 | const list< shared_ptr<devices::HardwareDevice> > &devices = | |
447 | device_manager_.devices(); | |
448 | ||
449 | if (devices.empty()) | |
450 | return; | |
451 | ||
452 | // Try and find the demo device and select that by default | |
453 | const auto iter = find_if(devices.begin(), devices.end(), | |
454 | [] (const shared_ptr<devices::HardwareDevice> &d) { | |
455 | return d->hardware_device()->driver()->name() == "demo"; }); | |
456 | set_device((iter == devices.end()) ? devices.front() : *iter); | |
457 | } | |
458 | ||
459 | /** | |
460 | * Convert generic options to data types that are specific to InputFormat. | |
461 | * | |
462 | * @param[in] user_spec Vector of tokenized words, string format. | |
463 | * @param[in] fmt_opts Input format's options, result of InputFormat::options(). | |
464 | * | |
465 | * @return Map of options suitable for InputFormat::create_input(). | |
466 | */ | |
467 | map<string, Glib::VariantBase> | |
468 | Session::input_format_options(vector<string> user_spec, | |
469 | map<string, shared_ptr<Option>> fmt_opts) | |
470 | { | |
471 | map<string, Glib::VariantBase> result; | |
472 | ||
473 | for (auto& entry : user_spec) { | |
474 | /* | |
475 | * Split key=value specs. Accept entries without separator | |
476 | * (for simplified boolean specifications). | |
477 | */ | |
478 | string key, val; | |
479 | size_t pos = entry.find("="); | |
480 | if (pos == std::string::npos) { | |
481 | key = entry; | |
482 | val = ""; | |
483 | } else { | |
484 | key = entry.substr(0, pos); | |
485 | val = entry.substr(pos + 1); | |
486 | } | |
487 | ||
488 | /* | |
489 | * Skip user specifications that are not a member of the | |
490 | * format's set of supported options. Have the text input | |
491 | * spec converted to the required input format specific | |
492 | * data type. | |
493 | */ | |
494 | auto found = fmt_opts.find(key); | |
495 | if (found == fmt_opts.end()) | |
496 | continue; | |
497 | shared_ptr<Option> opt = found->second; | |
498 | result[key] = opt->parse_string(val); | |
499 | } | |
500 | ||
501 | return result; | |
502 | } | |
503 | ||
504 | void Session::load_init_file(const string &file_name, const string &format) | |
505 | { | |
506 | shared_ptr<InputFormat> input_format; | |
507 | map<string, Glib::VariantBase> input_opts; | |
508 | ||
509 | if (!format.empty()) { | |
510 | const map<string, shared_ptr<InputFormat> > formats = | |
511 | device_manager_.context()->input_formats(); | |
512 | auto user_opts = pv::util::split_string(format, ":"); | |
513 | string user_name = user_opts.front(); | |
514 | user_opts.erase(user_opts.begin()); | |
515 | const auto iter = find_if(formats.begin(), formats.end(), | |
516 | [&](const pair<string, shared_ptr<InputFormat> > f) { | |
517 | return f.first == user_name; }); | |
518 | if (iter == formats.end()) { | |
519 | MainWindow::show_session_error(tr("Error"), | |
520 | tr("Unexpected input format: %s").arg(QString::fromStdString(format))); | |
521 | return; | |
522 | } | |
523 | input_format = (*iter).second; | |
524 | input_opts = input_format_options(user_opts, | |
525 | input_format->options()); | |
526 | } | |
527 | ||
528 | load_file(QString::fromStdString(file_name), input_format, input_opts); | |
529 | } | |
530 | ||
531 | void Session::load_file(QString file_name, | |
532 | shared_ptr<sigrok::InputFormat> format, | |
533 | const map<string, Glib::VariantBase> &options) | |
534 | { | |
535 | const QString errorMessage( | |
536 | QString("Failed to load file %1").arg(file_name)); | |
537 | ||
538 | // In the absence of a caller's format spec, try to auto detect. | |
539 | // Assume "sigrok session file" upon lookup miss. | |
540 | if (!format) | |
541 | format = device_manager_.context()->input_format_match(file_name.toStdString()); | |
542 | try { | |
543 | if (format) | |
544 | set_device(shared_ptr<devices::Device>( | |
545 | new devices::InputFile( | |
546 | device_manager_.context(), | |
547 | file_name.toStdString(), | |
548 | format, options))); | |
549 | else | |
550 | set_device(shared_ptr<devices::Device>( | |
551 | new devices::SessionFile( | |
552 | device_manager_.context(), | |
553 | file_name.toStdString()))); | |
554 | } catch (Error& e) { | |
555 | MainWindow::show_session_error(tr("Failed to load ") + file_name, e.what()); | |
556 | set_default_device(); | |
557 | main_bar_->update_device_list(); | |
558 | return; | |
559 | } | |
560 | ||
561 | main_bar_->update_device_list(); | |
562 | ||
563 | start_capture([&, errorMessage](QString infoMessage) { | |
564 | MainWindow::show_session_error(errorMessage, infoMessage); }); | |
565 | ||
566 | set_name(QFileInfo(file_name).fileName()); | |
567 | } | |
568 | ||
569 | Session::capture_state Session::get_capture_state() const | |
570 | { | |
571 | lock_guard<mutex> lock(sampling_mutex_); | |
572 | return capture_state_; | |
573 | } | |
574 | ||
575 | void Session::start_capture(function<void (const QString)> error_handler) | |
576 | { | |
577 | if (!device_) { | |
578 | error_handler(tr("No active device set, can't start acquisition.")); | |
579 | return; | |
580 | } | |
581 | ||
582 | stop_capture(); | |
583 | ||
584 | // Check that at least one channel is enabled | |
585 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
586 | if (sr_dev) { | |
587 | const auto channels = sr_dev->channels(); | |
588 | if (!any_of(channels.begin(), channels.end(), | |
589 | [](shared_ptr<Channel> channel) { | |
590 | return channel->enabled(); })) { | |
591 | error_handler(tr("No channels enabled.")); | |
592 | return; | |
593 | } | |
594 | } | |
595 | ||
596 | // Clear signal data | |
597 | for (const shared_ptr<data::SignalData>& d : all_signal_data_) | |
598 | d->clear(); | |
599 | ||
600 | trigger_list_.clear(); | |
601 | ||
602 | // Revert name back to default name (e.g. "Session 1") for real devices | |
603 | // as the (possibly saved) data is gone. File devices keep their name. | |
604 | shared_ptr<devices::HardwareDevice> hw_device = | |
605 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
606 | ||
607 | if (hw_device) { | |
608 | name_ = default_name_; | |
609 | name_changed(); | |
610 | } | |
611 | ||
612 | // Begin the session | |
613 | sampling_thread_ = std::thread( | |
614 | &Session::sample_thread_proc, this, error_handler); | |
615 | } | |
616 | ||
617 | void Session::stop_capture() | |
618 | { | |
619 | if (get_capture_state() != Stopped) | |
620 | device_->stop(); | |
621 | ||
622 | // Check that sampling stopped | |
623 | if (sampling_thread_.joinable()) | |
624 | sampling_thread_.join(); | |
625 | } | |
626 | ||
627 | void Session::register_view(shared_ptr<views::ViewBase> view) | |
628 | { | |
629 | if (views_.empty()) { | |
630 | main_view_ = view; | |
631 | } | |
632 | ||
633 | views_.push_back(view); | |
634 | ||
635 | // Add all device signals | |
636 | update_signals(); | |
637 | ||
638 | // Add all other signals | |
639 | unordered_set< shared_ptr<data::SignalBase> > view_signalbases = | |
640 | view->signalbases(); | |
641 | ||
642 | views::trace::View *trace_view = | |
643 | qobject_cast<views::trace::View*>(view.get()); | |
644 | ||
645 | if (trace_view) { | |
646 | for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) { | |
647 | const int sb_exists = count_if( | |
648 | view_signalbases.cbegin(), view_signalbases.cend(), | |
649 | [&](const shared_ptr<data::SignalBase> &sb) { | |
650 | return sb == signalbase; | |
651 | }); | |
652 | // Add the signal to the view as it doesn't have it yet | |
653 | if (!sb_exists) | |
654 | switch (signalbase->type()) { | |
655 | case data::SignalBase::AnalogChannel: | |
656 | case data::SignalBase::LogicChannel: | |
657 | case data::SignalBase::DecodeChannel: | |
658 | #ifdef ENABLE_DECODE | |
659 | trace_view->add_decode_signal( | |
660 | dynamic_pointer_cast<data::DecodeSignal>(signalbase)); | |
661 | #endif | |
662 | break; | |
663 | case data::SignalBase::MathChannel: | |
664 | // TBD | |
665 | break; | |
666 | } | |
667 | } | |
668 | } | |
669 | ||
670 | signals_changed(); | |
671 | } | |
672 | ||
673 | void Session::deregister_view(shared_ptr<views::ViewBase> view) | |
674 | { | |
675 | views_.remove_if([&](shared_ptr<views::ViewBase> v) { return v == view; }); | |
676 | ||
677 | if (views_.empty()) { | |
678 | main_view_.reset(); | |
679 | ||
680 | // Without a view there can be no main bar | |
681 | main_bar_.reset(); | |
682 | } | |
683 | } | |
684 | ||
685 | bool Session::has_view(shared_ptr<views::ViewBase> view) | |
686 | { | |
687 | for (shared_ptr<views::ViewBase>& v : views_) | |
688 | if (v == view) | |
689 | return true; | |
690 | ||
691 | return false; | |
692 | } | |
693 | ||
694 | double Session::get_samplerate() const | |
695 | { | |
696 | double samplerate = 0.0; | |
697 | ||
698 | for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) { | |
699 | assert(d); | |
700 | const vector< shared_ptr<pv::data::Segment> > segments = | |
701 | d->segments(); | |
702 | for (const shared_ptr<pv::data::Segment>& s : segments) | |
703 | samplerate = max(samplerate, s->samplerate()); | |
704 | } | |
705 | // If there is no sample rate given we use samples as unit | |
706 | if (samplerate == 0.0) | |
707 | samplerate = 1.0; | |
708 | ||
709 | return samplerate; | |
710 | } | |
711 | ||
712 | uint32_t Session::get_segment_count() const | |
713 | { | |
714 | uint32_t value = 0; | |
715 | ||
716 | // Find the highest number of segments | |
717 | for (const shared_ptr<data::SignalData>& data : all_signal_data_) | |
718 | if (data->get_segment_count() > value) | |
719 | value = data->get_segment_count(); | |
720 | ||
721 | return value; | |
722 | } | |
723 | ||
724 | vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const | |
725 | { | |
726 | vector<util::Timestamp> result; | |
727 | ||
728 | for (const pair<uint32_t, util::Timestamp>& entry : trigger_list_) | |
729 | if (entry.first == segment_id) | |
730 | result.push_back(entry.second); | |
731 | ||
732 | return result; | |
733 | } | |
734 | ||
735 | const unordered_set< shared_ptr<data::SignalBase> > Session::signalbases() const | |
736 | { | |
737 | return signalbases_; | |
738 | } | |
739 | ||
740 | bool Session::all_segments_complete(uint32_t segment_id) const | |
741 | { | |
742 | bool all_complete = true; | |
743 | ||
744 | for (const shared_ptr<data::SignalBase>& base : signalbases_) | |
745 | if (!base->segment_is_complete(segment_id)) | |
746 | all_complete = false; | |
747 | ||
748 | return all_complete; | |
749 | } | |
750 | ||
751 | #ifdef ENABLE_DECODE | |
752 | shared_ptr<data::DecodeSignal> Session::add_decode_signal() | |
753 | { | |
754 | shared_ptr<data::DecodeSignal> signal; | |
755 | ||
756 | try { | |
757 | // Create the decode signal | |
758 | signal = make_shared<data::DecodeSignal>(*this); | |
759 | ||
760 | signalbases_.insert(signal); | |
761 | ||
762 | // Add the decode signal to all views | |
763 | for (shared_ptr<views::ViewBase>& view : views_) | |
764 | view->add_decode_signal(signal); | |
765 | } catch (runtime_error& e) { | |
766 | remove_decode_signal(signal); | |
767 | return nullptr; | |
768 | } | |
769 | ||
770 | signals_changed(); | |
771 | ||
772 | return signal; | |
773 | } | |
774 | ||
775 | void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
776 | { | |
777 | signalbases_.erase(signal); | |
778 | ||
779 | for (shared_ptr<views::ViewBase>& view : views_) | |
780 | view->remove_decode_signal(signal); | |
781 | ||
782 | signals_changed(); | |
783 | } | |
784 | #endif | |
785 | ||
786 | void Session::set_capture_state(capture_state state) | |
787 | { | |
788 | bool changed; | |
789 | ||
790 | { | |
791 | lock_guard<mutex> lock(sampling_mutex_); | |
792 | changed = capture_state_ != state; | |
793 | capture_state_ = state; | |
794 | } | |
795 | ||
796 | if (changed) | |
797 | capture_state_changed(state); | |
798 | } | |
799 | ||
800 | void Session::update_signals() | |
801 | { | |
802 | if (!device_) { | |
803 | signalbases_.clear(); | |
804 | logic_data_.reset(); | |
805 | for (shared_ptr<views::ViewBase>& view : views_) { | |
806 | view->clear_signals(); | |
807 | #ifdef ENABLE_DECODE | |
808 | view->clear_decode_signals(); | |
809 | #endif | |
810 | } | |
811 | return; | |
812 | } | |
813 | ||
814 | lock_guard<recursive_mutex> lock(data_mutex_); | |
815 | ||
816 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
817 | if (!sr_dev) { | |
818 | signalbases_.clear(); | |
819 | logic_data_.reset(); | |
820 | for (shared_ptr<views::ViewBase>& view : views_) { | |
821 | view->clear_signals(); | |
822 | #ifdef ENABLE_DECODE | |
823 | view->clear_decode_signals(); | |
824 | #endif | |
825 | } | |
826 | return; | |
827 | } | |
828 | ||
829 | // Detect what data types we will receive | |
830 | auto channels = sr_dev->channels(); | |
831 | unsigned int logic_channel_count = count_if( | |
832 | channels.begin(), channels.end(), | |
833 | [] (shared_ptr<Channel> channel) { | |
834 | return channel->type() == sigrok::ChannelType::LOGIC; }); | |
835 | ||
836 | // Create data containers for the logic data segments | |
837 | { | |
838 | lock_guard<recursive_mutex> data_lock(data_mutex_); | |
839 | ||
840 | if (logic_channel_count == 0) { | |
841 | logic_data_.reset(); | |
842 | } else if (!logic_data_ || | |
843 | logic_data_->num_channels() != logic_channel_count) { | |
844 | logic_data_.reset(new data::Logic( | |
845 | logic_channel_count)); | |
846 | assert(logic_data_); | |
847 | } | |
848 | } | |
849 | ||
850 | // Make the signals list | |
851 | for (shared_ptr<views::ViewBase>& viewbase : views_) { | |
852 | views::trace::View *trace_view = | |
853 | qobject_cast<views::trace::View*>(viewbase.get()); | |
854 | ||
855 | if (trace_view) { | |
856 | unordered_set< shared_ptr<views::trace::Signal> > | |
857 | prev_sigs(trace_view->signals()); | |
858 | trace_view->clear_signals(); | |
859 | ||
860 | for (auto channel : sr_dev->channels()) { | |
861 | shared_ptr<data::SignalBase> signalbase; | |
862 | shared_ptr<views::trace::Signal> signal; | |
863 | ||
864 | // Find the channel in the old signals | |
865 | const auto iter = find_if( | |
866 | prev_sigs.cbegin(), prev_sigs.cend(), | |
867 | [&](const shared_ptr<views::trace::Signal> &s) { | |
868 | return s->base()->channel() == channel; | |
869 | }); | |
870 | if (iter != prev_sigs.end()) { | |
871 | // Copy the signal from the old set to the new | |
872 | signal = *iter; | |
873 | trace_view->add_signal(signal); | |
874 | } else { | |
875 | // Find the signalbase for this channel if possible | |
876 | signalbase.reset(); | |
877 | for (const shared_ptr<data::SignalBase>& b : signalbases_) | |
878 | if (b->channel() == channel) | |
879 | signalbase = b; | |
880 | ||
881 | switch(channel->type()->id()) { | |
882 | case SR_CHANNEL_LOGIC: | |
883 | if (!signalbase) { | |
884 | signalbase = make_shared<data::SignalBase>(channel, | |
885 | data::SignalBase::LogicChannel); | |
886 | signalbases_.insert(signalbase); | |
887 | ||
888 | all_signal_data_.insert(logic_data_); | |
889 | signalbase->set_data(logic_data_); | |
890 | ||
891 | connect(this, SIGNAL(capture_state_changed(int)), | |
892 | signalbase.get(), SLOT(on_capture_state_changed(int))); | |
893 | } | |
894 | ||
895 | signal = shared_ptr<views::trace::Signal>( | |
896 | new views::trace::LogicSignal(*this, | |
897 | device_, signalbase)); | |
898 | trace_view->add_signal(signal); | |
899 | break; | |
900 | ||
901 | case SR_CHANNEL_ANALOG: | |
902 | { | |
903 | if (!signalbase) { | |
904 | signalbase = make_shared<data::SignalBase>(channel, | |
905 | data::SignalBase::AnalogChannel); | |
906 | signalbases_.insert(signalbase); | |
907 | ||
908 | shared_ptr<data::Analog> data(new data::Analog()); | |
909 | all_signal_data_.insert(data); | |
910 | signalbase->set_data(data); | |
911 | ||
912 | connect(this, SIGNAL(capture_state_changed(int)), | |
913 | signalbase.get(), SLOT(on_capture_state_changed(int))); | |
914 | } | |
915 | ||
916 | signal = shared_ptr<views::trace::Signal>( | |
917 | new views::trace::AnalogSignal( | |
918 | *this, signalbase)); | |
919 | trace_view->add_signal(signal); | |
920 | break; | |
921 | } | |
922 | ||
923 | default: | |
924 | assert(false); | |
925 | break; | |
926 | } | |
927 | } | |
928 | } | |
929 | } | |
930 | } | |
931 | ||
932 | signals_changed(); | |
933 | } | |
934 | ||
935 | shared_ptr<data::SignalBase> Session::signalbase_from_channel( | |
936 | shared_ptr<sigrok::Channel> channel) const | |
937 | { | |
938 | for (shared_ptr<data::SignalBase> sig : signalbases_) { | |
939 | assert(sig); | |
940 | if (sig->channel() == channel) | |
941 | return sig; | |
942 | } | |
943 | return shared_ptr<data::SignalBase>(); | |
944 | } | |
945 | ||
946 | void Session::sample_thread_proc(function<void (const QString)> error_handler) | |
947 | { | |
948 | assert(error_handler); | |
949 | ||
950 | if (!device_) | |
951 | return; | |
952 | ||
953 | try { | |
954 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
955 | } catch (Error& e) { | |
956 | cur_samplerate_ = 0; | |
957 | } | |
958 | ||
959 | out_of_memory_ = false; | |
960 | ||
961 | { | |
962 | lock_guard<recursive_mutex> lock(data_mutex_); | |
963 | cur_logic_segment_.reset(); | |
964 | cur_analog_segments_.clear(); | |
965 | } | |
966 | highest_segment_id_ = -1; | |
967 | frame_began_ = false; | |
968 | ||
969 | try { | |
970 | device_->start(); | |
971 | } catch (Error& e) { | |
972 | error_handler(e.what()); | |
973 | return; | |
974 | } | |
975 | ||
976 | set_capture_state(device_->session()->trigger() ? | |
977 | AwaitingTrigger : Running); | |
978 | ||
979 | try { | |
980 | device_->run(); | |
981 | } catch (Error& e) { | |
982 | error_handler(e.what()); | |
983 | set_capture_state(Stopped); | |
984 | return; | |
985 | } | |
986 | ||
987 | set_capture_state(Stopped); | |
988 | ||
989 | // Confirm that SR_DF_END was received | |
990 | if (cur_logic_segment_) | |
991 | qDebug() << "WARNING: SR_DF_END was not received."; | |
992 | ||
993 | // Optimize memory usage | |
994 | free_unused_memory(); | |
995 | ||
996 | // We now have unsaved data unless we just "captured" from a file | |
997 | shared_ptr<devices::File> file_device = | |
998 | dynamic_pointer_cast<devices::File>(device_); | |
999 | ||
1000 | if (!file_device) | |
1001 | data_saved_ = false; | |
1002 | ||
1003 | if (out_of_memory_) | |
1004 | error_handler(tr("Out of memory, acquisition stopped.")); | |
1005 | } | |
1006 | ||
1007 | void Session::free_unused_memory() | |
1008 | { | |
1009 | for (const shared_ptr<data::SignalData>& data : all_signal_data_) { | |
1010 | const vector< shared_ptr<data::Segment> > segments = data->segments(); | |
1011 | ||
1012 | for (const shared_ptr<data::Segment>& segment : segments) | |
1013 | segment->free_unused_memory(); | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | void Session::signal_new_segment() | |
1018 | { | |
1019 | int new_segment_id = 0; | |
1020 | ||
1021 | if ((cur_logic_segment_ != nullptr) || !cur_analog_segments_.empty()) { | |
1022 | ||
1023 | // Determine new frame/segment number, assuming that all | |
1024 | // signals have the same number of frames/segments | |
1025 | if (cur_logic_segment_) { | |
1026 | new_segment_id = logic_data_->get_segment_count() - 1; | |
1027 | } else { | |
1028 | shared_ptr<sigrok::Channel> any_channel = | |
1029 | (*cur_analog_segments_.begin()).first; | |
1030 | ||
1031 | shared_ptr<data::SignalBase> base = signalbase_from_channel(any_channel); | |
1032 | assert(base); | |
1033 | ||
1034 | shared_ptr<data::Analog> data(base->analog_data()); | |
1035 | assert(data); | |
1036 | ||
1037 | new_segment_id = data->get_segment_count() - 1; | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | if (new_segment_id > highest_segment_id_) { | |
1042 | highest_segment_id_ = new_segment_id; | |
1043 | new_segment(highest_segment_id_); | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | void Session::signal_segment_completed() | |
1048 | { | |
1049 | int segment_id = 0; | |
1050 | ||
1051 | for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) { | |
1052 | // We only care about analog and logic channels, not derived ones | |
1053 | if (signalbase->type() == data::SignalBase::AnalogChannel) { | |
1054 | segment_id = signalbase->analog_data()->get_segment_count() - 1; | |
1055 | break; | |
1056 | } | |
1057 | ||
1058 | if (signalbase->type() == data::SignalBase::LogicChannel) { | |
1059 | segment_id = signalbase->logic_data()->get_segment_count() - 1; | |
1060 | break; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | if (segment_id >= 0) | |
1065 | segment_completed(segment_id); | |
1066 | } | |
1067 | ||
1068 | void Session::feed_in_header() | |
1069 | { | |
1070 | // Nothing to do here for now | |
1071 | } | |
1072 | ||
1073 | void Session::feed_in_meta(shared_ptr<Meta> meta) | |
1074 | { | |
1075 | for (auto& entry : meta->config()) { | |
1076 | switch (entry.first->id()) { | |
1077 | case SR_CONF_SAMPLERATE: | |
1078 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); | |
1079 | break; | |
1080 | default: | |
1081 | qDebug() << "Received meta data key" << entry.first->id() << ", ignoring."; | |
1082 | break; | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | signals_changed(); | |
1087 | } | |
1088 | ||
1089 | void Session::feed_in_trigger() | |
1090 | { | |
1091 | // The channel containing most samples should be most accurate | |
1092 | uint64_t sample_count = 0; | |
1093 | ||
1094 | { | |
1095 | for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) { | |
1096 | assert(d); | |
1097 | uint64_t temp_count = 0; | |
1098 | ||
1099 | const vector< shared_ptr<pv::data::Segment> > segments = | |
1100 | d->segments(); | |
1101 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
1102 | temp_count += s->get_sample_count(); | |
1103 | ||
1104 | if (temp_count > sample_count) | |
1105 | sample_count = temp_count; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | uint32_t segment_id = 0; // Default segment when no frames are used | |
1110 | ||
1111 | // If a frame began, we'd ideally be able to use the highest segment ID for | |
1112 | // the trigger. However, as new segments are only created when logic or | |
1113 | // analog data comes in, this doesn't work if the trigger appears right | |
1114 | // after the beginning of the frame, before any sample data. | |
1115 | // For this reason, we use highest segment ID + 1 if no sample data came in | |
1116 | // yet and the highest segment ID otherwise. | |
1117 | if (frame_began_) { | |
1118 | segment_id = highest_segment_id_; | |
1119 | if (!cur_logic_segment_ && (cur_analog_segments_.size() == 0)) | |
1120 | segment_id++; | |
1121 | } | |
1122 | ||
1123 | // TODO Create timestamp from segment start time + segment's current sample count | |
1124 | util::Timestamp timestamp = sample_count / get_samplerate(); | |
1125 | trigger_list_.emplace_back(segment_id, timestamp); | |
1126 | trigger_event(segment_id, timestamp); | |
1127 | } | |
1128 | ||
1129 | void Session::feed_in_frame_begin() | |
1130 | { | |
1131 | frame_began_ = true; | |
1132 | } | |
1133 | ||
1134 | void Session::feed_in_frame_end() | |
1135 | { | |
1136 | if (!frame_began_) | |
1137 | return; | |
1138 | ||
1139 | { | |
1140 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1141 | ||
1142 | if (cur_logic_segment_) | |
1143 | cur_logic_segment_->set_complete(); | |
1144 | ||
1145 | for (auto& entry : cur_analog_segments_) { | |
1146 | shared_ptr<data::AnalogSegment> segment = entry.second; | |
1147 | segment->set_complete(); | |
1148 | } | |
1149 | ||
1150 | cur_logic_segment_.reset(); | |
1151 | cur_analog_segments_.clear(); | |
1152 | } | |
1153 | ||
1154 | frame_began_ = false; | |
1155 | ||
1156 | signal_segment_completed(); | |
1157 | } | |
1158 | ||
1159 | void Session::feed_in_logic(shared_ptr<Logic> logic) | |
1160 | { | |
1161 | if (logic->data_length() == 0) { | |
1162 | qDebug() << "WARNING: Received logic packet with 0 samples."; | |
1163 | return; | |
1164 | } | |
1165 | ||
1166 | if (!cur_samplerate_) | |
1167 | try { | |
1168 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
1169 | } catch (Error& e) { | |
1170 | // Do nothing | |
1171 | } | |
1172 | ||
1173 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1174 | ||
1175 | if (!logic_data_) { | |
1176 | // The only reason logic_data_ would not have been created is | |
1177 | // if it was not possible to determine the signals when the | |
1178 | // device was created. | |
1179 | update_signals(); | |
1180 | } | |
1181 | ||
1182 | if (!cur_logic_segment_) { | |
1183 | // This could be the first packet after a trigger | |
1184 | set_capture_state(Running); | |
1185 | ||
1186 | // Create a new data segment | |
1187 | cur_logic_segment_ = make_shared<data::LogicSegment>( | |
1188 | *logic_data_, logic_data_->get_segment_count(), | |
1189 | logic->unit_size(), cur_samplerate_); | |
1190 | logic_data_->push_segment(cur_logic_segment_); | |
1191 | ||
1192 | signal_new_segment(); | |
1193 | } | |
1194 | ||
1195 | cur_logic_segment_->append_payload(logic); | |
1196 | ||
1197 | data_received(); | |
1198 | } | |
1199 | ||
1200 | void Session::feed_in_analog(shared_ptr<Analog> analog) | |
1201 | { | |
1202 | if (analog->num_samples() == 0) { | |
1203 | qDebug() << "WARNING: Received analog packet with 0 samples."; | |
1204 | return; | |
1205 | } | |
1206 | ||
1207 | if (!cur_samplerate_) | |
1208 | try { | |
1209 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
1210 | } catch (Error& e) { | |
1211 | // Do nothing | |
1212 | } | |
1213 | ||
1214 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1215 | ||
1216 | const vector<shared_ptr<Channel>> channels = analog->channels(); | |
1217 | bool sweep_beginning = false; | |
1218 | ||
1219 | unique_ptr<float[]> data(new float[analog->num_samples() * channels.size()]); | |
1220 | analog->get_data_as_float(data.get()); | |
1221 | ||
1222 | if (signalbases_.empty()) | |
1223 | update_signals(); | |
1224 | ||
1225 | float *channel_data = data.get(); | |
1226 | for (auto& channel : channels) { | |
1227 | shared_ptr<data::AnalogSegment> segment; | |
1228 | ||
1229 | // Try to get the segment of the channel | |
1230 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
1231 | iterator iter = cur_analog_segments_.find(channel); | |
1232 | if (iter != cur_analog_segments_.end()) | |
1233 | segment = (*iter).second; | |
1234 | else { | |
1235 | // If no segment was found, this means we haven't | |
1236 | // created one yet. i.e. this is the first packet | |
1237 | // in the sweep containing this segment. | |
1238 | sweep_beginning = true; | |
1239 | ||
1240 | // Find the analog data associated with the channel | |
1241 | shared_ptr<data::SignalBase> base = signalbase_from_channel(channel); | |
1242 | assert(base); | |
1243 | ||
1244 | shared_ptr<data::Analog> data(base->analog_data()); | |
1245 | assert(data); | |
1246 | ||
1247 | // Create a segment, keep it in the maps of channels | |
1248 | segment = make_shared<data::AnalogSegment>( | |
1249 | *data, data->get_segment_count(), cur_samplerate_); | |
1250 | cur_analog_segments_[channel] = segment; | |
1251 | ||
1252 | // Push the segment into the analog data. | |
1253 | data->push_segment(segment); | |
1254 | ||
1255 | signal_new_segment(); | |
1256 | } | |
1257 | ||
1258 | assert(segment); | |
1259 | ||
1260 | // Append the samples in the segment | |
1261 | segment->append_interleaved_samples(channel_data++, analog->num_samples(), | |
1262 | channels.size()); | |
1263 | } | |
1264 | ||
1265 | if (sweep_beginning) { | |
1266 | // This could be the first packet after a trigger | |
1267 | set_capture_state(Running); | |
1268 | } | |
1269 | ||
1270 | data_received(); | |
1271 | } | |
1272 | ||
1273 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, | |
1274 | shared_ptr<Packet> packet) | |
1275 | { | |
1276 | (void)device; | |
1277 | ||
1278 | assert(device); | |
1279 | assert(device == device_->device()); | |
1280 | assert(packet); | |
1281 | ||
1282 | switch (packet->type()->id()) { | |
1283 | case SR_DF_HEADER: | |
1284 | feed_in_header(); | |
1285 | break; | |
1286 | ||
1287 | case SR_DF_META: | |
1288 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); | |
1289 | break; | |
1290 | ||
1291 | case SR_DF_TRIGGER: | |
1292 | feed_in_trigger(); | |
1293 | break; | |
1294 | ||
1295 | case SR_DF_LOGIC: | |
1296 | try { | |
1297 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
1298 | } catch (bad_alloc&) { | |
1299 | out_of_memory_ = true; | |
1300 | device_->stop(); | |
1301 | } | |
1302 | break; | |
1303 | ||
1304 | case SR_DF_ANALOG: | |
1305 | try { | |
1306 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
1307 | } catch (bad_alloc&) { | |
1308 | out_of_memory_ = true; | |
1309 | device_->stop(); | |
1310 | } | |
1311 | break; | |
1312 | ||
1313 | case SR_DF_FRAME_BEGIN: | |
1314 | feed_in_frame_begin(); | |
1315 | break; | |
1316 | ||
1317 | case SR_DF_FRAME_END: | |
1318 | feed_in_frame_end(); | |
1319 | break; | |
1320 | ||
1321 | case SR_DF_END: | |
1322 | // Strictly speaking, this is performed when a frame end marker was | |
1323 | // received, so there's no point doing this again. However, not all | |
1324 | // devices use frames, and for those devices, we need to do it here. | |
1325 | { | |
1326 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1327 | ||
1328 | if (cur_logic_segment_) | |
1329 | cur_logic_segment_->set_complete(); | |
1330 | ||
1331 | for (auto& entry : cur_analog_segments_) { | |
1332 | shared_ptr<data::AnalogSegment> segment = entry.second; | |
1333 | segment->set_complete(); | |
1334 | } | |
1335 | ||
1336 | cur_logic_segment_.reset(); | |
1337 | cur_analog_segments_.clear(); | |
1338 | } | |
1339 | break; | |
1340 | ||
1341 | default: | |
1342 | break; | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | void Session::on_data_saved() | |
1347 | { | |
1348 | data_saved_ = true; | |
1349 | } | |
1350 | ||
1351 | } // namespace pv |