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