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