]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Enumerate probes when session is loaded
[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"
1b1ec774
JH
24#include "data/analog.h"
25#include "data/analogsnapshot.h"
26#include "data/logic.h"
27#include "data/logicsnapshot.h"
aba1dd16 28#include "view/analogsignal.h"
8d634081 29#include "view/logicsignal.h"
28a4c9c5 30
2953961c
JH
31#include <assert.h>
32
728e5ef7
JH
33#include <sys/stat.h>
34
79efbc53
JH
35#include <QDebug>
36
28a4c9c5 37using namespace boost;
e3f65ace 38using namespace std;
28a4c9c5 39
51e77110
JH
40namespace pv {
41
2953961c 42// TODO: This should not be necessary
04abfae9 43SigSession* SigSession::_session = NULL;
2953961c 44
dc0867ff
JH
45SigSession::SigSession(DeviceManager &device_manager) :
46 _device_manager(device_manager),
d64d1596 47 _sdi(NULL),
5b7cf66c 48 _capture_state(Stopped)
2953961c
JH
49{
50 // TODO: This should not be necessary
04abfae9 51 _session = this;
2953961c
JH
52}
53
54SigSession::~SigSession()
55{
5b7cf66c
JH
56 stop_capture();
57
333d5bbc 58 if (_sampling_thread.get())
2e2946fe
JH
59 _sampling_thread->join();
60 _sampling_thread.reset();
61
0f784939
JH
62 if (_sdi)
63 _device_manager.release_device(_sdi);
64 _sdi = NULL;
65
2953961c 66 // TODO: This should not be necessary
04abfae9 67 _session = NULL;
2953961c
JH
68}
69
dc0867ff
JH
70struct sr_dev_inst* SigSession::get_device() const
71{
72 return _sdi;
73}
74
d64d1596
JH
75void SigSession::set_device(struct sr_dev_inst *sdi)
76{
88497156 77 if (_sdi)
dc0867ff 78 _device_manager.release_device(_sdi);
88497156 79 if (sdi)
dc0867ff 80 _device_manager.use_device(sdi, this);
d64d1596 81 _sdi = sdi;
b698553c 82 update_signals(sdi);
d64d1596
JH
83}
84
dc0867ff
JH
85void SigSession::release_device(struct sr_dev_inst *sdi)
86{
87 (void)sdi;
88
89 assert(_capture_state == Stopped);
90 _sdi = NULL;
b698553c 91 update_signals(NULL);
dc0867ff
JH
92}
93
f2edb557
JH
94void SigSession::load_file(const string &name,
95 function<void (const QString)> error_handler)
2953961c 96{
8a67bd9e 97 stop_capture();
e8c9f8a5
JH
98
99 if (sr_session_load(name.c_str()) == SR_OK) {
100 GSList *devlist = NULL;
101 sr_session_dev_list(&devlist);
102
103 if (!devlist || !devlist->data ||
104 sr_session_start() != SR_OK) {
105 error_handler(tr("Failed to start session."));
106 return;
107 }
108
109 update_signals((sr_dev_inst*)devlist->data);
110 g_slist_free(devlist);
111
112 _sampling_thread.reset(new boost::thread(
113 &SigSession::load_session_thread_proc, this,
114 error_handler));
115
116 } else {
117 sr_input *in = NULL;
118
119 if (!(in = load_input_file_format(name.c_str(),
120 error_handler)))
121 return;
122
123 update_signals(in->sdi);
124
125 _sampling_thread.reset(new boost::thread(
126 &SigSession::load_input_thread_proc, this,
127 name, in, error_handler));
128 }
129
2953961c
JH
130}
131
5b7cf66c
JH
132SigSession::capture_state SigSession::get_capture_state() const
133{
949f8050 134 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
135 return _capture_state;
136}
137
d64d1596 138void SigSession::start_capture(uint64_t record_length,
f2edb557 139 function<void (const QString)> error_handler)
2e2946fe 140{
5b7cf66c
JH
141 stop_capture();
142
d64d1596
JH
143 // Check that a device instance has been selected.
144 if (!_sdi) {
145 qDebug() << "No device selected";
146 return;
147 }
148
9c48fa57
JH
149 // Check that at least one probe is enabled
150 const GSList *l;
d64d1596 151 for (l = _sdi->probes; l; l = l->next) {
9c48fa57
JH
152 sr_probe *const probe = (sr_probe*)l->data;
153 assert(probe);
154 if (probe->enabled)
155 break;
156 }
157
158 if (!l) {
159 error_handler(tr("No probes enabled."));
160 return;
161 }
162
163 // Begin the session
2e2946fe 164 _sampling_thread.reset(new boost::thread(
d64d1596 165 &SigSession::sample_thread_proc, this, _sdi,
f2edb557 166 record_length, error_handler));
2e2946fe
JH
167}
168
5b7cf66c
JH
169void SigSession::stop_capture()
170{
333d5bbc 171 if (get_capture_state() == Stopped)
5b7cf66c
JH
172 return;
173
174 sr_session_stop();
175
176 // Check that sampling stopped
333d5bbc 177 if (_sampling_thread.get())
5b7cf66c
JH
178 _sampling_thread->join();
179 _sampling_thread.reset();
5b7cf66c
JH
180}
181
3868e5fa 182vector< shared_ptr<view::Signal> > SigSession::get_signals()
2e2946fe 183{
3868e5fa 184 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
185 return _signals;
186}
187
1b1ec774 188boost::shared_ptr<data::Logic> SigSession::get_data()
2e2946fe
JH
189{
190 return _logic_data;
191}
192
6ac96c2e
JH
193void SigSession::set_capture_state(capture_state state)
194{
949f8050 195 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 196 const bool changed = _capture_state != state;
6ac96c2e 197 _capture_state = state;
2b49eeb0
JH
198 if(changed)
199 capture_state_changed(state);
6ac96c2e
JH
200}
201
728e5ef7
JH
202/**
203 * Attempts to autodetect the format. Failing that
204 * @param filename The filename of the input file.
205 * @return A pointer to the 'struct sr_input_format' that should be used,
206 * or NULL if no input format was selected or auto-detected.
207 */
208sr_input_format* SigSession::determine_input_file_format(
209 const string &filename)
210{
211 int i;
212
213 /* If there are no input formats, return NULL right away. */
214 sr_input_format *const *const inputs = sr_input_list();
215 if (!inputs) {
216 g_critical("No supported input formats available.");
217 return NULL;
218 }
219
220 /* Otherwise, try to find an input module that can handle this file. */
221 for (i = 0; inputs[i]; i++) {
222 if (inputs[i]->format_match(filename.c_str()))
223 break;
224 }
225
226 /* Return NULL if no input module wanted to touch this. */
227 if (!inputs[i]) {
228 g_critical("Error: no matching input module found.");
229 return NULL;
230 }
231
232 return inputs[i];
233}
234
235sr_input* SigSession::load_input_file_format(const string &filename,
236 function<void (const QString)> error_handler,
237 sr_input_format *format)
238{
239 struct stat st;
240 sr_input *in;
241
242 if (!format && !(format =
243 determine_input_file_format(filename.c_str()))) {
244 /* The exact cause was already logged. */
245 return NULL;
246 }
247
248 if (stat(filename.c_str(), &st) == -1) {
249 error_handler(tr("Failed to load file"));
250 return NULL;
251 }
252
253 /* Initialize the input module. */
254 if (!(in = new sr_input)) {
255 qDebug("Failed to allocate input module.\n");
256 return NULL;
257 }
258
259 in->format = format;
260 in->param = NULL;
261 if (in->format->init &&
262 in->format->init(in, filename.c_str()) != SR_OK) {
263 qDebug("Input format init failed.\n");
264 return NULL;
265 }
266
267 sr_session_new();
268
269 if (sr_session_dev_add(in->sdi) != SR_OK) {
270 qDebug("Failed to use device.\n");
271 sr_session_destroy();
272 return NULL;
273 }
274
275 return in;
276}
277
b698553c 278void SigSession::update_signals(const sr_dev_inst *const sdi)
b087ba7f
JH
279{
280 assert(_capture_state == Stopped);
281
282 shared_ptr<view::Signal> signal;
283 unsigned int logic_probe_count = 0;
284 unsigned int analog_probe_count = 0;
285
286 // Detect what data types we will receive
b698553c
JH
287 if(sdi) {
288 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
289 const sr_probe *const probe = (const sr_probe *)l->data;
290 if (!probe->enabled)
291 continue;
292
293 switch(probe->type) {
294 case SR_PROBE_LOGIC:
295 logic_probe_count++;
296 break;
297
298 case SR_PROBE_ANALOG:
299 analog_probe_count++;
300 break;
301 }
302 }
303 }
304
305 // Create data containers for the data snapshots
306 {
307 lock_guard<mutex> data_lock(_data_mutex);
308
309 _logic_data.reset();
310 if (logic_probe_count != 0) {
311 _logic_data.reset(new data::Logic(
312 logic_probe_count));
313 assert(_logic_data);
314 }
315
316 _analog_data.reset();
317 if (analog_probe_count != 0) {
318 _analog_data.reset(new data::Analog());
319 assert(_analog_data);
320 }
321 }
322
323 // Make the Signals list
324 {
325 lock_guard<mutex> lock(_signals_mutex);
326
327 _signals.clear();
328
b698553c
JH
329 if(sdi) {
330 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
331 const sr_probe *const probe =
332 (const sr_probe *)l->data;
333 assert(probe);
b087ba7f
JH
334
335 switch(probe->type) {
336 case SR_PROBE_LOGIC:
337 signal = shared_ptr<view::Signal>(
c0f86852 338 new view::LogicSignal(*this, probe,
cec48d16 339 _logic_data));
b087ba7f
JH
340 break;
341
342 case SR_PROBE_ANALOG:
343 signal = shared_ptr<view::Signal>(
c0f86852 344 new view::AnalogSignal(*this, probe,
cec48d16 345 _analog_data));
b087ba7f
JH
346 break;
347 }
348
349 _signals.push_back(signal);
350 }
351 }
352 }
353
354 signals_changed();
355}
356
2b49eeb0
JH
357bool SigSession::is_trigger_enabled() const
358{
359 assert(_sdi);
360 for (const GSList *l = _sdi->probes; l; l = l->next) {
361 const sr_probe *const p = (const sr_probe *)l->data;
362 assert(p);
363 if (p->trigger && p->trigger[0] != '\0')
364 return true;
365 }
366
367 return false;
368}
369
e8c9f8a5 370void SigSession::load_session_thread_proc(
f2edb557 371 function<void (const QString)> error_handler)
8a67bd9e 372{
e8c9f8a5 373 (void)error_handler;
728e5ef7 374
e8c9f8a5
JH
375 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
376
377 set_capture_state(Running);
378
379 sr_session_run();
380
381 sr_session_destroy();
382 set_capture_state(Stopped);
383
384 // Confirm that SR_DF_END was received
385 assert(!_cur_logic_snapshot);
386 assert(!_cur_analog_snapshot);
387}
388
389void SigSession::load_input_thread_proc(const string name,
390 sr_input *in, function<void (const QString)> error_handler)
391{
392 (void)error_handler;
393
394 assert(in);
395 assert(in->format);
8a67bd9e 396
5045f16d 397 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
8a67bd9e 398
8a67bd9e
JH
399 set_capture_state(Running);
400
e8c9f8a5 401 in->format->loadfile(in, name.c_str());
8a67bd9e 402
728e5ef7 403 sr_session_destroy();
8a67bd9e 404 set_capture_state(Stopped);
1a2fe44c
JH
405
406 // Confirm that SR_DF_END was received
407 assert(!_cur_logic_snapshot);
408 assert(!_cur_analog_snapshot);
728e5ef7
JH
409
410 delete in;
8a67bd9e
JH
411}
412
2e2946fe 413void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
f2edb557
JH
414 uint64_t record_length,
415 function<void (const QString)> error_handler)
274d4f13 416{
be73bdfa 417 assert(sdi);
f2edb557 418 assert(error_handler);
be73bdfa 419
274d4f13 420 sr_session_new();
5045f16d 421 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
274d4f13
JH
422
423 if (sr_session_dev_add(sdi) != SR_OK) {
f2edb557 424 error_handler(tr("Failed to use device."));
274d4f13
JH
425 sr_session_destroy();
426 return;
427 }
428
be73bdfa
JH
429 // Set the sample limit
430 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
8eb9d311 431 g_variant_new_uint64(record_length)) != SR_OK) {
f2edb557
JH
432 error_handler(tr("Failed to configure "
433 "time-based sample limit."));
274d4f13
JH
434 sr_session_destroy();
435 return;
436 }
437
eec446e1 438 if (sr_session_start() != SR_OK) {
f2edb557 439 error_handler(tr("Failed to start session."));
eec446e1
JH
440 return;
441 }
442
2b49eeb0 443 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
eec446e1
JH
444
445 sr_session_run();
446 sr_session_destroy();
447
448 set_capture_state(Stopped);
1a2fe44c
JH
449
450 // Confirm that SR_DF_END was received
451 assert(!_cur_logic_snapshot);
452 assert(!_cur_analog_snapshot);
eec446e1
JH
453}
454
455void SigSession::feed_in_header(const sr_dev_inst *sdi)
456{
8eb9d311
BV
457 GVariant *gvar;
458 uint64_t sample_rate = 0;
8d634081 459
7297c76e 460 // Read out the sample rate
728e5ef7
JH
461 if(sdi->driver)
462 {
463 const int ret = sr_config_get(sdi->driver,
464 SR_CONF_SAMPLERATE, &gvar, sdi);
465 if (ret != SR_OK) {
466 qDebug("Failed to get samplerate\n");
467 return;
468 }
c0d6d479 469
728e5ef7
JH
470 sample_rate = g_variant_get_uint64(gvar);
471 g_variant_unref(gvar);
174e27a1
JH
472 }
473
b087ba7f
JH
474 if(_analog_data)
475 _analog_data->set_samplerate(sample_rate);
476 if(_logic_data)
477 _logic_data->set_samplerate(sample_rate);
be73bdfa
JH
478}
479
480void SigSession::feed_in_meta(const sr_dev_inst *sdi,
481 const sr_datafeed_meta &meta)
482{
94fe58ef
UH
483 (void)sdi;
484
be73bdfa
JH
485 for (const GSList *l = meta.config; l; l = l->next) {
486 const sr_config *const src = (const sr_config*)l->data;
487 switch (src->key) {
488 case SR_CONF_SAMPLERATE:
489 /// @todo handle samplerate changes
490 /// samplerate = (uint64_t *)src->value;
491 break;
492 default:
493 // Unknown metadata is not an error.
494 break;
495 }
aba1dd16
JH
496 }
497}
498
9c112671
JH
499void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
500{
501 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
502
503 if (!_logic_data)
9c112671 504 {
79efbc53
JH
505 qDebug() << "Unexpected logic packet";
506 return;
507 }
be73bdfa 508
79efbc53
JH
509 if (!_cur_logic_snapshot)
510 {
2b49eeb0
JH
511 set_capture_state(Running);
512
9c112671 513 // Create a new data snapshot
1b1ec774
JH
514 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
515 new data::LogicSnapshot(logic));
9c112671
JH
516 _logic_data->push_snapshot(_cur_logic_snapshot);
517 }
518 else
519 {
520 // Append to the existing data snapshot
521 _cur_logic_snapshot->append_payload(logic);
522 }
523
524 data_updated();
525}
526
aba1dd16
JH
527void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
528{
529 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
530
531 if(!_analog_data)
aba1dd16 532 {
79efbc53
JH
533 qDebug() << "Unexpected analog packet";
534 return; // This analog packet was not expected.
535 }
be73bdfa 536
79efbc53
JH
537 if (!_cur_analog_snapshot)
538 {
2b49eeb0
JH
539 set_capture_state(Running);
540
aba1dd16 541 // Create a new data snapshot
1b1ec774
JH
542 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
543 new data::AnalogSnapshot(analog));
aba1dd16
JH
544 _analog_data->push_snapshot(_cur_analog_snapshot);
545 }
546 else
547 {
548 // Append to the existing data snapshot
549 _cur_analog_snapshot->append_payload(analog);
550 }
551
552 data_updated();
553}
9c112671 554
b0e1d01d
JH
555void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
556 const struct sr_datafeed_packet *packet)
557{
558 assert(sdi);
559 assert(packet);
560
561 switch (packet->type) {
562 case SR_DF_HEADER:
eec446e1 563 feed_in_header(sdi);
b0e1d01d
JH
564 break;
565
be73bdfa 566 case SR_DF_META:
aba1dd16 567 assert(packet->payload);
be73bdfa
JH
568 feed_in_meta(sdi,
569 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
570 break;
571
2e2946fe 572 case SR_DF_LOGIC:
28a4c9c5 573 assert(packet->payload);
9c112671 574 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
575 break;
576
aba1dd16
JH
577 case SR_DF_ANALOG:
578 assert(packet->payload);
579 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
580 break;
581
2953961c 582 case SR_DF_END:
2e2946fe
JH
583 {
584 {
585 lock_guard<mutex> lock(_data_mutex);
586 _cur_logic_snapshot.reset();
aba1dd16 587 _cur_analog_snapshot.reset();
2e2946fe 588 }
04abfae9 589 data_updated();
2953961c
JH
590 break;
591 }
2e2946fe 592 }
2953961c
JH
593}
594
04abfae9 595void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 596 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 597{
5045f16d 598 (void) cb_data;
04abfae9
JH
599 assert(_session);
600 _session->data_feed_in(sdi, packet);
2953961c 601}
51e77110
JH
602
603} // namespace pv