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