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