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