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