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 // TODO: This should not be necessary
67 struct sr_session *SigSession::_sr_session = NULL;
69 SigSession::SigSession(DeviceManager &device_manager) :
70 _device_manager(device_manager),
71 _capture_state(Stopped)
73 // TODO: This should not be necessary
79 SigSession::~SigSession()
81 using pv::device::Device;
83 // Stop and join to the thread
89 // TODO: This should not be necessary
93 shared_ptr<device::DevInst> SigSession::get_device() const
98 void SigSession::set_device(
99 shared_ptr<device::DevInst> dev_inst) throw(QString)
101 using pv::device::Device;
103 // Ensure we are not capturing before setting the device
107 sr_session_datafeed_callback_remove_all(_sr_session);
108 _dev_inst->release();
111 _dev_inst = dev_inst;
112 _decode_traces.clear();
116 sr_session_datafeed_callback_add(_sr_session, data_feed_in_proc, NULL);
117 update_signals(dev_inst);
121 void SigSession::set_file(const string &name) throw(QString)
123 // Deselect the old device, because file type detection in File::create
124 // destroys the old session inside libsigrok.
125 set_device(shared_ptr<device::DevInst>());
126 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
129 void SigSession::set_default_device()
131 shared_ptr<pv::device::DevInst> default_device;
132 const list< shared_ptr<device::Device> > &devices =
133 _device_manager.devices();
135 if (!devices.empty()) {
136 // Fall back to the first device in the list.
137 default_device = devices.front();
139 // Try and find the demo device and select that by default
140 for (shared_ptr<pv::device::Device> dev : devices)
141 if (strcmp(dev->dev_inst()->driver->name,
143 default_device = dev;
148 set_device(default_device);
151 void SigSession::release_device(device::DevInst *dev_inst)
154 assert(_dev_inst.get() == dev_inst);
156 assert(_capture_state == Stopped);
157 _dev_inst = shared_ptr<device::DevInst>();
160 SigSession::capture_state SigSession::get_capture_state() const
162 lock_guard<mutex> lock(_sampling_mutex);
163 return _capture_state;
166 void SigSession::start_capture(function<void (const QString)> error_handler)
170 // Check that a device instance has been selected.
172 qDebug() << "No device selected";
176 assert(_dev_inst->dev_inst());
178 // Check that at least one probe is enabled
180 for (l = _dev_inst->dev_inst()->channels; l; l = l->next) {
181 sr_channel *const probe = (sr_channel*)l->data;
188 error_handler(tr("No channels enabled."));
193 _sampling_thread = std::thread(
194 &SigSession::sample_thread_proc, this, _dev_inst,
198 void SigSession::stop_capture()
200 if (get_capture_state() != Stopped)
201 sr_session_stop(_sr_session);
203 // Check that sampling stopped
204 if (_sampling_thread.joinable())
205 _sampling_thread.join();
208 set< shared_ptr<data::SignalData> > SigSession::get_data() const
210 lock_guard<mutex> lock(_signals_mutex);
211 set< shared_ptr<data::SignalData> > data;
212 for (const shared_ptr<view::Signal> sig : _signals) {
214 data.insert(sig->data());
220 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
222 lock_guard<mutex> lock(_signals_mutex);
227 bool SigSession::add_decoder(srd_decoder *const dec)
229 map<const srd_channel*, shared_ptr<view::LogicSignal> > probes;
230 shared_ptr<data::DecoderStack> decoder_stack;
234 lock_guard<mutex> lock(_signals_mutex);
236 // Create the decoder
237 decoder_stack = shared_ptr<data::DecoderStack>(
238 new data::DecoderStack(*this, dec));
240 // Make a list of all the probes
241 std::vector<const srd_channel*> all_probes;
242 for(const GSList *i = dec->channels; i; i = i->next)
243 all_probes.push_back((const srd_channel*)i->data);
244 for(const GSList *i = dec->opt_channels; i; i = i->next)
245 all_probes.push_back((const srd_channel*)i->data);
247 // Auto select the initial probes
248 for (const srd_channel *pdch : all_probes)
249 for (shared_ptr<view::Signal> s : _signals)
251 shared_ptr<view::LogicSignal> l =
252 dynamic_pointer_cast<view::LogicSignal>(s);
253 if (l && QString::fromUtf8(pdch->name).
255 l->get_name().toLower()))
259 assert(decoder_stack);
260 assert(!decoder_stack->stack().empty());
261 assert(decoder_stack->stack().front());
262 decoder_stack->stack().front()->set_probes(probes);
264 // Create the decode signal
265 shared_ptr<view::DecodeTrace> d(
266 new view::DecodeTrace(*this, decoder_stack,
267 _decode_traces.size()));
268 _decode_traces.push_back(d);
270 catch(std::runtime_error e)
277 // Do an initial decode
278 decoder_stack->begin_decode();
283 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
285 lock_guard<mutex> lock(_signals_mutex);
286 return _decode_traces;
289 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
291 for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++)
292 if ((*i).get() == signal)
294 _decode_traces.erase(i);
301 void SigSession::set_capture_state(capture_state state)
303 lock_guard<mutex> lock(_sampling_mutex);
304 const bool changed = _capture_state != state;
305 _capture_state = state;
307 capture_state_changed(state);
310 void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
313 assert(_capture_state == Stopped);
315 unsigned int logic_probe_count = 0;
317 // Clear the decode traces
318 _decode_traces.clear();
320 // Detect what data types we will receive
322 assert(dev_inst->dev_inst());
323 for (const GSList *l = dev_inst->dev_inst()->channels;
325 const sr_channel *const probe = (const sr_channel *)l->data;
329 switch(probe->type) {
330 case SR_CHANNEL_LOGIC:
337 // Create data containers for the logic data snapshots
339 lock_guard<mutex> data_lock(_data_mutex);
342 if (logic_probe_count != 0) {
343 _logic_data.reset(new data::Logic(
349 // Make the Signals list
351 lock_guard<mutex> lock(_signals_mutex);
358 assert(dev_inst->dev_inst());
359 for (const GSList *l = dev_inst->dev_inst()->channels;
361 shared_ptr<view::Signal> signal;
362 sr_channel *const probe = (sr_channel *)l->data;
365 switch(probe->type) {
366 case SR_CHANNEL_LOGIC:
367 signal = shared_ptr<view::Signal>(
368 new view::LogicSignal(dev_inst,
369 probe, _logic_data));
372 case SR_CHANNEL_ANALOG:
374 shared_ptr<data::Analog> data(
376 signal = shared_ptr<view::Signal>(
377 new view::AnalogSignal(dev_inst,
388 _signals.push_back(signal);
396 shared_ptr<view::Signal> SigSession::signal_from_probe(
397 const sr_channel *probe) const
399 lock_guard<mutex> lock(_signals_mutex);
400 for (shared_ptr<view::Signal> sig : _signals) {
402 if (sig->probe() == probe)
405 return shared_ptr<view::Signal>();
408 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
411 uint64_t sample_rate = 0;
413 // Read out the sample rate
416 const int ret = sr_config_get(sdi->driver, sdi, NULL,
417 SR_CONF_SAMPLERATE, &gvar);
419 qDebug("Failed to get samplerate\n");
423 sample_rate = g_variant_get_uint64(gvar);
424 g_variant_unref(gvar);
427 // Set the sample rate of all data
428 const set< shared_ptr<data::SignalData> > data_set = get_data();
429 for (shared_ptr<data::SignalData> data : data_set) {
431 data->set_samplerate(sample_rate);
435 void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
436 function<void (const QString)> error_handler)
439 assert(dev_inst->dev_inst());
440 assert(error_handler);
442 read_sample_rate(dev_inst->dev_inst());
446 } catch(const QString e) {
451 set_capture_state(sr_session_trigger_get(_sr_session) ?
452 AwaitingTrigger : Running);
455 set_capture_state(Stopped);
457 // Confirm that SR_DF_END was received
458 if (_cur_logic_snapshot)
460 qDebug("SR_DF_END was not received.");
465 void SigSession::feed_in_header(const sr_dev_inst *sdi)
467 read_sample_rate(sdi);
470 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
471 const sr_datafeed_meta &meta)
475 for (const GSList *l = meta.config; l; l = l->next) {
476 const sr_config *const src = (const sr_config*)l->data;
478 case SR_CONF_SAMPLERATE:
479 /// @todo handle samplerate changes
480 /// samplerate = (uint64_t *)src->value;
483 // Unknown metadata is not an error.
491 void SigSession::feed_in_frame_begin()
493 if (_cur_logic_snapshot || !_cur_analog_snapshots.empty())
497 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
499 lock_guard<mutex> lock(_data_mutex);
503 qDebug() << "Unexpected logic packet";
507 if (!_cur_logic_snapshot)
509 // This could be the first packet after a trigger
510 set_capture_state(Running);
512 // Create a new data snapshot
513 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
514 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
515 _logic_data->push_snapshot(_cur_logic_snapshot);
517 // @todo Putting this here means that only listeners querying
518 // for logic will be notified. Currently the only user of
519 // frame_began is DecoderStack, but in future we need to signal
520 // this after both analog and logic sweeps have begun.
525 // Append to the existing data snapshot
526 _cur_logic_snapshot->append_payload(logic);
532 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
534 lock_guard<mutex> lock(_data_mutex);
536 const unsigned int probe_count = g_slist_length(analog.channels);
537 const size_t sample_count = analog.num_samples / probe_count;
538 const float *data = analog.data;
539 bool sweep_beginning = false;
541 for (GSList *p = analog.channels; p; p = p->next)
543 shared_ptr<data::AnalogSnapshot> snapshot;
545 sr_channel *const probe = (sr_channel*)p->data;
548 // Try to get the snapshot of the probe
549 const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >::
550 iterator iter = _cur_analog_snapshots.find(probe);
551 if (iter != _cur_analog_snapshots.end())
552 snapshot = (*iter).second;
555 // If no snapshot was found, this means we havn't
556 // created one yet. i.e. this is the first packet
557 // in the sweep containing this snapshot.
558 sweep_beginning = true;
560 // Create a snapshot, keep it in the maps of probes
561 snapshot = shared_ptr<data::AnalogSnapshot>(
562 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
563 _cur_analog_snapshots[probe] = snapshot;
565 // Find the annalog data associated with the probe
566 shared_ptr<view::AnalogSignal> sig =
567 dynamic_pointer_cast<view::AnalogSignal>(
568 signal_from_probe(probe));
571 shared_ptr<data::Analog> data(sig->analog_data());
574 // Push the snapshot into the analog data.
575 data->push_snapshot(snapshot);
580 // Append the samples in the snapshot
581 snapshot->append_interleaved_samples(data++, sample_count,
585 if (sweep_beginning) {
586 // This could be the first packet after a trigger
587 set_capture_state(Running);
593 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
594 const struct sr_datafeed_packet *packet)
599 switch (packet->type) {
605 assert(packet->payload);
607 *(const sr_datafeed_meta*)packet->payload);
610 case SR_DF_FRAME_BEGIN:
611 feed_in_frame_begin();
615 assert(packet->payload);
616 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
620 assert(packet->payload);
621 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
627 lock_guard<mutex> lock(_data_mutex);
628 _cur_logic_snapshot.reset();
629 _cur_analog_snapshots.clear();
637 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
638 const struct sr_datafeed_packet *packet, void *cb_data)
642 _session->data_feed_in(sdi, packet);