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