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