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