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