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