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