X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fsigsession.cpp;h=bda4b0d32c38c03fe5d7c26a5645ae03f85b64cc;hp=c5a7d494ecf8ed55237b4c440bd3f4497e72c398;hb=b087ba7fd88610cbf54f6371367c7b9f9829dc63;hpb=884971565598bdd7641f64d9e76dba2cc9b16c01 diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index c5a7d494..bda4b0d3 100644 --- a/pv/sigsession.cpp +++ b/pv/sigsession.cpp @@ -20,6 +20,7 @@ #include "sigsession.h" +#include "devicemanager.h" #include "data/analog.h" #include "data/analogsnapshot.h" #include "data/logic.h" @@ -29,6 +30,8 @@ #include +#include + #include using namespace boost; @@ -39,7 +42,8 @@ namespace pv { // TODO: This should not be necessary SigSession* SigSession::_session = NULL; -SigSession::SigSession() : +SigSession::SigSession(DeviceManager &device_manager) : + _device_manager(device_manager), _sdi(NULL), _capture_state(Stopped) { @@ -55,17 +59,36 @@ SigSession::~SigSession() _sampling_thread->join(); _sampling_thread.reset(); + if (_sdi) + _device_manager.release_device(_sdi); + _sdi = NULL; + // TODO: This should not be necessary _session = NULL; } +struct sr_dev_inst* SigSession::get_device() const +{ + return _sdi; +} + void SigSession::set_device(struct sr_dev_inst *sdi) { if (_sdi) - sr_dev_close(_sdi); + _device_manager.release_device(_sdi); if (sdi) - sr_dev_open(sdi); + _device_manager.use_device(sdi, this); _sdi = sdi; + update_signals(); +} + +void SigSession::release_device(struct sr_dev_inst *sdi) +{ + (void)sdi; + + assert(_capture_state == Stopped); + _sdi = NULL; + update_signals(); } void SigSession::load_file(const string &name, @@ -145,27 +168,199 @@ void SigSession::set_capture_state(capture_state state) capture_state_changed(state); } +/** + * Attempts to autodetect the format. Failing that + * @param filename The filename of the input file. + * @return A pointer to the 'struct sr_input_format' that should be used, + * or NULL if no input format was selected or auto-detected. + */ +sr_input_format* SigSession::determine_input_file_format( + const string &filename) +{ + int i; + + /* If there are no input formats, return NULL right away. */ + sr_input_format *const *const inputs = sr_input_list(); + if (!inputs) { + g_critical("No supported input formats available."); + return NULL; + } + + /* Otherwise, try to find an input module that can handle this file. */ + for (i = 0; inputs[i]; i++) { + if (inputs[i]->format_match(filename.c_str())) + break; + } + + /* Return NULL if no input module wanted to touch this. */ + if (!inputs[i]) { + g_critical("Error: no matching input module found."); + return NULL; + } + + return inputs[i]; +} + +sr_input* SigSession::load_input_file_format(const string &filename, + function error_handler, + sr_input_format *format) +{ + struct stat st; + sr_input *in; + + if (!format && !(format = + determine_input_file_format(filename.c_str()))) { + /* The exact cause was already logged. */ + return NULL; + } + + if (stat(filename.c_str(), &st) == -1) { + error_handler(tr("Failed to load file")); + return NULL; + } + + /* Initialize the input module. */ + if (!(in = new sr_input)) { + qDebug("Failed to allocate input module.\n"); + return NULL; + } + + in->format = format; + in->param = NULL; + if (in->format->init && + in->format->init(in, filename.c_str()) != SR_OK) { + qDebug("Input format init failed.\n"); + return NULL; + } + + sr_session_new(); + + if (sr_session_dev_add(in->sdi) != SR_OK) { + qDebug("Failed to use device.\n"); + sr_session_destroy(); + return NULL; + } + + return in; +} + +void SigSession::update_signals() +{ + assert(_capture_state == Stopped); + + shared_ptr signal; + unsigned int logic_probe_count = 0; + unsigned int analog_probe_count = 0; + + // Detect what data types we will receive + if(_sdi) { + for (const GSList *l = _sdi->probes; l; l = l->next) { + const sr_probe *const probe = (const sr_probe *)l->data; + if (!probe->enabled) + continue; + + switch(probe->type) { + case SR_PROBE_LOGIC: + logic_probe_count++; + break; + + case SR_PROBE_ANALOG: + analog_probe_count++; + break; + } + } + } + + // Create data containers for the data snapshots + { + lock_guard data_lock(_data_mutex); + + _logic_data.reset(); + if (logic_probe_count != 0) { + _logic_data.reset(new data::Logic( + logic_probe_count)); + assert(_logic_data); + } + + _analog_data.reset(); + if (analog_probe_count != 0) { + _analog_data.reset(new data::Analog()); + assert(_analog_data); + } + } + + // Make the Signals list + { + lock_guard lock(_signals_mutex); + + _signals.clear(); + + if(_sdi) { + for (const GSList *l = _sdi->probes; l; l = l->next) { + const sr_probe *const probe = + (const sr_probe *)l->data; + assert(probe); + if (!probe->enabled) + continue; + + switch(probe->type) { + case SR_PROBE_LOGIC: + signal = shared_ptr( + new view::LogicSignal( + probe->name, + _logic_data, + probe->index)); + break; + + case SR_PROBE_ANALOG: + signal = shared_ptr( + new view::AnalogSignal( + probe->name, + _analog_data, + probe->index)); + break; + } + + _signals.push_back(signal); + } + } + } + + signals_changed(); +} + void SigSession::load_thread_proc(const string name, function error_handler) { - if (sr_session_load(name.c_str()) != SR_OK) { - error_handler(tr("Failed to load file.")); - return; + sr_input *in = NULL; + + if (sr_session_load(name.c_str()) == SR_OK) { + if (sr_session_start() != SR_OK) { + error_handler(tr("Failed to start session.")); + return; + } } + else if(!(in = load_input_file_format(name.c_str(), error_handler))) + return; sr_session_datafeed_callback_add(data_feed_in_proc, NULL); - if (sr_session_start() != SR_OK) { - error_handler(tr("Failed to start session.")); - return; - } - set_capture_state(Running); - sr_session_run(); - sr_session_destroy(); + if(in) { + assert(in->format); + in->format->loadfile(in, name.c_str()); + } else + sr_session_run(); + sr_session_destroy(); set_capture_state(Stopped); + + // Confirm that SR_DF_END was received + assert(!_cur_logic_snapshot); + assert(!_cur_analog_snapshot); + + delete in; } void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, @@ -204,94 +399,35 @@ void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, sr_session_destroy(); set_capture_state(Stopped); + + // Confirm that SR_DF_END was received + assert(!_cur_logic_snapshot); + assert(!_cur_analog_snapshot); } void SigSession::feed_in_header(const sr_dev_inst *sdi) { - shared_ptr signal; GVariant *gvar; uint64_t sample_rate = 0; - unsigned int logic_probe_count = 0; - unsigned int analog_probe_count = 0; - - // Detect what data types we will receive - for (const GSList *l = sdi->probes; l; l = l->next) { - const sr_probe *const probe = (const sr_probe *)l->data; - if (!probe->enabled) - continue; - - switch(probe->type) { - case SR_PROBE_LOGIC: - logic_probe_count++; - break; - - case SR_PROBE_ANALOG: - analog_probe_count++; - break; - } - } // Read out the sample rate - assert(sdi->driver); - - const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, - &gvar, sdi); - if (ret != SR_OK) { - qDebug("Failed to get samplerate\n"); - return; - } - - sample_rate = g_variant_get_uint64(gvar); - g_variant_unref(gvar); - - // Create data containers for the coming data snapshots + if(sdi->driver) { - lock_guard data_lock(_data_mutex); - - if (logic_probe_count != 0) { - _logic_data.reset(new data::Logic( - logic_probe_count, sample_rate)); - assert(_logic_data); + const int ret = sr_config_get(sdi->driver, + SR_CONF_SAMPLERATE, &gvar, sdi); + if (ret != SR_OK) { + qDebug("Failed to get samplerate\n"); + return; } - if (analog_probe_count != 0) { - _analog_data.reset(new data::Analog(sample_rate)); - assert(_analog_data); - } + sample_rate = g_variant_get_uint64(gvar); + g_variant_unref(gvar); } - // Make the logic probe list - { - lock_guard lock(_signals_mutex); - - _signals.clear(); - - for (const GSList *l = sdi->probes; l; l = l->next) { - const sr_probe *const probe = - (const sr_probe *)l->data; - assert(probe); - if (!probe->enabled) - continue; - - switch(probe->type) { - case SR_PROBE_LOGIC: - signal = shared_ptr( - new view::LogicSignal(probe->name, - _logic_data, probe->index)); - break; - - case SR_PROBE_ANALOG: - signal = shared_ptr( - new view::AnalogSignal(probe->name, - _analog_data, probe->index)); - break; - } - - _signals.push_back(signal); - } - - signals_changed(); - } + if(_analog_data) + _analog_data->set_samplerate(sample_rate); + if(_logic_data) + _logic_data->set_samplerate(sample_rate); } void SigSession::feed_in_meta(const sr_dev_inst *sdi,