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