]>
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; |
b087ba7f | 82 | update_signals(); |
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; | |
b087ba7f | 91 | update_signals(); |
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); |
6ac96c2e JH |
167 | _capture_state = state; |
168 | capture_state_changed(state); | |
169 | } | |
170 | ||
728e5ef7 JH |
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 | */ | |
177 | sr_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 | ||
204 | sr_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 | ||
b087ba7f JH |
247 | void 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); | |
b087ba7f JH |
303 | |
304 | switch(probe->type) { | |
305 | case SR_PROBE_LOGIC: | |
306 | signal = shared_ptr<view::Signal>( | |
cec48d16 JH |
307 | new view::LogicSignal(probe, |
308 | _logic_data)); | |
b087ba7f JH |
309 | break; |
310 | ||
311 | case SR_PROBE_ANALOG: | |
312 | signal = shared_ptr<view::Signal>( | |
cec48d16 JH |
313 | new view::AnalogSignal(probe, |
314 | _analog_data)); | |
b087ba7f JH |
315 | break; |
316 | } | |
317 | ||
318 | _signals.push_back(signal); | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | signals_changed(); | |
324 | } | |
325 | ||
f2edb557 JH |
326 | void SigSession::load_thread_proc(const string name, |
327 | function<void (const QString)> error_handler) | |
8a67bd9e | 328 | { |
728e5ef7 JH |
329 | sr_input *in = NULL; |
330 | ||
331 | if (sr_session_load(name.c_str()) == SR_OK) { | |
332 | if (sr_session_start() != SR_OK) { | |
333 | error_handler(tr("Failed to start session.")); | |
334 | return; | |
335 | } | |
8a67bd9e | 336 | } |
728e5ef7 JH |
337 | else if(!(in = load_input_file_format(name.c_str(), error_handler))) |
338 | return; | |
8a67bd9e | 339 | |
5045f16d | 340 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
8a67bd9e | 341 | |
8a67bd9e JH |
342 | set_capture_state(Running); |
343 | ||
728e5ef7 JH |
344 | if(in) { |
345 | assert(in->format); | |
346 | in->format->loadfile(in, name.c_str()); | |
347 | } else | |
348 | sr_session_run(); | |
8a67bd9e | 349 | |
728e5ef7 | 350 | sr_session_destroy(); |
8a67bd9e | 351 | set_capture_state(Stopped); |
1a2fe44c JH |
352 | |
353 | // Confirm that SR_DF_END was received | |
354 | assert(!_cur_logic_snapshot); | |
355 | assert(!_cur_analog_snapshot); | |
728e5ef7 JH |
356 | |
357 | delete in; | |
8a67bd9e JH |
358 | } |
359 | ||
2e2946fe | 360 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, |
f2edb557 JH |
361 | uint64_t record_length, |
362 | function<void (const QString)> error_handler) | |
274d4f13 | 363 | { |
be73bdfa | 364 | assert(sdi); |
f2edb557 | 365 | assert(error_handler); |
be73bdfa | 366 | |
274d4f13 | 367 | sr_session_new(); |
5045f16d | 368 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
274d4f13 JH |
369 | |
370 | if (sr_session_dev_add(sdi) != SR_OK) { | |
f2edb557 | 371 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
372 | sr_session_destroy(); |
373 | return; | |
374 | } | |
375 | ||
be73bdfa JH |
376 | // Set the sample limit |
377 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
8eb9d311 | 378 | g_variant_new_uint64(record_length)) != SR_OK) { |
f2edb557 JH |
379 | error_handler(tr("Failed to configure " |
380 | "time-based sample limit.")); | |
274d4f13 JH |
381 | sr_session_destroy(); |
382 | return; | |
383 | } | |
384 | ||
eec446e1 | 385 | if (sr_session_start() != SR_OK) { |
f2edb557 | 386 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
387 | return; |
388 | } | |
389 | ||
390 | set_capture_state(Running); | |
391 | ||
392 | sr_session_run(); | |
393 | sr_session_destroy(); | |
394 | ||
395 | set_capture_state(Stopped); | |
1a2fe44c JH |
396 | |
397 | // Confirm that SR_DF_END was received | |
398 | assert(!_cur_logic_snapshot); | |
399 | assert(!_cur_analog_snapshot); | |
eec446e1 JH |
400 | } |
401 | ||
402 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
403 | { | |
8eb9d311 BV |
404 | GVariant *gvar; |
405 | uint64_t sample_rate = 0; | |
8d634081 | 406 | |
7297c76e | 407 | // Read out the sample rate |
728e5ef7 JH |
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 | } | |
c0d6d479 | 416 | |
728e5ef7 JH |
417 | sample_rate = g_variant_get_uint64(gvar); |
418 | g_variant_unref(gvar); | |
174e27a1 JH |
419 | } |
420 | ||
b087ba7f JH |
421 | if(_analog_data) |
422 | _analog_data->set_samplerate(sample_rate); | |
423 | if(_logic_data) | |
424 | _logic_data->set_samplerate(sample_rate); | |
be73bdfa JH |
425 | } |
426 | ||
427 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
428 | const sr_datafeed_meta &meta) | |
429 | { | |
94fe58ef UH |
430 | (void)sdi; |
431 | ||
be73bdfa JH |
432 | for (const GSList *l = meta.config; l; l = l->next) { |
433 | const sr_config *const src = (const sr_config*)l->data; | |
434 | switch (src->key) { | |
435 | case SR_CONF_SAMPLERATE: | |
436 | /// @todo handle samplerate changes | |
437 | /// samplerate = (uint64_t *)src->value; | |
438 | break; | |
439 | default: | |
440 | // Unknown metadata is not an error. | |
441 | break; | |
442 | } | |
aba1dd16 JH |
443 | } |
444 | } | |
445 | ||
9c112671 JH |
446 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
447 | { | |
448 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
449 | |
450 | if (!_logic_data) | |
9c112671 | 451 | { |
79efbc53 JH |
452 | qDebug() << "Unexpected logic packet"; |
453 | return; | |
454 | } | |
be73bdfa | 455 | |
79efbc53 JH |
456 | if (!_cur_logic_snapshot) |
457 | { | |
9c112671 | 458 | // Create a new data snapshot |
1b1ec774 JH |
459 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
460 | new data::LogicSnapshot(logic)); | |
9c112671 JH |
461 | _logic_data->push_snapshot(_cur_logic_snapshot); |
462 | } | |
463 | else | |
464 | { | |
465 | // Append to the existing data snapshot | |
466 | _cur_logic_snapshot->append_payload(logic); | |
467 | } | |
468 | ||
469 | data_updated(); | |
470 | } | |
471 | ||
aba1dd16 JH |
472 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
473 | { | |
474 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
475 | |
476 | if(!_analog_data) | |
aba1dd16 | 477 | { |
79efbc53 JH |
478 | qDebug() << "Unexpected analog packet"; |
479 | return; // This analog packet was not expected. | |
480 | } | |
be73bdfa | 481 | |
79efbc53 JH |
482 | if (!_cur_analog_snapshot) |
483 | { | |
aba1dd16 | 484 | // Create a new data snapshot |
1b1ec774 JH |
485 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( |
486 | new data::AnalogSnapshot(analog)); | |
aba1dd16 JH |
487 | _analog_data->push_snapshot(_cur_analog_snapshot); |
488 | } | |
489 | else | |
490 | { | |
491 | // Append to the existing data snapshot | |
492 | _cur_analog_snapshot->append_payload(analog); | |
493 | } | |
494 | ||
495 | data_updated(); | |
496 | } | |
9c112671 | 497 | |
b0e1d01d JH |
498 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
499 | const struct sr_datafeed_packet *packet) | |
500 | { | |
501 | assert(sdi); | |
502 | assert(packet); | |
503 | ||
504 | switch (packet->type) { | |
505 | case SR_DF_HEADER: | |
eec446e1 | 506 | feed_in_header(sdi); |
b0e1d01d JH |
507 | break; |
508 | ||
be73bdfa | 509 | case SR_DF_META: |
aba1dd16 | 510 | assert(packet->payload); |
be73bdfa JH |
511 | feed_in_meta(sdi, |
512 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
513 | break; |
514 | ||
2e2946fe | 515 | case SR_DF_LOGIC: |
28a4c9c5 | 516 | assert(packet->payload); |
9c112671 | 517 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
518 | break; |
519 | ||
aba1dd16 JH |
520 | case SR_DF_ANALOG: |
521 | assert(packet->payload); | |
522 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
523 | break; | |
524 | ||
2953961c | 525 | case SR_DF_END: |
2e2946fe JH |
526 | { |
527 | { | |
528 | lock_guard<mutex> lock(_data_mutex); | |
529 | _cur_logic_snapshot.reset(); | |
aba1dd16 | 530 | _cur_analog_snapshot.reset(); |
2e2946fe | 531 | } |
04abfae9 | 532 | data_updated(); |
2953961c JH |
533 | break; |
534 | } | |
2e2946fe | 535 | } |
2953961c JH |
536 | } |
537 | ||
04abfae9 | 538 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 539 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 540 | { |
5045f16d | 541 | (void) cb_data; |
04abfae9 JH |
542 | assert(_session); |
543 | _session->data_feed_in(sdi, packet); | |
2953961c | 544 | } |
51e77110 JH |
545 | |
546 | } // namespace pv |