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