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