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