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