]>
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 | // TODO: This should not be necessary | |
63 | _session = NULL; | |
64 | } | |
65 | ||
66 | struct sr_dev_inst* SigSession::get_device() const | |
67 | { | |
68 | return _sdi; | |
69 | } | |
70 | ||
71 | void SigSession::set_device(struct sr_dev_inst *sdi) | |
72 | { | |
73 | if (_sdi) | |
74 | _device_manager.release_device(_sdi); | |
75 | if (sdi) | |
76 | _device_manager.use_device(sdi, this); | |
77 | _sdi = sdi; | |
78 | } | |
79 | ||
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 | ||
88 | void SigSession::load_file(const string &name, | |
89 | function<void (const QString)> error_handler) | |
90 | { | |
91 | stop_capture(); | |
92 | _sampling_thread.reset(new boost::thread( | |
93 | &SigSession::load_thread_proc, this, name, | |
94 | error_handler)); | |
95 | } | |
96 | ||
97 | SigSession::capture_state SigSession::get_capture_state() const | |
98 | { | |
99 | lock_guard<mutex> lock(_sampling_mutex); | |
100 | return _capture_state; | |
101 | } | |
102 | ||
103 | void SigSession::start_capture(uint64_t record_length, | |
104 | function<void (const QString)> error_handler) | |
105 | { | |
106 | stop_capture(); | |
107 | ||
108 | // Check that a device instance has been selected. | |
109 | if (!_sdi) { | |
110 | qDebug() << "No device selected"; | |
111 | return; | |
112 | } | |
113 | ||
114 | // Check that at least one probe is enabled | |
115 | const GSList *l; | |
116 | for (l = _sdi->probes; l; l = l->next) { | |
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 | |
129 | _sampling_thread.reset(new boost::thread( | |
130 | &SigSession::sample_thread_proc, this, _sdi, | |
131 | record_length, error_handler)); | |
132 | } | |
133 | ||
134 | void SigSession::stop_capture() | |
135 | { | |
136 | if (get_capture_state() == Stopped) | |
137 | return; | |
138 | ||
139 | sr_session_stop(); | |
140 | ||
141 | // Check that sampling stopped | |
142 | if (_sampling_thread.get()) | |
143 | _sampling_thread->join(); | |
144 | _sampling_thread.reset(); | |
145 | } | |
146 | ||
147 | vector< shared_ptr<view::Signal> > SigSession::get_signals() | |
148 | { | |
149 | lock_guard<mutex> lock(_signals_mutex); | |
150 | return _signals; | |
151 | } | |
152 | ||
153 | boost::shared_ptr<data::Logic> SigSession::get_data() | |
154 | { | |
155 | return _logic_data; | |
156 | } | |
157 | ||
158 | void SigSession::set_capture_state(capture_state state) | |
159 | { | |
160 | lock_guard<mutex> lock(_sampling_mutex); | |
161 | _capture_state = state; | |
162 | capture_state_changed(state); | |
163 | } | |
164 | ||
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 | ||
241 | void SigSession::load_thread_proc(const string name, | |
242 | function<void (const QString)> error_handler) | |
243 | { | |
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 | } | |
251 | } | |
252 | else if(!(in = load_input_file_format(name.c_str(), error_handler))) | |
253 | return; | |
254 | ||
255 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); | |
256 | ||
257 | set_capture_state(Running); | |
258 | ||
259 | if(in) { | |
260 | assert(in->format); | |
261 | in->format->loadfile(in, name.c_str()); | |
262 | } else | |
263 | sr_session_run(); | |
264 | ||
265 | sr_session_destroy(); | |
266 | set_capture_state(Stopped); | |
267 | ||
268 | // Confirm that SR_DF_END was received | |
269 | assert(!_cur_logic_snapshot); | |
270 | assert(!_cur_analog_snapshot); | |
271 | ||
272 | delete in; | |
273 | } | |
274 | ||
275 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, | |
276 | uint64_t record_length, | |
277 | function<void (const QString)> error_handler) | |
278 | { | |
279 | assert(sdi); | |
280 | assert(error_handler); | |
281 | ||
282 | sr_session_new(); | |
283 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); | |
284 | ||
285 | if (sr_session_dev_add(sdi) != SR_OK) { | |
286 | error_handler(tr("Failed to use device.")); | |
287 | sr_session_destroy(); | |
288 | return; | |
289 | } | |
290 | ||
291 | // Set the sample limit | |
292 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
293 | g_variant_new_uint64(record_length)) != SR_OK) { | |
294 | error_handler(tr("Failed to configure " | |
295 | "time-based sample limit.")); | |
296 | sr_session_destroy(); | |
297 | return; | |
298 | } | |
299 | ||
300 | if (sr_session_start() != SR_OK) { | |
301 | error_handler(tr("Failed to start session.")); | |
302 | return; | |
303 | } | |
304 | ||
305 | set_capture_state(Running); | |
306 | ||
307 | sr_session_run(); | |
308 | sr_session_destroy(); | |
309 | ||
310 | set_capture_state(Stopped); | |
311 | ||
312 | // Confirm that SR_DF_END was received | |
313 | assert(!_cur_logic_snapshot); | |
314 | assert(!_cur_analog_snapshot); | |
315 | } | |
316 | ||
317 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
318 | { | |
319 | shared_ptr<view::Signal> signal; | |
320 | GVariant *gvar; | |
321 | uint64_t sample_rate = 0; | |
322 | unsigned int logic_probe_count = 0; | |
323 | unsigned int analog_probe_count = 0; | |
324 | ||
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; | |
330 | ||
331 | switch(probe->type) { | |
332 | case SR_PROBE_LOGIC: | |
333 | logic_probe_count++; | |
334 | break; | |
335 | ||
336 | case SR_PROBE_ANALOG: | |
337 | analog_probe_count++; | |
338 | break; | |
339 | } | |
340 | } | |
341 | ||
342 | // Read out the sample rate | |
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 | } | |
351 | ||
352 | sample_rate = g_variant_get_uint64(gvar); | |
353 | g_variant_unref(gvar); | |
354 | } | |
355 | ||
356 | // Create data containers for the coming data snapshots | |
357 | { | |
358 | lock_guard<mutex> data_lock(_data_mutex); | |
359 | ||
360 | if (logic_probe_count != 0) { | |
361 | _logic_data.reset(new data::Logic( | |
362 | logic_probe_count, sample_rate)); | |
363 | assert(_logic_data); | |
364 | } | |
365 | ||
366 | if (analog_probe_count != 0) { | |
367 | _analog_data.reset(new data::Analog(sample_rate)); | |
368 | assert(_analog_data); | |
369 | } | |
370 | } | |
371 | ||
372 | // Make the logic probe list | |
373 | { | |
374 | lock_guard<mutex> lock(_signals_mutex); | |
375 | ||
376 | _signals.clear(); | |
377 | ||
378 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
379 | const sr_probe *const probe = | |
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, | |
389 | _logic_data, probe->index)); | |
390 | break; | |
391 | ||
392 | case SR_PROBE_ANALOG: | |
393 | signal = shared_ptr<view::Signal>( | |
394 | new view::AnalogSignal(probe->name, | |
395 | _analog_data, probe->index)); | |
396 | break; | |
397 | } | |
398 | ||
399 | _signals.push_back(signal); | |
400 | } | |
401 | ||
402 | signals_changed(); | |
403 | } | |
404 | } | |
405 | ||
406 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
407 | const sr_datafeed_meta &meta) | |
408 | { | |
409 | (void)sdi; | |
410 | ||
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 | } | |
422 | } | |
423 | } | |
424 | ||
425 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) | |
426 | { | |
427 | lock_guard<mutex> lock(_data_mutex); | |
428 | ||
429 | if (!_logic_data) | |
430 | { | |
431 | qDebug() << "Unexpected logic packet"; | |
432 | return; | |
433 | } | |
434 | ||
435 | if (!_cur_logic_snapshot) | |
436 | { | |
437 | // Create a new data snapshot | |
438 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( | |
439 | new data::LogicSnapshot(logic)); | |
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 | ||
451 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) | |
452 | { | |
453 | lock_guard<mutex> lock(_data_mutex); | |
454 | ||
455 | if(!_analog_data) | |
456 | { | |
457 | qDebug() << "Unexpected analog packet"; | |
458 | return; // This analog packet was not expected. | |
459 | } | |
460 | ||
461 | if (!_cur_analog_snapshot) | |
462 | { | |
463 | // Create a new data snapshot | |
464 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( | |
465 | new data::AnalogSnapshot(analog)); | |
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 | } | |
476 | ||
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: | |
485 | feed_in_header(sdi); | |
486 | break; | |
487 | ||
488 | case SR_DF_META: | |
489 | assert(packet->payload); | |
490 | feed_in_meta(sdi, | |
491 | *(const sr_datafeed_meta*)packet->payload); | |
492 | break; | |
493 | ||
494 | case SR_DF_LOGIC: | |
495 | assert(packet->payload); | |
496 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); | |
497 | break; | |
498 | ||
499 | case SR_DF_ANALOG: | |
500 | assert(packet->payload); | |
501 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
502 | break; | |
503 | ||
504 | case SR_DF_END: | |
505 | { | |
506 | { | |
507 | lock_guard<mutex> lock(_data_mutex); | |
508 | _cur_logic_snapshot.reset(); | |
509 | _cur_analog_snapshot.reset(); | |
510 | } | |
511 | data_updated(); | |
512 | break; | |
513 | } | |
514 | } | |
515 | } | |
516 | ||
517 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, | |
518 | const struct sr_datafeed_packet *packet, void *cb_data) | |
519 | { | |
520 | (void) cb_data; | |
521 | assert(_session); | |
522 | _session->data_feed_in(sdi, packet); | |
523 | } | |
524 | ||
525 | } // namespace pv |