]>
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 | ||
35750e4d UH |
21 | #ifdef _WIN32 |
22 | // Windows: Avoid boost/thread namespace pollution (which includes windows.h). | |
23 | #define NOGDI | |
24 | #define NORESOURCE | |
25 | #endif | |
e71eb81c JH |
26 | #include <boost/thread/locks.hpp> |
27 | #include <boost/thread/shared_mutex.hpp> | |
28 | ||
269528f5 | 29 | #ifdef ENABLE_DECODE |
4e5a4405 | 30 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 31 | #endif |
4e5a4405 | 32 | |
f65cd27b | 33 | #include "session.hpp" |
2953961c | 34 | |
2acdb232 | 35 | #include "devicemanager.hpp" |
119aff65 | 36 | |
2acdb232 | 37 | #include "data/analog.hpp" |
f3d66e52 | 38 | #include "data/analogsegment.hpp" |
2acdb232 JH |
39 | #include "data/decoderstack.hpp" |
40 | #include "data/logic.hpp" | |
f3d66e52 | 41 | #include "data/logicsegment.hpp" |
bf0edd2b | 42 | #include "data/signalbase.hpp" |
2acdb232 | 43 | #include "data/decode/decoder.hpp" |
82c7f640 | 44 | |
da30ecb7 JH |
45 | #include "devices/hardwaredevice.hpp" |
46 | #include "devices/sessionfile.hpp" | |
47 | ||
2acdb232 JH |
48 | #include "view/analogsignal.hpp" |
49 | #include "view/decodetrace.hpp" | |
50 | #include "view/logicsignal.hpp" | |
28a4c9c5 | 51 | |
3b68d03d JH |
52 | #include <cassert> |
53 | #include <mutex> | |
e92cd4e4 JH |
54 | #include <stdexcept> |
55 | ||
728e5ef7 JH |
56 | #include <sys/stat.h> |
57 | ||
79efbc53 JH |
58 | #include <QDebug> |
59 | ||
fe3a1c21 | 60 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 61 | |
aca64cac JH |
62 | using boost::shared_lock; |
63 | using boost::shared_mutex; | |
64 | using boost::unique_lock; | |
65 | ||
f9abf97e | 66 | using std::dynamic_pointer_cast; |
d2344534 | 67 | using std::function; |
3b68d03d | 68 | using std::lock_guard; |
d873f4d6 | 69 | using std::list; |
819f4c25 | 70 | using std::map; |
aca64cac | 71 | using std::mutex; |
8524a597 | 72 | using std::recursive_mutex; |
02412f0b | 73 | using std::set; |
f9abf97e | 74 | using std::shared_ptr; |
819f4c25 | 75 | using std::string; |
78b0af3e | 76 | using std::unordered_set; |
819f4c25 | 77 | using std::vector; |
28a4c9c5 | 78 | |
e8d00928 ML |
79 | using sigrok::Analog; |
80 | using sigrok::Channel; | |
81 | using sigrok::ChannelType; | |
82 | using sigrok::ConfigKey; | |
83 | using sigrok::DatafeedCallbackFunction; | |
e8d00928 | 84 | using sigrok::Error; |
e8d00928 ML |
85 | using sigrok::Header; |
86 | using sigrok::Logic; | |
87 | using sigrok::Meta; | |
88 | using sigrok::Packet; | |
89 | using sigrok::PacketPayload; | |
90 | using sigrok::Session; | |
91 | using sigrok::SessionDevice; | |
92 | ||
93 | using Glib::VariantBase; | |
94 | using Glib::Variant; | |
51e77110 | 95 | |
e8d00928 | 96 | namespace pv { |
2b81ae46 | 97 | Session::Session(DeviceManager &device_manager) : |
8dbbc7f0 | 98 | device_manager_(device_manager), |
ff008de6 JH |
99 | capture_state_(Stopped), |
100 | cur_samplerate_(0) | |
2953961c | 101 | { |
2953961c JH |
102 | } |
103 | ||
2b81ae46 | 104 | Session::~Session() |
2953961c | 105 | { |
04463625 | 106 | // Stop and join to the thread |
5b7cf66c | 107 | stop_capture(); |
2953961c JH |
108 | } |
109 | ||
2b81ae46 | 110 | DeviceManager& Session::device_manager() |
4cb0b033 | 111 | { |
8dbbc7f0 | 112 | return device_manager_; |
4cb0b033 JH |
113 | } |
114 | ||
2b81ae46 | 115 | const DeviceManager& Session::device_manager() const |
4cb0b033 | 116 | { |
8dbbc7f0 | 117 | return device_manager_; |
4cb0b033 JH |
118 | } |
119 | ||
da30ecb7 | 120 | shared_ptr<sigrok::Session> Session::session() const |
1f4caa77 | 121 | { |
da30ecb7 JH |
122 | if (!device_) |
123 | return shared_ptr<sigrok::Session>(); | |
124 | return device_->session(); | |
1f4caa77 JH |
125 | } |
126 | ||
da30ecb7 | 127 | shared_ptr<devices::Device> Session::device() const |
dc0867ff | 128 | { |
8dbbc7f0 | 129 | return device_; |
dc0867ff JH |
130 | } |
131 | ||
da30ecb7 | 132 | void Session::set_device(shared_ptr<devices::Device> device) |
d64d1596 | 133 | { |
da30ecb7 JH |
134 | assert(device); |
135 | ||
82596f46 JH |
136 | // Ensure we are not capturing before setting the device |
137 | stop_capture(); | |
138 | ||
4d6c6ea3 SA |
139 | if (device_) |
140 | device_->close(); | |
141 | ||
86123e2e SA |
142 | device_.reset(); |
143 | ||
cc9a7898 | 144 | // Remove all stored data |
86123e2e | 145 | signals_.clear(); |
cc9a7898 SA |
146 | { |
147 | shared_lock<shared_mutex> lock(signals_mutex_); | |
148 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
149 | d->clear(); | |
150 | } | |
151 | all_signal_data_.clear(); | |
4b0d7046 SA |
152 | cur_logic_segment_.reset(); |
153 | ||
154 | for (auto entry : cur_analog_segments_) { | |
155 | shared_ptr<sigrok::Channel>(entry.first).reset(); | |
156 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
157 | } | |
158 | ||
159 | logic_data_.reset(); | |
86123e2e | 160 | decode_traces_.clear(); |
4b0d7046 | 161 | |
86123e2e SA |
162 | signals_changed(); |
163 | ||
da30ecb7 | 164 | device_ = std::move(device); |
7e0c99bf SA |
165 | |
166 | try { | |
167 | device_->open(); | |
168 | } catch (const QString &e) { | |
169 | device_.reset(); | |
170 | device_selected(); | |
171 | throw; | |
172 | } | |
173 | ||
da30ecb7 JH |
174 | device_->session()->add_datafeed_callback([=] |
175 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
176 | data_feed_in(device, packet); | |
177 | }); | |
ae2d1bc5 | 178 | |
4b8e787d | 179 | update_signals(); |
20f81a58 | 180 | device_selected(); |
ae2d1bc5 | 181 | } |
85843b14 | 182 | |
2b81ae46 | 183 | void Session::set_default_device() |
6fd0b639 | 184 | { |
da30ecb7 | 185 | const list< shared_ptr<devices::HardwareDevice> > &devices = |
8dbbc7f0 | 186 | device_manager_.devices(); |
6fd0b639 | 187 | |
da30ecb7 JH |
188 | if (devices.empty()) |
189 | return; | |
dc0867ff | 190 | |
da30ecb7 JH |
191 | // Try and find the demo device and select that by default |
192 | const auto iter = std::find_if(devices.begin(), devices.end(), | |
193 | [] (const shared_ptr<devices::HardwareDevice> &d) { | |
194 | return d->hardware_device()->driver()->name() == | |
195 | "demo"; }); | |
196 | set_device((iter == devices.end()) ? devices.front() : *iter); | |
dc0867ff JH |
197 | } |
198 | ||
2b81ae46 | 199 | Session::capture_state Session::get_capture_state() const |
5b7cf66c | 200 | { |
8dbbc7f0 JH |
201 | lock_guard<mutex> lock(sampling_mutex_); |
202 | return capture_state_; | |
5b7cf66c JH |
203 | } |
204 | ||
2b81ae46 | 205 | void Session::start_capture(function<void (const QString)> error_handler) |
2e2946fe | 206 | { |
34c11fa7 SA |
207 | if (!device_) { |
208 | error_handler(tr("No active device set, can't start acquisition.")); | |
209 | return; | |
210 | } | |
211 | ||
5b7cf66c JH |
212 | stop_capture(); |
213 | ||
6ac6242b | 214 | // Check that at least one channel is enabled |
e6c1382e JH |
215 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
216 | if (sr_dev) { | |
217 | const auto channels = sr_dev->channels(); | |
218 | if (!std::any_of(channels.begin(), channels.end(), | |
219 | [](shared_ptr<Channel> channel) { | |
220 | return channel->enabled(); })) { | |
221 | error_handler(tr("No channels enabled.")); | |
222 | return; | |
223 | } | |
9c48fa57 JH |
224 | } |
225 | ||
53ea4c6c | 226 | // Clear signal data |
cc9a7898 SA |
227 | { |
228 | shared_lock<shared_mutex> lock(signals_mutex_); | |
229 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
230 | d->clear(); | |
231 | } | |
53ea4c6c | 232 | |
9c48fa57 | 233 | // Begin the session |
8dbbc7f0 | 234 | sampling_thread_ = std::thread( |
2b05d311 | 235 | &Session::sample_thread_proc, this, error_handler); |
2e2946fe JH |
236 | } |
237 | ||
2b81ae46 | 238 | void Session::stop_capture() |
5b7cf66c | 239 | { |
04463625 | 240 | if (get_capture_state() != Stopped) |
da30ecb7 | 241 | device_->stop(); |
5b7cf66c JH |
242 | |
243 | // Check that sampling stopped | |
8dbbc7f0 JH |
244 | if (sampling_thread_.joinable()) |
245 | sampling_thread_.join(); | |
5b7cf66c JH |
246 | } |
247 | ||
2220e942 SA |
248 | double Session::get_samplerate() const |
249 | { | |
250 | double samplerate = 0.0; | |
251 | ||
cc9a7898 SA |
252 | { |
253 | shared_lock<shared_mutex> lock(signals_mutex_); | |
254 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
255 | assert(d); | |
256 | const vector< shared_ptr<pv::data::Segment> > segments = | |
257 | d->segments(); | |
258 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
259 | samplerate = std::max(samplerate, s->samplerate()); | |
260 | } | |
2220e942 | 261 | } |
2220e942 SA |
262 | // If there is no sample rate given we use samples as unit |
263 | if (samplerate == 0.0) | |
264 | samplerate = 1.0; | |
265 | ||
266 | return samplerate; | |
267 | } | |
268 | ||
bf914698 | 269 | const unordered_set< shared_ptr<view::Signal> > Session::signals() const |
2e2946fe | 270 | { |
bf914698 | 271 | shared_lock<shared_mutex> lock(signals_mutex_); |
8dbbc7f0 | 272 | return signals_; |
2e2946fe JH |
273 | } |
274 | ||
269528f5 | 275 | #ifdef ENABLE_DECODE |
2b81ae46 | 276 | bool Session::add_decoder(srd_decoder *const dec) |
82c7f640 | 277 | { |
6ac6242b | 278 | map<const srd_channel*, shared_ptr<view::LogicSignal> > channels; |
7491a29f | 279 | shared_ptr<data::DecoderStack> decoder_stack; |
4e5a4405 | 280 | |
2ad82c2e | 281 | try { |
8dbbc7f0 | 282 | lock_guard<boost::shared_mutex> lock(signals_mutex_); |
e0fc5810 | 283 | |
4e5a4405 | 284 | // Create the decoder |
7491a29f | 285 | decoder_stack = shared_ptr<data::DecoderStack>( |
bdc5c3b0 | 286 | new data::DecoderStack(*this, dec)); |
4e5a4405 | 287 | |
6ac6242b ML |
288 | // Make a list of all the channels |
289 | std::vector<const srd_channel*> all_channels; | |
f3290553 | 290 | for (const GSList *i = dec->channels; i; i = i->next) |
6ac6242b | 291 | all_channels.push_back((const srd_channel*)i->data); |
f3290553 | 292 | for (const GSList *i = dec->opt_channels; i; i = i->next) |
6ac6242b | 293 | all_channels.push_back((const srd_channel*)i->data); |
d2f46d27 | 294 | |
6ac6242b ML |
295 | // Auto select the initial channels |
296 | for (const srd_channel *pdch : all_channels) | |
2ad82c2e | 297 | for (shared_ptr<view::Signal> s : signals_) { |
4e5a4405 JH |
298 | shared_ptr<view::LogicSignal> l = |
299 | dynamic_pointer_cast<view::LogicSignal>(s); | |
8bd26d8b | 300 | if (l && QString::fromUtf8(pdch->name). |
27e8df22 | 301 | toLower().contains( |
73a25a6e | 302 | s->base()->name().toLower())) |
6ac6242b | 303 | channels[pdch] = l; |
4e5a4405 | 304 | } |
4e5a4405 | 305 | |
7491a29f JH |
306 | assert(decoder_stack); |
307 | assert(!decoder_stack->stack().empty()); | |
308 | assert(decoder_stack->stack().front()); | |
6ac6242b | 309 | decoder_stack->stack().front()->set_channels(channels); |
4e5a4405 JH |
310 | |
311 | // Create the decode signal | |
bf0edd2b SA |
312 | shared_ptr<data::SignalBase> signalbase = |
313 | shared_ptr<data::SignalBase>(new data::SignalBase(nullptr)); | |
314 | ||
b9329558 | 315 | shared_ptr<view::DecodeTrace> d( |
bf0edd2b | 316 | new view::DecodeTrace(*this, signalbase, decoder_stack, |
8dbbc7f0 JH |
317 | decode_traces_.size())); |
318 | decode_traces_.push_back(d); | |
2ad82c2e | 319 | } catch (std::runtime_error e) { |
e92cd4e4 JH |
320 | return false; |
321 | } | |
322 | ||
82c7f640 | 323 | signals_changed(); |
e92cd4e4 | 324 | |
7491a29f JH |
325 | // Do an initial decode |
326 | decoder_stack->begin_decode(); | |
327 | ||
e92cd4e4 | 328 | return true; |
82c7f640 JH |
329 | } |
330 | ||
2b81ae46 | 331 | vector< shared_ptr<view::DecodeTrace> > Session::get_decode_signals() const |
38eeddea | 332 | { |
8dbbc7f0 JH |
333 | shared_lock<shared_mutex> lock(signals_mutex_); |
334 | return decode_traces_; | |
38eeddea JH |
335 | } |
336 | ||
2b81ae46 | 337 | void Session::remove_decode_signal(view::DecodeTrace *signal) |
c51482b3 | 338 | { |
8dbbc7f0 | 339 | for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) |
2ad82c2e | 340 | if ((*i).get() == signal) { |
8dbbc7f0 | 341 | decode_traces_.erase(i); |
c51482b3 JH |
342 | signals_changed(); |
343 | return; | |
344 | } | |
345 | } | |
269528f5 | 346 | #endif |
c51482b3 | 347 | |
2b81ae46 | 348 | void Session::set_capture_state(capture_state state) |
6ac96c2e | 349 | { |
896936e5 TS |
350 | bool changed; |
351 | ||
352 | { | |
353 | lock_guard<mutex> lock(sampling_mutex_); | |
354 | changed = capture_state_ != state; | |
355 | capture_state_ = state; | |
356 | } | |
357 | ||
f3290553 | 358 | if (changed) |
2b49eeb0 | 359 | capture_state_changed(state); |
6ac96c2e JH |
360 | } |
361 | ||
ffe00119 | 362 | void Session::update_signals() |
b087ba7f | 363 | { |
076eefa4 SA |
364 | if (!device_) { |
365 | signals_.clear(); | |
366 | logic_data_.reset(); | |
367 | return; | |
368 | } | |
e6c1382e JH |
369 | |
370 | lock_guard<recursive_mutex> lock(data_mutex_); | |
b087ba7f | 371 | |
ffe00119 | 372 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
c6412b47 JH |
373 | if (!sr_dev) { |
374 | signals_.clear(); | |
375 | logic_data_.reset(); | |
376 | return; | |
377 | } | |
378 | ||
b087ba7f | 379 | // Detect what data types we will receive |
c6412b47 | 380 | auto channels = sr_dev->channels(); |
e8d00928 ML |
381 | unsigned int logic_channel_count = std::count_if( |
382 | channels.begin(), channels.end(), | |
383 | [] (shared_ptr<Channel> channel) { | |
384 | return channel->type() == ChannelType::LOGIC; }); | |
b087ba7f | 385 | |
f3d66e52 | 386 | // Create data containers for the logic data segments |
b087ba7f | 387 | { |
8524a597 | 388 | lock_guard<recursive_mutex> data_lock(data_mutex_); |
b087ba7f | 389 | |
3917ba77 JH |
390 | if (logic_channel_count == 0) { |
391 | logic_data_.reset(); | |
392 | } else if (!logic_data_ || | |
393 | logic_data_->num_channels() != logic_channel_count) { | |
8dbbc7f0 | 394 | logic_data_.reset(new data::Logic( |
6ac6242b | 395 | logic_channel_count)); |
8dbbc7f0 | 396 | assert(logic_data_); |
b087ba7f | 397 | } |
b087ba7f JH |
398 | } |
399 | ||
400 | // Make the Signals list | |
fbd3e234 | 401 | { |
8dbbc7f0 | 402 | unique_lock<shared_mutex> lock(signals_mutex_); |
b087ba7f | 403 | |
3917ba77 | 404 | unordered_set< shared_ptr<view::Signal> > prev_sigs(signals_); |
8dbbc7f0 | 405 | signals_.clear(); |
b087ba7f | 406 | |
c6412b47 | 407 | for (auto channel : sr_dev->channels()) { |
bf0edd2b | 408 | shared_ptr<data::SignalBase> signalbase; |
39e680c2 | 409 | shared_ptr<view::Signal> signal; |
39e680c2 | 410 | |
3917ba77 JH |
411 | // Find the channel in the old signals |
412 | const auto iter = std::find_if( | |
413 | prev_sigs.cbegin(), prev_sigs.cend(), | |
414 | [&](const shared_ptr<view::Signal> &s) { | |
73a25a6e | 415 | return s->base()->channel() == channel; |
3917ba77 JH |
416 | }); |
417 | if (iter != prev_sigs.end()) { | |
418 | // Copy the signal from the old set to the new | |
419 | signal = *iter; | |
420 | auto logic_signal = dynamic_pointer_cast< | |
421 | view::LogicSignal>(signal); | |
422 | if (logic_signal) | |
423 | logic_signal->set_logic_data( | |
424 | logic_data_); | |
425 | } else { | |
426 | // Create a new signal | |
bf0edd2b SA |
427 | signalbase = shared_ptr<data::SignalBase>( |
428 | new data::SignalBase(channel)); | |
429 | ||
3917ba77 JH |
430 | switch(channel->type()->id()) { |
431 | case SR_CHANNEL_LOGIC: | |
432 | signal = shared_ptr<view::Signal>( | |
433 | new view::LogicSignal(*this, | |
bf0edd2b | 434 | device_, signalbase, logic_data_)); |
cc9a7898 | 435 | all_signal_data_.insert(logic_data_); |
bf0edd2b | 436 | signalbases_.insert(signalbase); |
3917ba77 JH |
437 | break; |
438 | ||
439 | case SR_CHANNEL_ANALOG: | |
440 | { | |
441 | shared_ptr<data::Analog> data( | |
442 | new data::Analog()); | |
443 | signal = shared_ptr<view::Signal>( | |
444 | new view::AnalogSignal( | |
bf0edd2b | 445 | *this, signalbase, data)); |
cc9a7898 | 446 | all_signal_data_.insert(data); |
bf0edd2b | 447 | signalbases_.insert(signalbase); |
3917ba77 JH |
448 | break; |
449 | } | |
450 | ||
451 | default: | |
452 | assert(0); | |
453 | break; | |
454 | } | |
b087ba7f | 455 | } |
39e680c2 JH |
456 | |
457 | assert(signal); | |
78b0af3e | 458 | signals_.insert(signal); |
b087ba7f | 459 | } |
fbd3e234 | 460 | } |
b087ba7f JH |
461 | |
462 | signals_changed(); | |
463 | } | |
464 | ||
bf0edd2b SA |
465 | shared_ptr<data::SignalBase> Session::signal_from_channel( |
466 | shared_ptr<sigrok::Channel> channel) const | |
3ddcc083 | 467 | { |
bf0edd2b | 468 | for (shared_ptr<data::SignalBase> sig : signalbases_) { |
3ddcc083 | 469 | assert(sig); |
6ac6242b | 470 | if (sig->channel() == channel) |
3ddcc083 JH |
471 | return sig; |
472 | } | |
bf0edd2b | 473 | return shared_ptr<data::SignalBase>(); |
3ddcc083 JH |
474 | } |
475 | ||
2b05d311 | 476 | void Session::sample_thread_proc(function<void (const QString)> error_handler) |
274d4f13 | 477 | { |
f2edb557 | 478 | assert(error_handler); |
be73bdfa | 479 | |
34c11fa7 SA |
480 | if (!device_) |
481 | return; | |
45f0c7c9 | 482 | |
b48daed6 | 483 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
274d4f13 | 484 | |
e7216ae0 SA |
485 | out_of_memory_ = false; |
486 | ||
ae2d1bc5 | 487 | try { |
5237f0c5 | 488 | device_->start(); |
2ad82c2e | 489 | } catch (Error e) { |
e8d00928 | 490 | error_handler(e.what()); |
eec446e1 JH |
491 | return; |
492 | } | |
493 | ||
da30ecb7 | 494 | set_capture_state(device_->session()->trigger() ? |
07dcf561 | 495 | AwaitingTrigger : Running); |
eec446e1 | 496 | |
da30ecb7 | 497 | device_->run(); |
eec446e1 | 498 | set_capture_state(Stopped); |
1a2fe44c JH |
499 | |
500 | // Confirm that SR_DF_END was received | |
2ad82c2e | 501 | if (cur_logic_segment_) { |
bb2cdfff JH |
502 | qDebug("SR_DF_END was not received."); |
503 | assert(0); | |
504 | } | |
e7216ae0 SA |
505 | |
506 | if (out_of_memory_) | |
507 | error_handler(tr("Out of memory, acquisition stopped.")); | |
eec446e1 JH |
508 | } |
509 | ||
da30ecb7 | 510 | void Session::feed_in_header() |
eec446e1 | 511 | { |
b48daed6 | 512 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
be73bdfa JH |
513 | } |
514 | ||
da30ecb7 | 515 | void Session::feed_in_meta(shared_ptr<Meta> meta) |
be73bdfa | 516 | { |
e8d00928 ML |
517 | for (auto entry : meta->config()) { |
518 | switch (entry.first->id()) { | |
be73bdfa | 519 | case SR_CONF_SAMPLERATE: |
8a7b603b SA |
520 | // We can't rely on the header to always contain the sample rate, |
521 | // so in case it's supplied via a meta packet, we use it. | |
522 | if (!cur_samplerate_) | |
523 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); | |
524 | ||
be73bdfa | 525 | /// @todo handle samplerate changes |
be73bdfa JH |
526 | break; |
527 | default: | |
528 | // Unknown metadata is not an error. | |
529 | break; | |
530 | } | |
aba1dd16 | 531 | } |
74043039 JH |
532 | |
533 | signals_changed(); | |
aba1dd16 JH |
534 | } |
535 | ||
48257a69 SA |
536 | void Session::feed_in_trigger() |
537 | { | |
538 | // The channel containing most samples should be most accurate | |
539 | uint64_t sample_count = 0; | |
540 | ||
cc9a7898 SA |
541 | { |
542 | shared_lock<shared_mutex> lock(signals_mutex_); | |
543 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
544 | assert(d); | |
545 | uint64_t temp_count = 0; | |
546 | ||
547 | const vector< shared_ptr<pv::data::Segment> > segments = | |
548 | d->segments(); | |
549 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
550 | temp_count += s->get_sample_count(); | |
551 | ||
552 | if (temp_count > sample_count) | |
553 | sample_count = temp_count; | |
554 | } | |
48257a69 SA |
555 | } |
556 | ||
557 | trigger_event(sample_count / get_samplerate()); | |
558 | } | |
559 | ||
2b81ae46 | 560 | void Session::feed_in_frame_begin() |
82f50b10 | 561 | { |
f3d66e52 | 562 | if (cur_logic_segment_ || !cur_analog_segments_.empty()) |
82f50b10 JH |
563 | frame_began(); |
564 | } | |
565 | ||
2b81ae46 | 566 | void Session::feed_in_logic(shared_ptr<Logic> logic) |
9c112671 | 567 | { |
8524a597 | 568 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 569 | |
ff4bf6bd SA |
570 | const size_t sample_count = logic->data_length() / logic->unit_size(); |
571 | ||
2ad82c2e | 572 | if (!logic_data_) { |
e6c1382e JH |
573 | // The only reason logic_data_ would not have been created is |
574 | // if it was not possible to determine the signals when the | |
575 | // device was created. | |
576 | update_signals(); | |
79efbc53 | 577 | } |
be73bdfa | 578 | |
2ad82c2e | 579 | if (!cur_logic_segment_) { |
bb2cdfff | 580 | // This could be the first packet after a trigger |
2b49eeb0 JH |
581 | set_capture_state(Running); |
582 | ||
f3d66e52 JH |
583 | // Create a new data segment |
584 | cur_logic_segment_ = shared_ptr<data::LogicSegment>( | |
585 | new data::LogicSegment( | |
ff4bf6bd | 586 | logic, cur_samplerate_, sample_count)); |
f3d66e52 | 587 | logic_data_->push_segment(cur_logic_segment_); |
82f50b10 JH |
588 | |
589 | // @todo Putting this here means that only listeners querying | |
590 | // for logic will be notified. Currently the only user of | |
591 | // frame_began is DecoderStack, but in future we need to signal | |
592 | // this after both analog and logic sweeps have begun. | |
593 | frame_began(); | |
2ad82c2e | 594 | } else { |
f3d66e52 JH |
595 | // Append to the existing data segment |
596 | cur_logic_segment_->append_payload(logic); | |
9c112671 JH |
597 | } |
598 | ||
1f374035 | 599 | data_received(); |
9c112671 JH |
600 | } |
601 | ||
2b81ae46 | 602 | void Session::feed_in_analog(shared_ptr<Analog> analog) |
aba1dd16 | 603 | { |
8524a597 | 604 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 605 | |
e8d00928 ML |
606 | const vector<shared_ptr<Channel>> channels = analog->channels(); |
607 | const unsigned int channel_count = channels.size(); | |
608 | const size_t sample_count = analog->num_samples() / channel_count; | |
475f4d08 | 609 | const float *data = static_cast<const float *>(analog->data_pointer()); |
bb2cdfff | 610 | bool sweep_beginning = false; |
be73bdfa | 611 | |
2ad82c2e | 612 | if (signals_.empty()) |
a3f678a7 | 613 | update_signals(); |
a3f678a7 | 614 | |
2ad82c2e | 615 | for (auto channel : channels) { |
f3d66e52 | 616 | shared_ptr<data::AnalogSegment> segment; |
2b49eeb0 | 617 | |
f3d66e52 JH |
618 | // Try to get the segment of the channel |
619 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
620 | iterator iter = cur_analog_segments_.find(channel); | |
621 | if (iter != cur_analog_segments_.end()) | |
622 | segment = (*iter).second; | |
2ad82c2e | 623 | else { |
39ccf9c3 | 624 | // If no segment was found, this means we haven't |
bb2cdfff | 625 | // created one yet. i.e. this is the first packet |
f3d66e52 | 626 | // in the sweep containing this segment. |
bb2cdfff JH |
627 | sweep_beginning = true; |
628 | ||
f3d66e52 JH |
629 | // Create a segment, keep it in the maps of channels |
630 | segment = shared_ptr<data::AnalogSegment>( | |
631 | new data::AnalogSegment( | |
ff4bf6bd | 632 | cur_samplerate_, sample_count)); |
f3d66e52 | 633 | cur_analog_segments_[channel] = segment; |
bb2cdfff | 634 | |
ff4bf6bd | 635 | // Find the analog data associated with the channel |
bb2cdfff JH |
636 | shared_ptr<view::AnalogSignal> sig = |
637 | dynamic_pointer_cast<view::AnalogSignal>( | |
6ac6242b | 638 | signal_from_channel(channel)); |
bb2cdfff JH |
639 | assert(sig); |
640 | ||
641 | shared_ptr<data::Analog> data(sig->analog_data()); | |
642 | assert(data); | |
643 | ||
f3d66e52 JH |
644 | // Push the segment into the analog data. |
645 | data->push_segment(segment); | |
bb2cdfff JH |
646 | } |
647 | ||
f3d66e52 | 648 | assert(segment); |
bb2cdfff | 649 | |
f3d66e52 JH |
650 | // Append the samples in the segment |
651 | segment->append_interleaved_samples(data++, sample_count, | |
6ac6242b | 652 | channel_count); |
aba1dd16 | 653 | } |
bb2cdfff JH |
654 | |
655 | if (sweep_beginning) { | |
656 | // This could be the first packet after a trigger | |
657 | set_capture_state(Running); | |
aba1dd16 JH |
658 | } |
659 | ||
1f374035 | 660 | data_received(); |
aba1dd16 | 661 | } |
9c112671 | 662 | |
da30ecb7 JH |
663 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, |
664 | shared_ptr<Packet> packet) | |
b0e1d01d | 665 | { |
da30ecb7 JH |
666 | (void)device; |
667 | ||
e8d00928 | 668 | assert(device); |
da30ecb7 | 669 | assert(device == device_->device()); |
b0e1d01d JH |
670 | assert(packet); |
671 | ||
e8d00928 | 672 | switch (packet->type()->id()) { |
b0e1d01d | 673 | case SR_DF_HEADER: |
da30ecb7 | 674 | feed_in_header(); |
b0e1d01d JH |
675 | break; |
676 | ||
be73bdfa | 677 | case SR_DF_META: |
da30ecb7 | 678 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); |
aba1dd16 JH |
679 | break; |
680 | ||
48257a69 SA |
681 | case SR_DF_TRIGGER: |
682 | feed_in_trigger(); | |
683 | break; | |
684 | ||
82f50b10 JH |
685 | case SR_DF_FRAME_BEGIN: |
686 | feed_in_frame_begin(); | |
687 | break; | |
688 | ||
2e2946fe | 689 | case SR_DF_LOGIC: |
e7216ae0 SA |
690 | try { |
691 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
692 | } catch (std::bad_alloc) { | |
693 | out_of_memory_ = true; | |
694 | device_->stop(); | |
695 | } | |
2953961c JH |
696 | break; |
697 | ||
aba1dd16 | 698 | case SR_DF_ANALOG: |
e7216ae0 SA |
699 | try { |
700 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
701 | } catch (std::bad_alloc) { | |
702 | out_of_memory_ = true; | |
703 | device_->stop(); | |
704 | } | |
aba1dd16 JH |
705 | break; |
706 | ||
2953961c | 707 | case SR_DF_END: |
2e2946fe JH |
708 | { |
709 | { | |
8524a597 | 710 | lock_guard<recursive_mutex> lock(data_mutex_); |
f3d66e52 JH |
711 | cur_logic_segment_.reset(); |
712 | cur_analog_segments_.clear(); | |
2e2946fe | 713 | } |
1f374035 | 714 | frame_ended(); |
2953961c JH |
715 | break; |
716 | } | |
adb0a983 ML |
717 | default: |
718 | break; | |
2e2946fe | 719 | } |
2953961c JH |
720 | } |
721 | ||
51e77110 | 722 | } // namespace pv |