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