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