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"
50 using std::dynamic_pointer_cast;
52 using std::lock_guard;
57 using std::shared_ptr;
63 // TODO: This should not be necessary
64 SigSession* SigSession::_session = NULL;
66 SigSession::SigSession(DeviceManager &device_manager) :
67 _device_manager(device_manager),
68 _capture_state(Stopped)
70 // TODO: This should not be necessary
76 SigSession::~SigSession()
78 using pv::device::Device;
80 // Stop and join to the thread
85 // TODO: This should not be necessary
89 shared_ptr<device::DevInst> SigSession::get_device() const
94 void SigSession::set_device(
95 shared_ptr<device::DevInst> dev_inst) throw(QString)
97 using pv::device::Device;
99 // Ensure we are not capturing before setting the device
103 sr_session_datafeed_callback_remove_all();
104 _dev_inst->release();
107 _dev_inst = dev_inst;
108 _decode_traces.clear();
112 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
113 update_signals(dev_inst);
117 void SigSession::set_file(const string &name) throw(QString)
119 // Deselect the old device, because file type detection in File::create
120 // destroys the old session inside libsigrok.
121 set_device(shared_ptr<device::DevInst>());
122 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
125 void SigSession::set_default_device()
127 shared_ptr<pv::device::DevInst> default_device;
128 const list< shared_ptr<device::Device> > &devices =
129 _device_manager.devices();
131 if (!devices.empty()) {
132 // Fall back to the first device in the list.
133 default_device = devices.front();
135 // Try and find the demo device and select that by default
136 for (shared_ptr<pv::device::Device> dev : devices)
137 if (strcmp(dev->dev_inst()->driver->name,
139 default_device = dev;
144 set_device(default_device);
147 void SigSession::release_device(device::DevInst *dev_inst)
150 assert(_dev_inst.get() == dev_inst);
152 assert(_capture_state == Stopped);
153 _dev_inst = shared_ptr<device::DevInst>();
156 SigSession::capture_state SigSession::get_capture_state() const
158 lock_guard<mutex> lock(_sampling_mutex);
159 return _capture_state;
162 void SigSession::start_capture(function<void (const QString)> error_handler)
166 // Check that a device instance has been selected.
168 qDebug() << "No device selected";
172 assert(_dev_inst->dev_inst());
174 // Check that at least one probe is enabled
176 for (l = _dev_inst->dev_inst()->channels; l; l = l->next) {
177 sr_channel *const probe = (sr_channel*)l->data;
184 error_handler(tr("No channels enabled."));
189 _sampling_thread = std::thread(
190 &SigSession::sample_thread_proc, this, _dev_inst,
194 void SigSession::stop_capture()
196 if (get_capture_state() != Stopped)
199 // Check that sampling stopped
200 if (_sampling_thread.joinable())
201 _sampling_thread.join();
204 set< shared_ptr<data::SignalData> > SigSession::get_data() const
206 lock_guard<mutex> lock(_signals_mutex);
207 set< shared_ptr<data::SignalData> > data;
208 for (const shared_ptr<view::Signal> sig : _signals) {
210 data.insert(sig->data());
216 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
218 lock_guard<mutex> lock(_signals_mutex);
223 bool SigSession::add_decoder(srd_decoder *const dec)
225 map<const srd_channel*, shared_ptr<view::LogicSignal> > probes;
226 shared_ptr<data::DecoderStack> decoder_stack;
230 lock_guard<mutex> lock(_signals_mutex);
232 // Create the decoder
233 decoder_stack = shared_ptr<data::DecoderStack>(
234 new data::DecoderStack(*this, dec));
236 // Make a list of all the probes
237 std::vector<const srd_channel*> all_probes;
238 for(const GSList *i = dec->channels; i; i = i->next)
239 all_probes.push_back((const srd_channel*)i->data);
240 for(const GSList *i = dec->opt_channels; i; i = i->next)
241 all_probes.push_back((const srd_channel*)i->data);
243 // Auto select the initial probes
244 for (const srd_channel *pdch : all_probes)
245 for (shared_ptr<view::Signal> s : _signals)
247 shared_ptr<view::LogicSignal> l =
248 dynamic_pointer_cast<view::LogicSignal>(s);
249 if (l && QString::fromUtf8(pdch->name).
251 l->get_name().toLower()))
255 assert(decoder_stack);
256 assert(!decoder_stack->stack().empty());
257 assert(decoder_stack->stack().front());
258 decoder_stack->stack().front()->set_probes(probes);
260 // Create the decode signal
261 shared_ptr<view::DecodeTrace> d(
262 new view::DecodeTrace(*this, decoder_stack,
263 _decode_traces.size()));
264 _decode_traces.push_back(d);
266 catch(std::runtime_error e)
273 // Do an initial decode
274 decoder_stack->begin_decode();
279 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
281 lock_guard<mutex> lock(_signals_mutex);
282 return _decode_traces;
285 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
287 for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++)
288 if ((*i).get() == signal)
290 _decode_traces.erase(i);
297 void SigSession::set_capture_state(capture_state state)
299 lock_guard<mutex> lock(_sampling_mutex);
300 const bool changed = _capture_state != state;
301 _capture_state = state;
303 capture_state_changed(state);
306 void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
309 assert(_capture_state == Stopped);
311 unsigned int logic_probe_count = 0;
313 // Clear the decode traces
314 _decode_traces.clear();
316 // Detect what data types we will receive
318 assert(dev_inst->dev_inst());
319 for (const GSList *l = dev_inst->dev_inst()->channels;
321 const sr_channel *const probe = (const sr_channel *)l->data;
325 switch(probe->type) {
326 case SR_CHANNEL_LOGIC:
333 // Create data containers for the logic data snapshots
335 lock_guard<mutex> data_lock(_data_mutex);
338 if (logic_probe_count != 0) {
339 _logic_data.reset(new data::Logic(
345 // Make the Signals list
347 lock_guard<mutex> lock(_signals_mutex);
354 assert(dev_inst->dev_inst());
355 for (const GSList *l = dev_inst->dev_inst()->channels;
357 shared_ptr<view::Signal> signal;
358 sr_channel *const probe = (sr_channel *)l->data;
361 switch(probe->type) {
362 case SR_CHANNEL_LOGIC:
363 signal = shared_ptr<view::Signal>(
364 new view::LogicSignal(dev_inst,
365 probe, _logic_data));
368 case SR_CHANNEL_ANALOG:
370 shared_ptr<data::Analog> data(
372 signal = shared_ptr<view::Signal>(
373 new view::AnalogSignal(dev_inst,
384 _signals.push_back(signal);
392 shared_ptr<view::Signal> SigSession::signal_from_probe(
393 const sr_channel *probe) const
395 lock_guard<mutex> lock(_signals_mutex);
396 for (shared_ptr<view::Signal> sig : _signals) {
398 if (sig->probe() == probe)
401 return shared_ptr<view::Signal>();
404 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
407 uint64_t sample_rate = 0;
409 // Read out the sample rate
412 const int ret = sr_config_get(sdi->driver, sdi, NULL,
413 SR_CONF_SAMPLERATE, &gvar);
415 qDebug("Failed to get samplerate\n");
419 sample_rate = g_variant_get_uint64(gvar);
420 g_variant_unref(gvar);
423 // Set the sample rate of all data
424 const set< shared_ptr<data::SignalData> > data_set = get_data();
425 for (shared_ptr<data::SignalData> data : data_set) {
427 data->set_samplerate(sample_rate);
431 void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
432 function<void (const QString)> error_handler)
435 assert(dev_inst->dev_inst());
436 assert(error_handler);
438 read_sample_rate(dev_inst->dev_inst());
442 } catch(const QString e) {
447 set_capture_state(sr_session_trigger_get() ?
448 AwaitingTrigger : Running);
451 set_capture_state(Stopped);
453 // Confirm that SR_DF_END was received
454 if (_cur_logic_snapshot)
456 qDebug("SR_DF_END was not received.");
461 void SigSession::feed_in_header(const sr_dev_inst *sdi)
463 read_sample_rate(sdi);
466 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
467 const sr_datafeed_meta &meta)
471 for (const GSList *l = meta.config; l; l = l->next) {
472 const sr_config *const src = (const sr_config*)l->data;
474 case SR_CONF_SAMPLERATE:
475 /// @todo handle samplerate changes
476 /// samplerate = (uint64_t *)src->value;
479 // Unknown metadata is not an error.
487 void SigSession::feed_in_frame_begin()
489 if (_cur_logic_snapshot || !_cur_analog_snapshots.empty())
493 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
495 lock_guard<mutex> lock(_data_mutex);
499 qDebug() << "Unexpected logic packet";
503 if (!_cur_logic_snapshot)
505 // This could be the first packet after a trigger
506 set_capture_state(Running);
508 // Create a new data snapshot
509 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
510 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
511 _logic_data->push_snapshot(_cur_logic_snapshot);
513 // @todo Putting this here means that only listeners querying
514 // for logic will be notified. Currently the only user of
515 // frame_began is DecoderStack, but in future we need to signal
516 // this after both analog and logic sweeps have begun.
521 // Append to the existing data snapshot
522 _cur_logic_snapshot->append_payload(logic);
528 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
530 lock_guard<mutex> lock(_data_mutex);
532 const unsigned int probe_count = g_slist_length(analog.channels);
533 const size_t sample_count = analog.num_samples / probe_count;
534 const float *data = analog.data;
535 bool sweep_beginning = false;
537 for (GSList *p = analog.channels; p; p = p->next)
539 shared_ptr<data::AnalogSnapshot> snapshot;
541 sr_channel *const probe = (sr_channel*)p->data;
544 // Try to get the snapshot of the probe
545 const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >::
546 iterator iter = _cur_analog_snapshots.find(probe);
547 if (iter != _cur_analog_snapshots.end())
548 snapshot = (*iter).second;
551 // If no snapshot was found, this means we havn't
552 // created one yet. i.e. this is the first packet
553 // in the sweep containing this snapshot.
554 sweep_beginning = true;
556 // Create a snapshot, keep it in the maps of probes
557 snapshot = shared_ptr<data::AnalogSnapshot>(
558 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
559 _cur_analog_snapshots[probe] = snapshot;
561 // Find the annalog data associated with the probe
562 shared_ptr<view::AnalogSignal> sig =
563 dynamic_pointer_cast<view::AnalogSignal>(
564 signal_from_probe(probe));
567 shared_ptr<data::Analog> data(sig->analog_data());
570 // Push the snapshot into the analog data.
571 data->push_snapshot(snapshot);
576 // Append the samples in the snapshot
577 snapshot->append_interleaved_samples(data++, sample_count,
581 if (sweep_beginning) {
582 // This could be the first packet after a trigger
583 set_capture_state(Running);
589 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
590 const struct sr_datafeed_packet *packet)
595 switch (packet->type) {
601 assert(packet->payload);
603 *(const sr_datafeed_meta*)packet->payload);
606 case SR_DF_FRAME_BEGIN:
607 feed_in_frame_begin();
611 assert(packet->payload);
612 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
616 assert(packet->payload);
617 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
623 lock_guard<mutex> lock(_data_mutex);
624 _cur_logic_snapshot.reset();
625 _cur_analog_snapshots.clear();
633 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
634 const struct sr_datafeed_packet *packet, void *cb_data)
638 _session->data_feed_in(sdi, packet);