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