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