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