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