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