]> sigrok.org Git - pulseview.git/blob - pv/sigsession.cpp
Integrated decode
[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.push_back(d);
214         }
215         signals_changed();
216 }
217
218 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
219 {
220         lock_guard<mutex> lock(_signals_mutex);
221         return _decode_traces;
222 }
223
224 void SigSession::set_capture_state(capture_state state)
225 {
226         lock_guard<mutex> lock(_sampling_mutex);
227         const bool changed = _capture_state != state;
228         _capture_state = state;
229         if(changed)
230                 capture_state_changed(state);
231 }
232
233 /**
234  * Attempts to autodetect the format. Failing that
235  * @param filename The filename of the input file.
236  * @return A pointer to the 'struct sr_input_format' that should be used,
237  *         or NULL if no input format was selected or auto-detected.
238  */
239 sr_input_format* SigSession::determine_input_file_format(
240         const string &filename)
241 {
242         int i;
243
244         /* If there are no input formats, return NULL right away. */
245         sr_input_format *const *const inputs = sr_input_list();
246         if (!inputs) {
247                 g_critical("No supported input formats available.");
248                 return NULL;
249         }
250
251         /* Otherwise, try to find an input module that can handle this file. */
252         for (i = 0; inputs[i]; i++) {
253                 if (inputs[i]->format_match(filename.c_str()))
254                         break;
255         }
256
257         /* Return NULL if no input module wanted to touch this. */
258         if (!inputs[i]) {
259                 g_critical("Error: no matching input module found.");
260                 return NULL;
261         }
262
263         return inputs[i];
264 }
265
266 sr_input* SigSession::load_input_file_format(const string &filename,
267         function<void (const QString)> error_handler,
268         sr_input_format *format)
269 {
270         struct stat st;
271         sr_input *in;
272
273         if (!format && !(format =
274                 determine_input_file_format(filename.c_str()))) {
275                 /* The exact cause was already logged. */
276                 return NULL;
277         }
278
279         if (stat(filename.c_str(), &st) == -1) {
280                 error_handler(tr("Failed to load file"));
281                 return NULL;
282         }
283
284         /* Initialize the input module. */
285         if (!(in = new sr_input)) {
286                 qDebug("Failed to allocate input module.\n");
287                 return NULL;
288         }
289
290         in->format = format;
291         in->param = NULL;
292         if (in->format->init &&
293                 in->format->init(in, filename.c_str()) != SR_OK) {
294                 qDebug("Input format init failed.\n");
295                 return NULL;
296         }
297
298         sr_session_new();
299
300         if (sr_session_dev_add(in->sdi) != SR_OK) {
301                 qDebug("Failed to use device.\n");
302                 sr_session_destroy();
303                 return NULL;
304         }
305
306         return in;
307 }
308
309 void SigSession::update_signals(const sr_dev_inst *const sdi)
310 {
311         assert(_capture_state == Stopped);
312
313         shared_ptr<view::Signal> signal;
314         unsigned int logic_probe_count = 0;
315         unsigned int analog_probe_count = 0;
316
317         // Clear the decode traces
318         _decode_traces.clear();
319
320         // Detect what data types we will receive
321         if(sdi) {
322                 for (const GSList *l = sdi->probes; l; l = l->next) {
323                         const sr_probe *const probe = (const sr_probe *)l->data;
324                         if (!probe->enabled)
325                                 continue;
326
327                         switch(probe->type) {
328                         case SR_PROBE_LOGIC:
329                                 logic_probe_count++;
330                                 break;
331
332                         case SR_PROBE_ANALOG:
333                                 analog_probe_count++;
334                                 break;
335                         }
336                 }
337         }
338
339         // Create data containers for the data snapshots
340         {
341                 lock_guard<mutex> data_lock(_data_mutex);
342
343                 _logic_data.reset();
344                 if (logic_probe_count != 0) {
345                         _logic_data.reset(new data::Logic(
346                                 logic_probe_count));
347                         assert(_logic_data);
348                 }
349
350                 _analog_data.reset();
351                 if (analog_probe_count != 0) {
352                         _analog_data.reset(new data::Analog());
353                         assert(_analog_data);
354                 }
355         }
356
357         // Make the Signals list
358         {
359                 lock_guard<mutex> lock(_signals_mutex);
360
361                 _signals.clear();
362
363                 if(sdi) {
364                         for (const GSList *l = sdi->probes; l; l = l->next) {
365                                 const sr_probe *const probe =
366                                         (const sr_probe *)l->data;
367                                 assert(probe);
368
369                                 switch(probe->type) {
370                                 case SR_PROBE_LOGIC:
371                                         signal = shared_ptr<view::Signal>(
372                                                 new view::LogicSignal(*this, probe,
373                                                         _logic_data));
374                                         break;
375
376                                 case SR_PROBE_ANALOG:
377                                         signal = shared_ptr<view::Signal>(
378                                                 new view::AnalogSignal(*this, probe,
379                                                         _analog_data));
380                                         break;
381                                 }
382
383                                 _signals.push_back(signal);
384                         }
385                 }
386         }
387
388         signals_changed();
389 }
390
391 bool SigSession::is_trigger_enabled() const
392 {
393         assert(_sdi);
394         for (const GSList *l = _sdi->probes; l; l = l->next) {
395                 const sr_probe *const p = (const sr_probe *)l->data;
396                 assert(p);
397                 if (p->trigger && p->trigger[0] != '\0')
398                         return true;
399         }
400
401         return false;
402 }
403
404 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
405 {
406         GVariant *gvar;
407         uint64_t sample_rate = 0;
408
409         // Read out the sample rate
410         if(sdi->driver)
411         {
412                 const int ret = sr_config_get(sdi->driver,
413                         SR_CONF_SAMPLERATE, &gvar, sdi);
414                 if (ret != SR_OK) {
415                         qDebug("Failed to get samplerate\n");
416                         return;
417                 }
418
419                 sample_rate = g_variant_get_uint64(gvar);
420                 g_variant_unref(gvar);
421         }
422
423         if(_analog_data)
424                 _analog_data->set_samplerate(sample_rate);
425         if(_logic_data)
426                 _logic_data->set_samplerate(sample_rate);
427 }
428
429 void SigSession::load_session_thread_proc(
430         function<void (const QString)> error_handler)
431 {
432         (void)error_handler;
433
434         sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
435
436         set_capture_state(Running);
437
438         sr_session_run();
439
440         sr_session_destroy();
441         set_capture_state(Stopped);
442
443         // Confirm that SR_DF_END was received
444         assert(!_cur_logic_snapshot);
445         assert(!_cur_analog_snapshot);
446 }
447
448 void SigSession::load_input_thread_proc(const string name,
449         sr_input *in, function<void (const QString)> error_handler)
450 {
451         (void)error_handler;
452
453         assert(in);
454         assert(in->format);
455
456         sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
457
458         set_capture_state(Running);
459
460         in->format->loadfile(in, name.c_str());
461
462         sr_session_destroy();
463         set_capture_state(Stopped);
464
465         // Confirm that SR_DF_END was received
466         assert(!_cur_logic_snapshot);
467         assert(!_cur_analog_snapshot);
468
469         delete in;
470 }
471
472 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
473         uint64_t record_length,
474         function<void (const QString)> error_handler)
475 {
476         assert(sdi);
477         assert(error_handler);
478
479         sr_session_new();
480         sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
481
482         if (sr_session_dev_add(sdi) != SR_OK) {
483                 error_handler(tr("Failed to use device."));
484                 sr_session_destroy();
485                 return;
486         }
487
488         // Set the sample limit
489         if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
490                 g_variant_new_uint64(record_length)) != SR_OK) {
491                 error_handler(tr("Failed to configure "
492                         "time-based sample limit."));
493                 sr_session_destroy();
494                 return;
495         }
496
497         if (sr_session_start() != SR_OK) {
498                 error_handler(tr("Failed to start session."));
499                 return;
500         }
501
502         set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
503
504         sr_session_run();
505         sr_session_destroy();
506
507         set_capture_state(Stopped);
508
509         // Confirm that SR_DF_END was received
510         assert(!_cur_logic_snapshot);
511         assert(!_cur_analog_snapshot);
512 }
513
514 void SigSession::feed_in_header(const sr_dev_inst *sdi)
515 {
516         read_sample_rate(sdi);
517 }
518
519 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
520         const sr_datafeed_meta &meta)
521 {
522         (void)sdi;
523
524         for (const GSList *l = meta.config; l; l = l->next) {
525                 const sr_config *const src = (const sr_config*)l->data;
526                 switch (src->key) {
527                 case SR_CONF_SAMPLERATE:
528                         /// @todo handle samplerate changes
529                         /// samplerate = (uint64_t *)src->value;
530                         break;
531                 default:
532                         // Unknown metadata is not an error.
533                         break;
534                 }
535         }
536
537         signals_changed();
538 }
539
540 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
541 {
542         lock_guard<mutex> lock(_data_mutex);
543
544         if (!_logic_data)
545         {
546                 qDebug() << "Unexpected logic packet";
547                 return;
548         }
549
550         if (!_cur_logic_snapshot)
551         {
552                 set_capture_state(Running);
553
554                 // Create a new data snapshot
555                 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
556                         new data::LogicSnapshot(logic));
557                 _logic_data->push_snapshot(_cur_logic_snapshot);
558         }
559         else
560         {
561                 // Append to the existing data snapshot
562                 _cur_logic_snapshot->append_payload(logic);
563         }
564
565         data_updated();
566 }
567
568 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
569 {
570         lock_guard<mutex> lock(_data_mutex);
571
572         if(!_analog_data)
573         {
574                 qDebug() << "Unexpected analog packet";
575                 return; // This analog packet was not expected.
576         }
577
578         if (!_cur_analog_snapshot)
579         {
580                 set_capture_state(Running);
581
582                 // Create a new data snapshot
583                 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
584                         new data::AnalogSnapshot(analog));
585                 _analog_data->push_snapshot(_cur_analog_snapshot);
586         }
587         else
588         {
589                 // Append to the existing data snapshot
590                 _cur_analog_snapshot->append_payload(analog);
591         }
592
593         data_updated();
594 }
595
596 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
597         const struct sr_datafeed_packet *packet)
598 {
599         assert(sdi);
600         assert(packet);
601
602         switch (packet->type) {
603         case SR_DF_HEADER:
604                 feed_in_header(sdi);
605                 break;
606
607         case SR_DF_META:
608                 assert(packet->payload);
609                 feed_in_meta(sdi,
610                         *(const sr_datafeed_meta*)packet->payload);
611                 break;
612
613         case SR_DF_LOGIC:
614                 assert(packet->payload);
615                 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
616                 break;
617
618         case SR_DF_ANALOG:
619                 assert(packet->payload);
620                 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
621                 break;
622
623         case SR_DF_END:
624         {
625                 {
626                         lock_guard<mutex> lock(_data_mutex);
627                         _cur_logic_snapshot.reset();
628                         _cur_analog_snapshot.reset();
629                 }
630                 data_updated();
631                 break;
632         }
633         }
634 }
635
636 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
637         const struct sr_datafeed_packet *packet, void *cb_data)
638 {
639         (void) cb_data;
640         assert(_session);
641         _session->data_feed_in(sdi, packet);
642 }
643
644 } // namespace pv