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