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;
106 // Ensure we are not capturing before setting the device
110 sr_session_datafeed_callback_remove_all(_sr_session);
111 _dev_inst->release();
114 _dev_inst = dev_inst;
115 _decode_traces.clear();
119 sr_session_datafeed_callback_add(_sr_session, data_feed_in_proc, NULL);
120 update_signals(dev_inst);
124 void SigSession::set_file(const string &name) throw(QString)
126 // Deselect the old device, because file type detection in File::create
127 // destroys the old session inside libsigrok.
128 set_device(shared_ptr<device::DevInst>());
129 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
132 void SigSession::set_default_device()
134 shared_ptr<pv::device::DevInst> default_device;
135 const list< shared_ptr<device::Device> > &devices =
136 _device_manager.devices();
138 if (!devices.empty()) {
139 // Fall back to the first device in the list.
140 default_device = devices.front();
142 // Try and find the demo device and select that by default
143 for (shared_ptr<pv::device::Device> dev : devices)
144 if (strcmp(dev->dev_inst()->driver->name,
146 default_device = dev;
151 set_device(default_device);
154 void SigSession::release_device(device::DevInst *dev_inst)
157 assert(_dev_inst.get() == dev_inst);
159 assert(_capture_state == Stopped);
160 _dev_inst = shared_ptr<device::DevInst>();
163 SigSession::capture_state SigSession::get_capture_state() const
165 lock_guard<mutex> lock(_sampling_mutex);
166 return _capture_state;
169 void SigSession::start_capture(function<void (const QString)> error_handler)
173 // Check that a device instance has been selected.
175 qDebug() << "No device selected";
179 assert(_dev_inst->dev_inst());
181 // Check that at least one channel is enabled
183 for (l = _dev_inst->dev_inst()->channels; l; l = l->next) {
184 sr_channel *const channel = (sr_channel*)l->data;
186 if (channel->enabled)
191 error_handler(tr("No channels enabled."));
196 _sampling_thread = std::thread(
197 &SigSession::sample_thread_proc, this, _dev_inst,
201 void SigSession::stop_capture()
203 if (get_capture_state() != Stopped)
204 sr_session_stop(_sr_session);
206 // Check that sampling stopped
207 if (_sampling_thread.joinable())
208 _sampling_thread.join();
211 set< shared_ptr<data::SignalData> > SigSession::get_data() const
213 lock_guard<mutex> lock(_signals_mutex);
214 set< shared_ptr<data::SignalData> > data;
215 for (const shared_ptr<view::Signal> sig : _signals) {
217 data.insert(sig->data());
223 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
225 lock_guard<mutex> lock(_signals_mutex);
230 bool SigSession::add_decoder(srd_decoder *const dec)
232 map<const srd_channel*, shared_ptr<view::LogicSignal> > channels;
233 shared_ptr<data::DecoderStack> decoder_stack;
237 lock_guard<mutex> lock(_signals_mutex);
239 // Create the decoder
240 decoder_stack = shared_ptr<data::DecoderStack>(
241 new data::DecoderStack(*this, dec));
243 // Make a list of all the channels
244 std::vector<const srd_channel*> all_channels;
245 for(const GSList *i = dec->channels; i; i = i->next)
246 all_channels.push_back((const srd_channel*)i->data);
247 for(const GSList *i = dec->opt_channels; i; i = i->next)
248 all_channels.push_back((const srd_channel*)i->data);
250 // Auto select the initial channels
251 for (const srd_channel *pdch : all_channels)
252 for (shared_ptr<view::Signal> s : _signals)
254 shared_ptr<view::LogicSignal> l =
255 dynamic_pointer_cast<view::LogicSignal>(s);
256 if (l && QString::fromUtf8(pdch->name).
258 l->get_name().toLower()))
262 assert(decoder_stack);
263 assert(!decoder_stack->stack().empty());
264 assert(decoder_stack->stack().front());
265 decoder_stack->stack().front()->set_channels(channels);
267 // Create the decode signal
268 shared_ptr<view::DecodeTrace> d(
269 new view::DecodeTrace(*this, decoder_stack,
270 _decode_traces.size()));
271 _decode_traces.push_back(d);
273 catch(std::runtime_error e)
280 // Do an initial decode
281 decoder_stack->begin_decode();
286 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
288 lock_guard<mutex> lock(_signals_mutex);
289 return _decode_traces;
292 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
294 for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++)
295 if ((*i).get() == signal)
297 _decode_traces.erase(i);
304 void SigSession::set_capture_state(capture_state state)
306 lock_guard<mutex> lock(_sampling_mutex);
307 const bool changed = _capture_state != state;
308 _capture_state = state;
310 capture_state_changed(state);
313 void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
316 assert(_capture_state == Stopped);
318 unsigned int logic_channel_count = 0;
320 // Clear the decode traces
321 _decode_traces.clear();
323 // Detect what data types we will receive
325 assert(dev_inst->dev_inst());
326 for (const GSList *l = dev_inst->dev_inst()->channels;
328 const sr_channel *const channel = (const sr_channel *)l->data;
329 if (!channel->enabled)
332 switch(channel->type) {
333 case SR_CHANNEL_LOGIC:
334 logic_channel_count++;
340 // Create data containers for the logic data snapshots
342 lock_guard<mutex> data_lock(_data_mutex);
345 if (logic_channel_count != 0) {
346 _logic_data.reset(new data::Logic(
347 logic_channel_count));
352 // Make the Signals list
354 lock_guard<mutex> lock(_signals_mutex);
361 assert(dev_inst->dev_inst());
362 for (const GSList *l = dev_inst->dev_inst()->channels;
364 shared_ptr<view::Signal> signal;
365 sr_channel *const channel = (sr_channel *)l->data;
368 switch(channel->type) {
369 case SR_CHANNEL_LOGIC:
370 signal = shared_ptr<view::Signal>(
371 new view::LogicSignal(dev_inst,
372 channel, _logic_data));
375 case SR_CHANNEL_ANALOG:
377 shared_ptr<data::Analog> data(
379 signal = shared_ptr<view::Signal>(
380 new view::AnalogSignal(dev_inst,
391 _signals.push_back(signal);
399 shared_ptr<view::Signal> SigSession::signal_from_channel(
400 const sr_channel *channel) const
402 lock_guard<mutex> lock(_signals_mutex);
403 for (shared_ptr<view::Signal> sig : _signals) {
405 if (sig->channel() == channel)
408 return shared_ptr<view::Signal>();
411 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
414 uint64_t sample_rate = 0;
416 // Read out the sample rate
419 const int ret = sr_config_get(sdi->driver, sdi, NULL,
420 SR_CONF_SAMPLERATE, &gvar);
422 qDebug("Failed to get samplerate\n");
426 sample_rate = g_variant_get_uint64(gvar);
427 g_variant_unref(gvar);
430 // Set the sample rate of all data
431 const set< shared_ptr<data::SignalData> > data_set = get_data();
432 for (shared_ptr<data::SignalData> data : data_set) {
434 data->set_samplerate(sample_rate);
438 void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
439 function<void (const QString)> error_handler)
442 assert(dev_inst->dev_inst());
443 assert(error_handler);
445 read_sample_rate(dev_inst->dev_inst());
449 } catch(const QString e) {
454 set_capture_state(sr_session_trigger_get(_sr_session) ?
455 AwaitingTrigger : Running);
458 set_capture_state(Stopped);
460 // Confirm that SR_DF_END was received
461 if (_cur_logic_snapshot)
463 qDebug("SR_DF_END was not received.");
468 void SigSession::feed_in_header(const sr_dev_inst *sdi)
470 read_sample_rate(sdi);
473 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
474 const sr_datafeed_meta &meta)
478 for (const GSList *l = meta.config; l; l = l->next) {
479 const sr_config *const src = (const sr_config*)l->data;
481 case SR_CONF_SAMPLERATE:
482 /// @todo handle samplerate changes
483 /// samplerate = (uint64_t *)src->value;
486 // Unknown metadata is not an error.
494 void SigSession::feed_in_frame_begin()
496 if (_cur_logic_snapshot || !_cur_analog_snapshots.empty())
500 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
502 lock_guard<mutex> lock(_data_mutex);
506 qDebug() << "Unexpected logic packet";
510 if (!_cur_logic_snapshot)
512 // This could be the first packet after a trigger
513 set_capture_state(Running);
515 // Create a new data snapshot
516 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
517 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
518 _logic_data->push_snapshot(_cur_logic_snapshot);
520 // @todo Putting this here means that only listeners querying
521 // for logic will be notified. Currently the only user of
522 // frame_began is DecoderStack, but in future we need to signal
523 // this after both analog and logic sweeps have begun.
528 // Append to the existing data snapshot
529 _cur_logic_snapshot->append_payload(logic);
535 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
537 lock_guard<mutex> lock(_data_mutex);
539 const unsigned int channel_count = g_slist_length(analog.channels);
540 const size_t sample_count = analog.num_samples / channel_count;
541 const float *data = analog.data;
542 bool sweep_beginning = false;
544 for (GSList *p = analog.channels; p; p = p->next)
546 shared_ptr<data::AnalogSnapshot> snapshot;
548 sr_channel *const channel = (sr_channel*)p->data;
551 // Try to get the snapshot of the channel
552 const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >::
553 iterator iter = _cur_analog_snapshots.find(channel);
554 if (iter != _cur_analog_snapshots.end())
555 snapshot = (*iter).second;
558 // If no snapshot was found, this means we havn't
559 // created one yet. i.e. this is the first packet
560 // in the sweep containing this snapshot.
561 sweep_beginning = true;
563 // Create a snapshot, keep it in the maps of channels
564 snapshot = shared_ptr<data::AnalogSnapshot>(
565 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
566 _cur_analog_snapshots[channel] = snapshot;
568 // Find the annalog data associated with the channel
569 shared_ptr<view::AnalogSignal> sig =
570 dynamic_pointer_cast<view::AnalogSignal>(
571 signal_from_channel(channel));
574 shared_ptr<data::Analog> data(sig->analog_data());
577 // Push the snapshot into the analog data.
578 data->push_snapshot(snapshot);
583 // Append the samples in the snapshot
584 snapshot->append_interleaved_samples(data++, sample_count,
588 if (sweep_beginning) {
589 // This could be the first packet after a trigger
590 set_capture_state(Running);
596 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
597 const struct sr_datafeed_packet *packet)
602 switch (packet->type) {
608 assert(packet->payload);
610 *(const sr_datafeed_meta*)packet->payload);
613 case SR_DF_FRAME_BEGIN:
614 feed_in_frame_begin();
618 assert(packet->payload);
619 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
623 assert(packet->payload);
624 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
630 lock_guard<mutex> lock(_data_mutex);
631 _cur_logic_snapshot.reset();
632 _cur_analog_snapshots.clear();
642 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
643 const struct sr_datafeed_packet *packet, void *cb_data)
647 _session->data_feed_in(sdi, packet);