]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Invoke signals_changed outside locked region
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c
JH
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
dc0867ff 23#include "devicemanager.h"
119aff65 24
1b1ec774
JH
25#include "data/analog.h"
26#include "data/analogsnapshot.h"
119aff65 27#include "data/decoder.h"
1b1ec774
JH
28#include "data/logic.h"
29#include "data/logicsnapshot.h"
82c7f640 30
aba1dd16 31#include "view/analogsignal.h"
82c7f640 32#include "view/decodesignal.h"
8d634081 33#include "view/logicsignal.h"
28a4c9c5 34
2953961c
JH
35#include <assert.h>
36
728e5ef7
JH
37#include <sys/stat.h>
38
79efbc53
JH
39#include <QDebug>
40
28a4c9c5 41using namespace boost;
e3f65ace 42using namespace std;
28a4c9c5 43
51e77110
JH
44namespace pv {
45
2953961c 46// TODO: This should not be necessary
04abfae9 47SigSession* SigSession::_session = NULL;
2953961c 48
dc0867ff
JH
49SigSession::SigSession(DeviceManager &device_manager) :
50 _device_manager(device_manager),
d64d1596 51 _sdi(NULL),
5b7cf66c 52 _capture_state(Stopped)
2953961c
JH
53{
54 // TODO: This should not be necessary
04abfae9 55 _session = this;
2953961c
JH
56}
57
58SigSession::~SigSession()
59{
5b7cf66c
JH
60 stop_capture();
61
333d5bbc 62 if (_sampling_thread.get())
2e2946fe
JH
63 _sampling_thread->join();
64 _sampling_thread.reset();
65
0f784939
JH
66 if (_sdi)
67 _device_manager.release_device(_sdi);
68 _sdi = NULL;
69
2953961c 70 // TODO: This should not be necessary
04abfae9 71 _session = NULL;
2953961c
JH
72}
73
dc0867ff
JH
74struct sr_dev_inst* SigSession::get_device() const
75{
76 return _sdi;
77}
78
d64d1596
JH
79void SigSession::set_device(struct sr_dev_inst *sdi)
80{
88497156 81 if (_sdi)
dc0867ff 82 _device_manager.release_device(_sdi);
88497156 83 if (sdi)
dc0867ff 84 _device_manager.use_device(sdi, this);
d64d1596 85 _sdi = sdi;
b698553c 86 update_signals(sdi);
d64d1596
JH
87}
88
dc0867ff
JH
89void SigSession::release_device(struct sr_dev_inst *sdi)
90{
91 (void)sdi;
92
93 assert(_capture_state == Stopped);
94 _sdi = NULL;
b698553c 95 update_signals(NULL);
dc0867ff
JH
96}
97
f2edb557
JH
98void SigSession::load_file(const string &name,
99 function<void (const QString)> error_handler)
2953961c 100{
8a67bd9e 101 stop_capture();
e8c9f8a5
JH
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
deef291c 113 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
e8c9f8a5
JH
114 g_slist_free(devlist);
115
deef291c
JH
116 update_signals(sdi);
117 read_sample_rate(sdi);
118
e8c9f8a5
JH
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);
deef291c 131 read_sample_rate(in->sdi);
e8c9f8a5
JH
132
133 _sampling_thread.reset(new boost::thread(
134 &SigSession::load_input_thread_proc, this,
135 name, in, error_handler));
136 }
137
2953961c
JH
138}
139
5b7cf66c
JH
140SigSession::capture_state SigSession::get_capture_state() const
141{
949f8050 142 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
143 return _capture_state;
144}
145
d64d1596 146void SigSession::start_capture(uint64_t record_length,
f2edb557 147 function<void (const QString)> error_handler)
2e2946fe 148{
5b7cf66c
JH
149 stop_capture();
150
d64d1596
JH
151 // Check that a device instance has been selected.
152 if (!_sdi) {
153 qDebug() << "No device selected";
154 return;
155 }
156
9c48fa57
JH
157 // Check that at least one probe is enabled
158 const GSList *l;
d64d1596 159 for (l = _sdi->probes; l; l = l->next) {
9c48fa57
JH
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
2e2946fe 172 _sampling_thread.reset(new boost::thread(
d64d1596 173 &SigSession::sample_thread_proc, this, _sdi,
f2edb557 174 record_length, error_handler));
2e2946fe
JH
175}
176
5b7cf66c
JH
177void SigSession::stop_capture()
178{
333d5bbc 179 if (get_capture_state() == Stopped)
5b7cf66c
JH
180 return;
181
182 sr_session_stop();
183
184 // Check that sampling stopped
333d5bbc 185 if (_sampling_thread.get())
5b7cf66c
JH
186 _sampling_thread->join();
187 _sampling_thread.reset();
5b7cf66c
JH
188}
189
3868e5fa 190vector< shared_ptr<view::Signal> > SigSession::get_signals()
2e2946fe 191{
3868e5fa 192 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
193 return _signals;
194}
195
1b1ec774 196boost::shared_ptr<data::Logic> SigSession::get_data()
2e2946fe
JH
197{
198 return _logic_data;
199}
200
82c7f640
JH
201void SigSession::add_decoder(srd_decoder *const dec)
202{
203 {
119aff65
JH
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));
82c7f640
JH
209 _decode_traces.push_back(d);
210 }
211 signals_changed();
212}
213
6ac96c2e
JH
214void SigSession::set_capture_state(capture_state state)
215{
949f8050 216 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 217 const bool changed = _capture_state != state;
6ac96c2e 218 _capture_state = state;
2b49eeb0
JH
219 if(changed)
220 capture_state_changed(state);
6ac96c2e
JH
221}
222
728e5ef7
JH
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
b698553c 299void SigSession::update_signals(const sr_dev_inst *const sdi)
b087ba7f
JH
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
82c7f640
JH
307 // Clear the decode traces
308 _decode_traces.clear();
309
b087ba7f 310 // Detect what data types we will receive
b698553c
JH
311 if(sdi) {
312 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
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
b698553c
JH
353 if(sdi) {
354 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
355 const sr_probe *const probe =
356 (const sr_probe *)l->data;
357 assert(probe);
b087ba7f
JH
358
359 switch(probe->type) {
360 case SR_PROBE_LOGIC:
361 signal = shared_ptr<view::Signal>(
c0f86852 362 new view::LogicSignal(*this, probe,
cec48d16 363 _logic_data));
b087ba7f
JH
364 break;
365
366 case SR_PROBE_ANALOG:
367 signal = shared_ptr<view::Signal>(
c0f86852 368 new view::AnalogSignal(*this, probe,
cec48d16 369 _analog_data));
b087ba7f
JH
370 break;
371 }
372
373 _signals.push_back(signal);
374 }
375 }
376 }
377
378 signals_changed();
379}
380
2b49eeb0
JH
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
deef291c
JH
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
e8c9f8a5 419void SigSession::load_session_thread_proc(
f2edb557 420 function<void (const QString)> error_handler)
8a67bd9e 421{
e8c9f8a5 422 (void)error_handler;
728e5ef7 423
e8c9f8a5
JH
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);
8a67bd9e 445
5045f16d 446 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
8a67bd9e 447
8a67bd9e
JH
448 set_capture_state(Running);
449
e8c9f8a5 450 in->format->loadfile(in, name.c_str());
8a67bd9e 451
728e5ef7 452 sr_session_destroy();
8a67bd9e 453 set_capture_state(Stopped);
1a2fe44c
JH
454
455 // Confirm that SR_DF_END was received
456 assert(!_cur_logic_snapshot);
457 assert(!_cur_analog_snapshot);
728e5ef7
JH
458
459 delete in;
8a67bd9e
JH
460}
461
2e2946fe 462void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
f2edb557
JH
463 uint64_t record_length,
464 function<void (const QString)> error_handler)
274d4f13 465{
be73bdfa 466 assert(sdi);
f2edb557 467 assert(error_handler);
be73bdfa 468
274d4f13 469 sr_session_new();
5045f16d 470 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
274d4f13
JH
471
472 if (sr_session_dev_add(sdi) != SR_OK) {
f2edb557 473 error_handler(tr("Failed to use device."));
274d4f13
JH
474 sr_session_destroy();
475 return;
476 }
477
be73bdfa
JH
478 // Set the sample limit
479 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
8eb9d311 480 g_variant_new_uint64(record_length)) != SR_OK) {
f2edb557
JH
481 error_handler(tr("Failed to configure "
482 "time-based sample limit."));
274d4f13
JH
483 sr_session_destroy();
484 return;
485 }
486
eec446e1 487 if (sr_session_start() != SR_OK) {
f2edb557 488 error_handler(tr("Failed to start session."));
eec446e1
JH
489 return;
490 }
491
2b49eeb0 492 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
eec446e1
JH
493
494 sr_session_run();
495 sr_session_destroy();
496
497 set_capture_state(Stopped);
1a2fe44c
JH
498
499 // Confirm that SR_DF_END was received
500 assert(!_cur_logic_snapshot);
501 assert(!_cur_analog_snapshot);
eec446e1
JH
502}
503
504void SigSession::feed_in_header(const sr_dev_inst *sdi)
505{
deef291c 506 read_sample_rate(sdi);
be73bdfa
JH
507}
508
509void SigSession::feed_in_meta(const sr_dev_inst *sdi,
510 const sr_datafeed_meta &meta)
511{
94fe58ef
UH
512 (void)sdi;
513
be73bdfa
JH
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 }
aba1dd16 525 }
74043039
JH
526
527 signals_changed();
aba1dd16
JH
528}
529
9c112671
JH
530void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
531{
532 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
533
534 if (!_logic_data)
9c112671 535 {
79efbc53
JH
536 qDebug() << "Unexpected logic packet";
537 return;
538 }
be73bdfa 539
79efbc53
JH
540 if (!_cur_logic_snapshot)
541 {
2b49eeb0
JH
542 set_capture_state(Running);
543
9c112671 544 // Create a new data snapshot
1b1ec774
JH
545 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
546 new data::LogicSnapshot(logic));
9c112671
JH
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
aba1dd16
JH
558void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
559{
560 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
561
562 if(!_analog_data)
aba1dd16 563 {
79efbc53
JH
564 qDebug() << "Unexpected analog packet";
565 return; // This analog packet was not expected.
566 }
be73bdfa 567
79efbc53
JH
568 if (!_cur_analog_snapshot)
569 {
2b49eeb0
JH
570 set_capture_state(Running);
571
aba1dd16 572 // Create a new data snapshot
1b1ec774
JH
573 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
574 new data::AnalogSnapshot(analog));
aba1dd16
JH
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}
9c112671 585
b0e1d01d
JH
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:
eec446e1 594 feed_in_header(sdi);
b0e1d01d
JH
595 break;
596
be73bdfa 597 case SR_DF_META:
aba1dd16 598 assert(packet->payload);
be73bdfa
JH
599 feed_in_meta(sdi,
600 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
601 break;
602
2e2946fe 603 case SR_DF_LOGIC:
28a4c9c5 604 assert(packet->payload);
9c112671 605 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
606 break;
607
aba1dd16
JH
608 case SR_DF_ANALOG:
609 assert(packet->payload);
610 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
611 break;
612
2953961c 613 case SR_DF_END:
2e2946fe
JH
614 {
615 {
616 lock_guard<mutex> lock(_data_mutex);
617 _cur_logic_snapshot.reset();
aba1dd16 618 _cur_analog_snapshot.reset();
2e2946fe 619 }
04abfae9 620 data_updated();
2953961c
JH
621 break;
622 }
2e2946fe 623 }
2953961c
JH
624}
625
04abfae9 626void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 627 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 628{
5045f16d 629 (void) cb_data;
04abfae9
JH
630 assert(_session);
631 _session->data_feed_in(sdi, packet);
2953961c 632}
51e77110
JH
633
634} // namespace pv