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