]>
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 | |
2953961c JH |
30 | #include <assert.h> |
31 | ||
28a4c9c5 | 32 | using namespace boost; |
e3f65ace | 33 | using namespace std; |
28a4c9c5 | 34 | |
51e77110 JH |
35 | namespace pv { |
36 | ||
2953961c | 37 | // TODO: This should not be necessary |
04abfae9 | 38 | SigSession* SigSession::_session = NULL; |
2953961c | 39 | |
5b7cf66c JH |
40 | SigSession::SigSession() : |
41 | _capture_state(Stopped) | |
2953961c JH |
42 | { |
43 | // TODO: This should not be necessary | |
04abfae9 | 44 | _session = this; |
2953961c JH |
45 | } |
46 | ||
47 | SigSession::~SigSession() | |
48 | { | |
5b7cf66c JH |
49 | stop_capture(); |
50 | ||
333d5bbc | 51 | if (_sampling_thread.get()) |
2e2946fe JH |
52 | _sampling_thread->join(); |
53 | _sampling_thread.reset(); | |
54 | ||
2953961c | 55 | // TODO: This should not be necessary |
04abfae9 | 56 | _session = NULL; |
2953961c JH |
57 | } |
58 | ||
f2edb557 JH |
59 | void SigSession::load_file(const string &name, |
60 | function<void (const QString)> error_handler) | |
2953961c | 61 | { |
8a67bd9e JH |
62 | stop_capture(); |
63 | _sampling_thread.reset(new boost::thread( | |
f2edb557 JH |
64 | &SigSession::load_thread_proc, this, name, |
65 | error_handler)); | |
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, |
f2edb557 JH |
75 | uint64_t record_length, |
76 | function<void (const QString)> error_handler) | |
2e2946fe | 77 | { |
5b7cf66c JH |
78 | stop_capture(); |
79 | ||
2e2946fe JH |
80 | _sampling_thread.reset(new boost::thread( |
81 | &SigSession::sample_thread_proc, this, sdi, | |
f2edb557 | 82 | record_length, error_handler)); |
2e2946fe JH |
83 | } |
84 | ||
5b7cf66c JH |
85 | void SigSession::stop_capture() |
86 | { | |
333d5bbc | 87 | if (get_capture_state() == Stopped) |
5b7cf66c JH |
88 | return; |
89 | ||
90 | sr_session_stop(); | |
91 | ||
92 | // Check that sampling stopped | |
333d5bbc | 93 | if (_sampling_thread.get()) |
5b7cf66c JH |
94 | _sampling_thread->join(); |
95 | _sampling_thread.reset(); | |
5b7cf66c JH |
96 | } |
97 | ||
3868e5fa | 98 | vector< shared_ptr<view::Signal> > SigSession::get_signals() |
2e2946fe | 99 | { |
3868e5fa | 100 | lock_guard<mutex> lock(_signals_mutex); |
2e2946fe JH |
101 | return _signals; |
102 | } | |
103 | ||
1b1ec774 | 104 | boost::shared_ptr<data::Logic> SigSession::get_data() |
2e2946fe JH |
105 | { |
106 | return _logic_data; | |
107 | } | |
108 | ||
6ac96c2e JH |
109 | void SigSession::set_capture_state(capture_state state) |
110 | { | |
949f8050 | 111 | lock_guard<mutex> lock(_sampling_mutex); |
6ac96c2e JH |
112 | _capture_state = state; |
113 | capture_state_changed(state); | |
114 | } | |
115 | ||
f2edb557 JH |
116 | void SigSession::load_thread_proc(const string name, |
117 | function<void (const QString)> error_handler) | |
8a67bd9e JH |
118 | { |
119 | if (sr_session_load(name.c_str()) != SR_OK) { | |
f2edb557 | 120 | error_handler(tr("Failed to load file.")); |
8a67bd9e JH |
121 | return; |
122 | } | |
123 | ||
124 | sr_session_datafeed_callback_add(data_feed_in_proc); | |
125 | ||
126 | if (sr_session_start() != SR_OK) { | |
f2edb557 | 127 | error_handler(tr("Failed to start session.")); |
8a67bd9e JH |
128 | return; |
129 | } | |
130 | ||
131 | set_capture_state(Running); | |
132 | ||
133 | sr_session_run(); | |
134 | sr_session_stop(); | |
135 | ||
136 | set_capture_state(Stopped); | |
137 | } | |
138 | ||
2e2946fe | 139 | void SigSession::sample_thread_proc(struct sr_dev_inst *sdi, |
f2edb557 JH |
140 | uint64_t record_length, |
141 | function<void (const QString)> error_handler) | |
274d4f13 | 142 | { |
be73bdfa | 143 | assert(sdi); |
f2edb557 | 144 | assert(error_handler); |
be73bdfa | 145 | |
274d4f13 | 146 | sr_session_new(); |
04abfae9 | 147 | sr_session_datafeed_callback_add(data_feed_in_proc); |
274d4f13 JH |
148 | |
149 | if (sr_session_dev_add(sdi) != SR_OK) { | |
f2edb557 | 150 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
151 | sr_session_destroy(); |
152 | return; | |
153 | } | |
154 | ||
be73bdfa JH |
155 | // Set the sample limit |
156 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, | |
215f9499 | 157 | &record_length) != SR_OK) { |
f2edb557 JH |
158 | error_handler(tr("Failed to configure " |
159 | "time-based sample limit.")); | |
274d4f13 JH |
160 | sr_session_destroy(); |
161 | return; | |
162 | } | |
163 | ||
eec446e1 | 164 | if (sr_session_start() != SR_OK) { |
f2edb557 | 165 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
166 | return; |
167 | } | |
168 | ||
169 | set_capture_state(Running); | |
170 | ||
171 | sr_session_run(); | |
172 | sr_session_destroy(); | |
173 | ||
174 | set_capture_state(Stopped); | |
175 | } | |
176 | ||
177 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
178 | { | |
179 | shared_ptr<view::Signal> signal; | |
b85f2554 | 180 | uint64_t *sample_rate = NULL; |
eec446e1 JH |
181 | unsigned int logic_probe_count = 0; |
182 | unsigned int analog_probe_count = 0; | |
183 | ||
be73bdfa JH |
184 | // Detect what data types we will receive |
185 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
186 | const sr_probe *const probe = (const sr_probe *)l->data; | |
187 | if (!probe->enabled) | |
188 | continue; | |
274d4f13 | 189 | |
be73bdfa JH |
190 | switch(probe->type) { |
191 | case SR_PROBE_LOGIC: | |
192 | logic_probe_count++; | |
193 | break; | |
5b7cf66c | 194 | |
be73bdfa JH |
195 | case SR_PROBE_ANALOG: |
196 | analog_probe_count++; | |
197 | break; | |
198 | } | |
199 | } | |
8d634081 | 200 | |
7297c76e JH |
201 | // Read out the sample rate |
202 | assert(sdi->driver); | |
c0d6d479 JH |
203 | |
204 | const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, | |
205 | (const void**)&sample_rate, sdi); | |
206 | assert(ret == SR_OK); | |
7297c76e | 207 | |
be73bdfa | 208 | // Create data containers for the coming data snapshots |
3868e5fa | 209 | { |
aba1dd16 | 210 | lock_guard<mutex> data_lock(_data_mutex); |
3868e5fa | 211 | |
be73bdfa JH |
212 | if (logic_probe_count != 0) { |
213 | _logic_data.reset(new data::Logic( | |
7297c76e | 214 | logic_probe_count, *sample_rate)); |
be73bdfa JH |
215 | assert(_logic_data); |
216 | } | |
217 | ||
218 | if (analog_probe_count != 0) { | |
7297c76e | 219 | _analog_data.reset(new data::Analog(*sample_rate)); |
be73bdfa JH |
220 | assert(_analog_data); |
221 | } | |
3868e5fa JH |
222 | } |
223 | ||
be73bdfa | 224 | // Make the logic probe list |
3868e5fa JH |
225 | { |
226 | lock_guard<mutex> lock(_signals_mutex); | |
2e2946fe | 227 | |
be73bdfa JH |
228 | _signals.clear(); |
229 | ||
230 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
2e2946fe | 231 | const sr_probe *const probe = |
be73bdfa JH |
232 | (const sr_probe *)l->data; |
233 | assert(probe); | |
234 | if (!probe->enabled) | |
235 | continue; | |
236 | ||
237 | switch(probe->type) { | |
238 | case SR_PROBE_LOGIC: | |
239 | signal = shared_ptr<view::Signal>( | |
240 | new view::LogicSignal(probe->name, | |
b0e1d01d | 241 | _logic_data, probe->index)); |
be73bdfa JH |
242 | break; |
243 | ||
244 | case SR_PROBE_ANALOG: | |
245 | signal = shared_ptr<view::Signal>( | |
246 | new view::AnalogSignal(probe->name, | |
247 | _analog_data)); | |
248 | break; | |
e3f65ace | 249 | } |
be73bdfa JH |
250 | |
251 | _signals.push_back(signal); | |
2953961c | 252 | } |
28a4c9c5 | 253 | |
69dd2b03 | 254 | signals_changed(); |
2e2946fe | 255 | } |
be73bdfa JH |
256 | } |
257 | ||
258 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
259 | const sr_datafeed_meta &meta) | |
260 | { | |
94fe58ef UH |
261 | (void)sdi; |
262 | ||
be73bdfa JH |
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 |