]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 | ||
21 | #ifdef _WIN32 | |
22 | // Windows: Avoid boost/thread namespace pollution (which includes windows.h). | |
23 | #define NOGDI | |
24 | #define NORESOURCE | |
25 | #endif | |
26 | #include <boost/thread/locks.hpp> | |
27 | #include <boost/thread/shared_mutex.hpp> | |
28 | ||
29 | #include <QFileInfo> | |
30 | ||
31 | #include <cassert> | |
32 | #include <mutex> | |
33 | #include <stdexcept> | |
34 | ||
35 | #include <sys/stat.h> | |
36 | ||
37 | #include "session.hpp" | |
38 | #include "devicemanager.hpp" | |
39 | ||
40 | #include "data/analog.hpp" | |
41 | #include "data/analogsegment.hpp" | |
42 | #include "data/decoderstack.hpp" | |
43 | #include "data/logic.hpp" | |
44 | #include "data/logicsegment.hpp" | |
45 | #include "data/signalbase.hpp" | |
46 | #include "data/decode/decoder.hpp" | |
47 | ||
48 | #include "devices/hardwaredevice.hpp" | |
49 | #include "devices/sessionfile.hpp" | |
50 | ||
51 | #include "toolbars/mainbar.hpp" | |
52 | ||
53 | #include "view/analogsignal.hpp" | |
54 | #include "view/decodetrace.hpp" | |
55 | #include "view/logicsignal.hpp" | |
56 | #include "view/signal.hpp" | |
57 | #include "view/view.hpp" | |
58 | ||
59 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
60 | ||
61 | #ifdef ENABLE_DECODE | |
62 | #include <libsigrokdecode/libsigrokdecode.h> | |
63 | #endif | |
64 | ||
65 | using boost::shared_lock; | |
66 | using boost::shared_mutex; | |
67 | using boost::unique_lock; | |
68 | ||
69 | using std::dynamic_pointer_cast; | |
70 | using std::function; | |
71 | using std::lock_guard; | |
72 | using std::list; | |
73 | using std::map; | |
74 | using std::mutex; | |
75 | using std::recursive_mutex; | |
76 | using std::set; | |
77 | using std::shared_ptr; | |
78 | using std::string; | |
79 | using std::unordered_set; | |
80 | using std::vector; | |
81 | ||
82 | using sigrok::Analog; | |
83 | using sigrok::Channel; | |
84 | using sigrok::ChannelType; | |
85 | using sigrok::ConfigKey; | |
86 | using sigrok::DatafeedCallbackFunction; | |
87 | using sigrok::Error; | |
88 | using sigrok::Header; | |
89 | using sigrok::Logic; | |
90 | using sigrok::Meta; | |
91 | using sigrok::Packet; | |
92 | using sigrok::PacketPayload; | |
93 | using sigrok::Session; | |
94 | using sigrok::SessionDevice; | |
95 | ||
96 | using Glib::VariantBase; | |
97 | using Glib::Variant; | |
98 | ||
99 | namespace pv { | |
100 | Session::Session(DeviceManager &device_manager, QString name) : | |
101 | device_manager_(device_manager), | |
102 | default_name_(name), | |
103 | name_(name), | |
104 | capture_state_(Stopped), | |
105 | cur_samplerate_(0) | |
106 | { | |
107 | } | |
108 | ||
109 | Session::~Session() | |
110 | { | |
111 | // Stop and join to the thread | |
112 | stop_capture(); | |
113 | } | |
114 | ||
115 | DeviceManager& Session::device_manager() | |
116 | { | |
117 | return device_manager_; | |
118 | } | |
119 | ||
120 | const DeviceManager& Session::device_manager() const | |
121 | { | |
122 | return device_manager_; | |
123 | } | |
124 | ||
125 | shared_ptr<sigrok::Session> Session::session() const | |
126 | { | |
127 | if (!device_) | |
128 | return shared_ptr<sigrok::Session>(); | |
129 | return device_->session(); | |
130 | } | |
131 | ||
132 | shared_ptr<devices::Device> Session::device() const | |
133 | { | |
134 | return device_; | |
135 | } | |
136 | ||
137 | QString Session::name() const | |
138 | { | |
139 | return name_; | |
140 | } | |
141 | ||
142 | void Session::set_name(QString name) | |
143 | { | |
144 | if (default_name_.isEmpty()) | |
145 | default_name_ = name; | |
146 | ||
147 | name_ = name; | |
148 | ||
149 | name_changed(); | |
150 | } | |
151 | ||
152 | const std::list< std::shared_ptr<views::ViewBase> > Session::views() const | |
153 | { | |
154 | return views_; | |
155 | } | |
156 | ||
157 | std::shared_ptr<views::ViewBase> Session::main_view() const | |
158 | { | |
159 | return main_view_; | |
160 | } | |
161 | ||
162 | void Session::set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar) | |
163 | { | |
164 | main_bar_ = main_bar; | |
165 | } | |
166 | ||
167 | shared_ptr<pv::toolbars::MainBar> Session::main_bar() const | |
168 | { | |
169 | return main_bar_; | |
170 | } | |
171 | ||
172 | void Session::save_settings(QSettings &settings) const | |
173 | { | |
174 | map<string, string> dev_info; | |
175 | list<string> key_list; | |
176 | int stacks = 0, views = 0; | |
177 | ||
178 | if (device_) { | |
179 | shared_ptr<devices::HardwareDevice> hw_device = | |
180 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
181 | ||
182 | if (hw_device) { | |
183 | settings.setValue("device_type", "hardware"); | |
184 | settings.beginGroup("device"); | |
185 | ||
186 | key_list.push_back("vendor"); | |
187 | key_list.push_back("model"); | |
188 | key_list.push_back("version"); | |
189 | key_list.push_back("serial_num"); | |
190 | key_list.push_back("connection_id"); | |
191 | ||
192 | dev_info = device_manager_.get_device_info(device_); | |
193 | ||
194 | for (string key : key_list) { | |
195 | if (dev_info.count(key)) | |
196 | settings.setValue(QString::fromUtf8(key.c_str()), | |
197 | QString::fromUtf8(dev_info.at(key).c_str())); | |
198 | else | |
199 | settings.remove(QString::fromUtf8(key.c_str())); | |
200 | } | |
201 | ||
202 | settings.endGroup(); | |
203 | } | |
204 | ||
205 | shared_ptr<devices::SessionFile> sessionfile_device = | |
206 | dynamic_pointer_cast< devices::SessionFile >(device_); | |
207 | ||
208 | if (sessionfile_device) { | |
209 | settings.setValue("device_type", "sessionfile"); | |
210 | settings.beginGroup("device"); | |
211 | settings.setValue("filename", QString::fromStdString( | |
212 | sessionfile_device->full_name())); | |
213 | settings.endGroup(); | |
214 | } | |
215 | ||
216 | // Save channels and decoders | |
217 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
218 | #ifdef ENABLE_DECODE | |
219 | if (base->is_decode_signal()) { | |
220 | shared_ptr<pv::data::DecoderStack> decoder_stack = | |
221 | base->decoder_stack(); | |
222 | std::shared_ptr<data::decode::Decoder> top_decoder = | |
223 | decoder_stack->stack().front(); | |
224 | ||
225 | settings.beginGroup("decoder_stack" + QString::number(stacks++)); | |
226 | settings.setValue("id", top_decoder->decoder()->id); | |
227 | settings.setValue("name", top_decoder->decoder()->name); | |
228 | settings.endGroup(); | |
229 | } else | |
230 | #endif | |
231 | { | |
232 | settings.beginGroup(base->internal_name()); | |
233 | base->save_settings(settings); | |
234 | settings.endGroup(); | |
235 | } | |
236 | } | |
237 | ||
238 | settings.setValue("decoder_stacks", stacks); | |
239 | ||
240 | // Save view states and their signal settings | |
241 | // Note: main_view must be saved as view0 | |
242 | settings.beginGroup("view" + QString::number(views++)); | |
243 | main_view_->save_settings(settings); | |
244 | settings.endGroup(); | |
245 | ||
246 | for (shared_ptr<views::ViewBase> view : views_) { | |
247 | if (view != main_view_) { | |
248 | settings.beginGroup("view" + QString::number(views++)); | |
249 | view->save_settings(settings); | |
250 | settings.endGroup(); | |
251 | } | |
252 | } | |
253 | ||
254 | settings.setValue("views", views); | |
255 | } | |
256 | } | |
257 | ||
258 | void Session::restore_settings(QSettings &settings) | |
259 | { | |
260 | shared_ptr<devices::Device> device; | |
261 | ||
262 | QString device_type = settings.value("device_type").toString(); | |
263 | ||
264 | if (device_type == "hardware") { | |
265 | map<string, string> dev_info; | |
266 | list<string> key_list; | |
267 | ||
268 | // Re-select last used device if possible but only if it's not demo | |
269 | settings.beginGroup("device"); | |
270 | key_list.push_back("vendor"); | |
271 | key_list.push_back("model"); | |
272 | key_list.push_back("version"); | |
273 | key_list.push_back("serial_num"); | |
274 | key_list.push_back("connection_id"); | |
275 | ||
276 | for (string key : key_list) { | |
277 | const QString k = QString::fromStdString(key); | |
278 | if (!settings.contains(k)) | |
279 | continue; | |
280 | ||
281 | const string value = settings.value(k).toString().toStdString(); | |
282 | if (!value.empty()) | |
283 | dev_info.insert(std::make_pair(key, value)); | |
284 | } | |
285 | ||
286 | if (dev_info.count("model") > 0) | |
287 | device = device_manager_.find_device_from_info(dev_info); | |
288 | ||
289 | if (device) | |
290 | set_device(device); | |
291 | ||
292 | settings.endGroup(); | |
293 | } | |
294 | ||
295 | if (device_type == "sessionfile") { | |
296 | settings.beginGroup("device"); | |
297 | QString filename = settings.value("filename").toString(); | |
298 | settings.endGroup(); | |
299 | ||
300 | if (QFileInfo(filename).isReadable()) { | |
301 | device = std::make_shared<devices::SessionFile>(device_manager_.context(), | |
302 | filename.toStdString()); | |
303 | set_device(device); | |
304 | ||
305 | // TODO Perform error handling | |
306 | start_capture([](QString infoMessage) { (void)infoMessage; }); | |
307 | ||
308 | set_name(QFileInfo(filename).fileName()); | |
309 | } | |
310 | } | |
311 | ||
312 | if (device) { | |
313 | // Restore channels | |
314 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
315 | settings.beginGroup(base->internal_name()); | |
316 | base->restore_settings(settings); | |
317 | settings.endGroup(); | |
318 | } | |
319 | ||
320 | // Restore decoders | |
321 | #ifdef ENABLE_DECODE | |
322 | int stacks = settings.value("decoder_stacks").toInt(); | |
323 | ||
324 | for (int i = 0; i < stacks; i++) { | |
325 | settings.beginGroup("decoder_stack" + QString::number(i++)); | |
326 | ||
327 | QString id = settings.value("id").toString(); | |
328 | add_decoder(srd_decoder_get_by_id(id.toStdString().c_str())); | |
329 | ||
330 | settings.endGroup(); | |
331 | } | |
332 | #endif | |
333 | ||
334 | // Restore views | |
335 | int views = settings.value("views").toInt(); | |
336 | ||
337 | for (int i = 0; i < views; i++) { | |
338 | settings.beginGroup("view" + QString::number(i)); | |
339 | ||
340 | if (i > 0) { | |
341 | views::ViewType type = (views::ViewType)settings.value("type").toInt(); | |
342 | add_view(name_, type, this); | |
343 | views_.back()->restore_settings(settings); | |
344 | } else | |
345 | main_view_->restore_settings(settings); | |
346 | ||
347 | settings.endGroup(); | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | void Session::set_device(shared_ptr<devices::Device> device) | |
353 | { | |
354 | assert(device); | |
355 | ||
356 | // Ensure we are not capturing before setting the device | |
357 | stop_capture(); | |
358 | ||
359 | if (device_) | |
360 | device_->close(); | |
361 | ||
362 | device_.reset(); | |
363 | ||
364 | // Revert name back to default name (e.g. "Untitled-1") as the data is gone | |
365 | name_ = default_name_; | |
366 | name_changed(); | |
367 | ||
368 | // Remove all stored data | |
369 | for (std::shared_ptr<views::ViewBase> view : views_) { | |
370 | view->clear_signals(); | |
371 | #ifdef ENABLE_DECODE | |
372 | view->clear_decode_signals(); | |
373 | #endif | |
374 | } | |
375 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
376 | d->clear(); | |
377 | all_signal_data_.clear(); | |
378 | signalbases_.clear(); | |
379 | cur_logic_segment_.reset(); | |
380 | ||
381 | for (auto entry : cur_analog_segments_) { | |
382 | shared_ptr<sigrok::Channel>(entry.first).reset(); | |
383 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
384 | } | |
385 | ||
386 | logic_data_.reset(); | |
387 | ||
388 | signals_changed(); | |
389 | ||
390 | device_ = std::move(device); | |
391 | ||
392 | try { | |
393 | device_->open(); | |
394 | } catch (const QString &e) { | |
395 | device_.reset(); | |
396 | device_changed(); | |
397 | throw; | |
398 | } | |
399 | ||
400 | device_->session()->add_datafeed_callback([=] | |
401 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
402 | data_feed_in(device, packet); | |
403 | }); | |
404 | ||
405 | update_signals(); | |
406 | device_changed(); | |
407 | } | |
408 | ||
409 | void Session::set_default_device() | |
410 | { | |
411 | const list< shared_ptr<devices::HardwareDevice> > &devices = | |
412 | device_manager_.devices(); | |
413 | ||
414 | if (devices.empty()) | |
415 | return; | |
416 | ||
417 | // Try and find the demo device and select that by default | |
418 | const auto iter = std::find_if(devices.begin(), devices.end(), | |
419 | [] (const shared_ptr<devices::HardwareDevice> &d) { | |
420 | return d->hardware_device()->driver()->name() == | |
421 | "demo"; }); | |
422 | set_device((iter == devices.end()) ? devices.front() : *iter); | |
423 | } | |
424 | ||
425 | Session::capture_state Session::get_capture_state() const | |
426 | { | |
427 | lock_guard<mutex> lock(sampling_mutex_); | |
428 | return capture_state_; | |
429 | } | |
430 | ||
431 | void Session::start_capture(function<void (const QString)> error_handler) | |
432 | { | |
433 | if (!device_) { | |
434 | error_handler(tr("No active device set, can't start acquisition.")); | |
435 | return; | |
436 | } | |
437 | ||
438 | stop_capture(); | |
439 | ||
440 | // Check that at least one channel is enabled | |
441 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
442 | if (sr_dev) { | |
443 | const auto channels = sr_dev->channels(); | |
444 | if (!std::any_of(channels.begin(), channels.end(), | |
445 | [](shared_ptr<Channel> channel) { | |
446 | return channel->enabled(); })) { | |
447 | error_handler(tr("No channels enabled.")); | |
448 | return; | |
449 | } | |
450 | } | |
451 | ||
452 | // Clear signal data | |
453 | for (const shared_ptr<data::SignalData> d : all_signal_data_) | |
454 | d->clear(); | |
455 | ||
456 | // Revert name back to default name (e.g. "Untitled-1") as the data is gone | |
457 | name_ = default_name_; | |
458 | name_changed(); | |
459 | ||
460 | // Begin the session | |
461 | sampling_thread_ = std::thread( | |
462 | &Session::sample_thread_proc, this, error_handler); | |
463 | } | |
464 | ||
465 | void Session::stop_capture() | |
466 | { | |
467 | if (get_capture_state() != Stopped) | |
468 | device_->stop(); | |
469 | ||
470 | // Check that sampling stopped | |
471 | if (sampling_thread_.joinable()) | |
472 | sampling_thread_.join(); | |
473 | } | |
474 | ||
475 | void Session::register_view(std::shared_ptr<views::ViewBase> view) | |
476 | { | |
477 | if (views_.empty()) { | |
478 | main_view_ = view; | |
479 | } | |
480 | ||
481 | views_.push_back(view); | |
482 | ||
483 | update_signals(); | |
484 | } | |
485 | ||
486 | void Session::deregister_view(std::shared_ptr<views::ViewBase> view) | |
487 | { | |
488 | views_.remove_if([&](std::shared_ptr<views::ViewBase> v) { | |
489 | return v == view; }); | |
490 | ||
491 | if (views_.empty()) { | |
492 | main_view_.reset(); | |
493 | ||
494 | // Without a view there can be no main bar | |
495 | main_bar_.reset(); | |
496 | } | |
497 | } | |
498 | ||
499 | bool Session::has_view(std::shared_ptr<views::ViewBase> view) | |
500 | { | |
501 | for (std::shared_ptr<views::ViewBase> v : views_) | |
502 | if (v == view) | |
503 | return true; | |
504 | ||
505 | return false; | |
506 | } | |
507 | ||
508 | double Session::get_samplerate() const | |
509 | { | |
510 | double samplerate = 0.0; | |
511 | ||
512 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
513 | assert(d); | |
514 | const vector< shared_ptr<pv::data::Segment> > segments = | |
515 | d->segments(); | |
516 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
517 | samplerate = std::max(samplerate, s->samplerate()); | |
518 | } | |
519 | // If there is no sample rate given we use samples as unit | |
520 | if (samplerate == 0.0) | |
521 | samplerate = 1.0; | |
522 | ||
523 | return samplerate; | |
524 | } | |
525 | ||
526 | const std::unordered_set< std::shared_ptr<data::SignalBase> > | |
527 | Session::signalbases() const | |
528 | { | |
529 | return signalbases_; | |
530 | } | |
531 | ||
532 | #ifdef ENABLE_DECODE | |
533 | bool Session::add_decoder(srd_decoder *const dec) | |
534 | { | |
535 | map<const srd_channel*, shared_ptr<data::SignalBase> > channels; | |
536 | shared_ptr<data::DecoderStack> decoder_stack; | |
537 | ||
538 | try { | |
539 | // Create the decoder | |
540 | decoder_stack = shared_ptr<data::DecoderStack>( | |
541 | new data::DecoderStack(*this, dec)); | |
542 | ||
543 | // Make a list of all the channels | |
544 | std::vector<const srd_channel*> all_channels; | |
545 | for (const GSList *i = dec->channels; i; i = i->next) | |
546 | all_channels.push_back((const srd_channel*)i->data); | |
547 | for (const GSList *i = dec->opt_channels; i; i = i->next) | |
548 | all_channels.push_back((const srd_channel*)i->data); | |
549 | ||
550 | // Auto select the initial channels | |
551 | for (const srd_channel *pdch : all_channels) | |
552 | for (shared_ptr<data::SignalBase> b : signalbases_) { | |
553 | if (b->type() == ChannelType::LOGIC) { | |
554 | if (QString::fromUtf8(pdch->name).toLower(). | |
555 | contains(b->name().toLower())) | |
556 | channels[pdch] = b; | |
557 | } | |
558 | } | |
559 | ||
560 | assert(decoder_stack); | |
561 | assert(!decoder_stack->stack().empty()); | |
562 | assert(decoder_stack->stack().front()); | |
563 | decoder_stack->stack().front()->set_channels(channels); | |
564 | ||
565 | // Create the decode signal | |
566 | shared_ptr<data::SignalBase> signalbase = | |
567 | shared_ptr<data::SignalBase>(new data::SignalBase(nullptr)); | |
568 | ||
569 | signalbase->set_decoder_stack(decoder_stack); | |
570 | signalbases_.insert(signalbase); | |
571 | ||
572 | for (std::shared_ptr<views::ViewBase> view : views_) | |
573 | view->add_decode_signal(signalbase); | |
574 | } catch (std::runtime_error e) { | |
575 | return false; | |
576 | } | |
577 | ||
578 | signals_changed(); | |
579 | ||
580 | // Do an initial decode | |
581 | decoder_stack->begin_decode(); | |
582 | ||
583 | return true; | |
584 | } | |
585 | ||
586 | void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase) | |
587 | { | |
588 | signalbases_.erase(signalbase); | |
589 | ||
590 | for (std::shared_ptr<views::ViewBase> view : views_) | |
591 | view->remove_decode_signal(signalbase); | |
592 | ||
593 | signals_changed(); | |
594 | } | |
595 | #endif | |
596 | ||
597 | void Session::set_capture_state(capture_state state) | |
598 | { | |
599 | bool changed; | |
600 | ||
601 | { | |
602 | lock_guard<mutex> lock(sampling_mutex_); | |
603 | changed = capture_state_ != state; | |
604 | capture_state_ = state; | |
605 | } | |
606 | ||
607 | if (changed) | |
608 | capture_state_changed(state); | |
609 | } | |
610 | ||
611 | void Session::update_signals() | |
612 | { | |
613 | if (!device_) { | |
614 | signalbases_.clear(); | |
615 | logic_data_.reset(); | |
616 | for (std::shared_ptr<views::ViewBase> view : views_) { | |
617 | view->clear_signals(); | |
618 | #ifdef ENABLE_DECODE | |
619 | view->clear_decode_signals(); | |
620 | #endif | |
621 | } | |
622 | return; | |
623 | } | |
624 | ||
625 | lock_guard<recursive_mutex> lock(data_mutex_); | |
626 | ||
627 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); | |
628 | if (!sr_dev) { | |
629 | signalbases_.clear(); | |
630 | logic_data_.reset(); | |
631 | for (std::shared_ptr<views::ViewBase> view : views_) { | |
632 | view->clear_signals(); | |
633 | #ifdef ENABLE_DECODE | |
634 | view->clear_decode_signals(); | |
635 | #endif | |
636 | } | |
637 | return; | |
638 | } | |
639 | ||
640 | // Detect what data types we will receive | |
641 | auto channels = sr_dev->channels(); | |
642 | unsigned int logic_channel_count = std::count_if( | |
643 | channels.begin(), channels.end(), | |
644 | [] (shared_ptr<Channel> channel) { | |
645 | return channel->type() == ChannelType::LOGIC; }); | |
646 | ||
647 | // Create data containers for the logic data segments | |
648 | { | |
649 | lock_guard<recursive_mutex> data_lock(data_mutex_); | |
650 | ||
651 | if (logic_channel_count == 0) { | |
652 | logic_data_.reset(); | |
653 | } else if (!logic_data_ || | |
654 | logic_data_->num_channels() != logic_channel_count) { | |
655 | logic_data_.reset(new data::Logic( | |
656 | logic_channel_count)); | |
657 | assert(logic_data_); | |
658 | } | |
659 | } | |
660 | ||
661 | // Make the signals list | |
662 | for (std::shared_ptr<views::ViewBase> viewbase : views_) { | |
663 | views::TraceView::View *trace_view = | |
664 | qobject_cast<views::TraceView::View*>(viewbase.get()); | |
665 | ||
666 | if (trace_view) { | |
667 | unordered_set< shared_ptr<views::TraceView::Signal> > | |
668 | prev_sigs(trace_view->signals()); | |
669 | trace_view->clear_signals(); | |
670 | ||
671 | for (auto channel : sr_dev->channels()) { | |
672 | shared_ptr<data::SignalBase> signalbase; | |
673 | shared_ptr<views::TraceView::Signal> signal; | |
674 | ||
675 | // Find the channel in the old signals | |
676 | const auto iter = std::find_if( | |
677 | prev_sigs.cbegin(), prev_sigs.cend(), | |
678 | [&](const shared_ptr<views::TraceView::Signal> &s) { | |
679 | return s->base()->channel() == channel; | |
680 | }); | |
681 | if (iter != prev_sigs.end()) { | |
682 | // Copy the signal from the old set to the new | |
683 | signal = *iter; | |
684 | trace_view->add_signal(signal); | |
685 | } else { | |
686 | // Find the signalbase for this channel if possible | |
687 | signalbase.reset(); | |
688 | for (const shared_ptr<data::SignalBase> b : signalbases_) | |
689 | if (b->channel() == channel) | |
690 | signalbase = b; | |
691 | ||
692 | switch(channel->type()->id()) { | |
693 | case SR_CHANNEL_LOGIC: | |
694 | if (!signalbase) { | |
695 | signalbase = shared_ptr<data::SignalBase>( | |
696 | new data::SignalBase(channel)); | |
697 | signalbases_.insert(signalbase); | |
698 | ||
699 | all_signal_data_.insert(logic_data_); | |
700 | signalbase->set_data(logic_data_); | |
701 | } | |
702 | ||
703 | signal = shared_ptr<views::TraceView::Signal>( | |
704 | new views::TraceView::LogicSignal(*this, | |
705 | device_, signalbase)); | |
706 | trace_view->add_signal(signal); | |
707 | break; | |
708 | ||
709 | case SR_CHANNEL_ANALOG: | |
710 | { | |
711 | if (!signalbase) { | |
712 | signalbase = shared_ptr<data::SignalBase>( | |
713 | new data::SignalBase(channel)); | |
714 | signalbases_.insert(signalbase); | |
715 | ||
716 | shared_ptr<data::Analog> data(new data::Analog()); | |
717 | all_signal_data_.insert(data); | |
718 | signalbase->set_data(data); | |
719 | } | |
720 | ||
721 | signal = shared_ptr<views::TraceView::Signal>( | |
722 | new views::TraceView::AnalogSignal( | |
723 | *this, signalbase)); | |
724 | trace_view->add_signal(signal); | |
725 | break; | |
726 | } | |
727 | ||
728 | default: | |
729 | assert(0); | |
730 | break; | |
731 | } | |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
736 | ||
737 | signals_changed(); | |
738 | } | |
739 | ||
740 | shared_ptr<data::SignalBase> Session::signalbase_from_channel( | |
741 | shared_ptr<sigrok::Channel> channel) const | |
742 | { | |
743 | for (shared_ptr<data::SignalBase> sig : signalbases_) { | |
744 | assert(sig); | |
745 | if (sig->channel() == channel) | |
746 | return sig; | |
747 | } | |
748 | return shared_ptr<data::SignalBase>(); | |
749 | } | |
750 | ||
751 | void Session::sample_thread_proc(function<void (const QString)> error_handler) | |
752 | { | |
753 | assert(error_handler); | |
754 | ||
755 | if (!device_) | |
756 | return; | |
757 | ||
758 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
759 | ||
760 | out_of_memory_ = false; | |
761 | ||
762 | try { | |
763 | device_->start(); | |
764 | } catch (Error e) { | |
765 | error_handler(e.what()); | |
766 | return; | |
767 | } | |
768 | ||
769 | set_capture_state(device_->session()->trigger() ? | |
770 | AwaitingTrigger : Running); | |
771 | ||
772 | device_->run(); | |
773 | set_capture_state(Stopped); | |
774 | ||
775 | // Confirm that SR_DF_END was received | |
776 | if (cur_logic_segment_) { | |
777 | qDebug("SR_DF_END was not received."); | |
778 | assert(0); | |
779 | } | |
780 | ||
781 | if (out_of_memory_) | |
782 | error_handler(tr("Out of memory, acquisition stopped.")); | |
783 | } | |
784 | ||
785 | void Session::feed_in_header() | |
786 | { | |
787 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
788 | } | |
789 | ||
790 | void Session::feed_in_meta(shared_ptr<Meta> meta) | |
791 | { | |
792 | for (auto entry : meta->config()) { | |
793 | switch (entry.first->id()) { | |
794 | case SR_CONF_SAMPLERATE: | |
795 | // We can't rely on the header to always contain the sample rate, | |
796 | // so in case it's supplied via a meta packet, we use it. | |
797 | if (!cur_samplerate_) | |
798 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); | |
799 | ||
800 | /// @todo handle samplerate changes | |
801 | break; | |
802 | default: | |
803 | // Unknown metadata is not an error. | |
804 | break; | |
805 | } | |
806 | } | |
807 | ||
808 | signals_changed(); | |
809 | } | |
810 | ||
811 | void Session::feed_in_trigger() | |
812 | { | |
813 | // The channel containing most samples should be most accurate | |
814 | uint64_t sample_count = 0; | |
815 | ||
816 | { | |
817 | for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) { | |
818 | assert(d); | |
819 | uint64_t temp_count = 0; | |
820 | ||
821 | const vector< shared_ptr<pv::data::Segment> > segments = | |
822 | d->segments(); | |
823 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
824 | temp_count += s->get_sample_count(); | |
825 | ||
826 | if (temp_count > sample_count) | |
827 | sample_count = temp_count; | |
828 | } | |
829 | } | |
830 | ||
831 | trigger_event(sample_count / get_samplerate()); | |
832 | } | |
833 | ||
834 | void Session::feed_in_frame_begin() | |
835 | { | |
836 | if (cur_logic_segment_ || !cur_analog_segments_.empty()) | |
837 | frame_began(); | |
838 | } | |
839 | ||
840 | void Session::feed_in_logic(shared_ptr<Logic> logic) | |
841 | { | |
842 | lock_guard<recursive_mutex> lock(data_mutex_); | |
843 | ||
844 | const size_t sample_count = logic->data_length() / logic->unit_size(); | |
845 | ||
846 | if (!logic_data_) { | |
847 | // The only reason logic_data_ would not have been created is | |
848 | // if it was not possible to determine the signals when the | |
849 | // device was created. | |
850 | update_signals(); | |
851 | } | |
852 | ||
853 | if (!cur_logic_segment_) { | |
854 | // This could be the first packet after a trigger | |
855 | set_capture_state(Running); | |
856 | ||
857 | // Create a new data segment | |
858 | cur_logic_segment_ = shared_ptr<data::LogicSegment>( | |
859 | new data::LogicSegment( | |
860 | logic, cur_samplerate_, sample_count)); | |
861 | logic_data_->push_segment(cur_logic_segment_); | |
862 | ||
863 | // @todo Putting this here means that only listeners querying | |
864 | // for logic will be notified. Currently the only user of | |
865 | // frame_began is DecoderStack, but in future we need to signal | |
866 | // this after both analog and logic sweeps have begun. | |
867 | frame_began(); | |
868 | } else { | |
869 | // Append to the existing data segment | |
870 | cur_logic_segment_->append_payload(logic); | |
871 | } | |
872 | ||
873 | data_received(); | |
874 | } | |
875 | ||
876 | void Session::feed_in_analog(shared_ptr<Analog> analog) | |
877 | { | |
878 | lock_guard<recursive_mutex> lock(data_mutex_); | |
879 | ||
880 | const vector<shared_ptr<Channel>> channels = analog->channels(); | |
881 | const unsigned int channel_count = channels.size(); | |
882 | const size_t sample_count = analog->num_samples() / channel_count; | |
883 | const float *data = static_cast<const float *>(analog->data_pointer()); | |
884 | bool sweep_beginning = false; | |
885 | ||
886 | if (signalbases_.empty()) | |
887 | update_signals(); | |
888 | ||
889 | for (auto channel : channels) { | |
890 | shared_ptr<data::AnalogSegment> segment; | |
891 | ||
892 | // Try to get the segment of the channel | |
893 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
894 | iterator iter = cur_analog_segments_.find(channel); | |
895 | if (iter != cur_analog_segments_.end()) | |
896 | segment = (*iter).second; | |
897 | else { | |
898 | // If no segment was found, this means we haven't | |
899 | // created one yet. i.e. this is the first packet | |
900 | // in the sweep containing this segment. | |
901 | sweep_beginning = true; | |
902 | ||
903 | // Create a segment, keep it in the maps of channels | |
904 | segment = shared_ptr<data::AnalogSegment>( | |
905 | new data::AnalogSegment( | |
906 | cur_samplerate_, sample_count)); | |
907 | cur_analog_segments_[channel] = segment; | |
908 | ||
909 | // Find the analog data associated with the channel | |
910 | shared_ptr<data::SignalBase> base = signalbase_from_channel(channel); | |
911 | assert(base); | |
912 | ||
913 | shared_ptr<data::Analog> data(base->analog_data()); | |
914 | assert(data); | |
915 | ||
916 | // Push the segment into the analog data. | |
917 | data->push_segment(segment); | |
918 | } | |
919 | ||
920 | assert(segment); | |
921 | ||
922 | // Append the samples in the segment | |
923 | segment->append_interleaved_samples(data++, sample_count, | |
924 | channel_count); | |
925 | } | |
926 | ||
927 | if (sweep_beginning) { | |
928 | // This could be the first packet after a trigger | |
929 | set_capture_state(Running); | |
930 | } | |
931 | ||
932 | data_received(); | |
933 | } | |
934 | ||
935 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, | |
936 | shared_ptr<Packet> packet) | |
937 | { | |
938 | (void)device; | |
939 | ||
940 | assert(device); | |
941 | assert(device == device_->device()); | |
942 | assert(packet); | |
943 | ||
944 | switch (packet->type()->id()) { | |
945 | case SR_DF_HEADER: | |
946 | feed_in_header(); | |
947 | break; | |
948 | ||
949 | case SR_DF_META: | |
950 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); | |
951 | break; | |
952 | ||
953 | case SR_DF_TRIGGER: | |
954 | feed_in_trigger(); | |
955 | break; | |
956 | ||
957 | case SR_DF_FRAME_BEGIN: | |
958 | feed_in_frame_begin(); | |
959 | break; | |
960 | ||
961 | case SR_DF_LOGIC: | |
962 | try { | |
963 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
964 | } catch (std::bad_alloc) { | |
965 | out_of_memory_ = true; | |
966 | device_->stop(); | |
967 | } | |
968 | break; | |
969 | ||
970 | case SR_DF_ANALOG: | |
971 | try { | |
972 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
973 | } catch (std::bad_alloc) { | |
974 | out_of_memory_ = true; | |
975 | device_->stop(); | |
976 | } | |
977 | break; | |
978 | ||
979 | case SR_DF_END: | |
980 | { | |
981 | { | |
982 | lock_guard<recursive_mutex> lock(data_mutex_); | |
983 | cur_logic_segment_.reset(); | |
984 | cur_analog_segments_.clear(); | |
985 | } | |
986 | frame_ended(); | |
987 | break; | |
988 | } | |
989 | default: | |
990 | break; | |
991 | } | |
992 | } | |
993 | ||
994 | } // namespace pv |