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