]>
Commit | Line | Data |
---|---|---|
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 | 37 | using namespace boost; |
e3f65ace | 38 | using namespace std; |
28a4c9c5 | 39 | |
51e77110 JH |
40 | namespace pv { |
41 | ||
2953961c | 42 | // TODO: This should not be necessary |
04abfae9 | 43 | SigSession* SigSession::_session = NULL; |
2953961c | 44 | |
dc0867ff JH |
45 | SigSession::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 | ||
54 | SigSession::~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 |
70 | struct sr_dev_inst* SigSession::get_device() const |
71 | { | |
72 | return _sdi; | |
73 | } | |
74 | ||
d64d1596 JH |
75 | void 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 |
85 | void 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 |
94 | void SigSession::load_file(const string &name, |
95 | function<void (const QString)> error_handler) | |
2953961c | 96 | { |
8a67bd9e JH |
97 | stop_capture(); |
98 | _sampling_thread.reset(new boost::thread( | |
f2edb557 JH |
99 | &SigSession::load_thread_proc, this, name, |
100 | error_handler)); | |
2953961c JH |
101 | } |
102 | ||
5b7cf66c JH |
103 | SigSession::capture_state SigSession::get_capture_state() const |
104 | { | |
949f8050 | 105 | lock_guard<mutex> lock(_sampling_mutex); |
5b7cf66c JH |
106 | return _capture_state; |
107 | } | |
108 | ||
d64d1596 | 109 | void SigSession::start_capture(uint64_t record_length, |
f2edb557 | 110 | function<void (const QString)> error_handler) |
2e2946fe | 111 | { |
5b7cf66c JH |
112 | stop_capture(); |
113 | ||
d64d1596 JH |
114 | // Check that a device instance has been selected. |
115 | if (!_sdi) { | |
116 | qDebug() << "No device selected"; | |
117 | return; | |
118 | } | |
119 | ||
9c48fa57 JH |
120 | // Check that at least one probe is enabled |
121 | const GSList *l; | |
d64d1596 | 122 | for (l = _sdi->probes; l; l = l->next) { |
9c48fa57 JH |
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 | |
2e2946fe | 135 | _sampling_thread.reset(new boost::thread( |
d64d1596 | 136 | &SigSession::sample_thread_proc, this, _sdi, |
f2edb557 | 137 | record_length, error_handler)); |
2e2946fe JH |
138 | } |
139 | ||
5b7cf66c JH |
140 | void SigSession::stop_capture() |
141 | { | |
333d5bbc | 142 | if (get_capture_state() == Stopped) |
5b7cf66c JH |
143 | return; |
144 | ||
145 | sr_session_stop(); | |
146 | ||
147 | // Check that sampling stopped | |
333d5bbc | 148 | if (_sampling_thread.get()) |
5b7cf66c JH |
149 | _sampling_thread->join(); |
150 | _sampling_thread.reset(); | |
5b7cf66c JH |
151 | } |
152 | ||
3868e5fa | 153 | vector< shared_ptr<view::Signal> > SigSession::get_signals() |
2e2946fe | 154 | { |
3868e5fa | 155 | lock_guard<mutex> lock(_signals_mutex); |
2e2946fe JH |
156 | return _signals; |
157 | } | |
158 | ||
1b1ec774 | 159 | boost::shared_ptr<data::Logic> SigSession::get_data() |
2e2946fe JH |
160 | { |
161 | return _logic_data; | |
162 | } | |
163 | ||
6ac96c2e JH |
164 | void SigSession::set_capture_state(capture_state state) |
165 | { | |
949f8050 | 166 | lock_guard<mutex> lock(_sampling_mutex); |
2b49eeb0 | 167 | const bool changed = _capture_state != state; |
6ac96c2e | 168 | _capture_state = state; |
2b49eeb0 JH |
169 | if(changed) |
170 | capture_state_changed(state); | |
6ac96c2e JH |
171 | } |
172 | ||
728e5ef7 JH |
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 | ||
b698553c | 249 | void SigSession::update_signals(const sr_dev_inst *const sdi) |
b087ba7f JH |
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 | |
b698553c JH |
258 | if(sdi) { |
259 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
b087ba7f JH |
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 | ||
b698553c JH |
300 | if(sdi) { |
301 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
b087ba7f JH |
302 | const sr_probe *const probe = |
303 | (const sr_probe *)l->data; | |
304 | assert(probe); | |
b087ba7f JH |
305 | |
306 | switch(probe->type) { | |
307 | case SR_PROBE_LOGIC: | |
308 | signal = shared_ptr<view::Signal>( | |
c0f86852 | 309 | new view::LogicSignal(*this, probe, |
cec48d16 | 310 | _logic_data)); |
b087ba7f JH |
311 | break; |
312 | ||
313 | case SR_PROBE_ANALOG: | |
314 | signal = shared_ptr<view::Signal>( | |
c0f86852 | 315 | new view::AnalogSignal(*this, probe, |
cec48d16 | 316 | _analog_data)); |
b087ba7f JH |
317 | break; |
318 | } | |
319 | ||
320 | _signals.push_back(signal); | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | signals_changed(); | |
326 | } | |
327 | ||
2b49eeb0 JH |
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 | ||
f2edb557 JH |
341 | void SigSession::load_thread_proc(const string name, |
342 | function<void (const QString)> error_handler) | |
8a67bd9e | 343 | { |
728e5ef7 JH |
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 | } | |
8a67bd9e | 351 | } |
728e5ef7 JH |
352 | else if(!(in = load_input_file_format(name.c_str(), error_handler))) |
353 | return; | |
8a67bd9e | 354 | |
5045f16d | 355 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
8a67bd9e | 356 | |
8a67bd9e JH |
357 | set_capture_state(Running); |
358 | ||
728e5ef7 JH |
359 | if(in) { |
360 | assert(in->format); | |
361 | in->format->loadfile(in, name.c_str()); | |
362 | } else | |
363 | sr_session_run(); | |
8a67bd9e | 364 | |
728e5ef7 | 365 | sr_session_destroy(); |
8a67bd9e | 366 | set_capture_state(Stopped); |
1a2fe44c JH |
367 | |
368 | // Confirm that SR_DF_END was received | |
369 | assert(!_cur_logic_snapshot); | |
370 | assert(!_cur_analog_snapshot); | |
728e5ef7 JH |
371 | |
372 | delete in; | |
8a67bd9e JH |
373 | } |
374 | ||
2e2946fe | 375 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, |
f2edb557 JH |
376 | uint64_t record_length, |
377 | function<void (const QString)> error_handler) | |
274d4f13 | 378 | { |
be73bdfa | 379 | assert(sdi); |
f2edb557 | 380 | assert(error_handler); |
be73bdfa | 381 | |
274d4f13 | 382 | sr_session_new(); |
5045f16d | 383 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
274d4f13 JH |
384 | |
385 | if (sr_session_dev_add(sdi) != SR_OK) { | |
f2edb557 | 386 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
387 | sr_session_destroy(); |
388 | return; | |
389 | } | |
390 | ||
be73bdfa JH |
391 | // Set the sample limit |
392 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
8eb9d311 | 393 | g_variant_new_uint64(record_length)) != SR_OK) { |
f2edb557 JH |
394 | error_handler(tr("Failed to configure " |
395 | "time-based sample limit.")); | |
274d4f13 JH |
396 | sr_session_destroy(); |
397 | return; | |
398 | } | |
399 | ||
eec446e1 | 400 | if (sr_session_start() != SR_OK) { |
f2edb557 | 401 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
402 | return; |
403 | } | |
404 | ||
2b49eeb0 | 405 | set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running); |
eec446e1 JH |
406 | |
407 | sr_session_run(); | |
408 | sr_session_destroy(); | |
409 | ||
410 | set_capture_state(Stopped); | |
1a2fe44c JH |
411 | |
412 | // Confirm that SR_DF_END was received | |
413 | assert(!_cur_logic_snapshot); | |
414 | assert(!_cur_analog_snapshot); | |
eec446e1 JH |
415 | } |
416 | ||
417 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
418 | { | |
8eb9d311 BV |
419 | GVariant *gvar; |
420 | uint64_t sample_rate = 0; | |
8d634081 | 421 | |
7297c76e | 422 | // Read out the sample rate |
728e5ef7 JH |
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 | } | |
c0d6d479 | 431 | |
728e5ef7 JH |
432 | sample_rate = g_variant_get_uint64(gvar); |
433 | g_variant_unref(gvar); | |
174e27a1 JH |
434 | } |
435 | ||
b087ba7f JH |
436 | if(_analog_data) |
437 | _analog_data->set_samplerate(sample_rate); | |
438 | if(_logic_data) | |
439 | _logic_data->set_samplerate(sample_rate); | |
be73bdfa JH |
440 | } |
441 | ||
442 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
443 | const sr_datafeed_meta &meta) | |
444 | { | |
94fe58ef UH |
445 | (void)sdi; |
446 | ||
be73bdfa JH |
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 | } | |
aba1dd16 JH |
458 | } |
459 | } | |
460 | ||
9c112671 JH |
461 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
462 | { | |
463 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
464 | |
465 | if (!_logic_data) | |
9c112671 | 466 | { |
79efbc53 JH |
467 | qDebug() << "Unexpected logic packet"; |
468 | return; | |
469 | } | |
be73bdfa | 470 | |
79efbc53 JH |
471 | if (!_cur_logic_snapshot) |
472 | { | |
2b49eeb0 JH |
473 | set_capture_state(Running); |
474 | ||
9c112671 | 475 | // Create a new data snapshot |
1b1ec774 JH |
476 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
477 | new data::LogicSnapshot(logic)); | |
9c112671 JH |
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 | ||
aba1dd16 JH |
489 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
490 | { | |
491 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
492 | |
493 | if(!_analog_data) | |
aba1dd16 | 494 | { |
79efbc53 JH |
495 | qDebug() << "Unexpected analog packet"; |
496 | return; // This analog packet was not expected. | |
497 | } | |
be73bdfa | 498 | |
79efbc53 JH |
499 | if (!_cur_analog_snapshot) |
500 | { | |
2b49eeb0 JH |
501 | set_capture_state(Running); |
502 | ||
aba1dd16 | 503 | // Create a new data snapshot |
1b1ec774 JH |
504 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( |
505 | new data::AnalogSnapshot(analog)); | |
aba1dd16 JH |
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 | } | |
9c112671 | 516 | |
b0e1d01d JH |
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: | |
eec446e1 | 525 | feed_in_header(sdi); |
b0e1d01d JH |
526 | break; |
527 | ||
be73bdfa | 528 | case SR_DF_META: |
aba1dd16 | 529 | assert(packet->payload); |
be73bdfa JH |
530 | feed_in_meta(sdi, |
531 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
532 | break; |
533 | ||
2e2946fe | 534 | case SR_DF_LOGIC: |
28a4c9c5 | 535 | assert(packet->payload); |
9c112671 | 536 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
537 | break; |
538 | ||
aba1dd16 JH |
539 | case SR_DF_ANALOG: |
540 | assert(packet->payload); | |
541 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
542 | break; | |
543 | ||
2953961c | 544 | case SR_DF_END: |
2e2946fe JH |
545 | { |
546 | { | |
547 | lock_guard<mutex> lock(_data_mutex); | |
548 | _cur_logic_snapshot.reset(); | |
aba1dd16 | 549 | _cur_analog_snapshot.reset(); |
2e2946fe | 550 | } |
04abfae9 | 551 | data_updated(); |
2953961c JH |
552 | break; |
553 | } | |
2e2946fe | 554 | } |
2953961c JH |
555 | } |
556 | ||
04abfae9 | 557 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 558 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 559 | { |
5045f16d | 560 | (void) cb_data; |
04abfae9 JH |
561 | assert(_session); |
562 | _session->data_feed_in(sdi, packet); | |
2953961c | 563 | } |
51e77110 JH |
564 | |
565 | } // namespace pv |