]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Moved session creation into DevInst objects
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c 3 *
1bc6525b 4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
2953961c
JH
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
269528f5 21#ifdef ENABLE_DECODE
4e5a4405 22#include <libsigrokdecode/libsigrokdecode.h>
269528f5 23#endif
4e5a4405 24
2953961c
JH
25#include "sigsession.h"
26
dc0867ff 27#include "devicemanager.h"
921b90c0 28#include "device/device.h"
ae2d1bc5 29#include "device/file.h"
119aff65 30
1b1ec774
JH
31#include "data/analog.h"
32#include "data/analogsnapshot.h"
6e89374a 33#include "data/decoderstack.h"
1b1ec774
JH
34#include "data/logic.h"
35#include "data/logicsnapshot.h"
7491a29f 36#include "data/decode/decoder.h"
82c7f640 37
aba1dd16 38#include "view/analogsignal.h"
b9329558 39#include "view/decodetrace.h"
8d634081 40#include "view/logicsignal.h"
28a4c9c5 41
2953961c
JH
42#include <assert.h>
43
e92cd4e4
JH
44#include <stdexcept>
45
4e5a4405
JH
46#include <boost/foreach.hpp>
47
728e5ef7
JH
48#include <sys/stat.h>
49
79efbc53
JH
50#include <QDebug>
51
819f4c25
JH
52using boost::dynamic_pointer_cast;
53using boost::function;
54using boost::lock_guard;
55using boost::mutex;
56using boost::shared_ptr;
57using std::map;
02412f0b 58using std::set;
819f4c25
JH
59using std::string;
60using std::vector;
28a4c9c5 61
51e77110
JH
62namespace pv {
63
2953961c 64// TODO: This should not be necessary
04abfae9 65SigSession* SigSession::_session = NULL;
2953961c 66
dc0867ff
JH
67SigSession::SigSession(DeviceManager &device_manager) :
68 _device_manager(device_manager),
5b7cf66c 69 _capture_state(Stopped)
2953961c
JH
70{
71 // TODO: This should not be necessary
04abfae9 72 _session = this;
2953961c
JH
73}
74
75SigSession::~SigSession()
76{
85843b14
JH
77 using pv::device::Device;
78
5b7cf66c
JH
79 stop_capture();
80
6d483b8b
MC
81 if (_sampling_thread.joinable())
82 _sampling_thread.join();
2e2946fe 83
996b7c9d 84 _dev_inst->release();
0f784939 85
2953961c 86 // TODO: This should not be necessary
04abfae9 87 _session = NULL;
2953961c
JH
88}
89
94574501 90shared_ptr<device::DevInst> SigSession::get_device() const
dc0867ff 91{
19adbc2c 92 return _dev_inst;
dc0867ff
JH
93}
94
ae2d1bc5
JH
95void SigSession::set_device(
96 shared_ptr<device::DevInst> dev_inst) throw(QString)
d64d1596 97{
85843b14
JH
98 using pv::device::Device;
99
82596f46
JH
100 // Ensure we are not capturing before setting the device
101 stop_capture();
102
ae2d1bc5
JH
103 if (_dev_inst) {
104 sr_session_datafeed_callback_remove_all();
996b7c9d 105 _dev_inst->release();
ae2d1bc5
JH
106 }
107
108 _dev_inst = dev_inst;
109 _decode_traces.clear();
85843b14 110
ae2d1bc5 111 if (dev_inst) {
996b7c9d 112 dev_inst->use(this);
ae2d1bc5
JH
113 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
114 update_signals(dev_inst);
115 }
116}
85843b14 117
ae2d1bc5
JH
118void SigSession::set_file(const string &name) throw(QString)
119{
120 // Deslect the old device, because file type detection in File::create
121 // destorys the old session inside libsigrok.
122 set_device(shared_ptr<device::DevInst>());
123 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
d64d1596
JH
124}
125
996b7c9d 126void SigSession::release_device(device::DevInst *dev_inst)
dc0867ff 127{
19adbc2c 128 (void)dev_inst;
996b7c9d 129 assert(_dev_inst.get() == dev_inst);
dc0867ff
JH
130
131 assert(_capture_state == Stopped);
94574501 132 _dev_inst = shared_ptr<device::DevInst>();
dc0867ff
JH
133}
134
5b7cf66c
JH
135SigSession::capture_state SigSession::get_capture_state() const
136{
949f8050 137 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
138 return _capture_state;
139}
140
d99dc9f2 141void SigSession::start_capture(function<void (const QString)> error_handler)
2e2946fe 142{
5b7cf66c
JH
143 stop_capture();
144
d64d1596 145 // Check that a device instance has been selected.
19adbc2c 146 if (!_dev_inst) {
d64d1596
JH
147 qDebug() << "No device selected";
148 return;
149 }
150
19adbc2c
JH
151 assert(_dev_inst->dev_inst());
152
9c48fa57
JH
153 // Check that at least one probe is enabled
154 const GSList *l;
19adbc2c 155 for (l = _dev_inst->dev_inst()->probes; l; l = l->next) {
9c48fa57
JH
156 sr_probe *const probe = (sr_probe*)l->data;
157 assert(probe);
158 if (probe->enabled)
159 break;
160 }
161
162 if (!l) {
163 error_handler(tr("No probes enabled."));
164 return;
165 }
166
167 // Begin the session
e042ad64 168 _sampling_thread = boost::thread(
19adbc2c
JH
169 &SigSession::sample_thread_proc, this, _dev_inst,
170 error_handler);
2e2946fe
JH
171}
172
5b7cf66c
JH
173void SigSession::stop_capture()
174{
333d5bbc 175 if (get_capture_state() == Stopped)
5b7cf66c
JH
176 return;
177
178 sr_session_stop();
179
180 // Check that sampling stopped
6d483b8b
MC
181 if (_sampling_thread.joinable())
182 _sampling_thread.join();
5b7cf66c
JH
183}
184
02412f0b
JH
185set< shared_ptr<data::SignalData> > SigSession::get_data() const
186{
187 lock_guard<mutex> lock(_signals_mutex);
188 set< shared_ptr<data::SignalData> > data;
189 BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) {
190 assert(sig);
191 data.insert(sig->data());
192 }
193
194 return data;
195}
196
38eeddea 197vector< shared_ptr<view::Signal> > SigSession::get_signals() const
2e2946fe 198{
3868e5fa 199 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
200 return _signals;
201}
202
269528f5 203#ifdef ENABLE_DECODE
4e5a4405 204bool SigSession::add_decoder(srd_decoder *const dec)
82c7f640 205{
4e5a4405 206 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
7491a29f 207 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 208
e92cd4e4 209 try
82c7f640 210 {
119aff65 211 lock_guard<mutex> lock(_signals_mutex);
e0fc5810 212
4e5a4405 213 // Create the decoder
7491a29f
JH
214 decoder_stack = shared_ptr<data::DecoderStack>(
215 new data::DecoderStack(dec));
4e5a4405 216
d2f46d27
JH
217 // Make a list of all the probes
218 std::vector<const srd_probe*> all_probes;
4e5a4405 219 for(const GSList *i = dec->probes; i; i = i->next)
d2f46d27
JH
220 all_probes.push_back((const srd_probe*)i->data);
221 for(const GSList *i = dec->opt_probes; i; i = i->next)
222 all_probes.push_back((const srd_probe*)i->data);
223
224 // Auto select the initial probes
225 BOOST_FOREACH(const srd_probe *probe, all_probes)
4e5a4405
JH
226 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
227 {
228 shared_ptr<view::LogicSignal> l =
229 dynamic_pointer_cast<view::LogicSignal>(s);
27e8df22
JH
230 if (l && QString::fromUtf8(probe->name).
231 toLower().contains(
4e5a4405
JH
232 l->get_name().toLower()))
233 probes[probe] = l;
234 }
4e5a4405 235
7491a29f
JH
236 assert(decoder_stack);
237 assert(!decoder_stack->stack().empty());
238 assert(decoder_stack->stack().front());
239 decoder_stack->stack().front()->set_probes(probes);
4e5a4405
JH
240
241 // Create the decode signal
b9329558 242 shared_ptr<view::DecodeTrace> d(
7491a29f 243 new view::DecodeTrace(*this, decoder_stack,
06bb4e6a 244 _decode_traces.size()));
82c7f640
JH
245 _decode_traces.push_back(d);
246 }
e92cd4e4
JH
247 catch(std::runtime_error e)
248 {
249 return false;
250 }
251
82c7f640 252 signals_changed();
e92cd4e4 253
7491a29f
JH
254 // Do an initial decode
255 decoder_stack->begin_decode();
256
e92cd4e4 257 return true;
82c7f640
JH
258}
259
b9329558 260vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
38eeddea
JH
261{
262 lock_guard<mutex> lock(_signals_mutex);
263 return _decode_traces;
264}
265
b9329558 266void SigSession::remove_decode_signal(view::DecodeTrace *signal)
c51482b3 267{
b9329558 268 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
c51482b3
JH
269 _decode_traces.begin();
270 i != _decode_traces.end();
271 i++)
272 if ((*i).get() == signal)
273 {
274 _decode_traces.erase(i);
275 signals_changed();
276 return;
277 }
278}
269528f5 279#endif
c51482b3 280
6ac96c2e
JH
281void SigSession::set_capture_state(capture_state state)
282{
949f8050 283 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 284 const bool changed = _capture_state != state;
6ac96c2e 285 _capture_state = state;
2b49eeb0
JH
286 if(changed)
287 capture_state_changed(state);
6ac96c2e
JH
288}
289
94574501 290void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
b087ba7f 291{
19adbc2c 292 assert(dev_inst);
b087ba7f
JH
293 assert(_capture_state == Stopped);
294
b087ba7f 295 unsigned int logic_probe_count = 0;
b087ba7f 296
82c7f640
JH
297 // Clear the decode traces
298 _decode_traces.clear();
299
b087ba7f 300 // Detect what data types we will receive
19adbc2c
JH
301 if(dev_inst) {
302 assert(dev_inst->dev_inst());
303 for (const GSList *l = dev_inst->dev_inst()->probes;
304 l; l = l->next) {
b087ba7f
JH
305 const sr_probe *const probe = (const sr_probe *)l->data;
306 if (!probe->enabled)
307 continue;
308
309 switch(probe->type) {
310 case SR_PROBE_LOGIC:
311 logic_probe_count++;
312 break;
b087ba7f
JH
313 }
314 }
315 }
316
bb2cdfff 317 // Create data containers for the logic data snapshots
b087ba7f
JH
318 {
319 lock_guard<mutex> data_lock(_data_mutex);
320
321 _logic_data.reset();
322 if (logic_probe_count != 0) {
323 _logic_data.reset(new data::Logic(
324 logic_probe_count));
325 assert(_logic_data);
326 }
b087ba7f
JH
327 }
328
329 // Make the Signals list
39e680c2 330 do {
b087ba7f
JH
331 lock_guard<mutex> lock(_signals_mutex);
332
333 _signals.clear();
334
19adbc2c 335 if(!dev_inst)
39e680c2
JH
336 break;
337
19adbc2c
JH
338 assert(dev_inst->dev_inst());
339 for (const GSList *l = dev_inst->dev_inst()->probes;
340 l; l = l->next) {
39e680c2
JH
341 shared_ptr<view::Signal> signal;
342 sr_probe *const probe = (sr_probe *)l->data;
343 assert(probe);
344
345 switch(probe->type) {
346 case SR_PROBE_LOGIC:
347 signal = shared_ptr<view::Signal>(
83c23cc9 348 new view::LogicSignal(dev_inst,
8d3e0764 349 probe, _logic_data));
39e680c2
JH
350 break;
351
352 case SR_PROBE_ANALOG:
bb2cdfff
JH
353 {
354 shared_ptr<data::Analog> data(
355 new data::Analog());
39e680c2 356 signal = shared_ptr<view::Signal>(
83c23cc9 357 new view::AnalogSignal(dev_inst,
8d3e0764 358 probe, data));
39e680c2 359 break;
bb2cdfff 360 }
39e680c2
JH
361
362 default:
363 assert(0);
364 break;
b087ba7f 365 }
39e680c2
JH
366
367 assert(signal);
368 _signals.push_back(signal);
b087ba7f 369 }
39e680c2
JH
370
371 } while(0);
b087ba7f
JH
372
373 signals_changed();
374}
375
3ddcc083
JH
376shared_ptr<view::Signal> SigSession::signal_from_probe(
377 const sr_probe *probe) const
378{
379 lock_guard<mutex> lock(_signals_mutex);
380 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
381 assert(sig);
382 if (sig->probe() == probe)
383 return sig;
384 }
385 return shared_ptr<view::Signal>();
386}
387
deef291c
JH
388void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
389{
390 GVariant *gvar;
391 uint64_t sample_rate = 0;
392
393 // Read out the sample rate
394 if(sdi->driver)
395 {
68162c29
BV
396 const int ret = sr_config_get(sdi->driver, sdi, NULL,
397 SR_CONF_SAMPLERATE, &gvar);
deef291c
JH
398 if (ret != SR_OK) {
399 qDebug("Failed to get samplerate\n");
400 return;
401 }
402
403 sample_rate = g_variant_get_uint64(gvar);
404 g_variant_unref(gvar);
405 }
406
bb2cdfff
JH
407 // Set the sample rate of all data
408 const set< shared_ptr<data::SignalData> > data_set = get_data();
409 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
410 assert(data);
411 data->set_samplerate(sample_rate);
412 }
deef291c
JH
413}
414
94574501 415void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
f2edb557 416 function<void (const QString)> error_handler)
274d4f13 417{
19adbc2c
JH
418 assert(dev_inst);
419 assert(dev_inst->dev_inst());
f2edb557 420 assert(error_handler);
be73bdfa 421
ae2d1bc5 422 read_sample_rate(dev_inst->dev_inst());
274d4f13 423
ae2d1bc5
JH
424 try {
425 dev_inst->start();
426 } catch(const QString e) {
427 error_handler(e);
eec446e1
JH
428 return;
429 }
430
07dcf561
JH
431 set_capture_state(dev_inst->is_trigger_enabled() ?
432 AwaitingTrigger : Running);
eec446e1 433
ae2d1bc5 434 dev_inst->run();
eec446e1 435 set_capture_state(Stopped);
1a2fe44c
JH
436
437 // Confirm that SR_DF_END was received
bb2cdfff
JH
438 if (_cur_logic_snapshot)
439 {
440 qDebug("SR_DF_END was not received.");
441 assert(0);
442 }
eec446e1
JH
443}
444
445void SigSession::feed_in_header(const sr_dev_inst *sdi)
446{
deef291c 447 read_sample_rate(sdi);
be73bdfa
JH
448}
449
450void SigSession::feed_in_meta(const sr_dev_inst *sdi,
451 const sr_datafeed_meta &meta)
452{
94fe58ef
UH
453 (void)sdi;
454
be73bdfa
JH
455 for (const GSList *l = meta.config; l; l = l->next) {
456 const sr_config *const src = (const sr_config*)l->data;
457 switch (src->key) {
458 case SR_CONF_SAMPLERATE:
459 /// @todo handle samplerate changes
460 /// samplerate = (uint64_t *)src->value;
461 break;
462 default:
463 // Unknown metadata is not an error.
464 break;
465 }
aba1dd16 466 }
74043039
JH
467
468 signals_changed();
aba1dd16
JH
469}
470
9c112671
JH
471void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
472{
473 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
474
475 if (!_logic_data)
9c112671 476 {
79efbc53
JH
477 qDebug() << "Unexpected logic packet";
478 return;
479 }
be73bdfa 480
79efbc53
JH
481 if (!_cur_logic_snapshot)
482 {
bb2cdfff 483 // This could be the first packet after a trigger
2b49eeb0
JH
484 set_capture_state(Running);
485
9c112671 486 // Create a new data snapshot
1b1ec774 487 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
27d7c96b 488 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
9c112671
JH
489 _logic_data->push_snapshot(_cur_logic_snapshot);
490 }
491 else
492 {
493 // Append to the existing data snapshot
494 _cur_logic_snapshot->append_payload(logic);
495 }
496
497 data_updated();
498}
499
aba1dd16
JH
500void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
501{
502 lock_guard<mutex> lock(_data_mutex);
79efbc53 503
bb2cdfff
JH
504 const unsigned int probe_count = g_slist_length(analog.probes);
505 const size_t sample_count = analog.num_samples / probe_count;
506 const float *data = analog.data;
507 bool sweep_beginning = false;
be73bdfa 508
bb2cdfff 509 for (GSList *p = analog.probes; p; p = p->next)
79efbc53 510 {
bb2cdfff 511 shared_ptr<data::AnalogSnapshot> snapshot;
2b49eeb0 512
bb2cdfff
JH
513 sr_probe *const probe = (sr_probe*)p->data;
514 assert(probe);
515
516 // Try to get the snapshot of the probe
517 const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >::
518 iterator iter = _cur_analog_snapshots.find(probe);
519 if (iter != _cur_analog_snapshots.end())
520 snapshot = (*iter).second;
521 else
522 {
523 // If no snapshot was found, this means we havn't
524 // created one yet. i.e. this is the first packet
525 // in the sweep containing this snapshot.
526 sweep_beginning = true;
527
528 // Create a snapshot, keep it in the maps of probes
529 snapshot = shared_ptr<data::AnalogSnapshot>(
27d7c96b 530 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
bb2cdfff
JH
531 _cur_analog_snapshots[probe] = snapshot;
532
533 // Find the annalog data associated with the probe
534 shared_ptr<view::AnalogSignal> sig =
535 dynamic_pointer_cast<view::AnalogSignal>(
536 signal_from_probe(probe));
537 assert(sig);
538
539 shared_ptr<data::Analog> data(sig->analog_data());
540 assert(data);
541
542 // Push the snapshot into the analog data.
543 data->push_snapshot(snapshot);
544 }
545
546 assert(snapshot);
547
548 // Append the samples in the snapshot
549 snapshot->append_interleaved_samples(data++, sample_count,
550 probe_count);
aba1dd16 551 }
bb2cdfff
JH
552
553 if (sweep_beginning) {
554 // This could be the first packet after a trigger
555 set_capture_state(Running);
aba1dd16
JH
556 }
557
558 data_updated();
559}
9c112671 560
b0e1d01d
JH
561void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
562 const struct sr_datafeed_packet *packet)
563{
564 assert(sdi);
565 assert(packet);
566
567 switch (packet->type) {
568 case SR_DF_HEADER:
eec446e1 569 feed_in_header(sdi);
b0e1d01d
JH
570 break;
571
be73bdfa 572 case SR_DF_META:
aba1dd16 573 assert(packet->payload);
be73bdfa
JH
574 feed_in_meta(sdi,
575 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
576 break;
577
2e2946fe 578 case SR_DF_LOGIC:
28a4c9c5 579 assert(packet->payload);
9c112671 580 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
581 break;
582
aba1dd16
JH
583 case SR_DF_ANALOG:
584 assert(packet->payload);
585 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
586 break;
587
2953961c 588 case SR_DF_END:
2e2946fe
JH
589 {
590 {
591 lock_guard<mutex> lock(_data_mutex);
592 _cur_logic_snapshot.reset();
bb2cdfff 593 _cur_analog_snapshots.clear();
2e2946fe 594 }
04abfae9 595 data_updated();
2953961c
JH
596 break;
597 }
2e2946fe 598 }
2953961c
JH
599}
600
04abfae9 601void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 602 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 603{
5045f16d 604 (void) cb_data;
04abfae9
JH
605 assert(_session);
606 _session->data_feed_in(sdi, packet);
2953961c 607}
51e77110
JH
608
609} // namespace pv