]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Session: Allow an SR_DF_META packet to override the samplerate
[pulseview.git] / pv / session.cpp
index 008ee6a282cc3ba326abe6b106a503576d7d753c..4aba385b687d22ac8764da163b3f552792580682 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 
 #include "devicemanager.hpp"
+#include "mainwindow.hpp"
 #include "session.hpp"
 
 #include "data/analog.hpp"
@@ -299,8 +300,9 @@ void Session::restore_settings(QSettings &settings)
                                filename.toStdString());
                        set_device(device);
 
-                       // TODO Perform error handling
-                       start_capture([](QString infoMessage) { (void)infoMessage; });
+                       start_capture([](QString infoMessage) {
+                               // TODO Emulate noquote()
+                               qDebug() << "Session error:" << infoMessage; });
 
                        set_name(QFileInfo(filename).fileName());
                }
@@ -352,7 +354,7 @@ void Session::select_device(shared_ptr<devices::Device> device)
                else
                        set_default_device();
        } catch (const QString &e) {
-               main_bar_->session_error(tr("Failed to select device"), e);
+               MainWindow::show_session_error(tr("Failed to select device"), e);
        }
 }
 
@@ -400,7 +402,7 @@ void Session::set_device(shared_ptr<devices::Device> device)
                device_->open();
        } catch (const QString &e) {
                device_.reset();
-               main_bar_->session_error(tr("Failed to open device"), e);
+               MainWindow::show_session_error(tr("Failed to open device"), e);
        }
 
        if (device_) {
@@ -490,7 +492,7 @@ void Session::load_init_file(const string &file_name, const string &format)
                        [&](const pair<string, shared_ptr<InputFormat> > f) {
                                return f.first == user_name; });
                if (iter == formats.end()) {
-                       main_bar_->session_error(tr("Error"),
+                       MainWindow::show_session_error(tr("Error"),
                                tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
                        return;
                }
@@ -509,6 +511,10 @@ void Session::load_file(QString file_name,
        const QString errorMessage(
                QString("Failed to load file %1").arg(file_name));
 
+       // In the absence of a caller's format spec, try to auto detect.
+       // Assume "sigrok session file" upon lookup miss.
+       if (!format)
+               format = device_manager_.context()->input_format_match(file_name.toStdString());
        try {
                if (format)
                        set_device(shared_ptr<devices::Device>(
@@ -522,7 +528,7 @@ void Session::load_file(QString file_name,
                                        device_manager_.context(),
                                        file_name.toStdString())));
        } catch (Error& e) {
-               main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
+               MainWindow::show_session_error(tr("Failed to load ") + file_name, e.what());
                set_default_device();
                main_bar_->update_device_list();
                return;
@@ -531,7 +537,7 @@ void Session::load_file(QString file_name,
        main_bar_->update_device_list();
 
        start_capture([&, errorMessage](QString infoMessage) {
-               main_bar_->session_error(errorMessage, infoMessage); });
+               MainWindow::show_session_error(errorMessage, infoMessage); });
 
        set_name(QFileInfo(file_name).fileName());
 }
@@ -953,10 +959,8 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
        set_capture_state(Stopped);
 
        // Confirm that SR_DF_END was received
-       if (cur_logic_segment_) {
-               qDebug("SR_DF_END was not received.");
-               assert(false);
-       }
+       if (cur_logic_segment_)
+               qDebug() << "WARNING: SR_DF_END was not received.";
 
        // Optimize memory usage
        free_unused_memory();
@@ -1044,15 +1048,10 @@ void Session::feed_in_meta(shared_ptr<Meta> meta)
        for (auto entry : meta->config()) {
                switch (entry.first->id()) {
                case SR_CONF_SAMPLERATE:
-                       // We can't rely on the header to always contain the sample rate,
-                       // so in case it's supplied via a meta packet, we use it.
-                       if (!cur_samplerate_)
-                               cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
-
-                       /// @todo handle samplerate changes
+                       cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
                        break;
                default:
-                       // Unknown metadata is not an error.
+                       qDebug() << "Received meta data key" << entry.first->id() << ", ignoring.";
                        break;
                }
        }
@@ -1132,6 +1131,11 @@ void Session::feed_in_frame_end()
 
 void Session::feed_in_logic(shared_ptr<Logic> logic)
 {
+       if (logic->data_length() == 0) {
+               qDebug() << "WARNING: Received logic packet with 0 samples.";
+               return;
+       }
+
        if (!cur_samplerate_)
                cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 
@@ -1164,6 +1168,11 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
 
 void Session::feed_in_analog(shared_ptr<Analog> analog)
 {
+       if (analog->num_samples() == 0) {
+               qDebug() << "WARNING: Received analog packet with 0 samples.";
+               return;
+       }
+
        if (!cur_samplerate_)
                cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 
@@ -1282,6 +1291,15 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                // devices use frames, and for those devices, we need to do it here.
                {
                        lock_guard<recursive_mutex> lock(data_mutex_);
+
+                       if (cur_logic_segment_)
+                               cur_logic_segment_->set_complete();
+
+                       for (auto entry : cur_analog_segments_) {
+                               shared_ptr<data::AnalogSegment> segment = entry.second;
+                               segment->set_complete();
+                       }
+
                        cur_logic_segment_.reset();
                        cur_analog_segments_.clear();
                }