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