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