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