]>
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 JH |
81 | _sdi = sdi; |
82 | } | |
83 | ||
dc0867ff JH |
84 | void SigSession::release_device(struct sr_dev_inst *sdi) |
85 | { | |
86 | (void)sdi; | |
87 | ||
88 | assert(_capture_state == Stopped); | |
89 | _sdi = NULL; | |
90 | } | |
91 | ||
f2edb557 JH |
92 | void SigSession::load_file(const string &name, |
93 | function<void (const QString)> error_handler) | |
2953961c | 94 | { |
8a67bd9e JH |
95 | stop_capture(); |
96 | _sampling_thread.reset(new boost::thread( | |
f2edb557 JH |
97 | &SigSession::load_thread_proc, this, name, |
98 | error_handler)); | |
2953961c JH |
99 | } |
100 | ||
5b7cf66c JH |
101 | SigSession::capture_state SigSession::get_capture_state() const |
102 | { | |
949f8050 | 103 | lock_guard<mutex> lock(_sampling_mutex); |
5b7cf66c JH |
104 | return _capture_state; |
105 | } | |
106 | ||
d64d1596 | 107 | void SigSession::start_capture(uint64_t record_length, |
f2edb557 | 108 | function<void (const QString)> error_handler) |
2e2946fe | 109 | { |
5b7cf66c JH |
110 | stop_capture(); |
111 | ||
d64d1596 JH |
112 | // Check that a device instance has been selected. |
113 | if (!_sdi) { | |
114 | qDebug() << "No device selected"; | |
115 | return; | |
116 | } | |
117 | ||
9c48fa57 JH |
118 | // Check that at least one probe is enabled |
119 | const GSList *l; | |
d64d1596 | 120 | for (l = _sdi->probes; l; l = l->next) { |
9c48fa57 JH |
121 | sr_probe *const probe = (sr_probe*)l->data; |
122 | assert(probe); | |
123 | if (probe->enabled) | |
124 | break; | |
125 | } | |
126 | ||
127 | if (!l) { | |
128 | error_handler(tr("No probes enabled.")); | |
129 | return; | |
130 | } | |
131 | ||
132 | // Begin the session | |
2e2946fe | 133 | _sampling_thread.reset(new boost::thread( |
d64d1596 | 134 | &SigSession::sample_thread_proc, this, _sdi, |
f2edb557 | 135 | record_length, error_handler)); |
2e2946fe JH |
136 | } |
137 | ||
5b7cf66c JH |
138 | void SigSession::stop_capture() |
139 | { | |
333d5bbc | 140 | if (get_capture_state() == Stopped) |
5b7cf66c JH |
141 | return; |
142 | ||
143 | sr_session_stop(); | |
144 | ||
145 | // Check that sampling stopped | |
333d5bbc | 146 | if (_sampling_thread.get()) |
5b7cf66c JH |
147 | _sampling_thread->join(); |
148 | _sampling_thread.reset(); | |
5b7cf66c JH |
149 | } |
150 | ||
3868e5fa | 151 | vector< shared_ptr<view::Signal> > SigSession::get_signals() |
2e2946fe | 152 | { |
3868e5fa | 153 | lock_guard<mutex> lock(_signals_mutex); |
2e2946fe JH |
154 | return _signals; |
155 | } | |
156 | ||
1b1ec774 | 157 | boost::shared_ptr<data::Logic> SigSession::get_data() |
2e2946fe JH |
158 | { |
159 | return _logic_data; | |
160 | } | |
161 | ||
6ac96c2e JH |
162 | void SigSession::set_capture_state(capture_state state) |
163 | { | |
949f8050 | 164 | lock_guard<mutex> lock(_sampling_mutex); |
6ac96c2e JH |
165 | _capture_state = state; |
166 | capture_state_changed(state); | |
167 | } | |
168 | ||
728e5ef7 JH |
169 | /** |
170 | * Attempts to autodetect the format. Failing that | |
171 | * @param filename The filename of the input file. | |
172 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
173 | * or NULL if no input format was selected or auto-detected. | |
174 | */ | |
175 | sr_input_format* SigSession::determine_input_file_format( | |
176 | const string &filename) | |
177 | { | |
178 | int i; | |
179 | ||
180 | /* If there are no input formats, return NULL right away. */ | |
181 | sr_input_format *const *const inputs = sr_input_list(); | |
182 | if (!inputs) { | |
183 | g_critical("No supported input formats available."); | |
184 | return NULL; | |
185 | } | |
186 | ||
187 | /* Otherwise, try to find an input module that can handle this file. */ | |
188 | for (i = 0; inputs[i]; i++) { | |
189 | if (inputs[i]->format_match(filename.c_str())) | |
190 | break; | |
191 | } | |
192 | ||
193 | /* Return NULL if no input module wanted to touch this. */ | |
194 | if (!inputs[i]) { | |
195 | g_critical("Error: no matching input module found."); | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | return inputs[i]; | |
200 | } | |
201 | ||
202 | sr_input* SigSession::load_input_file_format(const string &filename, | |
203 | function<void (const QString)> error_handler, | |
204 | sr_input_format *format) | |
205 | { | |
206 | struct stat st; | |
207 | sr_input *in; | |
208 | ||
209 | if (!format && !(format = | |
210 | determine_input_file_format(filename.c_str()))) { | |
211 | /* The exact cause was already logged. */ | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | if (stat(filename.c_str(), &st) == -1) { | |
216 | error_handler(tr("Failed to load file")); | |
217 | return NULL; | |
218 | } | |
219 | ||
220 | /* Initialize the input module. */ | |
221 | if (!(in = new sr_input)) { | |
222 | qDebug("Failed to allocate input module.\n"); | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | in->format = format; | |
227 | in->param = NULL; | |
228 | if (in->format->init && | |
229 | in->format->init(in, filename.c_str()) != SR_OK) { | |
230 | qDebug("Input format init failed.\n"); | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | sr_session_new(); | |
235 | ||
236 | if (sr_session_dev_add(in->sdi) != SR_OK) { | |
237 | qDebug("Failed to use device.\n"); | |
238 | sr_session_destroy(); | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | return in; | |
243 | } | |
244 | ||
f2edb557 JH |
245 | void SigSession::load_thread_proc(const string name, |
246 | function<void (const QString)> error_handler) | |
8a67bd9e | 247 | { |
728e5ef7 JH |
248 | sr_input *in = NULL; |
249 | ||
250 | if (sr_session_load(name.c_str()) == SR_OK) { | |
251 | if (sr_session_start() != SR_OK) { | |
252 | error_handler(tr("Failed to start session.")); | |
253 | return; | |
254 | } | |
8a67bd9e | 255 | } |
728e5ef7 JH |
256 | else if(!(in = load_input_file_format(name.c_str(), error_handler))) |
257 | return; | |
8a67bd9e | 258 | |
5045f16d | 259 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
8a67bd9e | 260 | |
8a67bd9e JH |
261 | set_capture_state(Running); |
262 | ||
728e5ef7 JH |
263 | if(in) { |
264 | assert(in->format); | |
265 | in->format->loadfile(in, name.c_str()); | |
266 | } else | |
267 | sr_session_run(); | |
8a67bd9e | 268 | |
728e5ef7 | 269 | sr_session_destroy(); |
8a67bd9e | 270 | set_capture_state(Stopped); |
1a2fe44c JH |
271 | |
272 | // Confirm that SR_DF_END was received | |
273 | assert(!_cur_logic_snapshot); | |
274 | assert(!_cur_analog_snapshot); | |
728e5ef7 JH |
275 | |
276 | delete in; | |
8a67bd9e JH |
277 | } |
278 | ||
2e2946fe | 279 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, |
f2edb557 JH |
280 | uint64_t record_length, |
281 | function<void (const QString)> error_handler) | |
274d4f13 | 282 | { |
be73bdfa | 283 | assert(sdi); |
f2edb557 | 284 | assert(error_handler); |
be73bdfa | 285 | |
274d4f13 | 286 | sr_session_new(); |
5045f16d | 287 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
274d4f13 JH |
288 | |
289 | if (sr_session_dev_add(sdi) != SR_OK) { | |
f2edb557 | 290 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
291 | sr_session_destroy(); |
292 | return; | |
293 | } | |
294 | ||
be73bdfa JH |
295 | // Set the sample limit |
296 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
8eb9d311 | 297 | g_variant_new_uint64(record_length)) != SR_OK) { |
f2edb557 JH |
298 | error_handler(tr("Failed to configure " |
299 | "time-based sample limit.")); | |
274d4f13 JH |
300 | sr_session_destroy(); |
301 | return; | |
302 | } | |
303 | ||
eec446e1 | 304 | if (sr_session_start() != SR_OK) { |
f2edb557 | 305 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
306 | return; |
307 | } | |
308 | ||
309 | set_capture_state(Running); | |
310 | ||
311 | sr_session_run(); | |
312 | sr_session_destroy(); | |
313 | ||
314 | set_capture_state(Stopped); | |
1a2fe44c JH |
315 | |
316 | // Confirm that SR_DF_END was received | |
317 | assert(!_cur_logic_snapshot); | |
318 | assert(!_cur_analog_snapshot); | |
eec446e1 JH |
319 | } |
320 | ||
321 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
322 | { | |
323 | shared_ptr<view::Signal> signal; | |
8eb9d311 BV |
324 | GVariant *gvar; |
325 | uint64_t sample_rate = 0; | |
eec446e1 JH |
326 | unsigned int logic_probe_count = 0; |
327 | unsigned int analog_probe_count = 0; | |
328 | ||
be73bdfa JH |
329 | // Detect what data types we will receive |
330 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
331 | const sr_probe *const probe = (const sr_probe *)l->data; | |
332 | if (!probe->enabled) | |
333 | continue; | |
274d4f13 | 334 | |
be73bdfa JH |
335 | switch(probe->type) { |
336 | case SR_PROBE_LOGIC: | |
337 | logic_probe_count++; | |
338 | break; | |
5b7cf66c | 339 | |
be73bdfa JH |
340 | case SR_PROBE_ANALOG: |
341 | analog_probe_count++; | |
342 | break; | |
343 | } | |
344 | } | |
8d634081 | 345 | |
7297c76e | 346 | // Read out the sample rate |
728e5ef7 JH |
347 | if(sdi->driver) |
348 | { | |
349 | const int ret = sr_config_get(sdi->driver, | |
350 | SR_CONF_SAMPLERATE, &gvar, sdi); | |
351 | if (ret != SR_OK) { | |
352 | qDebug("Failed to get samplerate\n"); | |
353 | return; | |
354 | } | |
c0d6d479 | 355 | |
728e5ef7 JH |
356 | sample_rate = g_variant_get_uint64(gvar); |
357 | g_variant_unref(gvar); | |
174e27a1 JH |
358 | } |
359 | ||
be73bdfa | 360 | // Create data containers for the coming data snapshots |
3868e5fa | 361 | { |
aba1dd16 | 362 | lock_guard<mutex> data_lock(_data_mutex); |
3868e5fa | 363 | |
be73bdfa JH |
364 | if (logic_probe_count != 0) { |
365 | _logic_data.reset(new data::Logic( | |
8eb9d311 | 366 | logic_probe_count, sample_rate)); |
be73bdfa JH |
367 | assert(_logic_data); |
368 | } | |
369 | ||
370 | if (analog_probe_count != 0) { | |
8eb9d311 | 371 | _analog_data.reset(new data::Analog(sample_rate)); |
be73bdfa JH |
372 | assert(_analog_data); |
373 | } | |
3868e5fa JH |
374 | } |
375 | ||
be73bdfa | 376 | // Make the logic probe list |
3868e5fa JH |
377 | { |
378 | lock_guard<mutex> lock(_signals_mutex); | |
2e2946fe | 379 | |
be73bdfa JH |
380 | _signals.clear(); |
381 | ||
382 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
2e2946fe | 383 | const sr_probe *const probe = |
be73bdfa JH |
384 | (const sr_probe *)l->data; |
385 | assert(probe); | |
386 | if (!probe->enabled) | |
387 | continue; | |
388 | ||
389 | switch(probe->type) { | |
390 | case SR_PROBE_LOGIC: | |
391 | signal = shared_ptr<view::Signal>( | |
392 | new view::LogicSignal(probe->name, | |
b0e1d01d | 393 | _logic_data, probe->index)); |
be73bdfa JH |
394 | break; |
395 | ||
396 | case SR_PROBE_ANALOG: | |
397 | signal = shared_ptr<view::Signal>( | |
398 | new view::AnalogSignal(probe->name, | |
568b90d4 | 399 | _analog_data, probe->index)); |
be73bdfa | 400 | break; |
e3f65ace | 401 | } |
be73bdfa JH |
402 | |
403 | _signals.push_back(signal); | |
2953961c | 404 | } |
28a4c9c5 | 405 | |
69dd2b03 | 406 | signals_changed(); |
2e2946fe | 407 | } |
be73bdfa JH |
408 | } |
409 | ||
410 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
411 | const sr_datafeed_meta &meta) | |
412 | { | |
94fe58ef UH |
413 | (void)sdi; |
414 | ||
be73bdfa JH |
415 | for (const GSList *l = meta.config; l; l = l->next) { |
416 | const sr_config *const src = (const sr_config*)l->data; | |
417 | switch (src->key) { | |
418 | case SR_CONF_SAMPLERATE: | |
419 | /// @todo handle samplerate changes | |
420 | /// samplerate = (uint64_t *)src->value; | |
421 | break; | |
422 | default: | |
423 | // Unknown metadata is not an error. | |
424 | break; | |
425 | } | |
aba1dd16 JH |
426 | } |
427 | } | |
428 | ||
9c112671 JH |
429 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
430 | { | |
431 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
432 | |
433 | if (!_logic_data) | |
9c112671 | 434 | { |
79efbc53 JH |
435 | qDebug() << "Unexpected logic packet"; |
436 | return; | |
437 | } | |
be73bdfa | 438 | |
79efbc53 JH |
439 | if (!_cur_logic_snapshot) |
440 | { | |
9c112671 | 441 | // Create a new data snapshot |
1b1ec774 JH |
442 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
443 | new data::LogicSnapshot(logic)); | |
9c112671 JH |
444 | _logic_data->push_snapshot(_cur_logic_snapshot); |
445 | } | |
446 | else | |
447 | { | |
448 | // Append to the existing data snapshot | |
449 | _cur_logic_snapshot->append_payload(logic); | |
450 | } | |
451 | ||
452 | data_updated(); | |
453 | } | |
454 | ||
aba1dd16 JH |
455 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
456 | { | |
457 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
458 | |
459 | if(!_analog_data) | |
aba1dd16 | 460 | { |
79efbc53 JH |
461 | qDebug() << "Unexpected analog packet"; |
462 | return; // This analog packet was not expected. | |
463 | } | |
be73bdfa | 464 | |
79efbc53 JH |
465 | if (!_cur_analog_snapshot) |
466 | { | |
aba1dd16 | 467 | // Create a new data snapshot |
1b1ec774 JH |
468 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( |
469 | new data::AnalogSnapshot(analog)); | |
aba1dd16 JH |
470 | _analog_data->push_snapshot(_cur_analog_snapshot); |
471 | } | |
472 | else | |
473 | { | |
474 | // Append to the existing data snapshot | |
475 | _cur_analog_snapshot->append_payload(analog); | |
476 | } | |
477 | ||
478 | data_updated(); | |
479 | } | |
9c112671 | 480 | |
b0e1d01d JH |
481 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
482 | const struct sr_datafeed_packet *packet) | |
483 | { | |
484 | assert(sdi); | |
485 | assert(packet); | |
486 | ||
487 | switch (packet->type) { | |
488 | case SR_DF_HEADER: | |
eec446e1 | 489 | feed_in_header(sdi); |
b0e1d01d JH |
490 | break; |
491 | ||
be73bdfa | 492 | case SR_DF_META: |
aba1dd16 | 493 | assert(packet->payload); |
be73bdfa JH |
494 | feed_in_meta(sdi, |
495 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
496 | break; |
497 | ||
2e2946fe | 498 | case SR_DF_LOGIC: |
28a4c9c5 | 499 | assert(packet->payload); |
9c112671 | 500 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
501 | break; |
502 | ||
aba1dd16 JH |
503 | case SR_DF_ANALOG: |
504 | assert(packet->payload); | |
505 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
506 | break; | |
507 | ||
2953961c | 508 | case SR_DF_END: |
2e2946fe JH |
509 | { |
510 | { | |
511 | lock_guard<mutex> lock(_data_mutex); | |
512 | _cur_logic_snapshot.reset(); | |
aba1dd16 | 513 | _cur_analog_snapshot.reset(); |
2e2946fe | 514 | } |
04abfae9 | 515 | data_updated(); |
2953961c JH |
516 | break; |
517 | } | |
2e2946fe | 518 | } |
2953961c JH |
519 | } |
520 | ||
04abfae9 | 521 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 522 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 523 | { |
5045f16d | 524 | (void) cb_data; |
04abfae9 JH |
525 | assert(_session); |
526 | _session->data_feed_in(sdi, packet); | |
2953961c | 527 | } |
51e77110 JH |
528 | |
529 | } // namespace pv |