X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=pv%2Fsigsession.cpp;h=5cc999829f7bd1748aeb07b2732eed883f5d438c;hb=ae2d1bc5b5aba9fcdd7fef42ef1bc9069267d6f7;hp=4469a3eef367071c44a545d3c3a1a8ae746742cc;hpb=921b90c0b3ae0cf44247da3d87bd7dc0612e9681;p=pulseview.git diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index 4469a3e..5cc9998 100644 --- a/pv/sigsession.cpp +++ b/pv/sigsession.cpp @@ -26,6 +26,7 @@ #include "devicemanager.h" #include "device/device.h" +#include "device/file.h" #include "data/analog.h" #include "data/analogsnapshot.h" @@ -73,13 +74,14 @@ SigSession::SigSession(DeviceManager &device_manager) : SigSession::~SigSession() { + using pv::device::Device; + stop_capture(); if (_sampling_thread.joinable()) _sampling_thread.join(); - if (_dev_inst) - _device_manager.release_device(_dev_inst); + _dev_inst->release(); // TODO: This should not be necessary _session = NULL; @@ -90,70 +92,44 @@ shared_ptr SigSession::get_device() const return _dev_inst; } -void SigSession::set_device(shared_ptr dev_inst) +void SigSession::set_device( + shared_ptr dev_inst) throw(QString) { + using pv::device::Device; + // Ensure we are not capturing before setting the device stop_capture(); - if (_dev_inst) - _device_manager.release_device(_dev_inst); - if (dev_inst) - _device_manager.use_device(dev_inst, this); + if (_dev_inst) { + sr_session_datafeed_callback_remove_all(); + _dev_inst->release(); + } + _dev_inst = dev_inst; - update_signals(dev_inst); + _decode_traces.clear(); + + if (dev_inst) { + dev_inst->use(this); + sr_session_datafeed_callback_add(data_feed_in_proc, NULL); + update_signals(dev_inst); + } } -void SigSession::release_device(shared_ptr dev_inst) +void SigSession::set_file(const string &name) throw(QString) { - (void)dev_inst; - - assert(_capture_state == Stopped); - _dev_inst = shared_ptr(); + // Deslect the old device, because file type detection in File::create + // destorys the old session inside libsigrok. + set_device(shared_ptr()); + set_device(shared_ptr(device::File::create(name))); } -void SigSession::load_file(const string &name, - function error_handler) +void SigSession::release_device(device::DevInst *dev_inst) { - stop_capture(); - - if (sr_session_load(name.c_str()) == SR_OK) { - GSList *devlist = NULL; - sr_session_dev_list(&devlist); - - if (!devlist || !devlist->data || - sr_session_start() != SR_OK) { - error_handler(tr("Failed to start session.")); - return; - } - - shared_ptr dev_inst( - new device::Device((sr_dev_inst*)devlist->data)); - g_slist_free(devlist); - - _decode_traces.clear(); - update_signals(dev_inst); - read_sample_rate(dev_inst->dev_inst()); - - _sampling_thread = boost::thread( - &SigSession::load_session_thread_proc, this, - error_handler); - - } else { - sr_input *in = NULL; - - if (!(in = load_input_file_format(name.c_str(), - error_handler))) - return; - - _decode_traces.clear(); - update_signals(shared_ptr( - new device::Device(in->sdi))); - read_sample_rate(in->sdi); + (void)dev_inst; + assert(_dev_inst.get() == dev_inst); - _sampling_thread = boost::thread( - &SigSession::load_input_thread_proc, this, - name, in, error_handler); - } + assert(_capture_state == Stopped); + _dev_inst = shared_ptr(); } SigSession::capture_state SigSession::get_capture_state() const @@ -311,82 +287,6 @@ 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(shared_ptr dev_inst) { assert(dev_inst); @@ -473,20 +373,6 @@ void SigSession::update_signals(shared_ptr dev_inst) signals_changed(); } -bool SigSession::is_trigger_enabled() const -{ - assert(_dev_inst); - assert(_dev_inst->dev_inst()); - for (const GSList *l = _dev_inst->dev_inst()->probes; l; l = l->next) { - const sr_probe *const p = (const sr_probe *)l->data; - assert(p); - if (p->trigger && p->trigger[0] != '\0') - return true; - } - - return false; -} - shared_ptr SigSession::signal_from_probe( const sr_probe *probe) const { @@ -526,49 +412,6 @@ void SigSession::read_sample_rate(const sr_dev_inst *const sdi) } } -void SigSession::load_session_thread_proc( - function error_handler) -{ - (void)error_handler; - - sr_session_datafeed_callback_add(data_feed_in_proc, NULL); - - set_capture_state(Running); - - sr_session_run(); - - sr_session_destroy(); - set_capture_state(Stopped); - - // Confirm that SR_DF_END was received - assert(!_cur_logic_snapshot); - assert(_cur_analog_snapshots.empty()); -} - -void SigSession::load_input_thread_proc(const string name, - sr_input *in, function error_handler) -{ - (void)error_handler; - - assert(in); - assert(in->format); - - sr_session_datafeed_callback_add(data_feed_in_proc, NULL); - - set_capture_state(Running); - - in->format->loadfile(in, name.c_str()); - - sr_session_destroy(); - set_capture_state(Stopped); - - // Confirm that SR_DF_END was received - assert(!_cur_logic_snapshot); - assert(_cur_analog_snapshots.empty()); - - delete in; -} - void SigSession::sample_thread_proc(shared_ptr dev_inst, function error_handler) { @@ -576,25 +419,19 @@ void SigSession::sample_thread_proc(shared_ptr dev_inst, assert(dev_inst->dev_inst()); assert(error_handler); - sr_session_new(); - sr_session_datafeed_callback_add(data_feed_in_proc, NULL); + read_sample_rate(dev_inst->dev_inst()); - if (sr_session_dev_add(dev_inst->dev_inst()) != SR_OK) { - error_handler(tr("Failed to use device.")); - sr_session_destroy(); + try { + dev_inst->start(); + } catch(const QString e) { + error_handler(e); return; } - if (sr_session_start() != SR_OK) { - error_handler(tr("Failed to start session.")); - return; - } - - set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running); - - sr_session_run(); - sr_session_destroy(); + set_capture_state(dev_inst->is_trigger_enabled() ? + AwaitingTrigger : Running); + dev_inst->run(); set_capture_state(Stopped); // Confirm that SR_DF_END was received