]> sigrok.org Git - pulseview.git/blame - pv/session.cpp
Session: Support capture before the sigrok::Device object has been created
[pulseview.git] / pv / session.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c 3 *
1bc6525b 4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
2953961c
JH
5 *
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.
10 *
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.
15 *
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
19 */
20
269528f5 21#ifdef ENABLE_DECODE
4e5a4405 22#include <libsigrokdecode/libsigrokdecode.h>
269528f5 23#endif
4e5a4405 24
f65cd27b 25#include "session.hpp"
2953961c 26
2acdb232 27#include "devicemanager.hpp"
119aff65 28
2acdb232 29#include "data/analog.hpp"
f3d66e52 30#include "data/analogsegment.hpp"
2acdb232
JH
31#include "data/decoderstack.hpp"
32#include "data/logic.hpp"
f3d66e52 33#include "data/logicsegment.hpp"
2acdb232 34#include "data/decode/decoder.hpp"
82c7f640 35
da30ecb7
JH
36#include "devices/hardwaredevice.hpp"
37#include "devices/sessionfile.hpp"
38
2acdb232
JH
39#include "view/analogsignal.hpp"
40#include "view/decodetrace.hpp"
41#include "view/logicsignal.hpp"
28a4c9c5 42
3b68d03d
JH
43#include <cassert>
44#include <mutex>
e92cd4e4
JH
45#include <stdexcept>
46
728e5ef7
JH
47#include <sys/stat.h>
48
79efbc53
JH
49#include <QDebug>
50
fe3a1c21 51#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 52
aca64cac
JH
53using boost::shared_lock;
54using boost::shared_mutex;
55using boost::unique_lock;
56
f9abf97e 57using std::dynamic_pointer_cast;
d2344534 58using std::function;
3b68d03d 59using std::lock_guard;
d873f4d6 60using std::list;
819f4c25 61using std::map;
aca64cac 62using std::mutex;
8524a597 63using std::recursive_mutex;
02412f0b 64using std::set;
f9abf97e 65using std::shared_ptr;
819f4c25 66using std::string;
78b0af3e 67using std::unordered_set;
819f4c25 68using std::vector;
28a4c9c5 69
e8d00928
ML
70using sigrok::Analog;
71using sigrok::Channel;
72using sigrok::ChannelType;
73using sigrok::ConfigKey;
74using sigrok::DatafeedCallbackFunction;
e8d00928 75using sigrok::Error;
e8d00928
ML
76using sigrok::Header;
77using sigrok::Logic;
78using sigrok::Meta;
79using sigrok::Packet;
80using sigrok::PacketPayload;
81using sigrok::Session;
82using sigrok::SessionDevice;
83
84using Glib::VariantBase;
85using Glib::Variant;
51e77110 86
e8d00928 87namespace pv {
2b81ae46 88Session::Session(DeviceManager &device_manager) :
8dbbc7f0 89 device_manager_(device_manager),
ff008de6
JH
90 capture_state_(Stopped),
91 cur_samplerate_(0)
2953961c 92{
d873f4d6 93 set_default_device();
2953961c
JH
94}
95
2b81ae46 96Session::~Session()
2953961c 97{
04463625 98 // Stop and join to the thread
5b7cf66c 99 stop_capture();
2953961c
JH
100}
101
2b81ae46 102DeviceManager& Session::device_manager()
4cb0b033 103{
8dbbc7f0 104 return device_manager_;
4cb0b033
JH
105}
106
2b81ae46 107const DeviceManager& Session::device_manager() const
4cb0b033 108{
8dbbc7f0 109 return device_manager_;
4cb0b033
JH
110}
111
da30ecb7 112shared_ptr<sigrok::Session> Session::session() const
1f4caa77 113{
da30ecb7
JH
114 if (!device_)
115 return shared_ptr<sigrok::Session>();
116 return device_->session();
1f4caa77
JH
117}
118
da30ecb7 119shared_ptr<devices::Device> Session::device() const
dc0867ff 120{
8dbbc7f0 121 return device_;
dc0867ff
JH
122}
123
da30ecb7 124void Session::set_device(shared_ptr<devices::Device> device)
d64d1596 125{
da30ecb7
JH
126 assert(device);
127
82596f46
JH
128 // Ensure we are not capturing before setting the device
129 stop_capture();
130
da30ecb7
JH
131 device_ = std::move(device);
132 device_->create();
133 device_->session()->add_datafeed_callback([=]
134 (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
135 data_feed_in(device, packet);
136 });
ffe00119 137 update_signals();
ae2d1bc5 138
8dbbc7f0 139 decode_traces_.clear();
85843b14 140
20f81a58 141 device_selected();
ae2d1bc5 142}
85843b14 143
2b81ae46 144void Session::set_default_device()
6fd0b639 145{
da30ecb7 146 const list< shared_ptr<devices::HardwareDevice> > &devices =
8dbbc7f0 147 device_manager_.devices();
6fd0b639 148
da30ecb7
JH
149 if (devices.empty())
150 return;
dc0867ff 151
da30ecb7
JH
152 // Try and find the demo device and select that by default
153 const auto iter = std::find_if(devices.begin(), devices.end(),
154 [] (const shared_ptr<devices::HardwareDevice> &d) {
155 return d->hardware_device()->driver()->name() ==
156 "demo"; });
157 set_device((iter == devices.end()) ? devices.front() : *iter);
dc0867ff
JH
158}
159
2b81ae46 160Session::capture_state Session::get_capture_state() const
5b7cf66c 161{
8dbbc7f0
JH
162 lock_guard<mutex> lock(sampling_mutex_);
163 return capture_state_;
5b7cf66c
JH
164}
165
2b81ae46 166void Session::start_capture(function<void (const QString)> error_handler)
2e2946fe 167{
5b7cf66c
JH
168 stop_capture();
169
6ac6242b 170 // Check that at least one channel is enabled
da30ecb7 171 assert(device_);
e6c1382e
JH
172 const shared_ptr<sigrok::Device> sr_dev = device_->device();
173 if (sr_dev) {
174 const auto channels = sr_dev->channels();
175 if (!std::any_of(channels.begin(), channels.end(),
176 [](shared_ptr<Channel> channel) {
177 return channel->enabled(); })) {
178 error_handler(tr("No channels enabled."));
179 return;
180 }
9c48fa57
JH
181 }
182
183 // Begin the session
8dbbc7f0 184 sampling_thread_ = std::thread(
2b81ae46 185 &Session::sample_thread_proc, this, device_,
19adbc2c 186 error_handler);
2e2946fe
JH
187}
188
2b81ae46 189void Session::stop_capture()
5b7cf66c 190{
04463625 191 if (get_capture_state() != Stopped)
da30ecb7 192 device_->stop();
5b7cf66c
JH
193
194 // Check that sampling stopped
8dbbc7f0
JH
195 if (sampling_thread_.joinable())
196 sampling_thread_.join();
5b7cf66c
JH
197}
198
2b81ae46 199set< shared_ptr<data::SignalData> > Session::get_data() const
02412f0b 200{
8dbbc7f0 201 shared_lock<shared_mutex> lock(signals_mutex_);
02412f0b 202 set< shared_ptr<data::SignalData> > data;
8dbbc7f0 203 for (const shared_ptr<view::Signal> sig : signals_) {
02412f0b
JH
204 assert(sig);
205 data.insert(sig->data());
206 }
207
208 return data;
209}
210
2b81ae46 211boost::shared_mutex& Session::signals_mutex() const
c3a740dd 212{
8dbbc7f0 213 return signals_mutex_;
c3a740dd
JH
214}
215
78b0af3e 216const unordered_set< shared_ptr<view::Signal> >& Session::signals() const
2e2946fe 217{
8dbbc7f0 218 return signals_;
2e2946fe
JH
219}
220
269528f5 221#ifdef ENABLE_DECODE
2b81ae46 222bool Session::add_decoder(srd_decoder *const dec)
82c7f640 223{
6ac6242b 224 map<const srd_channel*, shared_ptr<view::LogicSignal> > channels;
7491a29f 225 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 226
e92cd4e4 227 try
82c7f640 228 {
8dbbc7f0 229 lock_guard<boost::shared_mutex> lock(signals_mutex_);
e0fc5810 230
4e5a4405 231 // Create the decoder
7491a29f 232 decoder_stack = shared_ptr<data::DecoderStack>(
bdc5c3b0 233 new data::DecoderStack(*this, dec));
4e5a4405 234
6ac6242b
ML
235 // Make a list of all the channels
236 std::vector<const srd_channel*> all_channels;
8bd26d8b 237 for(const GSList *i = dec->channels; i; i = i->next)
6ac6242b 238 all_channels.push_back((const srd_channel*)i->data);
8bd26d8b 239 for(const GSList *i = dec->opt_channels; i; i = i->next)
6ac6242b 240 all_channels.push_back((const srd_channel*)i->data);
d2f46d27 241
6ac6242b
ML
242 // Auto select the initial channels
243 for (const srd_channel *pdch : all_channels)
8dbbc7f0 244 for (shared_ptr<view::Signal> s : signals_)
4e5a4405
JH
245 {
246 shared_ptr<view::LogicSignal> l =
247 dynamic_pointer_cast<view::LogicSignal>(s);
8bd26d8b 248 if (l && QString::fromUtf8(pdch->name).
27e8df22 249 toLower().contains(
0a47889b 250 l->name().toLower()))
6ac6242b 251 channels[pdch] = l;
4e5a4405 252 }
4e5a4405 253
7491a29f
JH
254 assert(decoder_stack);
255 assert(!decoder_stack->stack().empty());
256 assert(decoder_stack->stack().front());
6ac6242b 257 decoder_stack->stack().front()->set_channels(channels);
4e5a4405
JH
258
259 // Create the decode signal
b9329558 260 shared_ptr<view::DecodeTrace> d(
7491a29f 261 new view::DecodeTrace(*this, decoder_stack,
8dbbc7f0
JH
262 decode_traces_.size()));
263 decode_traces_.push_back(d);
82c7f640 264 }
e92cd4e4
JH
265 catch(std::runtime_error e)
266 {
267 return false;
268 }
269
82c7f640 270 signals_changed();
e92cd4e4 271
7491a29f
JH
272 // Do an initial decode
273 decoder_stack->begin_decode();
274
e92cd4e4 275 return true;
82c7f640
JH
276}
277
2b81ae46 278vector< shared_ptr<view::DecodeTrace> > Session::get_decode_signals() const
38eeddea 279{
8dbbc7f0
JH
280 shared_lock<shared_mutex> lock(signals_mutex_);
281 return decode_traces_;
38eeddea
JH
282}
283
2b81ae46 284void Session::remove_decode_signal(view::DecodeTrace *signal)
c51482b3 285{
8dbbc7f0 286 for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++)
c51482b3
JH
287 if ((*i).get() == signal)
288 {
8dbbc7f0 289 decode_traces_.erase(i);
c51482b3
JH
290 signals_changed();
291 return;
292 }
293}
269528f5 294#endif
c51482b3 295
2b81ae46 296void Session::set_capture_state(capture_state state)
6ac96c2e 297{
8dbbc7f0
JH
298 lock_guard<mutex> lock(sampling_mutex_);
299 const bool changed = capture_state_ != state;
300 capture_state_ = state;
2b49eeb0
JH
301 if(changed)
302 capture_state_changed(state);
6ac96c2e
JH
303}
304
ffe00119 305void Session::update_signals()
b087ba7f 306{
ffe00119 307 assert(device_);
e6c1382e
JH
308
309 lock_guard<recursive_mutex> lock(data_mutex_);
b087ba7f 310
ffe00119 311 const shared_ptr<sigrok::Device> sr_dev = device_->device();
c6412b47
JH
312 if (!sr_dev) {
313 signals_.clear();
314 logic_data_.reset();
315 return;
316 }
317
b087ba7f 318 // Detect what data types we will receive
c6412b47 319 auto channels = sr_dev->channels();
e8d00928
ML
320 unsigned int logic_channel_count = std::count_if(
321 channels.begin(), channels.end(),
322 [] (shared_ptr<Channel> channel) {
323 return channel->type() == ChannelType::LOGIC; });
b087ba7f 324
f3d66e52 325 // Create data containers for the logic data segments
b087ba7f 326 {
8524a597 327 lock_guard<recursive_mutex> data_lock(data_mutex_);
b087ba7f 328
3917ba77
JH
329 if (logic_channel_count == 0) {
330 logic_data_.reset();
331 } else if (!logic_data_ ||
332 logic_data_->num_channels() != logic_channel_count) {
8dbbc7f0 333 logic_data_.reset(new data::Logic(
6ac6242b 334 logic_channel_count));
8dbbc7f0 335 assert(logic_data_);
b087ba7f 336 }
b087ba7f
JH
337 }
338
339 // Make the Signals list
fbd3e234 340 {
8dbbc7f0 341 unique_lock<shared_mutex> lock(signals_mutex_);
b087ba7f 342
3917ba77 343 unordered_set< shared_ptr<view::Signal> > prev_sigs(signals_);
8dbbc7f0 344 signals_.clear();
b087ba7f 345
c6412b47 346 for (auto channel : sr_dev->channels()) {
39e680c2 347 shared_ptr<view::Signal> signal;
39e680c2 348
3917ba77
JH
349 // Find the channel in the old signals
350 const auto iter = std::find_if(
351 prev_sigs.cbegin(), prev_sigs.cend(),
352 [&](const shared_ptr<view::Signal> &s) {
353 return s->channel() == channel;
354 });
355 if (iter != prev_sigs.end()) {
356 // Copy the signal from the old set to the new
357 signal = *iter;
358 auto logic_signal = dynamic_pointer_cast<
359 view::LogicSignal>(signal);
360 if (logic_signal)
361 logic_signal->set_logic_data(
362 logic_data_);
363 } else {
364 // Create a new signal
365 switch(channel->type()->id()) {
366 case SR_CHANNEL_LOGIC:
367 signal = shared_ptr<view::Signal>(
368 new view::LogicSignal(*this,
ffe00119 369 device_, channel,
3917ba77
JH
370 logic_data_));
371 break;
372
373 case SR_CHANNEL_ANALOG:
374 {
375 shared_ptr<data::Analog> data(
376 new data::Analog());
377 signal = shared_ptr<view::Signal>(
378 new view::AnalogSignal(
379 *this, channel, data));
380 break;
381 }
382
383 default:
384 assert(0);
385 break;
386 }
b087ba7f 387 }
39e680c2
JH
388
389 assert(signal);
78b0af3e 390 signals_.insert(signal);
b087ba7f 391 }
fbd3e234 392 }
b087ba7f
JH
393
394 signals_changed();
395}
396
2b81ae46 397shared_ptr<view::Signal> Session::signal_from_channel(
e8d00928 398 shared_ptr<Channel> channel) const
3ddcc083 399{
8dbbc7f0
JH
400 lock_guard<boost::shared_mutex> lock(signals_mutex_);
401 for (shared_ptr<view::Signal> sig : signals_) {
3ddcc083 402 assert(sig);
6ac6242b 403 if (sig->channel() == channel)
3ddcc083
JH
404 return sig;
405 }
406 return shared_ptr<view::Signal>();
407}
408
da30ecb7 409void Session::sample_thread_proc(shared_ptr<devices::Device> device,
f2edb557 410 function<void (const QString)> error_handler)
274d4f13 411{
e8d00928 412 assert(device);
f2edb557 413 assert(error_handler);
be73bdfa 414
b48daed6 415 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
274d4f13 416
ae2d1bc5 417 try {
da30ecb7 418 device_->session()->start();
e8d00928
ML
419 } catch(Error e) {
420 error_handler(e.what());
eec446e1
JH
421 return;
422 }
423
da30ecb7 424 set_capture_state(device_->session()->trigger() ?
07dcf561 425 AwaitingTrigger : Running);
eec446e1 426
da30ecb7 427 device_->run();
eec446e1 428 set_capture_state(Stopped);
1a2fe44c
JH
429
430 // Confirm that SR_DF_END was received
f3d66e52 431 if (cur_logic_segment_)
bb2cdfff
JH
432 {
433 qDebug("SR_DF_END was not received.");
434 assert(0);
435 }
eec446e1
JH
436}
437
da30ecb7 438void Session::feed_in_header()
eec446e1 439{
b48daed6 440 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
be73bdfa
JH
441}
442
da30ecb7 443void Session::feed_in_meta(shared_ptr<Meta> meta)
be73bdfa 444{
e8d00928
ML
445 for (auto entry : meta->config()) {
446 switch (entry.first->id()) {
be73bdfa
JH
447 case SR_CONF_SAMPLERATE:
448 /// @todo handle samplerate changes
be73bdfa
JH
449 break;
450 default:
451 // Unknown metadata is not an error.
452 break;
453 }
aba1dd16 454 }
74043039
JH
455
456 signals_changed();
aba1dd16
JH
457}
458
2b81ae46 459void Session::feed_in_frame_begin()
82f50b10 460{
f3d66e52 461 if (cur_logic_segment_ || !cur_analog_segments_.empty())
82f50b10
JH
462 frame_began();
463}
464
2b81ae46 465void Session::feed_in_logic(shared_ptr<Logic> logic)
9c112671 466{
8524a597 467 lock_guard<recursive_mutex> lock(data_mutex_);
79efbc53 468
8dbbc7f0 469 if (!logic_data_)
9c112671 470 {
e6c1382e
JH
471 // The only reason logic_data_ would not have been created is
472 // if it was not possible to determine the signals when the
473 // device was created.
474 update_signals();
79efbc53 475 }
be73bdfa 476
f3d66e52 477 if (!cur_logic_segment_)
79efbc53 478 {
bb2cdfff 479 // This could be the first packet after a trigger
2b49eeb0
JH
480 set_capture_state(Running);
481
e8d00928 482 // Get sample limit.
4365a440
JH
483 const uint64_t sample_limit = device_->read_config<uint64_t>(
484 ConfigKey::LIMIT_SAMPLES);
e8d00928 485
f3d66e52
JH
486 // Create a new data segment
487 cur_logic_segment_ = shared_ptr<data::LogicSegment>(
488 new data::LogicSegment(
ff008de6 489 logic, cur_samplerate_, sample_limit));
f3d66e52 490 logic_data_->push_segment(cur_logic_segment_);
82f50b10
JH
491
492 // @todo Putting this here means that only listeners querying
493 // for logic will be notified. Currently the only user of
494 // frame_began is DecoderStack, but in future we need to signal
495 // this after both analog and logic sweeps have begun.
496 frame_began();
9c112671
JH
497 }
498 else
499 {
f3d66e52
JH
500 // Append to the existing data segment
501 cur_logic_segment_->append_payload(logic);
9c112671
JH
502 }
503
1f374035 504 data_received();
9c112671
JH
505}
506
2b81ae46 507void Session::feed_in_analog(shared_ptr<Analog> analog)
aba1dd16 508{
8524a597 509 lock_guard<recursive_mutex> lock(data_mutex_);
79efbc53 510
e8d00928
ML
511 const vector<shared_ptr<Channel>> channels = analog->channels();
512 const unsigned int channel_count = channels.size();
513 const size_t sample_count = analog->num_samples() / channel_count;
514 const float *data = analog->data_pointer();
bb2cdfff 515 bool sweep_beginning = false;
be73bdfa 516
e8d00928 517 for (auto channel : channels)
79efbc53 518 {
f3d66e52 519 shared_ptr<data::AnalogSegment> segment;
2b49eeb0 520
f3d66e52
JH
521 // Try to get the segment of the channel
522 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >::
523 iterator iter = cur_analog_segments_.find(channel);
524 if (iter != cur_analog_segments_.end())
525 segment = (*iter).second;
bb2cdfff
JH
526 else
527 {
f3d66e52 528 // If no segment was found, this means we havn't
bb2cdfff 529 // created one yet. i.e. this is the first packet
f3d66e52 530 // in the sweep containing this segment.
bb2cdfff
JH
531 sweep_beginning = true;
532
e8d00928
ML
533 // Get sample limit.
534 uint64_t sample_limit;
535 try {
da30ecb7
JH
536 assert(device_);
537 const std::shared_ptr<sigrok::Device> device =
538 device_->device();
539 assert(device);
e8d00928 540 sample_limit = VariantBase::cast_dynamic<Variant<guint64>>(
da30ecb7 541 device->config_get(ConfigKey::LIMIT_SAMPLES)).get();
e8d00928
ML
542 } catch (Error) {
543 sample_limit = 0;
544 }
545
f3d66e52
JH
546 // Create a segment, keep it in the maps of channels
547 segment = shared_ptr<data::AnalogSegment>(
548 new data::AnalogSegment(
ff008de6 549 cur_samplerate_, sample_limit));
f3d66e52 550 cur_analog_segments_[channel] = segment;
bb2cdfff 551
6ac6242b 552 // Find the annalog data associated with the channel
bb2cdfff
JH
553 shared_ptr<view::AnalogSignal> sig =
554 dynamic_pointer_cast<view::AnalogSignal>(
6ac6242b 555 signal_from_channel(channel));
bb2cdfff
JH
556 assert(sig);
557
558 shared_ptr<data::Analog> data(sig->analog_data());
559 assert(data);
560
f3d66e52
JH
561 // Push the segment into the analog data.
562 data->push_segment(segment);
bb2cdfff
JH
563 }
564
f3d66e52 565 assert(segment);
bb2cdfff 566
f3d66e52
JH
567 // Append the samples in the segment
568 segment->append_interleaved_samples(data++, sample_count,
6ac6242b 569 channel_count);
aba1dd16 570 }
bb2cdfff
JH
571
572 if (sweep_beginning) {
573 // This could be the first packet after a trigger
574 set_capture_state(Running);
aba1dd16
JH
575 }
576
1f374035 577 data_received();
aba1dd16 578}
9c112671 579
da30ecb7
JH
580void Session::data_feed_in(shared_ptr<sigrok::Device> device,
581 shared_ptr<Packet> packet)
b0e1d01d 582{
da30ecb7
JH
583 (void)device;
584
e8d00928 585 assert(device);
da30ecb7 586 assert(device == device_->device());
b0e1d01d
JH
587 assert(packet);
588
e8d00928 589 switch (packet->type()->id()) {
b0e1d01d 590 case SR_DF_HEADER:
da30ecb7 591 feed_in_header();
b0e1d01d
JH
592 break;
593
be73bdfa 594 case SR_DF_META:
da30ecb7 595 feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
aba1dd16
JH
596 break;
597
82f50b10
JH
598 case SR_DF_FRAME_BEGIN:
599 feed_in_frame_begin();
600 break;
601
2e2946fe 602 case SR_DF_LOGIC:
e8d00928 603 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
2953961c
JH
604 break;
605
aba1dd16 606 case SR_DF_ANALOG:
e8d00928 607 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
aba1dd16
JH
608 break;
609
2953961c 610 case SR_DF_END:
2e2946fe
JH
611 {
612 {
8524a597 613 lock_guard<recursive_mutex> lock(data_mutex_);
f3d66e52
JH
614 cur_logic_segment_.reset();
615 cur_analog_segments_.clear();
2e2946fe 616 }
1f374035 617 frame_ended();
2953961c
JH
618 break;
619 }
adb0a983
ML
620 default:
621 break;
2e2946fe 622 }
2953961c
JH
623}
624
51e77110 625} // namespace pv