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