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