]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
SigSession: Added signals_mutex(), and made signals() give a reference
[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
c3a740dd
JH
242mutex& SigSession::signals_mutex() const
243{
244 return _signals_mutex;
245}
246
247const vector< shared_ptr<view::Signal> >& SigSession::signals() const
2e2946fe
JH
248{
249 return _signals;
250}
251
269528f5 252#ifdef ENABLE_DECODE
4e5a4405 253bool SigSession::add_decoder(srd_decoder *const dec)
82c7f640 254{
6ac6242b 255 map<const srd_channel*, shared_ptr<view::LogicSignal> > channels;
7491a29f 256 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 257
e92cd4e4 258 try
82c7f640 259 {
119aff65 260 lock_guard<mutex> lock(_signals_mutex);
e0fc5810 261
4e5a4405 262 // Create the decoder
7491a29f 263 decoder_stack = shared_ptr<data::DecoderStack>(
bdc5c3b0 264 new data::DecoderStack(*this, dec));
4e5a4405 265
6ac6242b
ML
266 // Make a list of all the channels
267 std::vector<const srd_channel*> all_channels;
8bd26d8b 268 for(const GSList *i = dec->channels; i; i = i->next)
6ac6242b 269 all_channels.push_back((const srd_channel*)i->data);
8bd26d8b 270 for(const GSList *i = dec->opt_channels; i; i = i->next)
6ac6242b 271 all_channels.push_back((const srd_channel*)i->data);
d2f46d27 272
6ac6242b
ML
273 // Auto select the initial channels
274 for (const srd_channel *pdch : all_channels)
d9aecf1f 275 for (shared_ptr<view::Signal> s : _signals)
4e5a4405
JH
276 {
277 shared_ptr<view::LogicSignal> l =
278 dynamic_pointer_cast<view::LogicSignal>(s);
8bd26d8b 279 if (l && QString::fromUtf8(pdch->name).
27e8df22 280 toLower().contains(
0a47889b 281 l->name().toLower()))
6ac6242b 282 channels[pdch] = l;
4e5a4405 283 }
4e5a4405 284
7491a29f
JH
285 assert(decoder_stack);
286 assert(!decoder_stack->stack().empty());
287 assert(decoder_stack->stack().front());
6ac6242b 288 decoder_stack->stack().front()->set_channels(channels);
4e5a4405
JH
289
290 // Create the decode signal
b9329558 291 shared_ptr<view::DecodeTrace> d(
7491a29f 292 new view::DecodeTrace(*this, decoder_stack,
06bb4e6a 293 _decode_traces.size()));
82c7f640
JH
294 _decode_traces.push_back(d);
295 }
e92cd4e4
JH
296 catch(std::runtime_error e)
297 {
298 return false;
299 }
300
82c7f640 301 signals_changed();
e92cd4e4 302
7491a29f
JH
303 // Do an initial decode
304 decoder_stack->begin_decode();
305
e92cd4e4 306 return true;
82c7f640
JH
307}
308
b9329558 309vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
38eeddea
JH
310{
311 lock_guard<mutex> lock(_signals_mutex);
312 return _decode_traces;
313}
314
b9329558 315void SigSession::remove_decode_signal(view::DecodeTrace *signal)
c51482b3 316{
f46e495e 317 for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++)
c51482b3
JH
318 if ((*i).get() == signal)
319 {
320 _decode_traces.erase(i);
321 signals_changed();
322 return;
323 }
324}
269528f5 325#endif
c51482b3 326
6ac96c2e
JH
327void SigSession::set_capture_state(capture_state state)
328{
949f8050 329 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 330 const bool changed = _capture_state != state;
6ac96c2e 331 _capture_state = state;
2b49eeb0
JH
332 if(changed)
333 capture_state_changed(state);
6ac96c2e
JH
334}
335
e8d00928 336void SigSession::update_signals(shared_ptr<Device> device)
b087ba7f 337{
e8d00928 338 assert(device);
b087ba7f
JH
339 assert(_capture_state == Stopped);
340
82c7f640
JH
341 // Clear the decode traces
342 _decode_traces.clear();
343
b087ba7f 344 // Detect what data types we will receive
e8d00928
ML
345 auto channels = device->channels();
346 unsigned int logic_channel_count = std::count_if(
347 channels.begin(), channels.end(),
348 [] (shared_ptr<Channel> channel) {
349 return channel->type() == ChannelType::LOGIC; });
b087ba7f 350
bb2cdfff 351 // Create data containers for the logic data snapshots
b087ba7f
JH
352 {
353 lock_guard<mutex> data_lock(_data_mutex);
354
355 _logic_data.reset();
6ac6242b 356 if (logic_channel_count != 0) {
b087ba7f 357 _logic_data.reset(new data::Logic(
6ac6242b 358 logic_channel_count));
b087ba7f
JH
359 assert(_logic_data);
360 }
b087ba7f
JH
361 }
362
363 // Make the Signals list
fbd3e234 364 {
b087ba7f
JH
365 lock_guard<mutex> lock(_signals_mutex);
366
367 _signals.clear();
368
e8d00928 369 for (auto channel : device->channels()) {
39e680c2 370 shared_ptr<view::Signal> signal;
39e680c2 371
e8d00928 372 switch(channel->type()->id()) {
4871ed92 373 case SR_CHANNEL_LOGIC:
39e680c2 374 signal = shared_ptr<view::Signal>(
b86aa8f4
JH
375 new view::LogicSignal(*this, device,
376 channel, _logic_data));
39e680c2
JH
377 break;
378
4871ed92 379 case SR_CHANNEL_ANALOG:
bb2cdfff
JH
380 {
381 shared_ptr<data::Analog> data(
382 new data::Analog());
39e680c2 383 signal = shared_ptr<view::Signal>(
b86aa8f4
JH
384 new view::AnalogSignal(
385 *this, channel, data));
39e680c2 386 break;
bb2cdfff 387 }
39e680c2
JH
388
389 default:
390 assert(0);
391 break;
b087ba7f 392 }
39e680c2
JH
393
394 assert(signal);
395 _signals.push_back(signal);
b087ba7f 396 }
39e680c2 397
fbd3e234 398 }
b087ba7f
JH
399
400 signals_changed();
401}
402
6ac6242b 403shared_ptr<view::Signal> SigSession::signal_from_channel(
e8d00928 404 shared_ptr<Channel> channel) const
3ddcc083
JH
405{
406 lock_guard<mutex> lock(_signals_mutex);
d9aecf1f 407 for (shared_ptr<view::Signal> sig : _signals) {
3ddcc083 408 assert(sig);
6ac6242b 409 if (sig->channel() == channel)
3ddcc083
JH
410 return sig;
411 }
412 return shared_ptr<view::Signal>();
413}
414
e8d00928 415void SigSession::read_sample_rate(shared_ptr<Device> device)
deef291c 416{
e8d00928
ML
417 uint64_t sample_rate = VariantBase::cast_dynamic<Variant<guint64>>(
418 device->config_get(ConfigKey::SAMPLERATE)).get();
deef291c 419
bb2cdfff
JH
420 // Set the sample rate of all data
421 const set< shared_ptr<data::SignalData> > data_set = get_data();
d9aecf1f 422 for (shared_ptr<data::SignalData> data : data_set) {
bb2cdfff
JH
423 assert(data);
424 data->set_samplerate(sample_rate);
425 }
deef291c
JH
426}
427
e8d00928 428void SigSession::sample_thread_proc(shared_ptr<Device> device,
f2edb557 429 function<void (const QString)> error_handler)
274d4f13 430{
e8d00928 431 assert(device);
f2edb557 432 assert(error_handler);
be73bdfa 433
e8d00928 434 read_sample_rate(device);
274d4f13 435
ae2d1bc5 436 try {
1f4caa77 437 _session->start();
e8d00928
ML
438 } catch(Error e) {
439 error_handler(e.what());
eec446e1
JH
440 return;
441 }
442
1f4caa77 443 set_capture_state(_session->trigger() ?
07dcf561 444 AwaitingTrigger : Running);
eec446e1 445
1f4caa77 446 _session->run();
eec446e1 447 set_capture_state(Stopped);
1a2fe44c
JH
448
449 // Confirm that SR_DF_END was received
bb2cdfff
JH
450 if (_cur_logic_snapshot)
451 {
452 qDebug("SR_DF_END was not received.");
453 assert(0);
454 }
eec446e1
JH
455}
456
e8d00928 457void SigSession::feed_in_header(shared_ptr<Device> device)
eec446e1 458{
e8d00928 459 read_sample_rate(device);
be73bdfa
JH
460}
461
e8d00928
ML
462void SigSession::feed_in_meta(shared_ptr<Device> device,
463 shared_ptr<Meta> meta)
be73bdfa 464{
e8d00928 465 (void)device;
94fe58ef 466
e8d00928
ML
467 for (auto entry : meta->config()) {
468 switch (entry.first->id()) {
be73bdfa
JH
469 case SR_CONF_SAMPLERATE:
470 /// @todo handle samplerate changes
be73bdfa
JH
471 break;
472 default:
473 // Unknown metadata is not an error.
474 break;
475 }
aba1dd16 476 }
74043039
JH
477
478 signals_changed();
aba1dd16
JH
479}
480
82f50b10
JH
481void SigSession::feed_in_frame_begin()
482{
483 if (_cur_logic_snapshot || !_cur_analog_snapshots.empty())
484 frame_began();
485}
486
e8d00928 487void SigSession::feed_in_logic(shared_ptr<Logic> logic)
9c112671
JH
488{
489 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
490
491 if (!_logic_data)
9c112671 492 {
79efbc53
JH
493 qDebug() << "Unexpected logic packet";
494 return;
495 }
be73bdfa 496
79efbc53
JH
497 if (!_cur_logic_snapshot)
498 {
bb2cdfff 499 // This could be the first packet after a trigger
2b49eeb0
JH
500 set_capture_state(Running);
501
e8d00928
ML
502 // Get sample limit.
503 uint64_t sample_limit;
504 try {
505 sample_limit = VariantBase::cast_dynamic<Variant<guint64>>(
506 _device->config_get(ConfigKey::LIMIT_SAMPLES)).get();
507 } catch (Error) {
508 sample_limit = 0;
509 }
510
9c112671 511 // Create a new data snapshot
1b1ec774 512 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
e8d00928 513 new data::LogicSnapshot(logic, sample_limit));
9c112671 514 _logic_data->push_snapshot(_cur_logic_snapshot);
82f50b10
JH
515
516 // @todo Putting this here means that only listeners querying
517 // for logic will be notified. Currently the only user of
518 // frame_began is DecoderStack, but in future we need to signal
519 // this after both analog and logic sweeps have begun.
520 frame_began();
9c112671
JH
521 }
522 else
523 {
524 // Append to the existing data snapshot
525 _cur_logic_snapshot->append_payload(logic);
526 }
527
1f374035 528 data_received();
9c112671
JH
529}
530
e8d00928 531void SigSession::feed_in_analog(shared_ptr<Analog> analog)
aba1dd16
JH
532{
533 lock_guard<mutex> lock(_data_mutex);
79efbc53 534
e8d00928
ML
535 const vector<shared_ptr<Channel>> channels = analog->channels();
536 const unsigned int channel_count = channels.size();
537 const size_t sample_count = analog->num_samples() / channel_count;
538 const float *data = analog->data_pointer();
bb2cdfff 539 bool sweep_beginning = false;
be73bdfa 540
e8d00928 541 for (auto channel : channels)
79efbc53 542 {
bb2cdfff 543 shared_ptr<data::AnalogSnapshot> snapshot;
2b49eeb0 544
6ac6242b 545 // Try to get the snapshot of the channel
e8d00928 546 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSnapshot> >::
6ac6242b 547 iterator iter = _cur_analog_snapshots.find(channel);
bb2cdfff
JH
548 if (iter != _cur_analog_snapshots.end())
549 snapshot = (*iter).second;
550 else
551 {
552 // If no snapshot was found, this means we havn't
553 // created one yet. i.e. this is the first packet
554 // in the sweep containing this snapshot.
555 sweep_beginning = true;
556
e8d00928
ML
557 // Get sample limit.
558 uint64_t sample_limit;
559 try {
560 sample_limit = VariantBase::cast_dynamic<Variant<guint64>>(
561 _device->config_get(ConfigKey::LIMIT_SAMPLES)).get();
562 } catch (Error) {
563 sample_limit = 0;
564 }
565
6ac6242b 566 // Create a snapshot, keep it in the maps of channels
bb2cdfff 567 snapshot = shared_ptr<data::AnalogSnapshot>(
e8d00928 568 new data::AnalogSnapshot(sample_limit));
6ac6242b 569 _cur_analog_snapshots[channel] = snapshot;
bb2cdfff 570
6ac6242b 571 // Find the annalog data associated with the channel
bb2cdfff
JH
572 shared_ptr<view::AnalogSignal> sig =
573 dynamic_pointer_cast<view::AnalogSignal>(
6ac6242b 574 signal_from_channel(channel));
bb2cdfff
JH
575 assert(sig);
576
577 shared_ptr<data::Analog> data(sig->analog_data());
578 assert(data);
579
580 // Push the snapshot into the analog data.
581 data->push_snapshot(snapshot);
582 }
583
584 assert(snapshot);
585
586 // Append the samples in the snapshot
587 snapshot->append_interleaved_samples(data++, sample_count,
6ac6242b 588 channel_count);
aba1dd16 589 }
bb2cdfff
JH
590
591 if (sweep_beginning) {
592 // This could be the first packet after a trigger
593 set_capture_state(Running);
aba1dd16
JH
594 }
595
1f374035 596 data_received();
aba1dd16 597}
9c112671 598
e8d00928 599void SigSession::data_feed_in(shared_ptr<Device> device, shared_ptr<Packet> packet)
b0e1d01d 600{
e8d00928 601 assert(device);
b0e1d01d
JH
602 assert(packet);
603
e8d00928 604 switch (packet->type()->id()) {
b0e1d01d 605 case SR_DF_HEADER:
e8d00928 606 feed_in_header(device);
b0e1d01d
JH
607 break;
608
be73bdfa 609 case SR_DF_META:
e8d00928 610 feed_in_meta(device, dynamic_pointer_cast<Meta>(packet->payload()));
aba1dd16
JH
611 break;
612
82f50b10
JH
613 case SR_DF_FRAME_BEGIN:
614 feed_in_frame_begin();
615 break;
616
2e2946fe 617 case SR_DF_LOGIC:
e8d00928 618 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
2953961c
JH
619 break;
620
aba1dd16 621 case SR_DF_ANALOG:
e8d00928 622 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
aba1dd16
JH
623 break;
624
2953961c 625 case SR_DF_END:
2e2946fe
JH
626 {
627 {
628 lock_guard<mutex> lock(_data_mutex);
629 _cur_logic_snapshot.reset();
bb2cdfff 630 _cur_analog_snapshots.clear();
2e2946fe 631 }
1f374035 632 frame_ended();
2953961c
JH
633 break;
634 }
adb0a983
ML
635 default:
636 break;
2e2946fe 637 }
2953961c
JH
638}
639
51e77110 640} // namespace pv