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