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