]> sigrok.org Git - pulseview.git/blame_incremental - pv/sigsession.cpp
Handle C-strings as UTF-8
[pulseview.git] / pv / sigsession.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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 <libsigrokdecode/libsigrokdecode.h>
22
23#include "sigsession.h"
24
25#include "devicemanager.h"
26
27#include "data/analog.h"
28#include "data/analogsnapshot.h"
29#include "data/decoderstack.h"
30#include "data/logic.h"
31#include "data/logicsnapshot.h"
32#include "data/decode/decoder.h"
33
34#include "view/analogsignal.h"
35#include "view/decodetrace.h"
36#include "view/logicsignal.h"
37
38#include <assert.h>
39
40#include <stdexcept>
41
42#include <boost/foreach.hpp>
43
44#include <sys/stat.h>
45
46#include <QDebug>
47
48using namespace boost;
49using namespace std;
50
51namespace pv {
52
53// TODO: This should not be necessary
54SigSession* SigSession::_session = NULL;
55
56SigSession::SigSession(DeviceManager &device_manager) :
57 _device_manager(device_manager),
58 _sdi(NULL),
59 _capture_state(Stopped)
60{
61 // TODO: This should not be necessary
62 _session = this;
63}
64
65SigSession::~SigSession()
66{
67 stop_capture();
68
69 _sampling_thread.join();
70
71 if (_sdi)
72 _device_manager.release_device(_sdi);
73 _sdi = NULL;
74
75 // TODO: This should not be necessary
76 _session = NULL;
77}
78
79struct sr_dev_inst* SigSession::get_device() const
80{
81 return _sdi;
82}
83
84void SigSession::set_device(struct sr_dev_inst *sdi)
85{
86 if (_sdi)
87 _device_manager.release_device(_sdi);
88 if (sdi)
89 _device_manager.use_device(sdi, this);
90 _sdi = sdi;
91 update_signals(sdi);
92}
93
94void SigSession::release_device(struct sr_dev_inst *sdi)
95{
96 (void)sdi;
97
98 assert(_capture_state == Stopped);
99 _sdi = NULL;
100 update_signals(NULL);
101}
102
103void SigSession::load_file(const string &name,
104 function<void (const QString)> error_handler)
105{
106 stop_capture();
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
118 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
119 g_slist_free(devlist);
120
121 _decode_traces.clear();
122 update_signals(sdi);
123 read_sample_rate(sdi);
124
125 _sampling_thread = boost::thread(
126 &SigSession::load_session_thread_proc, this,
127 error_handler);
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
136 _decode_traces.clear();
137 update_signals(in->sdi);
138 read_sample_rate(in->sdi);
139
140 _sampling_thread = boost::thread(
141 &SigSession::load_input_thread_proc, this,
142 name, in, error_handler);
143 }
144}
145
146SigSession::capture_state SigSession::get_capture_state() const
147{
148 lock_guard<mutex> lock(_sampling_mutex);
149 return _capture_state;
150}
151
152void SigSession::start_capture(uint64_t record_length,
153 function<void (const QString)> error_handler)
154{
155 stop_capture();
156
157 // Check that a device instance has been selected.
158 if (!_sdi) {
159 qDebug() << "No device selected";
160 return;
161 }
162
163 // Check that at least one probe is enabled
164 const GSList *l;
165 for (l = _sdi->probes; l; l = l->next) {
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
178 _sampling_thread = boost::thread(
179 &SigSession::sample_thread_proc, this, _sdi,
180 record_length, error_handler);
181}
182
183void SigSession::stop_capture()
184{
185 if (get_capture_state() == Stopped)
186 return;
187
188 sr_session_stop();
189
190 // Check that sampling stopped
191 _sampling_thread.join();
192}
193
194vector< shared_ptr<view::Signal> > SigSession::get_signals() const
195{
196 lock_guard<mutex> lock(_signals_mutex);
197 return _signals;
198}
199
200boost::shared_ptr<data::Logic> SigSession::get_data()
201{
202 return _logic_data;
203}
204
205bool SigSession::add_decoder(srd_decoder *const dec)
206{
207 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
208 shared_ptr<data::DecoderStack> decoder_stack;
209
210 try
211 {
212 lock_guard<mutex> lock(_signals_mutex);
213
214 // Create the decoder
215 decoder_stack = shared_ptr<data::DecoderStack>(
216 new data::DecoderStack(dec));
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);
226 if (l && QString::fromUtf8(probe->name).
227 toLower().contains(
228 l->get_name().toLower()))
229 probes[probe] = l;
230 }
231 }
232
233 assert(decoder_stack);
234 assert(!decoder_stack->stack().empty());
235 assert(decoder_stack->stack().front());
236 decoder_stack->stack().front()->set_probes(probes);
237
238 // Create the decode signal
239 shared_ptr<view::DecodeTrace> d(
240 new view::DecodeTrace(*this, decoder_stack,
241 _decode_traces.size()));
242 _decode_traces.push_back(d);
243 }
244 catch(std::runtime_error e)
245 {
246 return false;
247 }
248
249 signals_changed();
250
251 // Do an initial decode
252 decoder_stack->begin_decode();
253
254 return true;
255}
256
257vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
258{
259 lock_guard<mutex> lock(_signals_mutex);
260 return _decode_traces;
261}
262
263void SigSession::remove_decode_signal(view::DecodeTrace *signal)
264{
265 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
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
277void SigSession::set_capture_state(capture_state state)
278{
279 lock_guard<mutex> lock(_sampling_mutex);
280 const bool changed = _capture_state != state;
281 _capture_state = state;
282 if(changed)
283 capture_state_changed(state);
284}
285
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
362void SigSession::update_signals(const sr_dev_inst *const sdi)
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
370 // Clear the decode traces
371 _decode_traces.clear();
372
373 // Detect what data types we will receive
374 if(sdi) {
375 for (const GSList *l = sdi->probes; l; l = l->next) {
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
416 if(sdi) {
417 for (const GSList *l = sdi->probes; l; l = l->next) {
418 sr_probe *const probe = (sr_probe *)l->data;
419 assert(probe);
420
421 switch(probe->type) {
422 case SR_PROBE_LOGIC:
423 signal = shared_ptr<view::Signal>(
424 new view::LogicSignal(*this, probe,
425 _logic_data));
426 break;
427
428 case SR_PROBE_ANALOG:
429 signal = shared_ptr<view::Signal>(
430 new view::AnalogSignal(*this, probe,
431 _analog_data));
432 break;
433 }
434
435 _signals.push_back(signal);
436 }
437 }
438 }
439
440 signals_changed();
441}
442
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
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 {
464 const int ret = sr_config_get(sdi->driver, sdi, NULL,
465 SR_CONF_SAMPLERATE, &gvar);
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
481void SigSession::load_session_thread_proc(
482 function<void (const QString)> error_handler)
483{
484 (void)error_handler;
485
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);
507
508 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
509
510 set_capture_state(Running);
511
512 in->format->loadfile(in, name.c_str());
513
514 sr_session_destroy();
515 set_capture_state(Stopped);
516
517 // Confirm that SR_DF_END was received
518 assert(!_cur_logic_snapshot);
519 assert(!_cur_analog_snapshot);
520
521 delete in;
522}
523
524void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
525 uint64_t record_length,
526 function<void (const QString)> error_handler)
527{
528 assert(sdi);
529 assert(error_handler);
530
531 sr_session_new();
532 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
533
534 if (sr_session_dev_add(sdi) != SR_OK) {
535 error_handler(tr("Failed to use device."));
536 sr_session_destroy();
537 return;
538 }
539
540 // Set the sample limit
541 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
542 g_variant_new_uint64(record_length)) != SR_OK) {
543 error_handler(tr("Failed to configure "
544 "time-based sample limit."));
545 sr_session_destroy();
546 return;
547 }
548
549 if (sr_session_start() != SR_OK) {
550 error_handler(tr("Failed to start session."));
551 return;
552 }
553
554 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
555
556 sr_session_run();
557 sr_session_destroy();
558
559 set_capture_state(Stopped);
560
561 // Confirm that SR_DF_END was received
562 assert(!_cur_logic_snapshot);
563 assert(!_cur_analog_snapshot);
564}
565
566void SigSession::feed_in_header(const sr_dev_inst *sdi)
567{
568 read_sample_rate(sdi);
569}
570
571void SigSession::feed_in_meta(const sr_dev_inst *sdi,
572 const sr_datafeed_meta &meta)
573{
574 (void)sdi;
575
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 }
587 }
588
589 signals_changed();
590}
591
592void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
593{
594 lock_guard<mutex> lock(_data_mutex);
595
596 if (!_logic_data)
597 {
598 qDebug() << "Unexpected logic packet";
599 return;
600 }
601
602 if (!_cur_logic_snapshot)
603 {
604 set_capture_state(Running);
605
606 // Create a new data snapshot
607 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
608 new data::LogicSnapshot(logic));
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
620void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
621{
622 lock_guard<mutex> lock(_data_mutex);
623
624 if(!_analog_data)
625 {
626 qDebug() << "Unexpected analog packet";
627 return; // This analog packet was not expected.
628 }
629
630 if (!_cur_analog_snapshot)
631 {
632 set_capture_state(Running);
633
634 // Create a new data snapshot
635 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
636 new data::AnalogSnapshot(analog));
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}
647
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:
656 feed_in_header(sdi);
657 break;
658
659 case SR_DF_META:
660 assert(packet->payload);
661 feed_in_meta(sdi,
662 *(const sr_datafeed_meta*)packet->payload);
663 break;
664
665 case SR_DF_LOGIC:
666 assert(packet->payload);
667 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
668 break;
669
670 case SR_DF_ANALOG:
671 assert(packet->payload);
672 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
673 break;
674
675 case SR_DF_END:
676 {
677 {
678 lock_guard<mutex> lock(_data_mutex);
679 _cur_logic_snapshot.reset();
680 _cur_analog_snapshot.reset();
681 }
682 data_updated();
683 break;
684 }
685 }
686}
687
688void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
689 const struct sr_datafeed_packet *packet, void *cb_data)
690{
691 (void) cb_data;
692 assert(_session);
693 _session->data_feed_in(sdi, packet);
694}
695
696} // namespace pv