]> sigrok.org Git - pulseview.git/blame - pv/session.cpp
Implement multi-session handling
[pulseview.git] / pv / session.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
35750e4d
UH
21#ifdef _WIN32
22// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
23#define NOGDI
24#define NORESOURCE
25#endif
e71eb81c
JH
26#include <boost/thread/locks.hpp>
27#include <boost/thread/shared_mutex.hpp>
28
269528f5 29#ifdef ENABLE_DECODE
4e5a4405 30#include <libsigrokdecode/libsigrokdecode.h>
269528f5 31#endif
4e5a4405 32
f65cd27b 33#include "session.hpp"
2953961c 34
2acdb232 35#include "devicemanager.hpp"
119aff65 36
2acdb232 37#include "data/analog.hpp"
f3d66e52 38#include "data/analogsegment.hpp"
2acdb232
JH
39#include "data/decoderstack.hpp"
40#include "data/logic.hpp"
f3d66e52 41#include "data/logicsegment.hpp"
bf0edd2b 42#include "data/signalbase.hpp"
2acdb232 43#include "data/decode/decoder.hpp"
82c7f640 44
da30ecb7
JH
45#include "devices/hardwaredevice.hpp"
46#include "devices/sessionfile.hpp"
47
0f8f8c18
SA
48#include "toolbars/mainbar.hpp"
49
2acdb232
JH
50#include "view/analogsignal.hpp"
51#include "view/decodetrace.hpp"
52#include "view/logicsignal.hpp"
47e9e7bb
SA
53#include "view/signal.hpp"
54#include "view/view.hpp"
28a4c9c5 55
3b68d03d
JH
56#include <cassert>
57#include <mutex>
e92cd4e4
JH
58#include <stdexcept>
59
728e5ef7
JH
60#include <sys/stat.h>
61
79efbc53
JH
62#include <QDebug>
63
fe3a1c21 64#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 65
aca64cac
JH
66using boost::shared_lock;
67using boost::shared_mutex;
68using boost::unique_lock;
69
f9abf97e 70using std::dynamic_pointer_cast;
d2344534 71using std::function;
3b68d03d 72using std::lock_guard;
d873f4d6 73using std::list;
819f4c25 74using std::map;
aca64cac 75using std::mutex;
8524a597 76using std::recursive_mutex;
02412f0b 77using std::set;
f9abf97e 78using std::shared_ptr;
819f4c25 79using std::string;
78b0af3e 80using std::unordered_set;
819f4c25 81using std::vector;
28a4c9c5 82
e8d00928
ML
83using sigrok::Analog;
84using sigrok::Channel;
85using sigrok::ChannelType;
86using sigrok::ConfigKey;
87using sigrok::DatafeedCallbackFunction;
e8d00928 88using sigrok::Error;
e8d00928
ML
89using sigrok::Header;
90using sigrok::Logic;
91using sigrok::Meta;
92using sigrok::Packet;
93using sigrok::PacketPayload;
94using sigrok::Session;
95using sigrok::SessionDevice;
96
97using Glib::VariantBase;
98using Glib::Variant;
51e77110 99
e8d00928 100namespace pv {
101e7a9b 101Session::Session(DeviceManager &device_manager, QString name) :
8dbbc7f0 102 device_manager_(device_manager),
101e7a9b 103 name_(name),
ff008de6
JH
104 capture_state_(Stopped),
105 cur_samplerate_(0)
2953961c 106{
2953961c
JH
107}
108
2b81ae46 109Session::~Session()
2953961c 110{
04463625 111 // Stop and join to the thread
5b7cf66c 112 stop_capture();
2953961c
JH
113}
114
2b81ae46 115DeviceManager& Session::device_manager()
4cb0b033 116{
8dbbc7f0 117 return device_manager_;
4cb0b033
JH
118}
119
2b81ae46 120const DeviceManager& Session::device_manager() const
4cb0b033 121{
8dbbc7f0 122 return device_manager_;
4cb0b033
JH
123}
124
da30ecb7 125shared_ptr<sigrok::Session> Session::session() const
1f4caa77 126{
da30ecb7
JH
127 if (!device_)
128 return shared_ptr<sigrok::Session>();
129 return device_->session();
1f4caa77
JH
130}
131
da30ecb7 132shared_ptr<devices::Device> Session::device() const
dc0867ff 133{
8dbbc7f0 134 return device_;
dc0867ff
JH
135}
136
101e7a9b
SA
137QString Session::name() const
138{
139 return name_;
140}
141
142void Session::set_name(QString name)
143{
144 if (default_name_.isEmpty())
145 default_name_ = name;
146
147 name_ = name;
148
149 name_changed();
150}
151
0f8f8c18
SA
152std::shared_ptr<pv::view::View> Session::main_view() const
153{
154 return main_view_;
155}
156
157void Session::set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar)
158{
159 main_bar_ = main_bar;
160}
161
162shared_ptr<pv::toolbars::MainBar> Session::main_bar() const
163{
164 return main_bar_;
165}
166
101e7a9b
SA
167void Session::save_settings(QSettings &settings) const
168{
169 map<string, string> dev_info;
170 list<string> key_list;
171
172 if (device_) {
173 settings.beginGroup("Device");
174 key_list.push_back("vendor");
175 key_list.push_back("model");
176 key_list.push_back("version");
177 key_list.push_back("serial_num");
178 key_list.push_back("connection_id");
179
180 dev_info = device_manager_.get_device_info(device_);
181
182 for (string key : key_list) {
183 if (dev_info.count(key))
184 settings.setValue(QString::fromUtf8(key.c_str()),
185 QString::fromUtf8(dev_info.at(key).c_str()));
186 else
187 settings.remove(QString::fromUtf8(key.c_str()));
188 }
189
190 // TODO Save channel settings and decoders
191
192 settings.endGroup();
193 }
194}
195
196void Session::restore_settings(QSettings &settings)
197{
198 map<string, string> dev_info;
199 list<string> key_list;
200 shared_ptr<devices::HardwareDevice> device;
201
202 // Re-select last used device if possible but only if it's not demo
203 settings.beginGroup("Device");
204 key_list.push_back("vendor");
205 key_list.push_back("model");
206 key_list.push_back("version");
207 key_list.push_back("serial_num");
208 key_list.push_back("connection_id");
209
210 for (string key : key_list) {
211 const QString k = QString::fromStdString(key);
212 if (!settings.contains(k))
213 continue;
214
215 const string value = settings.value(k).toString().toStdString();
216 if (!value.empty())
217 dev_info.insert(std::make_pair(key, value));
218 }
219
220 if (dev_info.count("model") > 0)
221 device = device_manager_.find_device_from_info(dev_info);
222
223 if (device) {
224 set_device(device);
225
226 // TODO Restore channel settings and decoders
227 }
228
229 settings.endGroup();
230}
231
da30ecb7 232void Session::set_device(shared_ptr<devices::Device> device)
d64d1596 233{
da30ecb7
JH
234 assert(device);
235
82596f46
JH
236 // Ensure we are not capturing before setting the device
237 stop_capture();
238
4d6c6ea3
SA
239 if (device_)
240 device_->close();
241
86123e2e
SA
242 device_.reset();
243
101e7a9b
SA
244 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
245 name_ = default_name_;
246 name_changed();
247
cc9a7898 248 // Remove all stored data
bb7dd726 249 for (std::shared_ptr<pv::view::View> view : views_) {
47e9e7bb 250 view->clear_signals();
bb7dd726
SA
251#ifdef ENABLE_DECODE
252 view->clear_decode_traces();
253#endif
254 }
47e9e7bb
SA
255 for (const shared_ptr<data::SignalData> d : all_signal_data_)
256 d->clear();
cc9a7898 257 all_signal_data_.clear();
47e9e7bb 258 signalbases_.clear();
4b0d7046
SA
259 cur_logic_segment_.reset();
260
261 for (auto entry : cur_analog_segments_) {
262 shared_ptr<sigrok::Channel>(entry.first).reset();
263 shared_ptr<data::AnalogSegment>(entry.second).reset();
264 }
265
266 logic_data_.reset();
4b0d7046 267
86123e2e
SA
268 signals_changed();
269
da30ecb7 270 device_ = std::move(device);
7e0c99bf
SA
271
272 try {
273 device_->open();
274 } catch (const QString &e) {
275 device_.reset();
276 device_selected();
277 throw;
278 }
279
da30ecb7
JH
280 device_->session()->add_datafeed_callback([=]
281 (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
282 data_feed_in(device, packet);
283 });
ae2d1bc5 284
4b8e787d 285 update_signals();
20f81a58 286 device_selected();
ae2d1bc5 287}
85843b14 288
2b81ae46 289void Session::set_default_device()
6fd0b639 290{
da30ecb7 291 const list< shared_ptr<devices::HardwareDevice> > &devices =
8dbbc7f0 292 device_manager_.devices();
6fd0b639 293
da30ecb7
JH
294 if (devices.empty())
295 return;
dc0867ff 296
da30ecb7
JH
297 // Try and find the demo device and select that by default
298 const auto iter = std::find_if(devices.begin(), devices.end(),
299 [] (const shared_ptr<devices::HardwareDevice> &d) {
300 return d->hardware_device()->driver()->name() ==
301 "demo"; });
302 set_device((iter == devices.end()) ? devices.front() : *iter);
dc0867ff
JH
303}
304
2b81ae46 305Session::capture_state Session::get_capture_state() const
5b7cf66c 306{
8dbbc7f0
JH
307 lock_guard<mutex> lock(sampling_mutex_);
308 return capture_state_;
5b7cf66c
JH
309}
310
2b81ae46 311void Session::start_capture(function<void (const QString)> error_handler)
2e2946fe 312{
34c11fa7
SA
313 if (!device_) {
314 error_handler(tr("No active device set, can't start acquisition."));
315 return;
316 }
317
5b7cf66c
JH
318 stop_capture();
319
6ac6242b 320 // Check that at least one channel is enabled
e6c1382e
JH
321 const shared_ptr<sigrok::Device> sr_dev = device_->device();
322 if (sr_dev) {
323 const auto channels = sr_dev->channels();
324 if (!std::any_of(channels.begin(), channels.end(),
325 [](shared_ptr<Channel> channel) {
326 return channel->enabled(); })) {
327 error_handler(tr("No channels enabled."));
328 return;
329 }
9c48fa57
JH
330 }
331
53ea4c6c 332 // Clear signal data
47e9e7bb
SA
333 for (const shared_ptr<data::SignalData> d : all_signal_data_)
334 d->clear();
53ea4c6c 335
101e7a9b
SA
336 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
337 name_ = default_name_;
338 name_changed();
339
9c48fa57 340 // Begin the session
8dbbc7f0 341 sampling_thread_ = std::thread(
2b05d311 342 &Session::sample_thread_proc, this, error_handler);
2e2946fe
JH
343}
344
2b81ae46 345void Session::stop_capture()
5b7cf66c 346{
04463625 347 if (get_capture_state() != Stopped)
da30ecb7 348 device_->stop();
5b7cf66c
JH
349
350 // Check that sampling stopped
8dbbc7f0
JH
351 if (sampling_thread_.joinable())
352 sampling_thread_.join();
5b7cf66c
JH
353}
354
47e9e7bb
SA
355void Session::register_view(std::shared_ptr<pv::view::View> view)
356{
0f8f8c18
SA
357 if (views_.empty()) {
358 main_view_ = view;
359 }
360
47e9e7bb
SA
361 views_.insert(view);
362}
363
364void Session::deregister_view(std::shared_ptr<pv::view::View> view)
365{
366 views_.erase(view);
0f8f8c18
SA
367
368 if (views_.empty()) {
369 main_view_.reset();
370
371 // Without a view there can be no main bar
372 main_bar_.reset();
373 }
47e9e7bb
SA
374}
375
101e7a9b
SA
376bool Session::has_view(std::shared_ptr<pv::view::View> view)
377{
378 return views_.find(view) != views_.end();
379}
380
2220e942
SA
381double Session::get_samplerate() const
382{
383 double samplerate = 0.0;
384
47e9e7bb
SA
385 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
386 assert(d);
387 const vector< shared_ptr<pv::data::Segment> > segments =
388 d->segments();
389 for (const shared_ptr<pv::data::Segment> &s : segments)
390 samplerate = std::max(samplerate, s->samplerate());
2220e942 391 }
2220e942
SA
392 // If there is no sample rate given we use samples as unit
393 if (samplerate == 0.0)
394 samplerate = 1.0;
395
396 return samplerate;
397}
398
47e9e7bb
SA
399const std::unordered_set< std::shared_ptr<data::SignalBase> >
400 Session::signalbases() const
2e2946fe 401{
47e9e7bb 402 return signalbases_;
2e2946fe
JH
403}
404
269528f5 405#ifdef ENABLE_DECODE
2b81ae46 406bool Session::add_decoder(srd_decoder *const dec)
82c7f640 407{
04394ded 408 map<const srd_channel*, shared_ptr<data::SignalBase> > channels;
7491a29f 409 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 410
2ad82c2e 411 try {
4e5a4405 412 // Create the decoder
7491a29f 413 decoder_stack = shared_ptr<data::DecoderStack>(
bdc5c3b0 414 new data::DecoderStack(*this, dec));
4e5a4405 415
6ac6242b
ML
416 // Make a list of all the channels
417 std::vector<const srd_channel*> all_channels;
f3290553 418 for (const GSList *i = dec->channels; i; i = i->next)
6ac6242b 419 all_channels.push_back((const srd_channel*)i->data);
f3290553 420 for (const GSList *i = dec->opt_channels; i; i = i->next)
6ac6242b 421 all_channels.push_back((const srd_channel*)i->data);
d2f46d27 422
6ac6242b
ML
423 // Auto select the initial channels
424 for (const srd_channel *pdch : all_channels)
04394ded
SA
425 for (shared_ptr<data::SignalBase> b : signalbases_) {
426 if (b->type() == ChannelType::LOGIC) {
427 if (QString::fromUtf8(pdch->name).toLower().
428 contains(b->name().toLower()))
429 channels[pdch] = b;
430 }
4e5a4405 431 }
4e5a4405 432
7491a29f
JH
433 assert(decoder_stack);
434 assert(!decoder_stack->stack().empty());
435 assert(decoder_stack->stack().front());
6ac6242b 436 decoder_stack->stack().front()->set_channels(channels);
4e5a4405
JH
437
438 // Create the decode signal
bf0edd2b
SA
439 shared_ptr<data::SignalBase> signalbase =
440 shared_ptr<data::SignalBase>(new data::SignalBase(nullptr));
441
bb7dd726
SA
442 signalbase->set_decoder_stack(decoder_stack);
443
444 for (std::shared_ptr<pv::view::View> view : views_)
445 view->add_decode_trace(signalbase);
2ad82c2e 446 } catch (std::runtime_error e) {
e92cd4e4
JH
447 return false;
448 }
449
82c7f640 450 signals_changed();
e92cd4e4 451
7491a29f
JH
452 // Do an initial decode
453 decoder_stack->begin_decode();
454
e92cd4e4 455 return true;
82c7f640
JH
456}
457
bb7dd726 458void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase)
38eeddea 459{
bb7dd726
SA
460 for (std::shared_ptr<pv::view::View> view : views_)
461 view->remove_decode_trace(signalbase);
c51482b3 462}
269528f5 463#endif
c51482b3 464
2b81ae46 465void Session::set_capture_state(capture_state state)
6ac96c2e 466{
896936e5
TS
467 bool changed;
468
469 {
470 lock_guard<mutex> lock(sampling_mutex_);
471 changed = capture_state_ != state;
472 capture_state_ = state;
473 }
474
f3290553 475 if (changed)
2b49eeb0 476 capture_state_changed(state);
6ac96c2e
JH
477}
478
ffe00119 479void Session::update_signals()
b087ba7f 480{
076eefa4 481 if (!device_) {
47e9e7bb 482 signalbases_.clear();
076eefa4 483 logic_data_.reset();
bb7dd726 484 for (std::shared_ptr<pv::view::View> view : views_) {
47e9e7bb 485 view->clear_signals();
bb7dd726
SA
486#ifdef ENABLE_DECODE
487 view->clear_decode_traces();
488#endif
489 }
076eefa4
SA
490 return;
491 }
e6c1382e
JH
492
493 lock_guard<recursive_mutex> lock(data_mutex_);
b087ba7f 494
ffe00119 495 const shared_ptr<sigrok::Device> sr_dev = device_->device();
c6412b47 496 if (!sr_dev) {
47e9e7bb 497 signalbases_.clear();
c6412b47 498 logic_data_.reset();
bb7dd726 499 for (std::shared_ptr<pv::view::View> view : views_) {
47e9e7bb 500 view->clear_signals();
bb7dd726
SA
501#ifdef ENABLE_DECODE
502 view->clear_decode_traces();
503#endif
504 }
c6412b47
JH
505 return;
506 }
507
b087ba7f 508 // Detect what data types we will receive
c6412b47 509 auto channels = sr_dev->channels();
e8d00928
ML
510 unsigned int logic_channel_count = std::count_if(
511 channels.begin(), channels.end(),
512 [] (shared_ptr<Channel> channel) {
513 return channel->type() == ChannelType::LOGIC; });
b087ba7f 514
f3d66e52 515 // Create data containers for the logic data segments
b087ba7f 516 {
8524a597 517 lock_guard<recursive_mutex> data_lock(data_mutex_);
b087ba7f 518
3917ba77
JH
519 if (logic_channel_count == 0) {
520 logic_data_.reset();
521 } else if (!logic_data_ ||
522 logic_data_->num_channels() != logic_channel_count) {
8dbbc7f0 523 logic_data_.reset(new data::Logic(
6ac6242b 524 logic_channel_count));
8dbbc7f0 525 assert(logic_data_);
b087ba7f 526 }
b087ba7f
JH
527 }
528
47e9e7bb
SA
529 // Make the signals list
530 for (std::shared_ptr<pv::view::View> view : views_) {
531 unordered_set< shared_ptr<view::Signal> > prev_sigs(view->signals());
532 view->clear_signals();
b087ba7f 533
c6412b47 534 for (auto channel : sr_dev->channels()) {
bf0edd2b 535 shared_ptr<data::SignalBase> signalbase;
39e680c2 536 shared_ptr<view::Signal> signal;
39e680c2 537
3917ba77
JH
538 // Find the channel in the old signals
539 const auto iter = std::find_if(
540 prev_sigs.cbegin(), prev_sigs.cend(),
541 [&](const shared_ptr<view::Signal> &s) {
73a25a6e 542 return s->base()->channel() == channel;
3917ba77
JH
543 });
544 if (iter != prev_sigs.end()) {
545 // Copy the signal from the old set to the new
546 signal = *iter;
3917ba77 547 } else {
47e9e7bb
SA
548 // Find the signalbase for this channel if possible
549 signalbase.reset();
550 for (const shared_ptr<data::SignalBase> b : signalbases_)
551 if (b->channel() == channel)
552 signalbase = b;
bf0edd2b 553
3917ba77
JH
554 switch(channel->type()->id()) {
555 case SR_CHANNEL_LOGIC:
47e9e7bb
SA
556 if (!signalbase) {
557 signalbase = shared_ptr<data::SignalBase>(
558 new data::SignalBase(channel));
559 signalbases_.insert(signalbase);
560
561 all_signal_data_.insert(logic_data_);
562 signalbase->set_data(logic_data_);
563 }
564
3917ba77
JH
565 signal = shared_ptr<view::Signal>(
566 new view::LogicSignal(*this,
0aa57689 567 device_, signalbase));
47e9e7bb 568 view->add_signal(signal);
3917ba77
JH
569 break;
570
571 case SR_CHANNEL_ANALOG:
572 {
47e9e7bb
SA
573 if (!signalbase) {
574 signalbase = shared_ptr<data::SignalBase>(
575 new data::SignalBase(channel));
576 signalbases_.insert(signalbase);
577
578 shared_ptr<data::Analog> data(new data::Analog());
579 all_signal_data_.insert(data);
580 signalbase->set_data(data);
581 }
582
3917ba77
JH
583 signal = shared_ptr<view::Signal>(
584 new view::AnalogSignal(
cbd2a2de 585 *this, signalbase));
47e9e7bb 586 view->add_signal(signal);
3917ba77
JH
587 break;
588 }
589
590 default:
591 assert(0);
592 break;
593 }
b087ba7f
JH
594 }
595 }
fbd3e234 596 }
b087ba7f
JH
597
598 signals_changed();
599}
600
cbd2a2de 601shared_ptr<data::SignalBase> Session::signalbase_from_channel(
bf0edd2b 602 shared_ptr<sigrok::Channel> channel) const
3ddcc083 603{
bf0edd2b 604 for (shared_ptr<data::SignalBase> sig : signalbases_) {
3ddcc083 605 assert(sig);
6ac6242b 606 if (sig->channel() == channel)
3ddcc083
JH
607 return sig;
608 }
bf0edd2b 609 return shared_ptr<data::SignalBase>();
3ddcc083
JH
610}
611
2b05d311 612void Session::sample_thread_proc(function<void (const QString)> error_handler)
274d4f13 613{
f2edb557 614 assert(error_handler);
be73bdfa 615
34c11fa7
SA
616 if (!device_)
617 return;
45f0c7c9 618
b48daed6 619 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
274d4f13 620
e7216ae0
SA
621 out_of_memory_ = false;
622
ae2d1bc5 623 try {
5237f0c5 624 device_->start();
2ad82c2e 625 } catch (Error e) {
e8d00928 626 error_handler(e.what());
eec446e1
JH
627 return;
628 }
629
da30ecb7 630 set_capture_state(device_->session()->trigger() ?
07dcf561 631 AwaitingTrigger : Running);
eec446e1 632
da30ecb7 633 device_->run();
eec446e1 634 set_capture_state(Stopped);
1a2fe44c
JH
635
636 // Confirm that SR_DF_END was received
2ad82c2e 637 if (cur_logic_segment_) {
bb2cdfff
JH
638 qDebug("SR_DF_END was not received.");
639 assert(0);
640 }
e7216ae0
SA
641
642 if (out_of_memory_)
643 error_handler(tr("Out of memory, acquisition stopped."));
eec446e1
JH
644}
645
da30ecb7 646void Session::feed_in_header()
eec446e1 647{
b48daed6 648 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
be73bdfa
JH
649}
650
da30ecb7 651void Session::feed_in_meta(shared_ptr<Meta> meta)
be73bdfa 652{
e8d00928
ML
653 for (auto entry : meta->config()) {
654 switch (entry.first->id()) {
be73bdfa 655 case SR_CONF_SAMPLERATE:
8a7b603b
SA
656 // We can't rely on the header to always contain the sample rate,
657 // so in case it's supplied via a meta packet, we use it.
658 if (!cur_samplerate_)
659 cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
660
be73bdfa 661 /// @todo handle samplerate changes
be73bdfa
JH
662 break;
663 default:
664 // Unknown metadata is not an error.
665 break;
666 }
aba1dd16 667 }
74043039
JH
668
669 signals_changed();
aba1dd16
JH
670}
671
48257a69
SA
672void Session::feed_in_trigger()
673{
674 // The channel containing most samples should be most accurate
675 uint64_t sample_count = 0;
676
cc9a7898 677 {
cc9a7898
SA
678 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
679 assert(d);
680 uint64_t temp_count = 0;
681
682 const vector< shared_ptr<pv::data::Segment> > segments =
683 d->segments();
684 for (const shared_ptr<pv::data::Segment> &s : segments)
685 temp_count += s->get_sample_count();
686
687 if (temp_count > sample_count)
688 sample_count = temp_count;
689 }
48257a69
SA
690 }
691
692 trigger_event(sample_count / get_samplerate());
693}
694
2b81ae46 695void Session::feed_in_frame_begin()
82f50b10 696{
f3d66e52 697 if (cur_logic_segment_ || !cur_analog_segments_.empty())
82f50b10
JH
698 frame_began();
699}
700
2b81ae46 701void Session::feed_in_logic(shared_ptr<Logic> logic)
9c112671 702{
8524a597 703 lock_guard<recursive_mutex> lock(data_mutex_);
79efbc53 704
ff4bf6bd
SA
705 const size_t sample_count = logic->data_length() / logic->unit_size();
706
2ad82c2e 707 if (!logic_data_) {
e6c1382e
JH
708 // The only reason logic_data_ would not have been created is
709 // if it was not possible to determine the signals when the
710 // device was created.
711 update_signals();
79efbc53 712 }
be73bdfa 713
2ad82c2e 714 if (!cur_logic_segment_) {
bb2cdfff 715 // This could be the first packet after a trigger
2b49eeb0
JH
716 set_capture_state(Running);
717
f3d66e52
JH
718 // Create a new data segment
719 cur_logic_segment_ = shared_ptr<data::LogicSegment>(
720 new data::LogicSegment(
ff4bf6bd 721 logic, cur_samplerate_, sample_count));
f3d66e52 722 logic_data_->push_segment(cur_logic_segment_);
82f50b10
JH
723
724 // @todo Putting this here means that only listeners querying
725 // for logic will be notified. Currently the only user of
726 // frame_began is DecoderStack, but in future we need to signal
727 // this after both analog and logic sweeps have begun.
728 frame_began();
2ad82c2e 729 } else {
f3d66e52
JH
730 // Append to the existing data segment
731 cur_logic_segment_->append_payload(logic);
9c112671
JH
732 }
733
1f374035 734 data_received();
9c112671
JH
735}
736
2b81ae46 737void Session::feed_in_analog(shared_ptr<Analog> analog)
aba1dd16 738{
8524a597 739 lock_guard<recursive_mutex> lock(data_mutex_);
79efbc53 740
e8d00928
ML
741 const vector<shared_ptr<Channel>> channels = analog->channels();
742 const unsigned int channel_count = channels.size();
743 const size_t sample_count = analog->num_samples() / channel_count;
475f4d08 744 const float *data = static_cast<const float *>(analog->data_pointer());
bb2cdfff 745 bool sweep_beginning = false;
be73bdfa 746
47e9e7bb 747 if (signalbases_.empty())
a3f678a7 748 update_signals();
a3f678a7 749
2ad82c2e 750 for (auto channel : channels) {
f3d66e52 751 shared_ptr<data::AnalogSegment> segment;
2b49eeb0 752
f3d66e52
JH
753 // Try to get the segment of the channel
754 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >::
755 iterator iter = cur_analog_segments_.find(channel);
756 if (iter != cur_analog_segments_.end())
757 segment = (*iter).second;
2ad82c2e 758 else {
39ccf9c3 759 // If no segment was found, this means we haven't
bb2cdfff 760 // created one yet. i.e. this is the first packet
f3d66e52 761 // in the sweep containing this segment.
bb2cdfff
JH
762 sweep_beginning = true;
763
f3d66e52
JH
764 // Create a segment, keep it in the maps of channels
765 segment = shared_ptr<data::AnalogSegment>(
766 new data::AnalogSegment(
ff4bf6bd 767 cur_samplerate_, sample_count));
f3d66e52 768 cur_analog_segments_[channel] = segment;
bb2cdfff 769
ff4bf6bd 770 // Find the analog data associated with the channel
cbd2a2de
SA
771 shared_ptr<data::SignalBase> base = signalbase_from_channel(channel);
772 assert(base);
bb2cdfff 773
cbd2a2de 774 shared_ptr<data::Analog> data(base->analog_data());
bb2cdfff
JH
775 assert(data);
776
f3d66e52
JH
777 // Push the segment into the analog data.
778 data->push_segment(segment);
bb2cdfff
JH
779 }
780
f3d66e52 781 assert(segment);
bb2cdfff 782
f3d66e52
JH
783 // Append the samples in the segment
784 segment->append_interleaved_samples(data++, sample_count,
6ac6242b 785 channel_count);
aba1dd16 786 }
bb2cdfff
JH
787
788 if (sweep_beginning) {
789 // This could be the first packet after a trigger
790 set_capture_state(Running);
aba1dd16
JH
791 }
792
1f374035 793 data_received();
aba1dd16 794}
9c112671 795
da30ecb7
JH
796void Session::data_feed_in(shared_ptr<sigrok::Device> device,
797 shared_ptr<Packet> packet)
b0e1d01d 798{
da30ecb7
JH
799 (void)device;
800
e8d00928 801 assert(device);
da30ecb7 802 assert(device == device_->device());
b0e1d01d
JH
803 assert(packet);
804
e8d00928 805 switch (packet->type()->id()) {
b0e1d01d 806 case SR_DF_HEADER:
da30ecb7 807 feed_in_header();
b0e1d01d
JH
808 break;
809
be73bdfa 810 case SR_DF_META:
da30ecb7 811 feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
aba1dd16
JH
812 break;
813
48257a69
SA
814 case SR_DF_TRIGGER:
815 feed_in_trigger();
816 break;
817
82f50b10
JH
818 case SR_DF_FRAME_BEGIN:
819 feed_in_frame_begin();
820 break;
821
2e2946fe 822 case SR_DF_LOGIC:
e7216ae0
SA
823 try {
824 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
825 } catch (std::bad_alloc) {
826 out_of_memory_ = true;
827 device_->stop();
828 }
2953961c
JH
829 break;
830
aba1dd16 831 case SR_DF_ANALOG:
e7216ae0
SA
832 try {
833 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
834 } catch (std::bad_alloc) {
835 out_of_memory_ = true;
836 device_->stop();
837 }
aba1dd16
JH
838 break;
839
2953961c 840 case SR_DF_END:
2e2946fe
JH
841 {
842 {
8524a597 843 lock_guard<recursive_mutex> lock(data_mutex_);
f3d66e52
JH
844 cur_logic_segment_.reset();
845 cur_analog_segments_.clear();
2e2946fe 846 }
1f374035 847 frame_ended();
2953961c
JH
848 break;
849 }
adb0a983
ML
850 default:
851 break;
2e2946fe 852 }
2953961c
JH
853}
854
51e77110 855} // namespace pv