]>
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 | ||
2b81ae46 | 220 | boost::shared_mutex& Session::signals_mutex() const |
c3a740dd | 221 | { |
8dbbc7f0 | 222 | return signals_mutex_; |
c3a740dd JH |
223 | } |
224 | ||
78b0af3e | 225 | const unordered_set< shared_ptr<view::Signal> >& Session::signals() const |
2e2946fe | 226 | { |
8dbbc7f0 | 227 | return signals_; |
2e2946fe JH |
228 | } |
229 | ||
269528f5 | 230 | #ifdef ENABLE_DECODE |
2b81ae46 | 231 | bool Session::add_decoder(srd_decoder *const dec) |
82c7f640 | 232 | { |
6ac6242b | 233 | map<const srd_channel*, shared_ptr<view::LogicSignal> > channels; |
7491a29f | 234 | shared_ptr<data::DecoderStack> decoder_stack; |
4e5a4405 | 235 | |
e92cd4e4 | 236 | try |
82c7f640 | 237 | { |
8dbbc7f0 | 238 | lock_guard<boost::shared_mutex> lock(signals_mutex_); |
e0fc5810 | 239 | |
4e5a4405 | 240 | // Create the decoder |
7491a29f | 241 | decoder_stack = shared_ptr<data::DecoderStack>( |
bdc5c3b0 | 242 | new data::DecoderStack(*this, dec)); |
4e5a4405 | 243 | |
6ac6242b ML |
244 | // Make a list of all the channels |
245 | std::vector<const srd_channel*> all_channels; | |
f3290553 | 246 | for (const GSList *i = dec->channels; i; i = i->next) |
6ac6242b | 247 | all_channels.push_back((const srd_channel*)i->data); |
f3290553 | 248 | for (const GSList *i = dec->opt_channels; i; i = i->next) |
6ac6242b | 249 | all_channels.push_back((const srd_channel*)i->data); |
d2f46d27 | 250 | |
6ac6242b ML |
251 | // Auto select the initial channels |
252 | for (const srd_channel *pdch : all_channels) | |
8dbbc7f0 | 253 | for (shared_ptr<view::Signal> s : signals_) |
4e5a4405 JH |
254 | { |
255 | shared_ptr<view::LogicSignal> l = | |
256 | dynamic_pointer_cast<view::LogicSignal>(s); | |
8bd26d8b | 257 | if (l && QString::fromUtf8(pdch->name). |
27e8df22 | 258 | toLower().contains( |
0a47889b | 259 | l->name().toLower())) |
6ac6242b | 260 | channels[pdch] = l; |
4e5a4405 | 261 | } |
4e5a4405 | 262 | |
7491a29f JH |
263 | assert(decoder_stack); |
264 | assert(!decoder_stack->stack().empty()); | |
265 | assert(decoder_stack->stack().front()); | |
6ac6242b | 266 | decoder_stack->stack().front()->set_channels(channels); |
4e5a4405 JH |
267 | |
268 | // Create the decode signal | |
b9329558 | 269 | shared_ptr<view::DecodeTrace> d( |
7491a29f | 270 | new view::DecodeTrace(*this, decoder_stack, |
8dbbc7f0 JH |
271 | decode_traces_.size())); |
272 | decode_traces_.push_back(d); | |
82c7f640 | 273 | } |
e92cd4e4 JH |
274 | catch(std::runtime_error e) |
275 | { | |
276 | return false; | |
277 | } | |
278 | ||
82c7f640 | 279 | signals_changed(); |
e92cd4e4 | 280 | |
7491a29f JH |
281 | // Do an initial decode |
282 | decoder_stack->begin_decode(); | |
283 | ||
e92cd4e4 | 284 | return true; |
82c7f640 JH |
285 | } |
286 | ||
2b81ae46 | 287 | vector< shared_ptr<view::DecodeTrace> > Session::get_decode_signals() const |
38eeddea | 288 | { |
8dbbc7f0 JH |
289 | shared_lock<shared_mutex> lock(signals_mutex_); |
290 | return decode_traces_; | |
38eeddea JH |
291 | } |
292 | ||
2b81ae46 | 293 | void Session::remove_decode_signal(view::DecodeTrace *signal) |
c51482b3 | 294 | { |
8dbbc7f0 | 295 | for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) |
c51482b3 JH |
296 | if ((*i).get() == signal) |
297 | { | |
8dbbc7f0 | 298 | decode_traces_.erase(i); |
c51482b3 JH |
299 | signals_changed(); |
300 | return; | |
301 | } | |
302 | } | |
269528f5 | 303 | #endif |
c51482b3 | 304 | |
2b81ae46 | 305 | void Session::set_capture_state(capture_state state) |
6ac96c2e | 306 | { |
8dbbc7f0 JH |
307 | lock_guard<mutex> lock(sampling_mutex_); |
308 | const bool changed = capture_state_ != state; | |
309 | capture_state_ = state; | |
f3290553 | 310 | if (changed) |
2b49eeb0 | 311 | capture_state_changed(state); |
6ac96c2e JH |
312 | } |
313 | ||
ffe00119 | 314 | void Session::update_signals() |
b087ba7f | 315 | { |
ffe00119 | 316 | assert(device_); |
e6c1382e JH |
317 | |
318 | lock_guard<recursive_mutex> lock(data_mutex_); | |
b087ba7f | 319 | |
ffe00119 | 320 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
c6412b47 JH |
321 | if (!sr_dev) { |
322 | signals_.clear(); | |
323 | logic_data_.reset(); | |
324 | return; | |
325 | } | |
326 | ||
b087ba7f | 327 | // Detect what data types we will receive |
c6412b47 | 328 | auto channels = sr_dev->channels(); |
e8d00928 ML |
329 | unsigned int logic_channel_count = std::count_if( |
330 | channels.begin(), channels.end(), | |
331 | [] (shared_ptr<Channel> channel) { | |
332 | return channel->type() == ChannelType::LOGIC; }); | |
b087ba7f | 333 | |
f3d66e52 | 334 | // Create data containers for the logic data segments |
b087ba7f | 335 | { |
8524a597 | 336 | lock_guard<recursive_mutex> data_lock(data_mutex_); |
b087ba7f | 337 | |
3917ba77 JH |
338 | if (logic_channel_count == 0) { |
339 | logic_data_.reset(); | |
340 | } else if (!logic_data_ || | |
341 | logic_data_->num_channels() != logic_channel_count) { | |
8dbbc7f0 | 342 | logic_data_.reset(new data::Logic( |
6ac6242b | 343 | logic_channel_count)); |
8dbbc7f0 | 344 | assert(logic_data_); |
b087ba7f | 345 | } |
b087ba7f JH |
346 | } |
347 | ||
348 | // Make the Signals list | |
fbd3e234 | 349 | { |
8dbbc7f0 | 350 | unique_lock<shared_mutex> lock(signals_mutex_); |
b087ba7f | 351 | |
3917ba77 | 352 | unordered_set< shared_ptr<view::Signal> > prev_sigs(signals_); |
8dbbc7f0 | 353 | signals_.clear(); |
b087ba7f | 354 | |
c6412b47 | 355 | for (auto channel : sr_dev->channels()) { |
39e680c2 | 356 | shared_ptr<view::Signal> signal; |
39e680c2 | 357 | |
3917ba77 JH |
358 | // Find the channel in the old signals |
359 | const auto iter = std::find_if( | |
360 | prev_sigs.cbegin(), prev_sigs.cend(), | |
361 | [&](const shared_ptr<view::Signal> &s) { | |
362 | return s->channel() == channel; | |
363 | }); | |
364 | if (iter != prev_sigs.end()) { | |
365 | // Copy the signal from the old set to the new | |
366 | signal = *iter; | |
367 | auto logic_signal = dynamic_pointer_cast< | |
368 | view::LogicSignal>(signal); | |
369 | if (logic_signal) | |
370 | logic_signal->set_logic_data( | |
371 | logic_data_); | |
372 | } else { | |
373 | // Create a new signal | |
374 | switch(channel->type()->id()) { | |
375 | case SR_CHANNEL_LOGIC: | |
376 | signal = shared_ptr<view::Signal>( | |
377 | new view::LogicSignal(*this, | |
ffe00119 | 378 | device_, channel, |
3917ba77 JH |
379 | logic_data_)); |
380 | break; | |
381 | ||
382 | case SR_CHANNEL_ANALOG: | |
383 | { | |
384 | shared_ptr<data::Analog> data( | |
385 | new data::Analog()); | |
386 | signal = shared_ptr<view::Signal>( | |
387 | new view::AnalogSignal( | |
388 | *this, channel, data)); | |
389 | break; | |
390 | } | |
391 | ||
392 | default: | |
393 | assert(0); | |
394 | break; | |
395 | } | |
b087ba7f | 396 | } |
39e680c2 JH |
397 | |
398 | assert(signal); | |
78b0af3e | 399 | signals_.insert(signal); |
b087ba7f | 400 | } |
fbd3e234 | 401 | } |
b087ba7f JH |
402 | |
403 | signals_changed(); | |
404 | } | |
405 | ||
2b81ae46 | 406 | shared_ptr<view::Signal> Session::signal_from_channel( |
e8d00928 | 407 | shared_ptr<Channel> channel) const |
3ddcc083 | 408 | { |
8dbbc7f0 JH |
409 | lock_guard<boost::shared_mutex> lock(signals_mutex_); |
410 | for (shared_ptr<view::Signal> sig : signals_) { | |
3ddcc083 | 411 | assert(sig); |
6ac6242b | 412 | if (sig->channel() == channel) |
3ddcc083 JH |
413 | return sig; |
414 | } | |
415 | return shared_ptr<view::Signal>(); | |
416 | } | |
417 | ||
da30ecb7 | 418 | void Session::sample_thread_proc(shared_ptr<devices::Device> device, |
f2edb557 | 419 | function<void (const QString)> error_handler) |
274d4f13 | 420 | { |
e8d00928 | 421 | assert(device); |
f2edb557 | 422 | assert(error_handler); |
be73bdfa | 423 | |
45f0c7c9 UH |
424 | (void)device; |
425 | ||
b48daed6 | 426 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
274d4f13 | 427 | |
e7216ae0 SA |
428 | out_of_memory_ = false; |
429 | ||
ae2d1bc5 | 430 | try { |
5237f0c5 | 431 | device_->start(); |
e8d00928 ML |
432 | } catch(Error e) { |
433 | error_handler(e.what()); | |
eec446e1 JH |
434 | return; |
435 | } | |
436 | ||
da30ecb7 | 437 | set_capture_state(device_->session()->trigger() ? |
07dcf561 | 438 | AwaitingTrigger : Running); |
eec446e1 | 439 | |
da30ecb7 | 440 | device_->run(); |
eec446e1 | 441 | set_capture_state(Stopped); |
1a2fe44c JH |
442 | |
443 | // Confirm that SR_DF_END was received | |
f3d66e52 | 444 | if (cur_logic_segment_) |
bb2cdfff JH |
445 | { |
446 | qDebug("SR_DF_END was not received."); | |
447 | assert(0); | |
448 | } | |
e7216ae0 SA |
449 | |
450 | if (out_of_memory_) | |
451 | error_handler(tr("Out of memory, acquisition stopped.")); | |
eec446e1 JH |
452 | } |
453 | ||
da30ecb7 | 454 | void Session::feed_in_header() |
eec446e1 | 455 | { |
b48daed6 | 456 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
be73bdfa JH |
457 | } |
458 | ||
da30ecb7 | 459 | void Session::feed_in_meta(shared_ptr<Meta> meta) |
be73bdfa | 460 | { |
e8d00928 ML |
461 | for (auto entry : meta->config()) { |
462 | switch (entry.first->id()) { | |
be73bdfa JH |
463 | case SR_CONF_SAMPLERATE: |
464 | /// @todo handle samplerate changes | |
be73bdfa JH |
465 | break; |
466 | default: | |
467 | // Unknown metadata is not an error. | |
468 | break; | |
469 | } | |
aba1dd16 | 470 | } |
74043039 JH |
471 | |
472 | signals_changed(); | |
aba1dd16 JH |
473 | } |
474 | ||
2b81ae46 | 475 | void Session::feed_in_frame_begin() |
82f50b10 | 476 | { |
f3d66e52 | 477 | if (cur_logic_segment_ || !cur_analog_segments_.empty()) |
82f50b10 JH |
478 | frame_began(); |
479 | } | |
480 | ||
2b81ae46 | 481 | void Session::feed_in_logic(shared_ptr<Logic> logic) |
9c112671 | 482 | { |
8524a597 | 483 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 484 | |
ff4bf6bd SA |
485 | const size_t sample_count = logic->data_length() / logic->unit_size(); |
486 | ||
8dbbc7f0 | 487 | if (!logic_data_) |
9c112671 | 488 | { |
e6c1382e JH |
489 | // The only reason logic_data_ would not have been created is |
490 | // if it was not possible to determine the signals when the | |
491 | // device was created. | |
492 | update_signals(); | |
79efbc53 | 493 | } |
be73bdfa | 494 | |
f3d66e52 | 495 | if (!cur_logic_segment_) |
79efbc53 | 496 | { |
bb2cdfff | 497 | // This could be the first packet after a trigger |
2b49eeb0 JH |
498 | set_capture_state(Running); |
499 | ||
f3d66e52 JH |
500 | // Create a new data segment |
501 | cur_logic_segment_ = shared_ptr<data::LogicSegment>( | |
502 | new data::LogicSegment( | |
ff4bf6bd | 503 | logic, cur_samplerate_, sample_count)); |
f3d66e52 | 504 | logic_data_->push_segment(cur_logic_segment_); |
82f50b10 JH |
505 | |
506 | // @todo Putting this here means that only listeners querying | |
507 | // for logic will be notified. Currently the only user of | |
508 | // frame_began is DecoderStack, but in future we need to signal | |
509 | // this after both analog and logic sweeps have begun. | |
510 | frame_began(); | |
9c112671 JH |
511 | } |
512 | else | |
513 | { | |
f3d66e52 JH |
514 | // Append to the existing data segment |
515 | cur_logic_segment_->append_payload(logic); | |
9c112671 JH |
516 | } |
517 | ||
1f374035 | 518 | data_received(); |
9c112671 JH |
519 | } |
520 | ||
2b81ae46 | 521 | void Session::feed_in_analog(shared_ptr<Analog> analog) |
aba1dd16 | 522 | { |
8524a597 | 523 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 524 | |
e8d00928 ML |
525 | const vector<shared_ptr<Channel>> channels = analog->channels(); |
526 | const unsigned int channel_count = channels.size(); | |
527 | const size_t sample_count = analog->num_samples() / channel_count; | |
528 | const float *data = analog->data_pointer(); | |
bb2cdfff | 529 | bool sweep_beginning = false; |
be73bdfa | 530 | |
e8d00928 | 531 | for (auto channel : channels) |
79efbc53 | 532 | { |
f3d66e52 | 533 | shared_ptr<data::AnalogSegment> segment; |
2b49eeb0 | 534 | |
f3d66e52 JH |
535 | // Try to get the segment of the channel |
536 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
537 | iterator iter = cur_analog_segments_.find(channel); | |
538 | if (iter != cur_analog_segments_.end()) | |
539 | segment = (*iter).second; | |
bb2cdfff JH |
540 | else |
541 | { | |
f3d66e52 | 542 | // If no segment was found, this means we havn't |
bb2cdfff | 543 | // created one yet. i.e. this is the first packet |
f3d66e52 | 544 | // in the sweep containing this segment. |
bb2cdfff JH |
545 | sweep_beginning = true; |
546 | ||
f3d66e52 JH |
547 | // Create a segment, keep it in the maps of channels |
548 | segment = shared_ptr<data::AnalogSegment>( | |
549 | new data::AnalogSegment( | |
ff4bf6bd | 550 | cur_samplerate_, sample_count)); |
f3d66e52 | 551 | cur_analog_segments_[channel] = segment; |
bb2cdfff | 552 | |
ff4bf6bd | 553 | // Find the analog data associated with the channel |
bb2cdfff JH |
554 | shared_ptr<view::AnalogSignal> sig = |
555 | dynamic_pointer_cast<view::AnalogSignal>( | |
6ac6242b | 556 | signal_from_channel(channel)); |
bb2cdfff JH |
557 | assert(sig); |
558 | ||
559 | shared_ptr<data::Analog> data(sig->analog_data()); | |
560 | assert(data); | |
561 | ||
f3d66e52 JH |
562 | // Push the segment into the analog data. |
563 | data->push_segment(segment); | |
bb2cdfff JH |
564 | } |
565 | ||
f3d66e52 | 566 | assert(segment); |
bb2cdfff | 567 | |
f3d66e52 JH |
568 | // Append the samples in the segment |
569 | segment->append_interleaved_samples(data++, sample_count, | |
6ac6242b | 570 | channel_count); |
aba1dd16 | 571 | } |
bb2cdfff JH |
572 | |
573 | if (sweep_beginning) { | |
574 | // This could be the first packet after a trigger | |
575 | set_capture_state(Running); | |
aba1dd16 JH |
576 | } |
577 | ||
1f374035 | 578 | data_received(); |
aba1dd16 | 579 | } |
9c112671 | 580 | |
da30ecb7 JH |
581 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, |
582 | shared_ptr<Packet> packet) | |
b0e1d01d | 583 | { |
da30ecb7 JH |
584 | (void)device; |
585 | ||
e8d00928 | 586 | assert(device); |
da30ecb7 | 587 | assert(device == device_->device()); |
b0e1d01d JH |
588 | assert(packet); |
589 | ||
e8d00928 | 590 | switch (packet->type()->id()) { |
b0e1d01d | 591 | case SR_DF_HEADER: |
da30ecb7 | 592 | feed_in_header(); |
b0e1d01d JH |
593 | break; |
594 | ||
be73bdfa | 595 | case SR_DF_META: |
da30ecb7 | 596 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); |
aba1dd16 JH |
597 | break; |
598 | ||
82f50b10 JH |
599 | case SR_DF_FRAME_BEGIN: |
600 | feed_in_frame_begin(); | |
601 | break; | |
602 | ||
2e2946fe | 603 | case SR_DF_LOGIC: |
e7216ae0 SA |
604 | try { |
605 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
606 | } catch (std::bad_alloc) { | |
607 | out_of_memory_ = true; | |
608 | device_->stop(); | |
609 | } | |
2953961c JH |
610 | break; |
611 | ||
aba1dd16 | 612 | case SR_DF_ANALOG: |
e7216ae0 SA |
613 | try { |
614 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
615 | } catch (std::bad_alloc) { | |
616 | out_of_memory_ = true; | |
617 | device_->stop(); | |
618 | } | |
aba1dd16 JH |
619 | break; |
620 | ||
2953961c | 621 | case SR_DF_END: |
2e2946fe JH |
622 | { |
623 | { | |
8524a597 | 624 | lock_guard<recursive_mutex> lock(data_mutex_); |
f3d66e52 JH |
625 | cur_logic_segment_.reset(); |
626 | cur_analog_segments_.clear(); | |
2e2946fe | 627 | } |
1f374035 | 628 | frame_ended(); |
2953961c JH |
629 | break; |
630 | } | |
adb0a983 ML |
631 | default: |
632 | break; | |
2e2946fe | 633 | } |
2953961c JH |
634 | } |
635 | ||
51e77110 | 636 | } // namespace pv |