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