forked from sigrokproject/pulseview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.cpp
1739 lines (1398 loc) · 43.5 KB
/
session.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the PulseView project.
*
* Copyright (C) 2012-14 Joel Holdsworth <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <sys/stat.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include "devicemanager.hpp"
#include "mainwindow.hpp"
#include "session.hpp"
#include "util.hpp"
#include "data/analog.hpp"
#include "data/analogsegment.hpp"
#include "data/decode/decoder.hpp"
#include "data/logic.hpp"
#include "data/logicsegment.hpp"
#include "data/mathsignal.hpp"
#include "data/signalbase.hpp"
#include "devices/hardwaredevice.hpp"
#include "devices/inputfile.hpp"
#include "devices/sessionfile.hpp"
#include "toolbars/mainbar.hpp"
#include "views/trace/analogsignal.hpp"
#include "views/trace/decodetrace.hpp"
#include "views/trace/logicsignal.hpp"
#include "views/trace/signal.hpp"
#include "views/trace/view.hpp"
#include <libsigrokcxx/libsigrokcxx.hpp>
#ifdef ENABLE_FLOW
#include <gstreamermm.h>
#include <libsigrokflow/libsigrokflow.hpp>
#endif
#ifdef ENABLE_DECODE
#include <libsigrokdecode/libsigrokdecode.h>
#include "data/decodesignal.hpp"
#endif
using std::bad_alloc;
using std::dynamic_pointer_cast;
using std::find_if;
using std::function;
using std::list;
using std::lock_guard;
using std::make_pair;
using std::make_shared;
using std::map;
using std::max;
using std::move;
using std::mutex;
using std::pair;
using std::recursive_mutex;
using std::runtime_error;
using std::shared_ptr;
using std::string;
#ifdef ENABLE_FLOW
using std::unique_lock;
#endif
using std::unique_ptr;
using std::vector;
using sigrok::Analog;
using sigrok::Channel;
using sigrok::ConfigKey;
using sigrok::DatafeedCallbackFunction;
using sigrok::Error;
using sigrok::InputFormat;
using sigrok::Logic;
using sigrok::Meta;
using sigrok::Packet;
using sigrok::Session;
using Glib::VariantBase;
#ifdef ENABLE_FLOW
using Gst::Bus;
using Gst::ElementFactory;
using Gst::Pipeline;
#endif
using pv::data::SignalGroup;
using pv::util::Timestamp;
using pv::views::trace::Signal;
using pv::views::trace::AnalogSignal;
using pv::views::trace::LogicSignal;
namespace pv {
shared_ptr<sigrok::Context> Session::sr_context;
Session::Session(DeviceManager &device_manager, QString name) :
shutting_down_(false),
device_manager_(device_manager),
default_name_(name),
name_(name),
capture_state_(Stopped),
cur_samplerate_(0),
data_saved_(true)
{
// Use this name also for the QObject instance
setObjectName(name_);
}
Session::~Session()
{
shutting_down_ = true;
// Stop and join to the thread
stop_capture();
for (SignalGroup* group : signal_groups_) {
group->clear();
delete group;
}
}
DeviceManager& Session::device_manager()
{
return device_manager_;
}
const DeviceManager& Session::device_manager() const
{
return device_manager_;
}
shared_ptr<sigrok::Session> Session::session() const
{
if (!device_)
return shared_ptr<sigrok::Session>();
return device_->session();
}
shared_ptr<devices::Device> Session::device() const
{
return device_;
}
QString Session::name() const
{
return name_;
}
void Session::set_name(QString name)
{
if (default_name_.isEmpty())
default_name_ = name;
name_ = name;
// Use this name also for the QObject instance
setObjectName(name_);
name_changed();
}
QString Session::save_path() const
{
return save_path_;
}
void Session::set_save_path(QString path)
{
save_path_ = path;
}
const vector< shared_ptr<views::ViewBase> > Session::views() const
{
return views_;
}
shared_ptr<views::ViewBase> Session::main_view() const
{
return main_view_;
}
void Session::set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar)
{
main_bar_ = main_bar;
}
shared_ptr<pv::toolbars::MainBar> Session::main_bar() const
{
return main_bar_;
}
bool Session::data_saved() const
{
return data_saved_;
}
void Session::save_setup(QSettings &settings) const
{
int i;
int decode_signal_count = 0;
int gen_signal_count = 0;
// Save channels and decoders
for (const shared_ptr<data::SignalBase>& base : signalbases_) {
#ifdef ENABLE_DECODE
if (base->is_decode_signal()) {
settings.beginGroup("decode_signal" + QString::number(decode_signal_count++));
base->save_settings(settings);
settings.endGroup();
} else
#endif
if (base->is_generated()) {
settings.beginGroup("generated_signal" + QString::number(gen_signal_count++));
settings.setValue("type", base->type());
base->save_settings(settings);
settings.endGroup();
} else {
settings.beginGroup(base->internal_name());
base->save_settings(settings);
settings.endGroup();
}
}
settings.setValue("decode_signals", decode_signal_count);
settings.setValue("generated_signals", gen_signal_count);
// Save view states and their signal settings
// Note: main_view must be saved as view0
i = 0;
settings.beginGroup("view" + QString::number(i++));
main_view_->save_settings(settings);
settings.endGroup();
for (const shared_ptr<views::ViewBase>& view : views_) {
if (view != main_view_) {
settings.beginGroup("view" + QString::number(i++));
settings.setValue("type", view->get_type());
view->save_settings(settings);
settings.endGroup();
}
}
settings.setValue("views", i);
int view_id = 0;
i = 0;
for (const shared_ptr<views::ViewBase>& vb : views_) {
shared_ptr<views::trace::View> tv = dynamic_pointer_cast<views::trace::View>(vb);
if (tv) {
for (const shared_ptr<views::trace::TimeItem>& time_item : tv->time_items()) {
const shared_ptr<views::trace::Flag> flag =
dynamic_pointer_cast<views::trace::Flag>(time_item);
if (flag) {
if (!flag->enabled())
continue;
settings.beginGroup("meta_obj" + QString::number(i++));
settings.setValue("type", "time_marker");
settings.setValue("assoc_view", view_id);
GlobalSettings::store_timestamp(settings, "time", flag->time());
settings.setValue("text", flag->get_text());
settings.endGroup();
}
}
if (tv->cursors_shown()) {
settings.beginGroup("meta_obj" + QString::number(i++));
settings.setValue("type", "selection");
settings.setValue("assoc_view", view_id);
const shared_ptr<views::trace::CursorPair> cp = tv->cursors();
GlobalSettings::store_timestamp(settings, "start_time", cp->first()->time());
GlobalSettings::store_timestamp(settings, "end_time", cp->second()->time());
settings.endGroup();
}
}
view_id++;
}
settings.setValue("meta_objs", i);
}
void Session::save_settings(QSettings &settings) const
{
map<string, string> dev_info;
list<string> key_list;
if (device_) {
shared_ptr<devices::HardwareDevice> hw_device =
dynamic_pointer_cast< devices::HardwareDevice >(device_);
if (hw_device) {
settings.setValue("device_type", "hardware");
settings.beginGroup("device");
key_list.emplace_back("vendor");
key_list.emplace_back("model");
key_list.emplace_back("version");
key_list.emplace_back("serial_num");
key_list.emplace_back("connection_id");
dev_info = device_manager_.get_device_info(device_);
for (string& key : key_list) {
if (dev_info.count(key))
settings.setValue(QString::fromUtf8(key.c_str()),
QString::fromUtf8(dev_info.at(key).c_str()));
else
settings.remove(QString::fromUtf8(key.c_str()));
}
settings.endGroup();
}
// Having saved the data to srzip overrides the current device. This is
// a crappy hack around the fact that saving e.g. an imported file to
// srzip would require changing the underlying libsigrok device
if (!save_path_.isEmpty()) {
QFileInfo fi = QFileInfo(QDir(save_path_), name_);
settings.setValue("device_type", "sessionfile");
settings.beginGroup("device");
settings.setValue("filename", fi.absoluteFilePath());
settings.endGroup();
} else {
shared_ptr<devices::SessionFile> sessionfile_device =
dynamic_pointer_cast<devices::SessionFile>(device_);
if (sessionfile_device) {
settings.setValue("device_type", "sessionfile");
settings.beginGroup("device");
settings.setValue("filename", QString::fromStdString(
sessionfile_device->full_name()));
settings.endGroup();
}
shared_ptr<devices::InputFile> inputfile_device =
dynamic_pointer_cast<devices::InputFile>(device_);
if (inputfile_device) {
settings.setValue("device_type", "inputfile");
settings.beginGroup("device");
inputfile_device->save_meta_to_settings(settings);
settings.endGroup();
}
}
save_setup(settings);
}
}
void Session::restore_setup(QSettings &settings)
{
// Restore channels
for (shared_ptr<data::SignalBase> base : signalbases_) {
settings.beginGroup(base->internal_name());
base->restore_settings(settings);
settings.endGroup();
}
// Restore generated signals
int gen_signal_count = settings.value("generated_signals").toInt();
for (int i = 0; i < gen_signal_count; i++) {
settings.beginGroup("generated_signal" + QString::number(i));
SignalBase::ChannelType type = (SignalBase::ChannelType)settings.value("type").toInt();
shared_ptr<data::SignalBase> signal;
if (type == SignalBase::MathChannel)
signal = make_shared<data::MathSignal>(*this);
else
qWarning() << tr("Can't restore generated signal of unknown type %1 (%2)") \
.arg((int)type) \
.arg(settings.value("name").toString());
if (signal) {
add_generated_signal(signal);
signal->restore_settings(settings);
}
settings.endGroup();
}
// Restore decoders
#ifdef ENABLE_DECODE
int decode_signal_count = settings.value("decode_signals").toInt();
for (int i = 0; i < decode_signal_count; i++) {
settings.beginGroup("decode_signal" + QString::number(i));
shared_ptr<data::DecodeSignal> signal = add_decode_signal();
signal->restore_settings(settings);
settings.endGroup();
}
#endif
// Restore views
int views = settings.value("views").toInt();
for (int i = 0; i < views; i++) {
settings.beginGroup("view" + QString::number(i));
if (i > 0) {
views::ViewType type = (views::ViewType)settings.value("type").toInt();
add_view(type, this);
views_.back()->restore_settings(settings);
} else
main_view_->restore_settings(settings);
settings.endGroup();
}
// Restore meta objects like markers and cursors
int meta_objs = settings.value("meta_objs").toInt();
for (int i = 0; i < meta_objs; i++) {
settings.beginGroup("meta_obj" + QString::number(i));
shared_ptr<views::ViewBase> vb;
shared_ptr<views::trace::View> tv;
if (settings.contains("assoc_view"))
vb = views_.at(settings.value("assoc_view").toInt());
if (vb)
tv = dynamic_pointer_cast<views::trace::View>(vb);
const QString type = settings.value("type").toString();
if ((type == "time_marker") && tv) {
Timestamp ts = GlobalSettings::restore_timestamp(settings, "time");
shared_ptr<views::trace::Flag> flag = tv->add_flag(ts);
flag->set_text(settings.value("text").toString());
}
if ((type == "selection") && tv) {
Timestamp start = GlobalSettings::restore_timestamp(settings, "start_time");
Timestamp end = GlobalSettings::restore_timestamp(settings, "end_time");
tv->set_cursors(start, end);
tv->show_cursors();
}
settings.endGroup();
}
}
void Session::restore_settings(QSettings &settings)
{
shared_ptr<devices::Device> device;
const QString device_type = settings.value("device_type").toString();
if (device_type == "hardware") {
map<string, string> dev_info;
list<string> key_list;
// Re-select last used device if possible but only if it's not demo
settings.beginGroup("device");
key_list.emplace_back("vendor");
key_list.emplace_back("model");
key_list.emplace_back("version");
key_list.emplace_back("serial_num");
key_list.emplace_back("connection_id");
for (string key : key_list) {
const QString k = QString::fromStdString(key);
if (!settings.contains(k))
continue;
const string value = settings.value(k).toString().toStdString();
if (!value.empty())
dev_info.insert(make_pair(key, value));
}
if (dev_info.count("model") > 0)
device = device_manager_.find_device_from_info(dev_info);
if (device)
set_device(device);
settings.endGroup();
if (device)
restore_setup(settings);
}
QString filename;
if ((device_type == "sessionfile") || (device_type == "inputfile")) {
if (device_type == "sessionfile") {
settings.beginGroup("device");
filename = settings.value("filename").toString();
settings.endGroup();
if (QFileInfo(filename).isReadable())
device = make_shared<devices::SessionFile>(device_manager_.context(),
filename.toStdString());
}
if (device_type == "inputfile") {
settings.beginGroup("device");
device = make_shared<devices::InputFile>(device_manager_.context(),
settings);
settings.endGroup();
}
if (device) {
set_device(device);
restore_setup(settings);
start_capture([](QString infoMessage) {
// TODO Emulate noquote()
qDebug() << "Session error:" << infoMessage; });
set_name(QString::fromStdString(
dynamic_pointer_cast<devices::File>(device)->display_name(device_manager_)));
if (!filename.isEmpty()) {
// Only set the save path if we load an srzip file
if (device_type == "sessionfile")
set_save_path(QFileInfo(filename).absolutePath());
set_name(QFileInfo(filename).fileName());
}
}
}
}
void Session::select_device(shared_ptr<devices::Device> device)
{
try {
if (device)
set_device(device);
else
set_default_device();
} catch (const QString &e) {
MainWindow::show_session_error(tr("Failed to select device"), e);
}
}
void Session::set_device(shared_ptr<devices::Device> device)
{
assert(device);
// Ensure we are not capturing before setting the device
stop_capture();
if (device_)
device_->close();
device_.reset();
// Revert name back to default name (e.g. "Session 1") as the data is gone
name_ = default_name_;
name_changed();
// Remove all stored data and reset all views
for (shared_ptr<views::ViewBase> view : views_) {
view->clear_signalbases();
#ifdef ENABLE_DECODE
view->clear_decode_signals();
#endif
view->reset_view_state();
}
for (SignalGroup* group : signal_groups_) {
group->clear();
delete group;
}
signal_groups_.clear();
for (const shared_ptr<data::SignalData>& d : all_signal_data_)
d->clear();
all_signal_data_.clear();
signalbases_.clear();
cur_logic_segment_.reset();
for (auto& entry : cur_analog_segments_) {
shared_ptr<sigrok::Channel>(entry.first).reset();
shared_ptr<data::AnalogSegment>(entry.second).reset();
}
logic_data_.reset();
signals_changed();
device_ = std::move(device);
try {
device_->open();
} catch (const QString &e) {
device_.reset();
MainWindow::show_session_error(tr("Failed to open device"), e);
} catch (const sigrok::Error &e) {
device_.reset();
MainWindow::show_session_error(tr("Failed to open device"), QString(e.what()));
}
if (device_) {
device_->session()->add_datafeed_callback([=]
(shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
data_feed_in(device, packet);
});
update_signals();
}
device_changed();
}
void Session::set_default_device()
{
const list< shared_ptr<devices::HardwareDevice> > &devices =
device_manager_.devices();
if (devices.empty())
return;
// Try and find the demo device and select that by default
const auto iter = find_if(devices.begin(), devices.end(),
[] (const shared_ptr<devices::HardwareDevice> &d) {
return d->hardware_device()->driver()->name() == "demo"; });
set_device((iter == devices.end()) ? devices.front() : *iter);
}
bool Session::using_file_device() const
{
shared_ptr<devices::SessionFile> sessionfile_device =
dynamic_pointer_cast<devices::SessionFile>(device_);
shared_ptr<devices::InputFile> inputfile_device =
dynamic_pointer_cast<devices::InputFile>(device_);
return (sessionfile_device || inputfile_device);
}
/**
* Convert generic options to data types that are specific to InputFormat.
*
* @param[in] user_spec Vector of tokenized words, string format.
* @param[in] fmt_opts Input format's options, result of InputFormat::options().
*
* @return Map of options suitable for InputFormat::create_input().
*/
map<string, Glib::VariantBase>
Session::input_format_options(vector<string> user_spec,
map<string, shared_ptr<Option>> fmt_opts)
{
map<string, Glib::VariantBase> result;
for (auto& entry : user_spec) {
/*
* Split key=value specs. Accept entries without separator
* (for simplified boolean specifications).
*/
string key, val;
size_t pos = entry.find("=");
if (pos == std::string::npos) {
key = entry;
val = "";
} else {
key = entry.substr(0, pos);
val = entry.substr(pos + 1);
}
/*
* Skip user specifications that are not a member of the
* format's set of supported options. Have the text input
* spec converted to the required input format specific
* data type.
*/
auto found = fmt_opts.find(key);
if (found == fmt_opts.end()) {
qCritical() << "Supplied input option" << QString::fromStdString(key) <<
"is not a valid option for this input module, it will be ignored!";
continue;
}
shared_ptr<Option> opt = found->second;
result[key] = opt->parse_string(val);
}
return result;
}
void Session::load_init_file(const string &file_name,
const string &format, const string &setup_file_name)
{
shared_ptr<InputFormat> input_format;
map<string, Glib::VariantBase> input_opts;
if (!format.empty()) {
const map<string, shared_ptr<InputFormat> > formats =
device_manager_.context()->input_formats();
auto user_opts = pv::util::split_string(format, ":");
string user_name = user_opts.front();
user_opts.erase(user_opts.begin());
const auto iter = find_if(formats.begin(), formats.end(),
[&](const pair<string, shared_ptr<InputFormat> > f) {
return f.first == user_name; });
if (iter == formats.end()) {
MainWindow::show_session_error(tr("Error"),
tr("Unexpected input format: %1").arg(QString::fromStdString(format)));
return;
}
input_format = (*iter).second;
input_opts = input_format_options(user_opts,
input_format->options());
}
load_file(QString::fromStdString(file_name), QString::fromStdString(setup_file_name),
input_format, input_opts);
}
void Session::load_file(QString file_name, QString setup_file_name,
shared_ptr<sigrok::InputFormat> format, const map<string, Glib::VariantBase> &options)
{
const QString errorMessage(
QString("Failed to load file %1").arg(file_name));
// In the absence of a caller's format spec, try to auto detect.
// Assume "sigrok session file" upon lookup miss.
if (!format)
format = device_manager_.context()->input_format_match(file_name.toStdString());
try {
if (format)
set_device(shared_ptr<devices::Device>(
new devices::InputFile(
device_manager_.context(),
file_name.toStdString(),
format, options)));
else
set_device(shared_ptr<devices::Device>(
new devices::SessionFile(
device_manager_.context(),
file_name.toStdString())));
} catch (Error& e) {
MainWindow::show_session_error(tr("Failed to load %1").arg(file_name), e.what());
return;
}
if (!device_) {
MainWindow::show_session_error(errorMessage, "");
return;
}
// Use the input file with .pvs extension if no setup file was given
if (setup_file_name.isEmpty()) {
setup_file_name = file_name;
setup_file_name.truncate(setup_file_name.lastIndexOf('.'));
setup_file_name.append(".pvs");
}
if (QFileInfo::exists(setup_file_name) && QFileInfo(setup_file_name).isReadable()) {
QSettings settings_storage(setup_file_name, QSettings::IniFormat);
restore_setup(settings_storage);
}
main_bar_->update_device_list();
start_capture([&, errorMessage](QString infoMessage) {
Q_EMIT session_error_raised(errorMessage, infoMessage); });
// Only set save path if we loaded an srzip file
if (dynamic_pointer_cast<devices::SessionFile>(device_))
set_save_path(QFileInfo(file_name).absolutePath());
set_name(QFileInfo(file_name).fileName());
}
Session::capture_state Session::get_capture_state() const
{
lock_guard<mutex> lock(sampling_mutex_);
return capture_state_;
}
void Session::start_capture(function<void (const QString)> error_handler)
{
if (!device_) {
error_handler(tr("No active device set, can't start acquisition."));
return;
}
stop_capture();
// Check that at least one channel is enabled
const shared_ptr<sigrok::Device> sr_dev = device_->device();
if (sr_dev) {
const auto channels = sr_dev->channels();
if (!any_of(channels.begin(), channels.end(),
[](shared_ptr<Channel> channel) {
return channel->enabled(); })) {
error_handler(tr("No channels enabled."));
return;
}
}
// Clear signal data
for (const shared_ptr<data::SignalData>& d : all_signal_data_)
d->clear();
trigger_list_.clear();
segment_sample_count_.clear();
// Revert name back to default name (e.g. "Session 1") for real devices
// as the (possibly saved) data is gone. File devices keep their name.
shared_ptr<devices::HardwareDevice> hw_device =
dynamic_pointer_cast< devices::HardwareDevice >(device_);
if (hw_device) {
name_ = default_name_;
name_changed();
}
acq_start_time_ = Glib::DateTime::create_now_local();
// Begin the session
sampling_thread_ = std::thread(&Session::sample_thread_proc, this, error_handler);
}
void Session::stop_capture()
{
if (get_capture_state() != Stopped)
device_->stop();
// Check that sampling stopped
if (sampling_thread_.joinable())
sampling_thread_.join();
}
void Session::register_view(shared_ptr<views::ViewBase> view)
{
if (views_.empty())
main_view_ = view;
views_.push_back(view);
// Add all device signals
update_signals();
// Add all other signals
vector< shared_ptr<data::SignalBase> > view_signalbases = view->signalbases();
for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) {
const int sb_exists = count_if(
view_signalbases.cbegin(), view_signalbases.cend(),
[&](const shared_ptr<data::SignalBase> &sb) {
return sb == signalbase;
});
// Add the signal to the view if it doesn't have it yet
if (!sb_exists)
switch (signalbase->type()) {
case data::SignalBase::AnalogChannel:
case data::SignalBase::LogicChannel:
case data::SignalBase::MathChannel:
view->add_signalbase(signalbase);
break;
case data::SignalBase::DecodeChannel:
#ifdef ENABLE_DECODE
view->add_decode_signal(dynamic_pointer_cast<data::DecodeSignal>(signalbase));
#endif
break;
}
}
signals_changed();
}
void Session::deregister_view(shared_ptr<views::ViewBase> view)
{
views_.erase(std::remove_if(views_.begin(), views_.end(),
[&](shared_ptr<views::ViewBase> v) { return v == view; }),
views_.end());
if (views_.empty()) {
main_view_.reset();
// Without a view there can be no main bar
main_bar_.reset();
}
}
bool Session::has_view(shared_ptr<views::ViewBase> view)
{
for (shared_ptr<views::ViewBase>& v : views_)
if (v == view)
return true;
return false;
}
double Session::get_samplerate() const
{
double samplerate = 0.0;
for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) {
assert(d);
const vector< shared_ptr<pv::data::Segment> > segments =
d->segments();
for (const shared_ptr<pv::data::Segment>& s : segments)
samplerate = max(samplerate, s->samplerate());
}
// If there is no sample rate given we use samples as unit
if (samplerate == 0.0)
samplerate = 1.0;
return samplerate;
}
Glib::DateTime Session::get_acquisition_start_time() const
{
return acq_start_time_;
}
uint32_t Session::get_highest_segment_id() const
{
return highest_segment_id_;
}
uint64_t Session::get_segment_sample_count(uint32_t segment_id) const
{
if (segment_id < segment_sample_count_.size())
return segment_sample_count_[segment_id];
else
return 0;
}
vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const
{
vector<util::Timestamp> result;
for (const pair<uint32_t, util::Timestamp>& entry : trigger_list_)
if (entry.first == segment_id)
result.push_back(entry.second);
return result;
}
const vector< shared_ptr<data::SignalBase> > Session::signalbases() const
{
return signalbases_;
}
uint32_t Session::get_signal_count(data::SignalBase::ChannelType type) const
{
return count_if(signalbases_.begin(), signalbases_.end(),
[&] (shared_ptr<SignalBase> sb) { return sb->type() == type; });
}
uint32_t Session::get_next_signal_index(data::SignalBase::ChannelType type)
{
next_index_list_[type]++;
return next_index_list_[type];
}
void Session::add_generated_signal(shared_ptr<data::SignalBase> signal)
{
signalbases_.push_back(signal);
for (shared_ptr<views::ViewBase>& view : views_)
view->add_signalbase(signal);
update_signals();
}
void Session::remove_generated_signal(shared_ptr<data::SignalBase> signal)
{
if (shutting_down_)
return;
signalbases_.erase(std::remove_if(signalbases_.begin(), signalbases_.end(),
[&](shared_ptr<data::SignalBase> s) { return s == signal; }),
signalbases_.end());
for (shared_ptr<views::ViewBase>& view : views_)
view->remove_signalbase(signal);