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