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