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