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