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