]>
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 <QDebug> | |
34 | ||
35 | using namespace boost; | |
36 | using namespace std; | |
37 | ||
38 | namespace pv { | |
39 | ||
40 | // TODO: This should not be necessary | |
41 | SigSession* SigSession::_session = NULL; | |
42 | ||
43 | SigSession::SigSession(DeviceManager &device_manager) : | |
44 | _device_manager(device_manager), | |
45 | _sdi(NULL), | |
46 | _capture_state(Stopped) | |
47 | { | |
48 | // TODO: This should not be necessary | |
49 | _session = this; | |
50 | } | |
51 | ||
52 | SigSession::~SigSession() | |
53 | { | |
54 | stop_capture(); | |
55 | ||
56 | if (_sampling_thread.get()) | |
57 | _sampling_thread->join(); | |
58 | _sampling_thread.reset(); | |
59 | ||
60 | // TODO: This should not be necessary | |
61 | _session = NULL; | |
62 | } | |
63 | ||
64 | struct sr_dev_inst* SigSession::get_device() const | |
65 | { | |
66 | return _sdi; | |
67 | } | |
68 | ||
69 | void SigSession::set_device(struct sr_dev_inst *sdi) | |
70 | { | |
71 | if (_sdi) | |
72 | _device_manager.release_device(_sdi); | |
73 | if (sdi) | |
74 | _device_manager.use_device(sdi, this); | |
75 | _sdi = sdi; | |
76 | } | |
77 | ||
78 | void SigSession::release_device(struct sr_dev_inst *sdi) | |
79 | { | |
80 | (void)sdi; | |
81 | ||
82 | assert(_capture_state == Stopped); | |
83 | _sdi = NULL; | |
84 | } | |
85 | ||
86 | void SigSession::load_file(const string &name, | |
87 | function<void (const QString)> error_handler) | |
88 | { | |
89 | stop_capture(); | |
90 | _sampling_thread.reset(new boost::thread( | |
91 | &SigSession::load_thread_proc, this, name, | |
92 | error_handler)); | |
93 | } | |
94 | ||
95 | SigSession::capture_state SigSession::get_capture_state() const | |
96 | { | |
97 | lock_guard<mutex> lock(_sampling_mutex); | |
98 | return _capture_state; | |
99 | } | |
100 | ||
101 | void SigSession::start_capture(uint64_t record_length, | |
102 | function<void (const QString)> error_handler) | |
103 | { | |
104 | stop_capture(); | |
105 | ||
106 | // Check that a device instance has been selected. | |
107 | if (!_sdi) { | |
108 | qDebug() << "No device selected"; | |
109 | return; | |
110 | } | |
111 | ||
112 | // Check that at least one probe is enabled | |
113 | const GSList *l; | |
114 | for (l = _sdi->probes; l; l = l->next) { | |
115 | sr_probe *const probe = (sr_probe*)l->data; | |
116 | assert(probe); | |
117 | if (probe->enabled) | |
118 | break; | |
119 | } | |
120 | ||
121 | if (!l) { | |
122 | error_handler(tr("No probes enabled.")); | |
123 | return; | |
124 | } | |
125 | ||
126 | // Begin the session | |
127 | _sampling_thread.reset(new boost::thread( | |
128 | &SigSession::sample_thread_proc, this, _sdi, | |
129 | record_length, error_handler)); | |
130 | } | |
131 | ||
132 | void SigSession::stop_capture() | |
133 | { | |
134 | if (get_capture_state() == Stopped) | |
135 | return; | |
136 | ||
137 | sr_session_stop(); | |
138 | ||
139 | // Check that sampling stopped | |
140 | if (_sampling_thread.get()) | |
141 | _sampling_thread->join(); | |
142 | _sampling_thread.reset(); | |
143 | } | |
144 | ||
145 | vector< shared_ptr<view::Signal> > SigSession::get_signals() | |
146 | { | |
147 | lock_guard<mutex> lock(_signals_mutex); | |
148 | return _signals; | |
149 | } | |
150 | ||
151 | boost::shared_ptr<data::Logic> SigSession::get_data() | |
152 | { | |
153 | return _logic_data; | |
154 | } | |
155 | ||
156 | void SigSession::set_capture_state(capture_state state) | |
157 | { | |
158 | lock_guard<mutex> lock(_sampling_mutex); | |
159 | _capture_state = state; | |
160 | capture_state_changed(state); | |
161 | } | |
162 | ||
163 | void SigSession::load_thread_proc(const string name, | |
164 | function<void (const QString)> error_handler) | |
165 | { | |
166 | if (sr_session_load(name.c_str()) != SR_OK) { | |
167 | error_handler(tr("Failed to load file.")); | |
168 | return; | |
169 | } | |
170 | ||
171 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); | |
172 | ||
173 | if (sr_session_start() != SR_OK) { | |
174 | error_handler(tr("Failed to start session.")); | |
175 | return; | |
176 | } | |
177 | ||
178 | set_capture_state(Running); | |
179 | ||
180 | sr_session_run(); | |
181 | sr_session_destroy(); | |
182 | ||
183 | set_capture_state(Stopped); | |
184 | ||
185 | // Confirm that SR_DF_END was received | |
186 | assert(!_cur_logic_snapshot); | |
187 | assert(!_cur_analog_snapshot); | |
188 | } | |
189 | ||
190 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, | |
191 | uint64_t record_length, | |
192 | function<void (const QString)> error_handler) | |
193 | { | |
194 | assert(sdi); | |
195 | assert(error_handler); | |
196 | ||
197 | sr_session_new(); | |
198 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); | |
199 | ||
200 | if (sr_session_dev_add(sdi) != SR_OK) { | |
201 | error_handler(tr("Failed to use device.")); | |
202 | sr_session_destroy(); | |
203 | return; | |
204 | } | |
205 | ||
206 | // Set the sample limit | |
207 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
208 | g_variant_new_uint64(record_length)) != SR_OK) { | |
209 | error_handler(tr("Failed to configure " | |
210 | "time-based sample limit.")); | |
211 | sr_session_destroy(); | |
212 | return; | |
213 | } | |
214 | ||
215 | if (sr_session_start() != SR_OK) { | |
216 | error_handler(tr("Failed to start session.")); | |
217 | return; | |
218 | } | |
219 | ||
220 | set_capture_state(Running); | |
221 | ||
222 | sr_session_run(); | |
223 | sr_session_destroy(); | |
224 | ||
225 | set_capture_state(Stopped); | |
226 | ||
227 | // Confirm that SR_DF_END was received | |
228 | assert(!_cur_logic_snapshot); | |
229 | assert(!_cur_analog_snapshot); | |
230 | } | |
231 | ||
232 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
233 | { | |
234 | shared_ptr<view::Signal> signal; | |
235 | GVariant *gvar; | |
236 | uint64_t sample_rate = 0; | |
237 | unsigned int logic_probe_count = 0; | |
238 | unsigned int analog_probe_count = 0; | |
239 | ||
240 | // Detect what data types we will receive | |
241 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
242 | const sr_probe *const probe = (const sr_probe *)l->data; | |
243 | if (!probe->enabled) | |
244 | continue; | |
245 | ||
246 | switch(probe->type) { | |
247 | case SR_PROBE_LOGIC: | |
248 | logic_probe_count++; | |
249 | break; | |
250 | ||
251 | case SR_PROBE_ANALOG: | |
252 | analog_probe_count++; | |
253 | break; | |
254 | } | |
255 | } | |
256 | ||
257 | // Read out the sample rate | |
258 | assert(sdi->driver); | |
259 | ||
260 | const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, | |
261 | &gvar, sdi); | |
262 | if (ret != SR_OK) { | |
263 | qDebug("Failed to get samplerate\n"); | |
264 | return; | |
265 | } | |
266 | ||
267 | sample_rate = g_variant_get_uint64(gvar); | |
268 | g_variant_unref(gvar); | |
269 | ||
270 | // Create data containers for the coming data snapshots | |
271 | { | |
272 | lock_guard<mutex> data_lock(_data_mutex); | |
273 | ||
274 | if (logic_probe_count != 0) { | |
275 | _logic_data.reset(new data::Logic( | |
276 | logic_probe_count, sample_rate)); | |
277 | assert(_logic_data); | |
278 | } | |
279 | ||
280 | if (analog_probe_count != 0) { | |
281 | _analog_data.reset(new data::Analog(sample_rate)); | |
282 | assert(_analog_data); | |
283 | } | |
284 | } | |
285 | ||
286 | // Make the logic probe list | |
287 | { | |
288 | lock_guard<mutex> lock(_signals_mutex); | |
289 | ||
290 | _signals.clear(); | |
291 | ||
292 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
293 | const sr_probe *const probe = | |
294 | (const sr_probe *)l->data; | |
295 | assert(probe); | |
296 | if (!probe->enabled) | |
297 | continue; | |
298 | ||
299 | switch(probe->type) { | |
300 | case SR_PROBE_LOGIC: | |
301 | signal = shared_ptr<view::Signal>( | |
302 | new view::LogicSignal(probe->name, | |
303 | _logic_data, probe->index)); | |
304 | break; | |
305 | ||
306 | case SR_PROBE_ANALOG: | |
307 | signal = shared_ptr<view::Signal>( | |
308 | new view::AnalogSignal(probe->name, | |
309 | _analog_data, probe->index)); | |
310 | break; | |
311 | } | |
312 | ||
313 | _signals.push_back(signal); | |
314 | } | |
315 | ||
316 | signals_changed(); | |
317 | } | |
318 | } | |
319 | ||
320 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
321 | const sr_datafeed_meta &meta) | |
322 | { | |
323 | (void)sdi; | |
324 | ||
325 | for (const GSList *l = meta.config; l; l = l->next) { | |
326 | const sr_config *const src = (const sr_config*)l->data; | |
327 | switch (src->key) { | |
328 | case SR_CONF_SAMPLERATE: | |
329 | /// @todo handle samplerate changes | |
330 | /// samplerate = (uint64_t *)src->value; | |
331 | break; | |
332 | default: | |
333 | // Unknown metadata is not an error. | |
334 | break; | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) | |
340 | { | |
341 | lock_guard<mutex> lock(_data_mutex); | |
342 | ||
343 | if (!_logic_data) | |
344 | { | |
345 | qDebug() << "Unexpected logic packet"; | |
346 | return; | |
347 | } | |
348 | ||
349 | if (!_cur_logic_snapshot) | |
350 | { | |
351 | // Create a new data snapshot | |
352 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( | |
353 | new data::LogicSnapshot(logic)); | |
354 | _logic_data->push_snapshot(_cur_logic_snapshot); | |
355 | } | |
356 | else | |
357 | { | |
358 | // Append to the existing data snapshot | |
359 | _cur_logic_snapshot->append_payload(logic); | |
360 | } | |
361 | ||
362 | data_updated(); | |
363 | } | |
364 | ||
365 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) | |
366 | { | |
367 | lock_guard<mutex> lock(_data_mutex); | |
368 | ||
369 | if(!_analog_data) | |
370 | { | |
371 | qDebug() << "Unexpected analog packet"; | |
372 | return; // This analog packet was not expected. | |
373 | } | |
374 | ||
375 | if (!_cur_analog_snapshot) | |
376 | { | |
377 | // Create a new data snapshot | |
378 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( | |
379 | new data::AnalogSnapshot(analog)); | |
380 | _analog_data->push_snapshot(_cur_analog_snapshot); | |
381 | } | |
382 | else | |
383 | { | |
384 | // Append to the existing data snapshot | |
385 | _cur_analog_snapshot->append_payload(analog); | |
386 | } | |
387 | ||
388 | data_updated(); | |
389 | } | |
390 | ||
391 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, | |
392 | const struct sr_datafeed_packet *packet) | |
393 | { | |
394 | assert(sdi); | |
395 | assert(packet); | |
396 | ||
397 | switch (packet->type) { | |
398 | case SR_DF_HEADER: | |
399 | feed_in_header(sdi); | |
400 | break; | |
401 | ||
402 | case SR_DF_META: | |
403 | assert(packet->payload); | |
404 | feed_in_meta(sdi, | |
405 | *(const sr_datafeed_meta*)packet->payload); | |
406 | break; | |
407 | ||
408 | case SR_DF_LOGIC: | |
409 | assert(packet->payload); | |
410 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); | |
411 | break; | |
412 | ||
413 | case SR_DF_ANALOG: | |
414 | assert(packet->payload); | |
415 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
416 | break; | |
417 | ||
418 | case SR_DF_END: | |
419 | { | |
420 | { | |
421 | lock_guard<mutex> lock(_data_mutex); | |
422 | _cur_logic_snapshot.reset(); | |
423 | _cur_analog_snapshot.reset(); | |
424 | } | |
425 | data_updated(); | |
426 | break; | |
427 | } | |
428 | } | |
429 | } | |
430 | ||
431 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, | |
432 | const struct sr_datafeed_packet *packet, void *cb_data) | |
433 | { | |
434 | (void) cb_data; | |
435 | assert(_session); | |
436 | _session->data_feed_in(sdi, packet); | |
437 | } | |
438 | ||
439 | } // namespace pv |