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