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