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