]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
2953961c JH |
18 | */ |
19 | ||
47747218 | 20 | #include <QDebug> |
43017494 SA |
21 | #include <QFileInfo> |
22 | ||
1e4d7c4d | 23 | #include <cassert> |
303eec78 | 24 | #include <memory> |
1e4d7c4d SA |
25 | #include <mutex> |
26 | #include <stdexcept> | |
4e5a4405 | 27 | |
1e4d7c4d | 28 | #include <sys/stat.h> |
2953961c | 29 | |
2acdb232 | 30 | #include "devicemanager.hpp" |
5e685656 | 31 | #include "mainwindow.hpp" |
aca9aa83 | 32 | #include "session.hpp" |
119aff65 | 33 | |
2acdb232 | 34 | #include "data/analog.hpp" |
f3d66e52 | 35 | #include "data/analogsegment.hpp" |
aca9aa83 | 36 | #include "data/decode/decoder.hpp" |
2acdb232 | 37 | #include "data/logic.hpp" |
f3d66e52 | 38 | #include "data/logicsegment.hpp" |
bf0edd2b | 39 | #include "data/signalbase.hpp" |
82c7f640 | 40 | |
da30ecb7 | 41 | #include "devices/hardwaredevice.hpp" |
fd22c71c | 42 | #include "devices/inputfile.hpp" |
da30ecb7 JH |
43 | #include "devices/sessionfile.hpp" |
44 | ||
0f8f8c18 SA |
45 | #include "toolbars/mainbar.hpp" |
46 | ||
1573bf16 SA |
47 | #include "views/trace/analogsignal.hpp" |
48 | #include "views/trace/decodetrace.hpp" | |
49 | #include "views/trace/logicsignal.hpp" | |
50 | #include "views/trace/signal.hpp" | |
51 | #include "views/trace/view.hpp" | |
28a4c9c5 | 52 | |
fe3a1c21 | 53 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 54 | |
5f9a5209 SA |
55 | #ifdef ENABLE_FLOW |
56 | #include <gstreamermm.h> | |
57 | #include <libsigrokflow/libsigrokflow.hpp> | |
58 | #endif | |
59 | ||
1e4d7c4d SA |
60 | #ifdef ENABLE_DECODE |
61 | #include <libsigrokdecode/libsigrokdecode.h> | |
ad908057 | 62 | #include "data/decodesignal.hpp" |
1e4d7c4d SA |
63 | #endif |
64 | ||
6f925ba9 | 65 | using std::bad_alloc; |
f9abf97e | 66 | using std::dynamic_pointer_cast; |
6f925ba9 | 67 | using std::find_if; |
d2344534 | 68 | using std::function; |
3b68d03d | 69 | using std::lock_guard; |
d873f4d6 | 70 | using std::list; |
6f925ba9 UH |
71 | using std::make_pair; |
72 | using std::make_shared; | |
819f4c25 | 73 | using std::map; |
6f925ba9 UH |
74 | using std::max; |
75 | using std::move; | |
aca64cac | 76 | using std::mutex; |
fd22c71c | 77 | using std::pair; |
8524a597 | 78 | using std::recursive_mutex; |
6f925ba9 | 79 | using std::runtime_error; |
f9abf97e | 80 | using std::shared_ptr; |
819f4c25 | 81 | using std::string; |
5f9a5209 SA |
82 | #ifdef ENABLE_FLOW |
83 | using std::unique_lock; | |
84 | #endif | |
303eec78 | 85 | using std::unique_ptr; |
78b0af3e | 86 | using std::unordered_set; |
819f4c25 | 87 | using std::vector; |
28a4c9c5 | 88 | |
e8d00928 ML |
89 | using sigrok::Analog; |
90 | using sigrok::Channel; | |
e8d00928 ML |
91 | using sigrok::ConfigKey; |
92 | using sigrok::DatafeedCallbackFunction; | |
e8d00928 | 93 | using sigrok::Error; |
fd22c71c | 94 | using sigrok::InputFormat; |
e8d00928 ML |
95 | using sigrok::Logic; |
96 | using sigrok::Meta; | |
97 | using sigrok::Packet; | |
e8d00928 | 98 | using sigrok::Session; |
e8d00928 ML |
99 | |
100 | using Glib::VariantBase; | |
51e77110 | 101 | |
5f9a5209 SA |
102 | #ifdef ENABLE_FLOW |
103 | using Gst::Bus; | |
104 | using Gst::ElementFactory; | |
105 | using Gst::Pipeline; | |
106 | #endif | |
107 | ||
0a952555 SA |
108 | using pv::views::trace::Signal; |
109 | using pv::views::trace::AnalogSignal; | |
110 | using pv::views::trace::LogicSignal; | |
111 | ||
e8d00928 | 112 | namespace pv { |
419ec4e1 SA |
113 | |
114 | shared_ptr<sigrok::Context> Session::sr_context; | |
115 | ||
101e7a9b | 116 | Session::Session(DeviceManager &device_manager, QString name) : |
8dbbc7f0 | 117 | device_manager_(device_manager), |
33e1afbe | 118 | default_name_(name), |
101e7a9b | 119 | name_(name), |
ff008de6 | 120 | capture_state_(Stopped), |
5ccfc97e SA |
121 | cur_samplerate_(0), |
122 | data_saved_(true) | |
2953961c | 123 | { |
2953961c JH |
124 | } |
125 | ||
2b81ae46 | 126 | Session::~Session() |
2953961c | 127 | { |
04463625 | 128 | // Stop and join to the thread |
5b7cf66c | 129 | stop_capture(); |
2953961c JH |
130 | } |
131 | ||
2b81ae46 | 132 | DeviceManager& Session::device_manager() |
4cb0b033 | 133 | { |
8dbbc7f0 | 134 | return device_manager_; |
4cb0b033 JH |
135 | } |
136 | ||
2b81ae46 | 137 | const DeviceManager& Session::device_manager() const |
4cb0b033 | 138 | { |
8dbbc7f0 | 139 | return device_manager_; |
4cb0b033 JH |
140 | } |
141 | ||
da30ecb7 | 142 | shared_ptr<sigrok::Session> Session::session() const |
1f4caa77 | 143 | { |
da30ecb7 JH |
144 | if (!device_) |
145 | return shared_ptr<sigrok::Session>(); | |
146 | return device_->session(); | |
1f4caa77 JH |
147 | } |
148 | ||
da30ecb7 | 149 | shared_ptr<devices::Device> Session::device() const |
dc0867ff | 150 | { |
8dbbc7f0 | 151 | return device_; |
dc0867ff JH |
152 | } |
153 | ||
101e7a9b SA |
154 | QString Session::name() const |
155 | { | |
156 | return name_; | |
157 | } | |
158 | ||
159 | void Session::set_name(QString name) | |
160 | { | |
161 | if (default_name_.isEmpty()) | |
162 | default_name_ = name; | |
163 | ||
164 | name_ = name; | |
165 | ||
166 | name_changed(); | |
167 | } | |
168 | ||
6f925ba9 | 169 | const list< shared_ptr<views::ViewBase> > Session::views() const |
36a8185e SA |
170 | { |
171 | return views_; | |
172 | } | |
173 | ||
6f925ba9 | 174 | shared_ptr<views::ViewBase> Session::main_view() const |
0f8f8c18 SA |
175 | { |
176 | return main_view_; | |
177 | } | |
178 | ||
6f925ba9 | 179 | void Session::set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar) |
0f8f8c18 SA |
180 | { |
181 | main_bar_ = main_bar; | |
182 | } | |
183 | ||
184 | shared_ptr<pv::toolbars::MainBar> Session::main_bar() const | |
185 | { | |
186 | return main_bar_; | |
187 | } | |
188 | ||
5ccfc97e SA |
189 | bool Session::data_saved() const |
190 | { | |
191 | return data_saved_; | |
192 | } | |
193 | ||
323715c4 SA |
194 | void Session::save_setup(QSettings &settings) const |
195 | { | |
196 | int decode_signals = 0, views = 0; | |
197 | ||
198 | // Save channels and decoders | |
199 | for (const shared_ptr<data::SignalBase>& base : signalbases_) { | |
200 | #ifdef ENABLE_DECODE | |
201 | if (base->is_decode_signal()) { | |
202 | settings.beginGroup("decode_signal" + QString::number(decode_signals++)); | |
203 | base->save_settings(settings); | |
204 | settings.endGroup(); | |
205 | } else | |
206 | #endif | |
207 | { | |
208 | settings.beginGroup(base->internal_name()); | |
209 | base->save_settings(settings); | |
210 | settings.endGroup(); | |
211 | } | |
212 | } | |
213 | ||
214 | settings.setValue("decode_signals", decode_signals); | |
215 | ||
216 | // Save view states and their signal settings | |
217 | // Note: main_view must be saved as view0 | |
218 | settings.beginGroup("view" + QString::number(views++)); | |
219 | main_view_->save_settings(settings); | |
220 | settings.endGroup(); | |
221 | ||
222 | for (const shared_ptr<views::ViewBase>& view : views_) { | |
223 | if (view != main_view_) { | |
224 | settings.beginGroup("view" + QString::number(views++)); | |
db298158 | 225 | settings.setValue("type", view->get_type()); |
323715c4 SA |
226 | view->save_settings(settings); |
227 | settings.endGroup(); | |
228 | } | |
229 | } | |
230 | ||
231 | settings.setValue("views", views); | |
232 | } | |
233 | ||
101e7a9b SA |
234 | void Session::save_settings(QSettings &settings) const |
235 | { | |
236 | map<string, string> dev_info; | |
237 | list<string> key_list; | |
238 | ||
239 | if (device_) { | |
43017494 SA |
240 | shared_ptr<devices::HardwareDevice> hw_device = |
241 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
242 | ||
243 | if (hw_device) { | |
244 | settings.setValue("device_type", "hardware"); | |
245 | settings.beginGroup("device"); | |
246 | ||
326cf6fe UH |
247 | key_list.emplace_back("vendor"); |
248 | key_list.emplace_back("model"); | |
249 | key_list.emplace_back("version"); | |
250 | key_list.emplace_back("serial_num"); | |
251 | key_list.emplace_back("connection_id"); | |
43017494 SA |
252 | |
253 | dev_info = device_manager_.get_device_info(device_); | |
254 | ||
f4ab4b5c | 255 | for (string& key : key_list) { |
43017494 SA |
256 | if (dev_info.count(key)) |
257 | settings.setValue(QString::fromUtf8(key.c_str()), | |
258 | QString::fromUtf8(dev_info.at(key).c_str())); | |
259 | else | |
260 | settings.remove(QString::fromUtf8(key.c_str())); | |
261 | } | |
101e7a9b | 262 | |
43017494 SA |
263 | settings.endGroup(); |
264 | } | |
101e7a9b | 265 | |
43017494 | 266 | shared_ptr<devices::SessionFile> sessionfile_device = |
1325ce42 | 267 | dynamic_pointer_cast<devices::SessionFile>(device_); |
43017494 SA |
268 | |
269 | if (sessionfile_device) { | |
270 | settings.setValue("device_type", "sessionfile"); | |
271 | settings.beginGroup("device"); | |
272 | settings.setValue("filename", QString::fromStdString( | |
273 | sessionfile_device->full_name())); | |
274 | settings.endGroup(); | |
101e7a9b SA |
275 | } |
276 | ||
1325ce42 SA |
277 | shared_ptr<devices::InputFile> inputfile_device = |
278 | dynamic_pointer_cast<devices::InputFile>(device_); | |
279 | ||
280 | if (inputfile_device) { | |
281 | settings.setValue("device_type", "inputfile"); | |
282 | settings.beginGroup("device"); | |
283 | inputfile_device->save_meta_to_settings(settings); | |
284 | settings.endGroup(); | |
285 | } | |
286 | ||
323715c4 SA |
287 | save_setup(settings); |
288 | } | |
289 | } | |
290 | ||
291 | void Session::restore_setup(QSettings &settings) | |
292 | { | |
293 | // Restore channels | |
294 | for (shared_ptr<data::SignalBase> base : signalbases_) { | |
295 | settings.beginGroup(base->internal_name()); | |
296 | base->restore_settings(settings); | |
297 | settings.endGroup(); | |
298 | } | |
299 | ||
300 | // Restore decoders | |
aecae05c | 301 | #ifdef ENABLE_DECODE |
323715c4 SA |
302 | int decode_signals = settings.value("decode_signals").toInt(); |
303 | ||
304 | for (int i = 0; i < decode_signals; i++) { | |
305 | settings.beginGroup("decode_signal" + QString::number(i)); | |
306 | shared_ptr<data::DecodeSignal> signal = add_decode_signal(); | |
307 | signal->restore_settings(settings); | |
308 | settings.endGroup(); | |
309 | } | |
aecae05c | 310 | #endif |
101e7a9b | 311 | |
323715c4 SA |
312 | // Restore views |
313 | int views = settings.value("views").toInt(); | |
3a21afa6 | 314 | |
323715c4 SA |
315 | for (int i = 0; i < views; i++) { |
316 | settings.beginGroup("view" + QString::number(i)); | |
3a21afa6 | 317 | |
323715c4 SA |
318 | if (i > 0) { |
319 | views::ViewType type = (views::ViewType)settings.value("type").toInt(); | |
baf867ed | 320 | add_view(type, this); |
323715c4 SA |
321 | views_.back()->restore_settings(settings); |
322 | } else | |
323 | main_view_->restore_settings(settings); | |
3a21afa6 | 324 | |
323715c4 | 325 | settings.endGroup(); |
101e7a9b SA |
326 | } |
327 | } | |
328 | ||
329 | void Session::restore_settings(QSettings &settings) | |
330 | { | |
43017494 SA |
331 | shared_ptr<devices::Device> device; |
332 | ||
333 | QString device_type = settings.value("device_type").toString(); | |
334 | ||
335 | if (device_type == "hardware") { | |
336 | map<string, string> dev_info; | |
337 | list<string> key_list; | |
338 | ||
339 | // Re-select last used device if possible but only if it's not demo | |
340 | settings.beginGroup("device"); | |
326cf6fe UH |
341 | key_list.emplace_back("vendor"); |
342 | key_list.emplace_back("model"); | |
343 | key_list.emplace_back("version"); | |
344 | key_list.emplace_back("serial_num"); | |
345 | key_list.emplace_back("connection_id"); | |
43017494 SA |
346 | |
347 | for (string key : key_list) { | |
348 | const QString k = QString::fromStdString(key); | |
349 | if (!settings.contains(k)) | |
350 | continue; | |
351 | ||
352 | const string value = settings.value(k).toString().toStdString(); | |
353 | if (!value.empty()) | |
6f925ba9 | 354 | dev_info.insert(make_pair(key, value)); |
43017494 SA |
355 | } |
356 | ||
357 | if (dev_info.count("model") > 0) | |
358 | device = device_manager_.find_device_from_info(dev_info); | |
359 | ||
360 | if (device) | |
361 | set_device(device); | |
362 | ||
363 | settings.endGroup(); | |
101e7a9b SA |
364 | } |
365 | ||
1325ce42 SA |
366 | if ((device_type == "sessionfile") || (device_type == "inputfile")) { |
367 | if (device_type == "sessionfile") { | |
368 | settings.beginGroup("device"); | |
369 | QString filename = settings.value("filename").toString(); | |
370 | settings.endGroup(); | |
371 | ||
372 | if (QFileInfo(filename).isReadable()) { | |
373 | device = make_shared<devices::SessionFile>(device_manager_.context(), | |
374 | filename.toStdString()); | |
375 | } | |
376 | } | |
377 | ||
378 | if (device_type == "inputfile") { | |
379 | settings.beginGroup("device"); | |
380 | device = make_shared<devices::InputFile>(device_manager_.context(), | |
381 | settings); | |
382 | settings.endGroup(); | |
383 | } | |
101e7a9b | 384 | |
1325ce42 | 385 | if (device) { |
43017494 | 386 | set_device(device); |
101e7a9b | 387 | |
403c3e87 | 388 | start_capture([](QString infoMessage) { |
fe060a48 SA |
389 | // TODO Emulate noquote() |
390 | qDebug() << "Session error:" << infoMessage; }); | |
33e1afbe | 391 | |
1325ce42 SA |
392 | set_name(QString::fromStdString( |
393 | dynamic_pointer_cast<devices::File>(device)->display_name(device_manager_))); | |
43017494 SA |
394 | } |
395 | } | |
396 | ||
323715c4 SA |
397 | if (device) |
398 | restore_setup(settings); | |
101e7a9b SA |
399 | } |
400 | ||
fd22c71c SA |
401 | void Session::select_device(shared_ptr<devices::Device> device) |
402 | { | |
403 | try { | |
404 | if (device) | |
405 | set_device(device); | |
406 | else | |
407 | set_default_device(); | |
408 | } catch (const QString &e) { | |
5e685656 | 409 | MainWindow::show_session_error(tr("Failed to select device"), e); |
fd22c71c SA |
410 | } |
411 | } | |
412 | ||
da30ecb7 | 413 | void Session::set_device(shared_ptr<devices::Device> device) |
d64d1596 | 414 | { |
da30ecb7 JH |
415 | assert(device); |
416 | ||
82596f46 JH |
417 | // Ensure we are not capturing before setting the device |
418 | stop_capture(); | |
419 | ||
4d6c6ea3 SA |
420 | if (device_) |
421 | device_->close(); | |
422 | ||
86123e2e SA |
423 | device_.reset(); |
424 | ||
7b254679 | 425 | // Revert name back to default name (e.g. "Session 1") as the data is gone |
101e7a9b SA |
426 | name_ = default_name_; |
427 | name_changed(); | |
428 | ||
5a206446 | 429 | // Remove all stored data and reset all views |
6f925ba9 | 430 | for (shared_ptr<views::ViewBase> view : views_) { |
47e9e7bb | 431 | view->clear_signals(); |
bb7dd726 | 432 | #ifdef ENABLE_DECODE |
f4e57597 | 433 | view->clear_decode_signals(); |
bb7dd726 | 434 | #endif |
5a206446 | 435 | view->reset_view_state(); |
bb7dd726 | 436 | } |
f4ab4b5c | 437 | for (const shared_ptr<data::SignalData>& d : all_signal_data_) |
47e9e7bb | 438 | d->clear(); |
cc9a7898 | 439 | all_signal_data_.clear(); |
47e9e7bb | 440 | signalbases_.clear(); |
4b0d7046 SA |
441 | cur_logic_segment_.reset(); |
442 | ||
f4ab4b5c | 443 | for (auto& entry : cur_analog_segments_) { |
4b0d7046 SA |
444 | shared_ptr<sigrok::Channel>(entry.first).reset(); |
445 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
446 | } | |
447 | ||
448 | logic_data_.reset(); | |
4b0d7046 | 449 | |
86123e2e SA |
450 | signals_changed(); |
451 | ||
6f925ba9 | 452 | device_ = move(device); |
7e0c99bf SA |
453 | |
454 | try { | |
455 | device_->open(); | |
456 | } catch (const QString &e) { | |
457 | device_.reset(); | |
5e685656 | 458 | MainWindow::show_session_error(tr("Failed to open device"), e); |
7e0c99bf SA |
459 | } |
460 | ||
bcd64b9b SA |
461 | if (device_) { |
462 | device_->session()->add_datafeed_callback([=] | |
463 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
464 | data_feed_in(device, packet); | |
465 | }); | |
466 | ||
467 | update_signals(); | |
468 | } | |
ae2d1bc5 | 469 | |
91f8fe8c | 470 | device_changed(); |
ae2d1bc5 | 471 | } |
85843b14 | 472 | |
2b81ae46 | 473 | void Session::set_default_device() |
6fd0b639 | 474 | { |
da30ecb7 | 475 | const list< shared_ptr<devices::HardwareDevice> > &devices = |
8dbbc7f0 | 476 | device_manager_.devices(); |
6fd0b639 | 477 | |
da30ecb7 JH |
478 | if (devices.empty()) |
479 | return; | |
dc0867ff | 480 | |
da30ecb7 | 481 | // Try and find the demo device and select that by default |
6f925ba9 | 482 | const auto iter = find_if(devices.begin(), devices.end(), |
da30ecb7 | 483 | [] (const shared_ptr<devices::HardwareDevice> &d) { |
0fb98cf8 | 484 | return d->hardware_device()->driver()->name() == "demo"; }); |
da30ecb7 | 485 | set_device((iter == devices.end()) ? devices.front() : *iter); |
dc0867ff JH |
486 | } |
487 | ||
31d1aca6 SA |
488 | bool Session::using_file_device() const |
489 | { | |
490 | shared_ptr<devices::SessionFile> sessionfile_device = | |
491 | dynamic_pointer_cast<devices::SessionFile>(device_); | |
492 | ||
493 | shared_ptr<devices::InputFile> inputfile_device = | |
494 | dynamic_pointer_cast<devices::InputFile>(device_); | |
495 | ||
496 | return (sessionfile_device || inputfile_device); | |
497 | } | |
498 | ||
724f29f3 GS |
499 | /** |
500 | * Convert generic options to data types that are specific to InputFormat. | |
501 | * | |
3083cda8 UH |
502 | * @param[in] user_spec Vector of tokenized words, string format. |
503 | * @param[in] fmt_opts Input format's options, result of InputFormat::options(). | |
724f29f3 | 504 | * |
3083cda8 | 505 | * @return Map of options suitable for InputFormat::create_input(). |
724f29f3 GS |
506 | */ |
507 | map<string, Glib::VariantBase> | |
508 | Session::input_format_options(vector<string> user_spec, | |
509 | map<string, shared_ptr<Option>> fmt_opts) | |
510 | { | |
511 | map<string, Glib::VariantBase> result; | |
512 | ||
f4ab4b5c | 513 | for (auto& entry : user_spec) { |
724f29f3 GS |
514 | /* |
515 | * Split key=value specs. Accept entries without separator | |
516 | * (for simplified boolean specifications). | |
517 | */ | |
518 | string key, val; | |
519 | size_t pos = entry.find("="); | |
520 | if (pos == std::string::npos) { | |
521 | key = entry; | |
522 | val = ""; | |
523 | } else { | |
524 | key = entry.substr(0, pos); | |
525 | val = entry.substr(pos + 1); | |
526 | } | |
527 | ||
528 | /* | |
529 | * Skip user specifications that are not a member of the | |
530 | * format's set of supported options. Have the text input | |
531 | * spec converted to the required input format specific | |
532 | * data type. | |
533 | */ | |
534 | auto found = fmt_opts.find(key); | |
535 | if (found == fmt_opts.end()) | |
536 | continue; | |
537 | shared_ptr<Option> opt = found->second; | |
538 | result[key] = opt->parse_string(val); | |
539 | } | |
540 | ||
541 | return result; | |
542 | } | |
543 | ||
611c8625 | 544 | void Session::load_init_file(const string &file_name, |
96dbf014 | 545 | const string &format, const string &setup_file_name) |
fd22c71c SA |
546 | { |
547 | shared_ptr<InputFormat> input_format; | |
724f29f3 | 548 | map<string, Glib::VariantBase> input_opts; |
fd22c71c SA |
549 | |
550 | if (!format.empty()) { | |
551 | const map<string, shared_ptr<InputFormat> > formats = | |
552 | device_manager_.context()->input_formats(); | |
724f29f3 GS |
553 | auto user_opts = pv::util::split_string(format, ":"); |
554 | string user_name = user_opts.front(); | |
555 | user_opts.erase(user_opts.begin()); | |
fd22c71c SA |
556 | const auto iter = find_if(formats.begin(), formats.end(), |
557 | [&](const pair<string, shared_ptr<InputFormat> > f) { | |
724f29f3 | 558 | return f.first == user_name; }); |
fd22c71c | 559 | if (iter == formats.end()) { |
5e685656 | 560 | MainWindow::show_session_error(tr("Error"), |
fd22c71c SA |
561 | tr("Unexpected input format: %s").arg(QString::fromStdString(format))); |
562 | return; | |
563 | } | |
fd22c71c | 564 | input_format = (*iter).second; |
724f29f3 GS |
565 | input_opts = input_format_options(user_opts, |
566 | input_format->options()); | |
fd22c71c SA |
567 | } |
568 | ||
96dbf014 | 569 | load_file(QString::fromStdString(file_name), QString::fromStdString(setup_file_name), |
611c8625 | 570 | input_format, input_opts); |
fd22c71c SA |
571 | } |
572 | ||
96dbf014 SA |
573 | void Session::load_file(QString file_name, QString setup_file_name, |
574 | shared_ptr<sigrok::InputFormat> format, const map<string, Glib::VariantBase> &options) | |
fd22c71c SA |
575 | { |
576 | const QString errorMessage( | |
577 | QString("Failed to load file %1").arg(file_name)); | |
578 | ||
a9674d1c GS |
579 | // In the absence of a caller's format spec, try to auto detect. |
580 | // Assume "sigrok session file" upon lookup miss. | |
581 | if (!format) | |
582 | format = device_manager_.context()->input_format_match(file_name.toStdString()); | |
fd22c71c SA |
583 | try { |
584 | if (format) | |
585 | set_device(shared_ptr<devices::Device>( | |
586 | new devices::InputFile( | |
587 | device_manager_.context(), | |
588 | file_name.toStdString(), | |
589 | format, options))); | |
590 | else | |
591 | set_device(shared_ptr<devices::Device>( | |
592 | new devices::SessionFile( | |
593 | device_manager_.context(), | |
594 | file_name.toStdString()))); | |
30677c13 | 595 | } catch (Error& e) { |
0ea2cfc5 | 596 | MainWindow::show_session_error(tr("Failed to load %1").arg(file_name), e.what()); |
fd22c71c SA |
597 | set_default_device(); |
598 | main_bar_->update_device_list(); | |
599 | return; | |
600 | } | |
601 | ||
96dbf014 | 602 | // Use the input file with .pvs extension if no setup file was given |
b4cc75e0 | 603 | if (setup_file_name.isEmpty()) { |
611c8625 DL |
604 | setup_file_name = file_name; |
605 | setup_file_name.truncate(setup_file_name.lastIndexOf('.')); | |
606 | setup_file_name.append(".pvs"); | |
607 | } | |
96dbf014 | 608 | |
8962d7b3 SA |
609 | if (QFileInfo::exists(setup_file_name) && QFileInfo(setup_file_name).isReadable()) { |
610 | QSettings settings_storage(setup_file_name, QSettings::IniFormat); | |
611 | restore_setup(settings_storage); | |
612 | } | |
613 | ||
fd22c71c SA |
614 | main_bar_->update_device_list(); |
615 | ||
616 | start_capture([&, errorMessage](QString infoMessage) { | |
5e685656 | 617 | MainWindow::show_session_error(errorMessage, infoMessage); }); |
fd22c71c SA |
618 | |
619 | set_name(QFileInfo(file_name).fileName()); | |
620 | } | |
621 | ||
2b81ae46 | 622 | Session::capture_state Session::get_capture_state() const |
5b7cf66c | 623 | { |
8dbbc7f0 JH |
624 | lock_guard<mutex> lock(sampling_mutex_); |
625 | return capture_state_; | |
5b7cf66c JH |
626 | } |
627 | ||
2b81ae46 | 628 | void Session::start_capture(function<void (const QString)> error_handler) |
2e2946fe | 629 | { |
34c11fa7 SA |
630 | if (!device_) { |
631 | error_handler(tr("No active device set, can't start acquisition.")); | |
632 | return; | |
633 | } | |
634 | ||
5b7cf66c JH |
635 | stop_capture(); |
636 | ||
6ac6242b | 637 | // Check that at least one channel is enabled |
e6c1382e JH |
638 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
639 | if (sr_dev) { | |
640 | const auto channels = sr_dev->channels(); | |
6f925ba9 | 641 | if (!any_of(channels.begin(), channels.end(), |
e6c1382e JH |
642 | [](shared_ptr<Channel> channel) { |
643 | return channel->enabled(); })) { | |
644 | error_handler(tr("No channels enabled.")); | |
645 | return; | |
646 | } | |
9c48fa57 JH |
647 | } |
648 | ||
53ea4c6c | 649 | // Clear signal data |
f4ab4b5c | 650 | for (const shared_ptr<data::SignalData>& d : all_signal_data_) |
47e9e7bb | 651 | d->clear(); |
53ea4c6c | 652 | |
7ea2a4ff SA |
653 | trigger_list_.clear(); |
654 | ||
53bb2e1d SA |
655 | // Revert name back to default name (e.g. "Session 1") for real devices |
656 | // as the (possibly saved) data is gone. File devices keep their name. | |
657 | shared_ptr<devices::HardwareDevice> hw_device = | |
658 | dynamic_pointer_cast< devices::HardwareDevice >(device_); | |
659 | ||
660 | if (hw_device) { | |
661 | name_ = default_name_; | |
662 | name_changed(); | |
663 | } | |
101e7a9b | 664 | |
9c48fa57 | 665 | // Begin the session |
8dbbc7f0 | 666 | sampling_thread_ = std::thread( |
2b05d311 | 667 | &Session::sample_thread_proc, this, error_handler); |
2e2946fe JH |
668 | } |
669 | ||
2b81ae46 | 670 | void Session::stop_capture() |
5b7cf66c | 671 | { |
04463625 | 672 | if (get_capture_state() != Stopped) |
da30ecb7 | 673 | device_->stop(); |
5b7cf66c JH |
674 | |
675 | // Check that sampling stopped | |
8dbbc7f0 JH |
676 | if (sampling_thread_.joinable()) |
677 | sampling_thread_.join(); | |
5b7cf66c JH |
678 | } |
679 | ||
6f925ba9 | 680 | void Session::register_view(shared_ptr<views::ViewBase> view) |
47e9e7bb | 681 | { |
bdbc561f | 682 | if (views_.empty()) |
0f8f8c18 | 683 | main_view_ = view; |
0f8f8c18 | 684 | |
3a21afa6 | 685 | views_.push_back(view); |
53e8927d | 686 | |
f5a5b019 | 687 | // Add all device signals |
53e8927d | 688 | update_signals(); |
f5a5b019 SA |
689 | |
690 | // Add all other signals | |
bdbc561f SA |
691 | unordered_set< shared_ptr<data::SignalBase> > view_signalbases = view->signalbases(); |
692 | ||
693 | for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) { | |
694 | const int sb_exists = count_if( | |
695 | view_signalbases.cbegin(), view_signalbases.cend(), | |
696 | [&](const shared_ptr<data::SignalBase> &sb) { | |
697 | return sb == signalbase; | |
698 | }); | |
699 | ||
700 | // Add the signal to the view if it doesn't have it yet | |
701 | if (!sb_exists) | |
702 | switch (signalbase->type()) { | |
703 | case data::SignalBase::AnalogChannel: | |
704 | case data::SignalBase::LogicChannel: | |
705 | case data::SignalBase::MathChannel: | |
706 | view->add_signalbase(signalbase); | |
707 | break; | |
708 | case data::SignalBase::DecodeChannel: | |
f5a5b019 | 709 | #ifdef ENABLE_DECODE |
bdbc561f | 710 | view->add_decode_signal(dynamic_pointer_cast<data::DecodeSignal>(signalbase)); |
f5a5b019 | 711 | #endif |
bdbc561f SA |
712 | break; |
713 | } | |
f5a5b019 SA |
714 | } |
715 | ||
716 | signals_changed(); | |
47e9e7bb SA |
717 | } |
718 | ||
6f925ba9 | 719 | void Session::deregister_view(shared_ptr<views::ViewBase> view) |
47e9e7bb | 720 | { |
6f925ba9 | 721 | views_.remove_if([&](shared_ptr<views::ViewBase> v) { return v == view; }); |
0f8f8c18 SA |
722 | |
723 | if (views_.empty()) { | |
724 | main_view_.reset(); | |
725 | ||
726 | // Without a view there can be no main bar | |
727 | main_bar_.reset(); | |
728 | } | |
47e9e7bb SA |
729 | } |
730 | ||
6f925ba9 | 731 | bool Session::has_view(shared_ptr<views::ViewBase> view) |
101e7a9b | 732 | { |
f4ab4b5c | 733 | for (shared_ptr<views::ViewBase>& v : views_) |
3a21afa6 SA |
734 | if (v == view) |
735 | return true; | |
736 | ||
737 | return false; | |
101e7a9b SA |
738 | } |
739 | ||
2220e942 SA |
740 | double Session::get_samplerate() const |
741 | { | |
742 | double samplerate = 0.0; | |
743 | ||
f4ab4b5c | 744 | for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) { |
47e9e7bb SA |
745 | assert(d); |
746 | const vector< shared_ptr<pv::data::Segment> > segments = | |
747 | d->segments(); | |
f4ab4b5c | 748 | for (const shared_ptr<pv::data::Segment>& s : segments) |
6f925ba9 | 749 | samplerate = max(samplerate, s->samplerate()); |
2220e942 | 750 | } |
2220e942 SA |
751 | // If there is no sample rate given we use samples as unit |
752 | if (samplerate == 0.0) | |
753 | samplerate = 1.0; | |
754 | ||
755 | return samplerate; | |
756 | } | |
757 | ||
9009d9b5 | 758 | uint32_t Session::get_segment_count() const |
7f965464 | 759 | { |
9009d9b5 | 760 | uint32_t value = 0; |
7f965464 | 761 | |
9009d9b5 | 762 | // Find the highest number of segments |
f4ab4b5c | 763 | for (const shared_ptr<data::SignalData>& data : all_signal_data_) |
9009d9b5 SA |
764 | if (data->get_segment_count() > value) |
765 | value = data->get_segment_count(); | |
7f965464 | 766 | |
9009d9b5 | 767 | return value; |
7f965464 SA |
768 | } |
769 | ||
7ea2a4ff SA |
770 | vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const |
771 | { | |
772 | vector<util::Timestamp> result; | |
773 | ||
f4ab4b5c | 774 | for (const pair<uint32_t, util::Timestamp>& entry : trigger_list_) |
7ea2a4ff SA |
775 | if (entry.first == segment_id) |
776 | result.push_back(entry.second); | |
777 | ||
778 | return result; | |
779 | } | |
780 | ||
6f925ba9 | 781 | const unordered_set< shared_ptr<data::SignalBase> > Session::signalbases() const |
2e2946fe | 782 | { |
47e9e7bb | 783 | return signalbases_; |
2e2946fe JH |
784 | } |
785 | ||
89914a86 SA |
786 | bool Session::all_segments_complete(uint32_t segment_id) const |
787 | { | |
788 | bool all_complete = true; | |
789 | ||
f4ab4b5c | 790 | for (const shared_ptr<data::SignalBase>& base : signalbases_) |
89914a86 SA |
791 | if (!base->segment_is_complete(segment_id)) |
792 | all_complete = false; | |
793 | ||
794 | return all_complete; | |
795 | } | |
796 | ||
269528f5 | 797 | #ifdef ENABLE_DECODE |
e771b42d | 798 | shared_ptr<data::DecodeSignal> Session::add_decode_signal() |
82c7f640 | 799 | { |
e771b42d | 800 | shared_ptr<data::DecodeSignal> signal; |
a7336fb2 | 801 | |
2ad82c2e | 802 | try { |
4e5a4405 | 803 | // Create the decode signal |
e771b42d | 804 | signal = make_shared<data::DecodeSignal>(*this); |
bf0edd2b | 805 | |
ad908057 | 806 | signalbases_.insert(signal); |
bb7dd726 | 807 | |
47747218 | 808 | // Add the decode signal to all views |
f4ab4b5c | 809 | for (shared_ptr<views::ViewBase>& view : views_) |
ad908057 | 810 | view->add_decode_signal(signal); |
30677c13 | 811 | } catch (runtime_error& e) { |
e771b42d SA |
812 | remove_decode_signal(signal); |
813 | return nullptr; | |
e92cd4e4 JH |
814 | } |
815 | ||
82c7f640 | 816 | signals_changed(); |
e92cd4e4 | 817 | |
e771b42d | 818 | return signal; |
82c7f640 JH |
819 | } |
820 | ||
ad908057 | 821 | void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) |
38eeddea | 822 | { |
ad908057 | 823 | signalbases_.erase(signal); |
2d25fc47 | 824 | |
f4ab4b5c | 825 | for (shared_ptr<views::ViewBase>& view : views_) |
ad908057 | 826 | view->remove_decode_signal(signal); |
2d25fc47 SA |
827 | |
828 | signals_changed(); | |
c51482b3 | 829 | } |
269528f5 | 830 | #endif |
c51482b3 | 831 | |
2b81ae46 | 832 | void Session::set_capture_state(capture_state state) |
6ac96c2e | 833 | { |
896936e5 TS |
834 | bool changed; |
835 | ||
268fd2b8 | 836 | if (state == Running) |
6592e8fe | 837 | acq_time_.restart(); |
268fd2b8 SA |
838 | if (state == Stopped) |
839 | qDebug("Acquisition took %.2f s", acq_time_.elapsed() / 1000.); | |
840 | ||
896936e5 TS |
841 | { |
842 | lock_guard<mutex> lock(sampling_mutex_); | |
843 | changed = capture_state_ != state; | |
844 | capture_state_ = state; | |
845 | } | |
846 | ||
f3290553 | 847 | if (changed) |
2b49eeb0 | 848 | capture_state_changed(state); |
6ac96c2e JH |
849 | } |
850 | ||
ffe00119 | 851 | void Session::update_signals() |
b087ba7f | 852 | { |
076eefa4 | 853 | if (!device_) { |
47e9e7bb | 854 | signalbases_.clear(); |
076eefa4 | 855 | logic_data_.reset(); |
f4ab4b5c | 856 | for (shared_ptr<views::ViewBase>& view : views_) { |
47e9e7bb | 857 | view->clear_signals(); |
bb7dd726 | 858 | #ifdef ENABLE_DECODE |
f4e57597 | 859 | view->clear_decode_signals(); |
bb7dd726 SA |
860 | #endif |
861 | } | |
076eefa4 SA |
862 | return; |
863 | } | |
e6c1382e JH |
864 | |
865 | lock_guard<recursive_mutex> lock(data_mutex_); | |
b087ba7f | 866 | |
ffe00119 | 867 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
c6412b47 | 868 | if (!sr_dev) { |
47e9e7bb | 869 | signalbases_.clear(); |
c6412b47 | 870 | logic_data_.reset(); |
f4ab4b5c | 871 | for (shared_ptr<views::ViewBase>& view : views_) { |
47e9e7bb | 872 | view->clear_signals(); |
bb7dd726 | 873 | #ifdef ENABLE_DECODE |
f4e57597 | 874 | view->clear_decode_signals(); |
bb7dd726 SA |
875 | #endif |
876 | } | |
c6412b47 JH |
877 | return; |
878 | } | |
879 | ||
b087ba7f | 880 | // Detect what data types we will receive |
c6412b47 | 881 | auto channels = sr_dev->channels(); |
6f925ba9 | 882 | unsigned int logic_channel_count = count_if( |
e8d00928 ML |
883 | channels.begin(), channels.end(), |
884 | [] (shared_ptr<Channel> channel) { | |
472a80c5 | 885 | return channel->type() == sigrok::ChannelType::LOGIC; }); |
b087ba7f | 886 | |
f3d66e52 | 887 | // Create data containers for the logic data segments |
b087ba7f | 888 | { |
8524a597 | 889 | lock_guard<recursive_mutex> data_lock(data_mutex_); |
b087ba7f | 890 | |
3917ba77 JH |
891 | if (logic_channel_count == 0) { |
892 | logic_data_.reset(); | |
893 | } else if (!logic_data_ || | |
894 | logic_data_->num_channels() != logic_channel_count) { | |
8dbbc7f0 | 895 | logic_data_.reset(new data::Logic( |
6ac6242b | 896 | logic_channel_count)); |
8dbbc7f0 | 897 | assert(logic_data_); |
b087ba7f | 898 | } |
b087ba7f JH |
899 | } |
900 | ||
47e9e7bb | 901 | // Make the signals list |
f4ab4b5c | 902 | for (shared_ptr<views::ViewBase>& viewbase : views_) { |
f23c4692 SA |
903 | views::trace::View *trace_view = |
904 | qobject_cast<views::trace::View*>(viewbase.get()); | |
f4e57597 SA |
905 | |
906 | if (trace_view) { | |
0a952555 | 907 | unordered_set< shared_ptr<Signal> > prev_sigs(trace_view->signals()); |
f4e57597 SA |
908 | trace_view->clear_signals(); |
909 | ||
910 | for (auto channel : sr_dev->channels()) { | |
911 | shared_ptr<data::SignalBase> signalbase; | |
0a952555 | 912 | shared_ptr<Signal> signal; |
f4e57597 SA |
913 | |
914 | // Find the channel in the old signals | |
6f925ba9 | 915 | const auto iter = find_if( |
f4e57597 | 916 | prev_sigs.cbegin(), prev_sigs.cend(), |
0a952555 | 917 | [&](const shared_ptr<Signal> &s) { |
f4e57597 SA |
918 | return s->base()->channel() == channel; |
919 | }); | |
920 | if (iter != prev_sigs.end()) { | |
921 | // Copy the signal from the old set to the new | |
922 | signal = *iter; | |
923 | trace_view->add_signal(signal); | |
924 | } else { | |
925 | // Find the signalbase for this channel if possible | |
926 | signalbase.reset(); | |
f4ab4b5c | 927 | for (const shared_ptr<data::SignalBase>& b : signalbases_) |
f4e57597 SA |
928 | if (b->channel() == channel) |
929 | signalbase = b; | |
930 | ||
0a952555 SA |
931 | shared_ptr<Signal> signal; |
932 | ||
f4e57597 SA |
933 | switch(channel->type()->id()) { |
934 | case SR_CHANNEL_LOGIC: | |
935 | if (!signalbase) { | |
472a80c5 SA |
936 | signalbase = make_shared<data::SignalBase>(channel, |
937 | data::SignalBase::LogicChannel); | |
f4e57597 SA |
938 | signalbases_.insert(signalbase); |
939 | ||
940 | all_signal_data_.insert(logic_data_); | |
941 | signalbase->set_data(logic_data_); | |
12ea3616 SA |
942 | |
943 | connect(this, SIGNAL(capture_state_changed(int)), | |
944 | signalbase.get(), SLOT(on_capture_state_changed(int))); | |
f4e57597 SA |
945 | } |
946 | ||
0a952555 | 947 | signal = shared_ptr<Signal>(new LogicSignal(*this, device_, signalbase)); |
f4e57597 SA |
948 | break; |
949 | ||
950 | case SR_CHANNEL_ANALOG: | |
951 | { | |
952 | if (!signalbase) { | |
472a80c5 SA |
953 | signalbase = make_shared<data::SignalBase>(channel, |
954 | data::SignalBase::AnalogChannel); | |
f4e57597 SA |
955 | signalbases_.insert(signalbase); |
956 | ||
957 | shared_ptr<data::Analog> data(new data::Analog()); | |
958 | all_signal_data_.insert(data); | |
959 | signalbase->set_data(data); | |
12ea3616 SA |
960 | |
961 | connect(this, SIGNAL(capture_state_changed(int)), | |
962 | signalbase.get(), SLOT(on_capture_state_changed(int))); | |
f4e57597 SA |
963 | } |
964 | ||
0a952555 | 965 | signal = shared_ptr<Signal>(new AnalogSignal(*this, signalbase)); |
f4e57597 | 966 | break; |
47e9e7bb SA |
967 | } |
968 | ||
f4e57597 | 969 | default: |
0402d7a3 | 970 | assert(false); |
f4e57597 | 971 | break; |
47e9e7bb | 972 | } |
0a952555 SA |
973 | |
974 | // New views take their signal settings from the main view | |
975 | if (!viewbase->is_main_view()) { | |
976 | shared_ptr<pv::views::trace::View> main_tv = | |
977 | dynamic_pointer_cast<pv::views::trace::View>(main_view_); | |
978 | shared_ptr<Signal> main_signal = | |
979 | main_tv->get_signal_by_signalbase(signalbase); | |
980 | signal->restore_settings(main_signal->save_settings()); | |
981 | } | |
982 | ||
983 | trace_view->add_signal(signal); | |
3917ba77 | 984 | } |
b087ba7f JH |
985 | } |
986 | } | |
fbd3e234 | 987 | } |
b087ba7f JH |
988 | |
989 | signals_changed(); | |
990 | } | |
991 | ||
cbd2a2de | 992 | shared_ptr<data::SignalBase> Session::signalbase_from_channel( |
bf0edd2b | 993 | shared_ptr<sigrok::Channel> channel) const |
3ddcc083 | 994 | { |
bf0edd2b | 995 | for (shared_ptr<data::SignalBase> sig : signalbases_) { |
3ddcc083 | 996 | assert(sig); |
6ac6242b | 997 | if (sig->channel() == channel) |
3ddcc083 JH |
998 | return sig; |
999 | } | |
bf0edd2b | 1000 | return shared_ptr<data::SignalBase>(); |
3ddcc083 JH |
1001 | } |
1002 | ||
2b05d311 | 1003 | void Session::sample_thread_proc(function<void (const QString)> error_handler) |
274d4f13 | 1004 | { |
f2edb557 | 1005 | assert(error_handler); |
be73bdfa | 1006 | |
5f9a5209 SA |
1007 | #ifdef ENABLE_FLOW |
1008 | pipeline_ = Pipeline::create(); | |
1009 | ||
1010 | source_ = ElementFactory::create_element("filesrc", "source"); | |
1011 | sink_ = RefPtr<AppSink>::cast_dynamic(ElementFactory::create_element("appsink", "sink")); | |
1012 | ||
1013 | pipeline_->add(source_)->add(sink_); | |
1014 | source_->link(sink_); | |
1015 | ||
1016 | source_->set_property("location", Glib::ustring("/tmp/dummy_binary")); | |
1017 | ||
1018 | sink_->set_property("emit-signals", TRUE); | |
1019 | sink_->signal_new_sample().connect(sigc::mem_fun(*this, &Session::on_gst_new_sample)); | |
1020 | ||
1021 | // Get the bus from the pipeline and add a bus watch to the default main context | |
1022 | RefPtr<Bus> bus = pipeline_->get_bus(); | |
1023 | bus->add_watch(sigc::mem_fun(this, &Session::on_gst_bus_message)); | |
1024 | ||
1025 | // Start pipeline and Wait until it finished processing | |
1026 | pipeline_done_interrupt_ = false; | |
1027 | pipeline_->set_state(Gst::STATE_PLAYING); | |
1028 | ||
1029 | unique_lock<mutex> pipeline_done_lock_(pipeline_done_mutex_); | |
1030 | pipeline_done_cond_.wait(pipeline_done_lock_); | |
1031 | ||
1032 | // Let the pipeline free all resources | |
1033 | pipeline_->set_state(Gst::STATE_NULL); | |
1034 | ||
1035 | #else | |
34c11fa7 SA |
1036 | if (!device_) |
1037 | return; | |
45f0c7c9 | 1038 | |
e170ab65 SA |
1039 | try { |
1040 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
1041 | } catch (Error& e) { | |
1042 | cur_samplerate_ = 0; | |
1043 | } | |
274d4f13 | 1044 | |
e7216ae0 SA |
1045 | out_of_memory_ = false; |
1046 | ||
4e86ec70 SA |
1047 | { |
1048 | lock_guard<recursive_mutex> lock(data_mutex_); | |
1049 | cur_logic_segment_.reset(); | |
1050 | cur_analog_segments_.clear(); | |
1051 | } | |
1052 | highest_segment_id_ = -1; | |
7ea2a4ff | 1053 | frame_began_ = false; |
4e86ec70 | 1054 | |
ae2d1bc5 | 1055 | try { |
5237f0c5 | 1056 | device_->start(); |
30677c13 | 1057 | } catch (Error& e) { |
e8d00928 | 1058 | error_handler(e.what()); |
eec446e1 JH |
1059 | return; |
1060 | } | |
1061 | ||
da30ecb7 | 1062 | set_capture_state(device_->session()->trigger() ? |
07dcf561 | 1063 | AwaitingTrigger : Running); |
eec446e1 | 1064 | |
dc4ada2b SA |
1065 | try { |
1066 | device_->run(); | |
30677c13 | 1067 | } catch (Error& e) { |
dc4ada2b SA |
1068 | error_handler(e.what()); |
1069 | set_capture_state(Stopped); | |
1070 | return; | |
6f381ee7 SA |
1071 | } catch (QString& e) { |
1072 | error_handler(e); | |
1073 | set_capture_state(Stopped); | |
1074 | return; | |
dc4ada2b SA |
1075 | } |
1076 | ||
eec446e1 | 1077 | set_capture_state(Stopped); |
1a2fe44c JH |
1078 | |
1079 | // Confirm that SR_DF_END was received | |
403c3e87 SA |
1080 | if (cur_logic_segment_) |
1081 | qDebug() << "WARNING: SR_DF_END was not received."; | |
5f9a5209 | 1082 | #endif |
e7216ae0 | 1083 | |
5e6967cb SA |
1084 | // Optimize memory usage |
1085 | free_unused_memory(); | |
1086 | ||
5ccfc97e SA |
1087 | // We now have unsaved data unless we just "captured" from a file |
1088 | shared_ptr<devices::File> file_device = | |
1089 | dynamic_pointer_cast<devices::File>(device_); | |
1090 | ||
1091 | if (!file_device) | |
1092 | data_saved_ = false; | |
1093 | ||
e7216ae0 SA |
1094 | if (out_of_memory_) |
1095 | error_handler(tr("Out of memory, acquisition stopped.")); | |
eec446e1 JH |
1096 | } |
1097 | ||
5e6967cb SA |
1098 | void Session::free_unused_memory() |
1099 | { | |
f4ab4b5c | 1100 | for (const shared_ptr<data::SignalData>& data : all_signal_data_) { |
5e6967cb SA |
1101 | const vector< shared_ptr<data::Segment> > segments = data->segments(); |
1102 | ||
f4ab4b5c | 1103 | for (const shared_ptr<data::Segment>& segment : segments) |
5e6967cb | 1104 | segment->free_unused_memory(); |
5e6967cb SA |
1105 | } |
1106 | } | |
1107 | ||
4e86ec70 SA |
1108 | void Session::signal_new_segment() |
1109 | { | |
341d9a79 | 1110 | int new_segment_id = 0; |
4e86ec70 SA |
1111 | |
1112 | if ((cur_logic_segment_ != nullptr) || !cur_analog_segments_.empty()) { | |
1113 | ||
1114 | // Determine new frame/segment number, assuming that all | |
1115 | // signals have the same number of frames/segments | |
1116 | if (cur_logic_segment_) { | |
341d9a79 | 1117 | new_segment_id = logic_data_->get_segment_count() - 1; |
4e86ec70 SA |
1118 | } else { |
1119 | shared_ptr<sigrok::Channel> any_channel = | |
1120 | (*cur_analog_segments_.begin()).first; | |
1121 | ||
1122 | shared_ptr<data::SignalBase> base = signalbase_from_channel(any_channel); | |
1123 | assert(base); | |
1124 | ||
1125 | shared_ptr<data::Analog> data(base->analog_data()); | |
1126 | assert(data); | |
1127 | ||
341d9a79 | 1128 | new_segment_id = data->get_segment_count() - 1; |
4e86ec70 SA |
1129 | } |
1130 | } | |
1131 | ||
1132 | if (new_segment_id > highest_segment_id_) { | |
1133 | highest_segment_id_ = new_segment_id; | |
1134 | new_segment(highest_segment_id_); | |
1135 | } | |
1136 | } | |
1137 | ||
558ad6ce SA |
1138 | void Session::signal_segment_completed() |
1139 | { | |
1140 | int segment_id = 0; | |
1141 | ||
f4ab4b5c | 1142 | for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) { |
558ad6ce SA |
1143 | // We only care about analog and logic channels, not derived ones |
1144 | if (signalbase->type() == data::SignalBase::AnalogChannel) { | |
1145 | segment_id = signalbase->analog_data()->get_segment_count() - 1; | |
1146 | break; | |
1147 | } | |
1148 | ||
1149 | if (signalbase->type() == data::SignalBase::LogicChannel) { | |
1150 | segment_id = signalbase->logic_data()->get_segment_count() - 1; | |
1151 | break; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | if (segment_id >= 0) | |
1156 | segment_completed(segment_id); | |
1157 | } | |
1158 | ||
5f9a5209 SA |
1159 | #ifdef ENABLE_FLOW |
1160 | bool Session::on_gst_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message) | |
1161 | { | |
1162 | (void)bus; | |
1163 | ||
1164 | if ((message->get_source() == pipeline_) && \ | |
1165 | ((message->get_message_type() == Gst::MESSAGE_EOS))) | |
1166 | pipeline_done_cond_.notify_one(); | |
1167 | ||
1168 | // TODO Also evaluate MESSAGE_STREAM_STATUS to receive error notifications | |
1169 | ||
1170 | return true; | |
1171 | } | |
1172 | ||
1173 | Gst::FlowReturn Session::on_gst_new_sample() | |
1174 | { | |
1175 | RefPtr<Gst::Sample> sample = sink_->pull_sample(); | |
1176 | RefPtr<Gst::Buffer> buf = sample->get_buffer(); | |
1177 | ||
1178 | for (uint32_t block_id = 0; block_id < buf->n_memory(); block_id++) { | |
1179 | RefPtr<Gst::Memory> buf_mem = buf->get_memory(block_id); | |
1180 | Gst::MapInfo mapinfo; | |
1181 | buf_mem->map(mapinfo, Gst::MAP_READ); | |
1182 | ||
1183 | shared_ptr<sigrok::Packet> logic_packet = | |
1184 | sr_context->create_logic_packet(mapinfo.get_data(), buf->get_size(), 1); | |
1185 | ||
1186 | try { | |
1187 | feed_in_logic(dynamic_pointer_cast<sigrok::Logic>(logic_packet->payload())); | |
1188 | } catch (bad_alloc&) { | |
1189 | out_of_memory_ = true; | |
1190 | device_->stop(); | |
1191 | buf_mem->unmap(mapinfo); | |
1192 | return Gst::FLOW_ERROR; | |
1193 | } | |
1194 | ||
1195 | buf_mem->unmap(mapinfo); | |
1196 | } | |
1197 | ||
1198 | return Gst::FLOW_OK; | |
1199 | } | |
1200 | #endif | |
1201 | ||
da30ecb7 | 1202 | void Session::feed_in_header() |
eec446e1 | 1203 | { |
b5940cf0 | 1204 | // Nothing to do here for now |
be73bdfa JH |
1205 | } |
1206 | ||
da30ecb7 | 1207 | void Session::feed_in_meta(shared_ptr<Meta> meta) |
be73bdfa | 1208 | { |
f4ab4b5c | 1209 | for (auto& entry : meta->config()) { |
e8d00928 | 1210 | switch (entry.first->id()) { |
be73bdfa | 1211 | case SR_CONF_SAMPLERATE: |
014293fc | 1212 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); |
be73bdfa JH |
1213 | break; |
1214 | default: | |
014293fc | 1215 | qDebug() << "Received meta data key" << entry.first->id() << ", ignoring."; |
be73bdfa JH |
1216 | break; |
1217 | } | |
aba1dd16 | 1218 | } |
74043039 JH |
1219 | |
1220 | signals_changed(); | |
aba1dd16 JH |
1221 | } |
1222 | ||
48257a69 SA |
1223 | void Session::feed_in_trigger() |
1224 | { | |
1225 | // The channel containing most samples should be most accurate | |
1226 | uint64_t sample_count = 0; | |
1227 | ||
cc9a7898 | 1228 | { |
f4ab4b5c | 1229 | for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) { |
cc9a7898 SA |
1230 | assert(d); |
1231 | uint64_t temp_count = 0; | |
1232 | ||
1233 | const vector< shared_ptr<pv::data::Segment> > segments = | |
1234 | d->segments(); | |
1235 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
1236 | temp_count += s->get_sample_count(); | |
1237 | ||
1238 | if (temp_count > sample_count) | |
1239 | sample_count = temp_count; | |
1240 | } | |
48257a69 SA |
1241 | } |
1242 | ||
a66e286e SA |
1243 | uint32_t segment_id = 0; // Default segment when no frames are used |
1244 | ||
1245 | // If a frame began, we'd ideally be able to use the highest segment ID for | |
1246 | // the trigger. However, as new segments are only created when logic or | |
1247 | // analog data comes in, this doesn't work if the trigger appears right | |
1248 | // after the beginning of the frame, before any sample data. | |
1249 | // For this reason, we use highest segment ID + 1 if no sample data came in | |
1250 | // yet and the highest segment ID otherwise. | |
1251 | if (frame_began_) { | |
1252 | segment_id = highest_segment_id_; | |
1253 | if (!cur_logic_segment_ && (cur_analog_segments_.size() == 0)) | |
1254 | segment_id++; | |
1255 | } | |
7ea2a4ff | 1256 | |
a66e286e | 1257 | // TODO Create timestamp from segment start time + segment's current sample count |
7ea2a4ff SA |
1258 | util::Timestamp timestamp = sample_count / get_samplerate(); |
1259 | trigger_list_.emplace_back(segment_id, timestamp); | |
1260 | trigger_event(segment_id, timestamp); | |
48257a69 SA |
1261 | } |
1262 | ||
2b81ae46 | 1263 | void Session::feed_in_frame_begin() |
82f50b10 | 1264 | { |
837bb15a | 1265 | frame_began_ = true; |
82f50b10 JH |
1266 | } |
1267 | ||
837bb15a SA |
1268 | void Session::feed_in_frame_end() |
1269 | { | |
558ad6ce SA |
1270 | if (!frame_began_) |
1271 | return; | |
1272 | ||
837bb15a SA |
1273 | { |
1274 | lock_guard<recursive_mutex> lock(data_mutex_); | |
558ad6ce SA |
1275 | |
1276 | if (cur_logic_segment_) | |
1277 | cur_logic_segment_->set_complete(); | |
1278 | ||
f4ab4b5c | 1279 | for (auto& entry : cur_analog_segments_) { |
558ad6ce SA |
1280 | shared_ptr<data::AnalogSegment> segment = entry.second; |
1281 | segment->set_complete(); | |
1282 | } | |
1283 | ||
837bb15a SA |
1284 | cur_logic_segment_.reset(); |
1285 | cur_analog_segments_.clear(); | |
1286 | } | |
1287 | ||
7ea2a4ff | 1288 | frame_began_ = false; |
558ad6ce SA |
1289 | |
1290 | signal_segment_completed(); | |
837bb15a SA |
1291 | } |
1292 | ||
2b81ae46 | 1293 | void Session::feed_in_logic(shared_ptr<Logic> logic) |
9c112671 | 1294 | { |
eaa02ef4 SA |
1295 | if (logic->data_length() == 0) { |
1296 | qDebug() << "WARNING: Received logic packet with 0 samples."; | |
1297 | return; | |
1298 | } | |
1299 | ||
6f381ee7 SA |
1300 | if (logic->unit_size() > 8) |
1301 | throw QString(tr("Can't handle more than 64 logic channels.")); | |
1302 | ||
b5940cf0 | 1303 | if (!cur_samplerate_) |
e170ab65 SA |
1304 | try { |
1305 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
1306 | } catch (Error& e) { | |
1307 | // Do nothing | |
1308 | } | |
b5940cf0 | 1309 | |
8524a597 | 1310 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 1311 | |
2ad82c2e | 1312 | if (!logic_data_) { |
e6c1382e JH |
1313 | // The only reason logic_data_ would not have been created is |
1314 | // if it was not possible to determine the signals when the | |
1315 | // device was created. | |
1316 | update_signals(); | |
79efbc53 | 1317 | } |
be73bdfa | 1318 | |
2ad82c2e | 1319 | if (!cur_logic_segment_) { |
bb2cdfff | 1320 | // This could be the first packet after a trigger |
2b49eeb0 JH |
1321 | set_capture_state(Running); |
1322 | ||
f3d66e52 | 1323 | // Create a new data segment |
067bb624 | 1324 | cur_logic_segment_ = make_shared<data::LogicSegment>( |
85a70280 SA |
1325 | *logic_data_, logic_data_->get_segment_count(), |
1326 | logic->unit_size(), cur_samplerate_); | |
f3d66e52 | 1327 | logic_data_->push_segment(cur_logic_segment_); |
82f50b10 | 1328 | |
4e86ec70 | 1329 | signal_new_segment(); |
9c112671 JH |
1330 | } |
1331 | ||
04e1acc2 SA |
1332 | cur_logic_segment_->append_payload(logic); |
1333 | ||
1f374035 | 1334 | data_received(); |
9c112671 JH |
1335 | } |
1336 | ||
2b81ae46 | 1337 | void Session::feed_in_analog(shared_ptr<Analog> analog) |
aba1dd16 | 1338 | { |
eaa02ef4 SA |
1339 | if (analog->num_samples() == 0) { |
1340 | qDebug() << "WARNING: Received analog packet with 0 samples."; | |
1341 | return; | |
1342 | } | |
1343 | ||
b5940cf0 | 1344 | if (!cur_samplerate_) |
e170ab65 SA |
1345 | try { |
1346 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); | |
1347 | } catch (Error& e) { | |
1348 | // Do nothing | |
1349 | } | |
b5940cf0 | 1350 | |
8524a597 | 1351 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 1352 | |
e8d00928 | 1353 | const vector<shared_ptr<Channel>> channels = analog->channels(); |
bb2cdfff | 1354 | bool sweep_beginning = false; |
be73bdfa | 1355 | |
3e617752 | 1356 | unique_ptr<float[]> data(new float[analog->num_samples() * channels.size()]); |
303eec78 SA |
1357 | analog->get_data_as_float(data.get()); |
1358 | ||
47e9e7bb | 1359 | if (signalbases_.empty()) |
a3f678a7 | 1360 | update_signals(); |
a3f678a7 | 1361 | |
303eec78 | 1362 | float *channel_data = data.get(); |
f4ab4b5c | 1363 | for (auto& channel : channels) { |
f3d66e52 | 1364 | shared_ptr<data::AnalogSegment> segment; |
2b49eeb0 | 1365 | |
f3d66e52 JH |
1366 | // Try to get the segment of the channel |
1367 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
1368 | iterator iter = cur_analog_segments_.find(channel); | |
1369 | if (iter != cur_analog_segments_.end()) | |
1370 | segment = (*iter).second; | |
2ad82c2e | 1371 | else { |
39ccf9c3 | 1372 | // If no segment was found, this means we haven't |
bb2cdfff | 1373 | // created one yet. i.e. this is the first packet |
f3d66e52 | 1374 | // in the sweep containing this segment. |
bb2cdfff JH |
1375 | sweep_beginning = true; |
1376 | ||
ff4bf6bd | 1377 | // Find the analog data associated with the channel |
cbd2a2de SA |
1378 | shared_ptr<data::SignalBase> base = signalbase_from_channel(channel); |
1379 | assert(base); | |
bb2cdfff | 1380 | |
cbd2a2de | 1381 | shared_ptr<data::Analog> data(base->analog_data()); |
bb2cdfff JH |
1382 | assert(data); |
1383 | ||
7db61e77 | 1384 | // Create a segment, keep it in the maps of channels |
067bb624 | 1385 | segment = make_shared<data::AnalogSegment>( |
85a70280 | 1386 | *data, data->get_segment_count(), cur_samplerate_); |
7db61e77 SA |
1387 | cur_analog_segments_[channel] = segment; |
1388 | ||
f3d66e52 JH |
1389 | // Push the segment into the analog data. |
1390 | data->push_segment(segment); | |
4e86ec70 SA |
1391 | |
1392 | signal_new_segment(); | |
bb2cdfff JH |
1393 | } |
1394 | ||
f3d66e52 | 1395 | assert(segment); |
bb2cdfff | 1396 | |
f3d66e52 | 1397 | // Append the samples in the segment |
3e617752 SA |
1398 | segment->append_interleaved_samples(channel_data++, analog->num_samples(), |
1399 | channels.size()); | |
aba1dd16 | 1400 | } |
bb2cdfff JH |
1401 | |
1402 | if (sweep_beginning) { | |
1403 | // This could be the first packet after a trigger | |
1404 | set_capture_state(Running); | |
aba1dd16 JH |
1405 | } |
1406 | ||
1f374035 | 1407 | data_received(); |
aba1dd16 | 1408 | } |
9c112671 | 1409 | |
da30ecb7 JH |
1410 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, |
1411 | shared_ptr<Packet> packet) | |
b0e1d01d | 1412 | { |
da30ecb7 JH |
1413 | (void)device; |
1414 | ||
e8d00928 | 1415 | assert(device); |
da30ecb7 | 1416 | assert(device == device_->device()); |
b0e1d01d JH |
1417 | assert(packet); |
1418 | ||
e8d00928 | 1419 | switch (packet->type()->id()) { |
b0e1d01d | 1420 | case SR_DF_HEADER: |
da30ecb7 | 1421 | feed_in_header(); |
b0e1d01d JH |
1422 | break; |
1423 | ||
be73bdfa | 1424 | case SR_DF_META: |
da30ecb7 | 1425 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); |
aba1dd16 JH |
1426 | break; |
1427 | ||
48257a69 SA |
1428 | case SR_DF_TRIGGER: |
1429 | feed_in_trigger(); | |
1430 | break; | |
1431 | ||
2e2946fe | 1432 | case SR_DF_LOGIC: |
e7216ae0 SA |
1433 | try { |
1434 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
30677c13 | 1435 | } catch (bad_alloc&) { |
e7216ae0 SA |
1436 | out_of_memory_ = true; |
1437 | device_->stop(); | |
1438 | } | |
2953961c JH |
1439 | break; |
1440 | ||
aba1dd16 | 1441 | case SR_DF_ANALOG: |
e7216ae0 SA |
1442 | try { |
1443 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
30677c13 | 1444 | } catch (bad_alloc&) { |
e7216ae0 SA |
1445 | out_of_memory_ = true; |
1446 | device_->stop(); | |
1447 | } | |
aba1dd16 JH |
1448 | break; |
1449 | ||
837bb15a SA |
1450 | case SR_DF_FRAME_BEGIN: |
1451 | feed_in_frame_begin(); | |
1452 | break; | |
1453 | ||
0a4162a4 | 1454 | case SR_DF_FRAME_END: |
837bb15a SA |
1455 | feed_in_frame_end(); |
1456 | break; | |
1457 | ||
2953961c | 1458 | case SR_DF_END: |
837bb15a SA |
1459 | // Strictly speaking, this is performed when a frame end marker was |
1460 | // received, so there's no point doing this again. However, not all | |
1461 | // devices use frames, and for those devices, we need to do it here. | |
2e2946fe | 1462 | { |
8524a597 | 1463 | lock_guard<recursive_mutex> lock(data_mutex_); |
7c611046 SA |
1464 | |
1465 | if (cur_logic_segment_) | |
1466 | cur_logic_segment_->set_complete(); | |
1467 | ||
f4ab4b5c | 1468 | for (auto& entry : cur_analog_segments_) { |
7c611046 SA |
1469 | shared_ptr<data::AnalogSegment> segment = entry.second; |
1470 | segment->set_complete(); | |
1471 | } | |
1472 | ||
f3d66e52 JH |
1473 | cur_logic_segment_.reset(); |
1474 | cur_analog_segments_.clear(); | |
2e2946fe | 1475 | } |
2953961c | 1476 | break; |
837bb15a | 1477 | |
adb0a983 ML |
1478 | default: |
1479 | break; | |
2e2946fe | 1480 | } |
2953961c JH |
1481 | } |
1482 | ||
5ccfc97e SA |
1483 | void Session::on_data_saved() |
1484 | { | |
1485 | data_saved_ = true; | |
1486 | } | |
1487 | ||
97378470 SA |
1488 | #ifdef ENABLE_DECODE |
1489 | void Session::on_new_decoders_selected(vector<const srd_decoder*> decoders) | |
1490 | { | |
1491 | assert(decoders.size() > 0); | |
1492 | ||
1493 | shared_ptr<data::DecodeSignal> signal = add_decode_signal(); | |
1494 | ||
1495 | if (signal) | |
ed75426f SA |
1496 | for (unsigned int i = 0; i < decoders.size(); i++) { |
1497 | const srd_decoder* d = decoders[i]; | |
1498 | signal->stack_decoder(d, !(i < decoders.size() - 1)); | |
1499 | } | |
97378470 SA |
1500 | } |
1501 | #endif | |
1502 | ||
51e77110 | 1503 | } // namespace pv |