X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=pv%2Fsigsession.cpp;h=5cc999829f7bd1748aeb07b2732eed883f5d438c;hb=ae2d1bc5b5aba9fcdd7fef42ef1bc9069267d6f7;hp=9af7df74e7a5f967023a20a62c4f6e68ed3d8c16;hpb=07dcf5615620315f4eda91a930b8540e64b625f2;p=pulseview.git diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index 9af7df7..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" @@ -91,21 +92,35 @@ 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) + if (_dev_inst) { + sr_session_datafeed_callback_remove_all(); _dev_inst->release(); + } + + _dev_inst = dev_inst; + _decode_traces.clear(); - if (dev_inst) + if (dev_inst) { dev_inst->use(this); + sr_session_datafeed_callback_add(data_feed_in_proc, NULL); + update_signals(dev_inst); + } +} - _dev_inst = dev_inst; - update_signals(dev_inst); +void SigSession::set_file(const string &name) throw(QString) +{ + // 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::release_device(device::DevInst *dev_inst) @@ -117,51 +132,6 @@ void SigSession::release_device(device::DevInst *dev_inst) _dev_inst = shared_ptr(); } -void SigSession::load_file(const string &name, - function error_handler) -{ - 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); - - _sampling_thread = boost::thread( - &SigSession::load_input_thread_proc, this, - name, in, error_handler); - } -} - SigSession::capture_state SigSession::get_capture_state() const { lock_guard lock(_sampling_mutex); @@ -317,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); @@ -518,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) { @@ -568,26 +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(); - return; - } - - if (sr_session_start() != SR_OK) { - error_handler(tr("Failed to start session.")); + try { + dev_inst->start(); + } catch(const QString e) { + error_handler(e); return; } set_capture_state(dev_inst->is_trigger_enabled() ? AwaitingTrigger : Running); - sr_session_run(); - sr_session_destroy(); - + dev_inst->run(); set_capture_state(Stopped); // Confirm that SR_DF_END was received