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