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