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