]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Refactored Trace::get_label_rect to use get_y
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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
21#include "sigsession.h"
22
dc0867ff 23#include "devicemanager.h"
119aff65 24
1b1ec774
JH
25#include "data/analog.h"
26#include "data/analogsnapshot.h"
119aff65 27#include "data/decoder.h"
1b1ec774
JH
28#include "data/logic.h"
29#include "data/logicsnapshot.h"
82c7f640 30
aba1dd16 31#include "view/analogsignal.h"
82c7f640 32#include "view/decodesignal.h"
8d634081 33#include "view/logicsignal.h"
28a4c9c5 34
2953961c
JH
35#include <assert.h>
36
728e5ef7
JH
37#include <sys/stat.h>
38
79efbc53
JH
39#include <QDebug>
40
28a4c9c5 41using namespace boost;
e3f65ace 42using namespace std;
28a4c9c5 43
51e77110
JH
44namespace pv {
45
2953961c 46// TODO: This should not be necessary
04abfae9 47SigSession* SigSession::_session = NULL;
2953961c 48
dc0867ff
JH
49SigSession::SigSession(DeviceManager &device_manager) :
50 _device_manager(device_manager),
d64d1596 51 _sdi(NULL),
5b7cf66c 52 _capture_state(Stopped)
2953961c
JH
53{
54 // TODO: This should not be necessary
04abfae9 55 _session = this;
2953961c
JH
56}
57
58SigSession::~SigSession()
59{
5b7cf66c
JH
60 stop_capture();
61
e042ad64 62 _sampling_thread.join();
2e2946fe 63
0f784939
JH
64 if (_sdi)
65 _device_manager.release_device(_sdi);
66 _sdi = NULL;
67
2953961c 68 // TODO: This should not be necessary
04abfae9 69 _session = NULL;
2953961c
JH
70}
71
dc0867ff
JH
72struct sr_dev_inst* SigSession::get_device() const
73{
74 return _sdi;
75}
76
d64d1596
JH
77void SigSession::set_device(struct sr_dev_inst *sdi)
78{
88497156 79 if (_sdi)
dc0867ff 80 _device_manager.release_device(_sdi);
88497156 81 if (sdi)
dc0867ff 82 _device_manager.use_device(sdi, this);
d64d1596 83 _sdi = sdi;
b698553c 84 update_signals(sdi);
d64d1596
JH
85}
86
dc0867ff
JH
87void SigSession::release_device(struct sr_dev_inst *sdi)
88{
89 (void)sdi;
90
91 assert(_capture_state == Stopped);
92 _sdi = NULL;
b698553c 93 update_signals(NULL);
dc0867ff
JH
94}
95
f2edb557
JH
96void SigSession::load_file(const string &name,
97 function<void (const QString)> error_handler)
2953961c 98{
8a67bd9e 99 stop_capture();
e8c9f8a5
JH
100
101 if (sr_session_load(name.c_str()) == SR_OK) {
102 GSList *devlist = NULL;
103 sr_session_dev_list(&devlist);
104
105 if (!devlist || !devlist->data ||
106 sr_session_start() != SR_OK) {
107 error_handler(tr("Failed to start session."));
108 return;
109 }
110
deef291c 111 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
e8c9f8a5
JH
112 g_slist_free(devlist);
113
deef291c
JH
114 update_signals(sdi);
115 read_sample_rate(sdi);
116
e042ad64 117 _sampling_thread = boost::thread(
e8c9f8a5 118 &SigSession::load_session_thread_proc, this,
e042ad64 119 error_handler);
e8c9f8a5
JH
120
121 } else {
122 sr_input *in = NULL;
123
124 if (!(in = load_input_file_format(name.c_str(),
125 error_handler)))
126 return;
127
128 update_signals(in->sdi);
deef291c 129 read_sample_rate(in->sdi);
e8c9f8a5 130
e042ad64 131 _sampling_thread = boost::thread(
e8c9f8a5 132 &SigSession::load_input_thread_proc, this,
e042ad64 133 name, in, error_handler);
e8c9f8a5 134 }
2953961c
JH
135}
136
5b7cf66c
JH
137SigSession::capture_state SigSession::get_capture_state() const
138{
949f8050 139 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
140 return _capture_state;
141}
142
d64d1596 143void SigSession::start_capture(uint64_t record_length,
f2edb557 144 function<void (const QString)> error_handler)
2e2946fe 145{
5b7cf66c
JH
146 stop_capture();
147
d64d1596
JH
148 // Check that a device instance has been selected.
149 if (!_sdi) {
150 qDebug() << "No device selected";
151 return;
152 }
153
9c48fa57
JH
154 // Check that at least one probe is enabled
155 const GSList *l;
d64d1596 156 for (l = _sdi->probes; l; l = l->next) {
9c48fa57
JH
157 sr_probe *const probe = (sr_probe*)l->data;
158 assert(probe);
159 if (probe->enabled)
160 break;
161 }
162
163 if (!l) {
164 error_handler(tr("No probes enabled."));
165 return;
166 }
167
168 // Begin the session
e042ad64 169 _sampling_thread = boost::thread(
d64d1596 170 &SigSession::sample_thread_proc, this, _sdi,
e042ad64 171 record_length, error_handler);
2e2946fe
JH
172}
173
5b7cf66c
JH
174void SigSession::stop_capture()
175{
333d5bbc 176 if (get_capture_state() == Stopped)
5b7cf66c
JH
177 return;
178
179 sr_session_stop();
180
181 // Check that sampling stopped
e042ad64 182 _sampling_thread.join();
5b7cf66c
JH
183}
184
38eeddea 185vector< shared_ptr<view::Signal> > SigSession::get_signals() const
2e2946fe 186{
3868e5fa 187 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
188 return _signals;
189}
190
1b1ec774 191boost::shared_ptr<data::Logic> SigSession::get_data()
2e2946fe
JH
192{
193 return _logic_data;
194}
195
708c5523
JH
196void SigSession::add_decoder(srd_decoder *const dec,
197 std::map<const srd_probe*,
67fe5e9c
JH
198 boost::shared_ptr<view::Signal> > probes,
199 GHashTable *options)
82c7f640
JH
200{
201 {
119aff65 202 lock_guard<mutex> lock(_signals_mutex);
e0fc5810
JH
203
204 if (!_decode_traces.empty()) {
205 qDebug("Currently only one decode trace can be added "
206 "at once");
207 _decode_traces.clear();
208 }
209
119aff65 210 shared_ptr<data::Decoder> decoder(
67fe5e9c 211 new data::Decoder(dec, probes, options));
119aff65 212 shared_ptr<view::DecodeSignal> d(
06bb4e6a
JH
213 new view::DecodeSignal(*this, decoder,
214 _decode_traces.size()));
82c7f640
JH
215 _decode_traces.push_back(d);
216 }
217 signals_changed();
218}
219
38eeddea
JH
220vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
221{
222 lock_guard<mutex> lock(_signals_mutex);
223 return _decode_traces;
224}
225
c51482b3
JH
226void SigSession::remove_decode_signal(view::DecodeSignal *signal)
227{
228 for (vector< shared_ptr<view::DecodeSignal> >::iterator i =
229 _decode_traces.begin();
230 i != _decode_traces.end();
231 i++)
232 if ((*i).get() == signal)
233 {
234 _decode_traces.erase(i);
235 signals_changed();
236 return;
237 }
238}
239
6ac96c2e
JH
240void SigSession::set_capture_state(capture_state state)
241{
949f8050 242 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 243 const bool changed = _capture_state != state;
6ac96c2e 244 _capture_state = state;
2b49eeb0
JH
245 if(changed)
246 capture_state_changed(state);
6ac96c2e
JH
247}
248
728e5ef7
JH
249/**
250 * Attempts to autodetect the format. Failing that
251 * @param filename The filename of the input file.
252 * @return A pointer to the 'struct sr_input_format' that should be used,
253 * or NULL if no input format was selected or auto-detected.
254 */
255sr_input_format* SigSession::determine_input_file_format(
256 const string &filename)
257{
258 int i;
259
260 /* If there are no input formats, return NULL right away. */
261 sr_input_format *const *const inputs = sr_input_list();
262 if (!inputs) {
263 g_critical("No supported input formats available.");
264 return NULL;
265 }
266
267 /* Otherwise, try to find an input module that can handle this file. */
268 for (i = 0; inputs[i]; i++) {
269 if (inputs[i]->format_match(filename.c_str()))
270 break;
271 }
272
273 /* Return NULL if no input module wanted to touch this. */
274 if (!inputs[i]) {
275 g_critical("Error: no matching input module found.");
276 return NULL;
277 }
278
279 return inputs[i];
280}
281
282sr_input* SigSession::load_input_file_format(const string &filename,
283 function<void (const QString)> error_handler,
284 sr_input_format *format)
285{
286 struct stat st;
287 sr_input *in;
288
289 if (!format && !(format =
290 determine_input_file_format(filename.c_str()))) {
291 /* The exact cause was already logged. */
292 return NULL;
293 }
294
295 if (stat(filename.c_str(), &st) == -1) {
296 error_handler(tr("Failed to load file"));
297 return NULL;
298 }
299
300 /* Initialize the input module. */
301 if (!(in = new sr_input)) {
302 qDebug("Failed to allocate input module.\n");
303 return NULL;
304 }
305
306 in->format = format;
307 in->param = NULL;
308 if (in->format->init &&
309 in->format->init(in, filename.c_str()) != SR_OK) {
310 qDebug("Input format init failed.\n");
311 return NULL;
312 }
313
314 sr_session_new();
315
316 if (sr_session_dev_add(in->sdi) != SR_OK) {
317 qDebug("Failed to use device.\n");
318 sr_session_destroy();
319 return NULL;
320 }
321
322 return in;
323}
324
b698553c 325void SigSession::update_signals(const sr_dev_inst *const sdi)
b087ba7f
JH
326{
327 assert(_capture_state == Stopped);
328
329 shared_ptr<view::Signal> signal;
330 unsigned int logic_probe_count = 0;
331 unsigned int analog_probe_count = 0;
332
82c7f640
JH
333 // Clear the decode traces
334 _decode_traces.clear();
335
b087ba7f 336 // Detect what data types we will receive
b698553c
JH
337 if(sdi) {
338 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
339 const sr_probe *const probe = (const sr_probe *)l->data;
340 if (!probe->enabled)
341 continue;
342
343 switch(probe->type) {
344 case SR_PROBE_LOGIC:
345 logic_probe_count++;
346 break;
347
348 case SR_PROBE_ANALOG:
349 analog_probe_count++;
350 break;
351 }
352 }
353 }
354
355 // Create data containers for the data snapshots
356 {
357 lock_guard<mutex> data_lock(_data_mutex);
358
359 _logic_data.reset();
360 if (logic_probe_count != 0) {
361 _logic_data.reset(new data::Logic(
362 logic_probe_count));
363 assert(_logic_data);
364 }
365
366 _analog_data.reset();
367 if (analog_probe_count != 0) {
368 _analog_data.reset(new data::Analog());
369 assert(_analog_data);
370 }
371 }
372
373 // Make the Signals list
374 {
375 lock_guard<mutex> lock(_signals_mutex);
376
377 _signals.clear();
378
b698553c
JH
379 if(sdi) {
380 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
381 const sr_probe *const probe =
382 (const sr_probe *)l->data;
383 assert(probe);
b087ba7f
JH
384
385 switch(probe->type) {
386 case SR_PROBE_LOGIC:
387 signal = shared_ptr<view::Signal>(
c0f86852 388 new view::LogicSignal(*this, probe,
cec48d16 389 _logic_data));
b087ba7f
JH
390 break;
391
392 case SR_PROBE_ANALOG:
393 signal = shared_ptr<view::Signal>(
c0f86852 394 new view::AnalogSignal(*this, probe,
cec48d16 395 _analog_data));
b087ba7f
JH
396 break;
397 }
398
399 _signals.push_back(signal);
400 }
401 }
402 }
403
404 signals_changed();
405}
406
2b49eeb0
JH
407bool SigSession::is_trigger_enabled() const
408{
409 assert(_sdi);
410 for (const GSList *l = _sdi->probes; l; l = l->next) {
411 const sr_probe *const p = (const sr_probe *)l->data;
412 assert(p);
413 if (p->trigger && p->trigger[0] != '\0')
414 return true;
415 }
416
417 return false;
418}
419
deef291c
JH
420void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
421{
422 GVariant *gvar;
423 uint64_t sample_rate = 0;
424
425 // Read out the sample rate
426 if(sdi->driver)
427 {
428 const int ret = sr_config_get(sdi->driver,
429 SR_CONF_SAMPLERATE, &gvar, sdi);
430 if (ret != SR_OK) {
431 qDebug("Failed to get samplerate\n");
432 return;
433 }
434
435 sample_rate = g_variant_get_uint64(gvar);
436 g_variant_unref(gvar);
437 }
438
439 if(_analog_data)
440 _analog_data->set_samplerate(sample_rate);
441 if(_logic_data)
442 _logic_data->set_samplerate(sample_rate);
443}
444
e8c9f8a5 445void SigSession::load_session_thread_proc(
f2edb557 446 function<void (const QString)> error_handler)
8a67bd9e 447{
e8c9f8a5 448 (void)error_handler;
728e5ef7 449
e8c9f8a5
JH
450 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
451
452 set_capture_state(Running);
453
454 sr_session_run();
455
456 sr_session_destroy();
457 set_capture_state(Stopped);
458
459 // Confirm that SR_DF_END was received
460 assert(!_cur_logic_snapshot);
461 assert(!_cur_analog_snapshot);
462}
463
464void SigSession::load_input_thread_proc(const string name,
465 sr_input *in, function<void (const QString)> error_handler)
466{
467 (void)error_handler;
468
469 assert(in);
470 assert(in->format);
8a67bd9e 471
5045f16d 472 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
8a67bd9e 473
8a67bd9e
JH
474 set_capture_state(Running);
475
e8c9f8a5 476 in->format->loadfile(in, name.c_str());
8a67bd9e 477
728e5ef7 478 sr_session_destroy();
8a67bd9e 479 set_capture_state(Stopped);
1a2fe44c
JH
480
481 // Confirm that SR_DF_END was received
482 assert(!_cur_logic_snapshot);
483 assert(!_cur_analog_snapshot);
728e5ef7
JH
484
485 delete in;
8a67bd9e
JH
486}
487
2e2946fe 488void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
f2edb557
JH
489 uint64_t record_length,
490 function<void (const QString)> error_handler)
274d4f13 491{
be73bdfa 492 assert(sdi);
f2edb557 493 assert(error_handler);
be73bdfa 494
274d4f13 495 sr_session_new();
5045f16d 496 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
274d4f13
JH
497
498 if (sr_session_dev_add(sdi) != SR_OK) {
f2edb557 499 error_handler(tr("Failed to use device."));
274d4f13
JH
500 sr_session_destroy();
501 return;
502 }
503
be73bdfa
JH
504 // Set the sample limit
505 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
8eb9d311 506 g_variant_new_uint64(record_length)) != SR_OK) {
f2edb557
JH
507 error_handler(tr("Failed to configure "
508 "time-based sample limit."));
274d4f13
JH
509 sr_session_destroy();
510 return;
511 }
512
eec446e1 513 if (sr_session_start() != SR_OK) {
f2edb557 514 error_handler(tr("Failed to start session."));
eec446e1
JH
515 return;
516 }
517
2b49eeb0 518 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
eec446e1
JH
519
520 sr_session_run();
521 sr_session_destroy();
522
523 set_capture_state(Stopped);
1a2fe44c
JH
524
525 // Confirm that SR_DF_END was received
526 assert(!_cur_logic_snapshot);
527 assert(!_cur_analog_snapshot);
eec446e1
JH
528}
529
530void SigSession::feed_in_header(const sr_dev_inst *sdi)
531{
deef291c 532 read_sample_rate(sdi);
be73bdfa
JH
533}
534
535void SigSession::feed_in_meta(const sr_dev_inst *sdi,
536 const sr_datafeed_meta &meta)
537{
94fe58ef
UH
538 (void)sdi;
539
be73bdfa
JH
540 for (const GSList *l = meta.config; l; l = l->next) {
541 const sr_config *const src = (const sr_config*)l->data;
542 switch (src->key) {
543 case SR_CONF_SAMPLERATE:
544 /// @todo handle samplerate changes
545 /// samplerate = (uint64_t *)src->value;
546 break;
547 default:
548 // Unknown metadata is not an error.
549 break;
550 }
aba1dd16 551 }
74043039
JH
552
553 signals_changed();
aba1dd16
JH
554}
555
9c112671
JH
556void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
557{
558 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
559
560 if (!_logic_data)
9c112671 561 {
79efbc53
JH
562 qDebug() << "Unexpected logic packet";
563 return;
564 }
be73bdfa 565
79efbc53
JH
566 if (!_cur_logic_snapshot)
567 {
2b49eeb0
JH
568 set_capture_state(Running);
569
9c112671 570 // Create a new data snapshot
1b1ec774
JH
571 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
572 new data::LogicSnapshot(logic));
9c112671
JH
573 _logic_data->push_snapshot(_cur_logic_snapshot);
574 }
575 else
576 {
577 // Append to the existing data snapshot
578 _cur_logic_snapshot->append_payload(logic);
579 }
580
581 data_updated();
582}
583
aba1dd16
JH
584void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
585{
586 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
587
588 if(!_analog_data)
aba1dd16 589 {
79efbc53
JH
590 qDebug() << "Unexpected analog packet";
591 return; // This analog packet was not expected.
592 }
be73bdfa 593
79efbc53
JH
594 if (!_cur_analog_snapshot)
595 {
2b49eeb0
JH
596 set_capture_state(Running);
597
aba1dd16 598 // Create a new data snapshot
1b1ec774
JH
599 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
600 new data::AnalogSnapshot(analog));
aba1dd16
JH
601 _analog_data->push_snapshot(_cur_analog_snapshot);
602 }
603 else
604 {
605 // Append to the existing data snapshot
606 _cur_analog_snapshot->append_payload(analog);
607 }
608
609 data_updated();
610}
9c112671 611
b0e1d01d
JH
612void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
613 const struct sr_datafeed_packet *packet)
614{
615 assert(sdi);
616 assert(packet);
617
618 switch (packet->type) {
619 case SR_DF_HEADER:
eec446e1 620 feed_in_header(sdi);
b0e1d01d
JH
621 break;
622
be73bdfa 623 case SR_DF_META:
aba1dd16 624 assert(packet->payload);
be73bdfa
JH
625 feed_in_meta(sdi,
626 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
627 break;
628
2e2946fe 629 case SR_DF_LOGIC:
28a4c9c5 630 assert(packet->payload);
9c112671 631 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
632 break;
633
aba1dd16
JH
634 case SR_DF_ANALOG:
635 assert(packet->payload);
636 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
637 break;
638
2953961c 639 case SR_DF_END:
2e2946fe
JH
640 {
641 {
642 lock_guard<mutex> lock(_data_mutex);
643 _cur_logic_snapshot.reset();
aba1dd16 644 _cur_analog_snapshot.reset();
2e2946fe 645 }
04abfae9 646 data_updated();
2953961c
JH
647 break;
648 }
2e2946fe 649 }
2953961c
JH
650}
651
04abfae9 652void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 653 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 654{
5045f16d 655 (void) cb_data;
04abfae9
JH
656 assert(_session);
657 _session->data_feed_in(sdi, packet);
2953961c 658}
51e77110
JH
659
660} // namespace pv