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