2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libsigrokdecode/libsigrokdecode.h>
25 #include "sigsession.h"
27 #include "devicemanager.h"
28 #include "device/device.h"
29 #include "device/file.h"
31 #include "data/analog.h"
32 #include "data/analogsnapshot.h"
33 #include "data/decoderstack.h"
34 #include "data/logic.h"
35 #include "data/logicsnapshot.h"
36 #include "data/decode/decoder.h"
38 #include "view/analogsignal.h"
39 #include "view/decodetrace.h"
40 #include "view/logicsignal.h"
46 #include <boost/foreach.hpp>
52 using boost::dynamic_pointer_cast;
53 using boost::function;
54 using boost::lock_guard;
56 using boost::shared_ptr;
64 // TODO: This should not be necessary
65 SigSession* SigSession::_session = NULL;
67 SigSession::SigSession(DeviceManager &device_manager) :
68 _device_manager(device_manager),
69 _capture_state(Stopped)
71 // TODO: This should not be necessary
75 SigSession::~SigSession()
77 using pv::device::Device;
81 if (_sampling_thread.joinable())
82 _sampling_thread.join();
86 // TODO: This should not be necessary
90 shared_ptr<device::DevInst> SigSession::get_device() const
95 void SigSession::set_device(
96 shared_ptr<device::DevInst> dev_inst) throw(QString)
98 using pv::device::Device;
100 // Ensure we are not capturing before setting the device
104 sr_session_datafeed_callback_remove_all();
105 _dev_inst->release();
108 _dev_inst = dev_inst;
109 _decode_traces.clear();
113 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
114 update_signals(dev_inst);
118 void SigSession::set_file(const string &name) throw(QString)
120 // Deslect the old device, because file type detection in File::create
121 // destorys the old session inside libsigrok.
122 set_device(shared_ptr<device::DevInst>());
123 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
126 void SigSession::release_device(device::DevInst *dev_inst)
129 assert(_dev_inst.get() == dev_inst);
131 assert(_capture_state == Stopped);
132 _dev_inst = shared_ptr<device::DevInst>();
135 SigSession::capture_state SigSession::get_capture_state() const
137 lock_guard<mutex> lock(_sampling_mutex);
138 return _capture_state;
141 void SigSession::start_capture(function<void (const QString)> error_handler)
145 // Check that a device instance has been selected.
147 qDebug() << "No device selected";
151 assert(_dev_inst->dev_inst());
153 // Check that at least one probe is enabled
155 for (l = _dev_inst->dev_inst()->probes; l; l = l->next) {
156 sr_probe *const probe = (sr_probe*)l->data;
163 error_handler(tr("No probes enabled."));
168 _sampling_thread = boost::thread(
169 &SigSession::sample_thread_proc, this, _dev_inst,
173 void SigSession::stop_capture()
175 if (get_capture_state() == Stopped)
180 // Check that sampling stopped
181 if (_sampling_thread.joinable())
182 _sampling_thread.join();
185 set< shared_ptr<data::SignalData> > SigSession::get_data() const
187 lock_guard<mutex> lock(_signals_mutex);
188 set< shared_ptr<data::SignalData> > data;
189 BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) {
191 data.insert(sig->data());
197 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
199 lock_guard<mutex> lock(_signals_mutex);
204 bool SigSession::add_decoder(srd_decoder *const dec)
206 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
207 shared_ptr<data::DecoderStack> decoder_stack;
211 lock_guard<mutex> lock(_signals_mutex);
213 // Create the decoder
214 decoder_stack = shared_ptr<data::DecoderStack>(
215 new data::DecoderStack(dec));
217 // Make a list of all the probes
218 std::vector<const srd_probe*> all_probes;
219 for(const GSList *i = dec->probes; i; i = i->next)
220 all_probes.push_back((const srd_probe*)i->data);
221 for(const GSList *i = dec->opt_probes; i; i = i->next)
222 all_probes.push_back((const srd_probe*)i->data);
224 // Auto select the initial probes
225 BOOST_FOREACH(const srd_probe *probe, all_probes)
226 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
228 shared_ptr<view::LogicSignal> l =
229 dynamic_pointer_cast<view::LogicSignal>(s);
230 if (l && QString::fromUtf8(probe->name).
232 l->get_name().toLower()))
236 assert(decoder_stack);
237 assert(!decoder_stack->stack().empty());
238 assert(decoder_stack->stack().front());
239 decoder_stack->stack().front()->set_probes(probes);
241 // Create the decode signal
242 shared_ptr<view::DecodeTrace> d(
243 new view::DecodeTrace(*this, decoder_stack,
244 _decode_traces.size()));
245 _decode_traces.push_back(d);
247 catch(std::runtime_error e)
254 // Do an initial decode
255 decoder_stack->begin_decode();
260 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
262 lock_guard<mutex> lock(_signals_mutex);
263 return _decode_traces;
266 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
268 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
269 _decode_traces.begin();
270 i != _decode_traces.end();
272 if ((*i).get() == signal)
274 _decode_traces.erase(i);
281 void SigSession::set_capture_state(capture_state state)
283 lock_guard<mutex> lock(_sampling_mutex);
284 const bool changed = _capture_state != state;
285 _capture_state = state;
287 capture_state_changed(state);
290 void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
293 assert(_capture_state == Stopped);
295 unsigned int logic_probe_count = 0;
297 // Clear the decode traces
298 _decode_traces.clear();
300 // Detect what data types we will receive
302 assert(dev_inst->dev_inst());
303 for (const GSList *l = dev_inst->dev_inst()->probes;
305 const sr_probe *const probe = (const sr_probe *)l->data;
309 switch(probe->type) {
317 // Create data containers for the logic data snapshots
319 lock_guard<mutex> data_lock(_data_mutex);
322 if (logic_probe_count != 0) {
323 _logic_data.reset(new data::Logic(
329 // Make the Signals list
331 lock_guard<mutex> lock(_signals_mutex);
338 assert(dev_inst->dev_inst());
339 for (const GSList *l = dev_inst->dev_inst()->probes;
341 shared_ptr<view::Signal> signal;
342 sr_probe *const probe = (sr_probe *)l->data;
345 switch(probe->type) {
347 signal = shared_ptr<view::Signal>(
348 new view::LogicSignal(dev_inst,
349 probe, _logic_data));
352 case SR_PROBE_ANALOG:
354 shared_ptr<data::Analog> data(
356 signal = shared_ptr<view::Signal>(
357 new view::AnalogSignal(dev_inst,
368 _signals.push_back(signal);
376 shared_ptr<view::Signal> SigSession::signal_from_probe(
377 const sr_probe *probe) const
379 lock_guard<mutex> lock(_signals_mutex);
380 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
382 if (sig->probe() == probe)
385 return shared_ptr<view::Signal>();
388 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
391 uint64_t sample_rate = 0;
393 // Read out the sample rate
396 const int ret = sr_config_get(sdi->driver, sdi, NULL,
397 SR_CONF_SAMPLERATE, &gvar);
399 qDebug("Failed to get samplerate\n");
403 sample_rate = g_variant_get_uint64(gvar);
404 g_variant_unref(gvar);
407 // Set the sample rate of all data
408 const set< shared_ptr<data::SignalData> > data_set = get_data();
409 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
411 data->set_samplerate(sample_rate);
415 void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
416 function<void (const QString)> error_handler)
419 assert(dev_inst->dev_inst());
420 assert(error_handler);
422 read_sample_rate(dev_inst->dev_inst());
426 } catch(const QString e) {
431 set_capture_state(dev_inst->is_trigger_enabled() ?
432 AwaitingTrigger : Running);
435 set_capture_state(Stopped);
437 // Confirm that SR_DF_END was received
438 if (_cur_logic_snapshot)
440 qDebug("SR_DF_END was not received.");
445 void SigSession::feed_in_header(const sr_dev_inst *sdi)
447 read_sample_rate(sdi);
450 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
451 const sr_datafeed_meta &meta)
455 for (const GSList *l = meta.config; l; l = l->next) {
456 const sr_config *const src = (const sr_config*)l->data;
458 case SR_CONF_SAMPLERATE:
459 /// @todo handle samplerate changes
460 /// samplerate = (uint64_t *)src->value;
463 // Unknown metadata is not an error.
471 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
473 lock_guard<mutex> lock(_data_mutex);
477 qDebug() << "Unexpected logic packet";
481 if (!_cur_logic_snapshot)
483 // This could be the first packet after a trigger
484 set_capture_state(Running);
486 // Create a new data snapshot
487 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
488 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
489 _logic_data->push_snapshot(_cur_logic_snapshot);
493 // Append to the existing data snapshot
494 _cur_logic_snapshot->append_payload(logic);
500 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
502 lock_guard<mutex> lock(_data_mutex);
504 const unsigned int probe_count = g_slist_length(analog.probes);
505 const size_t sample_count = analog.num_samples / probe_count;
506 const float *data = analog.data;
507 bool sweep_beginning = false;
509 for (GSList *p = analog.probes; p; p = p->next)
511 shared_ptr<data::AnalogSnapshot> snapshot;
513 sr_probe *const probe = (sr_probe*)p->data;
516 // Try to get the snapshot of the probe
517 const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >::
518 iterator iter = _cur_analog_snapshots.find(probe);
519 if (iter != _cur_analog_snapshots.end())
520 snapshot = (*iter).second;
523 // If no snapshot was found, this means we havn't
524 // created one yet. i.e. this is the first packet
525 // in the sweep containing this snapshot.
526 sweep_beginning = true;
528 // Create a snapshot, keep it in the maps of probes
529 snapshot = shared_ptr<data::AnalogSnapshot>(
530 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
531 _cur_analog_snapshots[probe] = snapshot;
533 // Find the annalog data associated with the probe
534 shared_ptr<view::AnalogSignal> sig =
535 dynamic_pointer_cast<view::AnalogSignal>(
536 signal_from_probe(probe));
539 shared_ptr<data::Analog> data(sig->analog_data());
542 // Push the snapshot into the analog data.
543 data->push_snapshot(snapshot);
548 // Append the samples in the snapshot
549 snapshot->append_interleaved_samples(data++, sample_count,
553 if (sweep_beginning) {
554 // This could be the first packet after a trigger
555 set_capture_state(Running);
561 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
562 const struct sr_datafeed_packet *packet)
567 switch (packet->type) {
573 assert(packet->payload);
575 *(const sr_datafeed_meta*)packet->payload);
579 assert(packet->payload);
580 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
584 assert(packet->payload);
585 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
591 lock_guard<mutex> lock(_data_mutex);
592 _cur_logic_snapshot.reset();
593 _cur_analog_snapshots.clear();
601 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
602 const struct sr_datafeed_packet *packet, void *cb_data)
606 _session->data_feed_in(sdi, packet);