]>
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 JH |
142 | { |
143 | sr_session_new(); | |
04abfae9 | 144 | sr_session_datafeed_callback_add(data_feed_in_proc); |
274d4f13 JH |
145 | |
146 | if (sr_session_dev_add(sdi) != SR_OK) { | |
147 | qDebug() << "Failed to use device."; | |
148 | sr_session_destroy(); | |
149 | return; | |
150 | } | |
151 | ||
274d4f13 | 152 | if (sr_dev_config_set(sdi, SR_HWCAP_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 | ||
aba1dd16 JH |
159 | { |
160 | lock_guard<mutex> lock(_sampling_mutex); | |
161 | if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE, | |
162 | &_sample_rate) != SR_OK) { | |
163 | qDebug() << "Failed to configure samplerate."; | |
164 | sr_session_destroy(); | |
165 | return; | |
166 | } | |
274d4f13 JH |
167 | } |
168 | ||
169 | if (sr_session_start() != SR_OK) { | |
170 | qDebug() << "Failed to start session."; | |
171 | return; | |
172 | } | |
173 | ||
6ac96c2e | 174 | set_capture_state(Running); |
5b7cf66c | 175 | |
274d4f13 JH |
176 | sr_session_run(); |
177 | sr_session_destroy(); | |
6ac96c2e JH |
178 | |
179 | set_capture_state(Stopped); | |
274d4f13 JH |
180 | } |
181 | ||
b0e1d01d JH |
182 | void SigSession::feed_in_meta_logic(const struct sr_dev_inst *sdi, |
183 | const sr_datafeed_meta_logic &meta_logic) | |
2953961c | 184 | { |
8d634081 JH |
185 | using view::LogicSignal; |
186 | ||
3868e5fa | 187 | { |
aba1dd16 JH |
188 | lock_guard<mutex> data_lock(_data_mutex); |
189 | lock_guard<mutex> sampling_lock(_sampling_mutex); | |
3868e5fa | 190 | |
1b1ec774 JH |
191 | // Create an empty data::Logic for coming data snapshots |
192 | _logic_data.reset(new data::Logic(meta_logic, _sample_rate)); | |
2e2946fe | 193 | assert(_logic_data); |
333d5bbc | 194 | if (!_logic_data) |
b0e1d01d | 195 | return; |
3868e5fa JH |
196 | } |
197 | ||
198 | { | |
199 | lock_guard<mutex> lock(_signals_mutex); | |
2e2946fe JH |
200 | |
201 | // Add the signals | |
b0e1d01d | 202 | for (int i = 0; i < meta_logic.num_probes; i++) { |
2e2946fe JH |
203 | const sr_probe *const probe = |
204 | (const sr_probe*)g_slist_nth_data( | |
205 | sdi->probes, i); | |
b0e1d01d | 206 | if (probe->enabled) { |
2e2946fe JH |
207 | shared_ptr<LogicSignal> signal( |
208 | new LogicSignal(probe->name, | |
b0e1d01d | 209 | _logic_data, probe->index)); |
2e2946fe | 210 | _signals.push_back(signal); |
e3f65ace | 211 | } |
2953961c | 212 | } |
28a4c9c5 | 213 | |
69dd2b03 | 214 | signals_changed(); |
2e2946fe | 215 | } |
b0e1d01d JH |
216 | } |
217 | ||
e314eca4 | 218 | void SigSession::feed_in_meta_analog(const struct sr_dev_inst*, |
aba1dd16 JH |
219 | const sr_datafeed_meta_analog &meta_analog) |
220 | { | |
221 | using view::AnalogSignal; | |
222 | ||
223 | { | |
224 | lock_guard<mutex> data_lock(_data_mutex); | |
225 | lock_guard<mutex> sampling_lock(_sampling_mutex); | |
226 | ||
1b1ec774 JH |
227 | // Create an empty data::Analog for coming data snapshots |
228 | _analog_data.reset(new data::Analog( | |
aba1dd16 JH |
229 | meta_analog, _sample_rate)); |
230 | assert(_analog_data); | |
231 | if (!_analog_data) | |
232 | return; | |
233 | } | |
234 | ||
235 | { | |
236 | lock_guard<mutex> lock(_signals_mutex); | |
237 | ||
238 | // Add the signals | |
239 | shared_ptr<AnalogSignal> signal( | |
240 | new AnalogSignal(QString("???"), _analog_data)); | |
241 | _signals.push_back(signal); | |
242 | ||
243 | signals_changed(); | |
244 | } | |
245 | } | |
246 | ||
9c112671 JH |
247 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
248 | { | |
249 | lock_guard<mutex> lock(_data_mutex); | |
250 | if (!_cur_logic_snapshot) | |
251 | { | |
252 | // Create a new data snapshot | |
1b1ec774 JH |
253 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
254 | new data::LogicSnapshot(logic)); | |
9c112671 JH |
255 | _logic_data->push_snapshot(_cur_logic_snapshot); |
256 | } | |
257 | else | |
258 | { | |
259 | // Append to the existing data snapshot | |
260 | _cur_logic_snapshot->append_payload(logic); | |
261 | } | |
262 | ||
263 | data_updated(); | |
264 | } | |
265 | ||
aba1dd16 JH |
266 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
267 | { | |
268 | lock_guard<mutex> lock(_data_mutex); | |
269 | if (!_cur_analog_snapshot) | |
270 | { | |
271 | // Create a new data snapshot | |
1b1ec774 JH |
272 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( |
273 | new data::AnalogSnapshot(analog)); | |
aba1dd16 JH |
274 | _analog_data->push_snapshot(_cur_analog_snapshot); |
275 | } | |
276 | else | |
277 | { | |
278 | // Append to the existing data snapshot | |
279 | _cur_analog_snapshot->append_payload(analog); | |
280 | } | |
281 | ||
282 | data_updated(); | |
283 | } | |
9c112671 | 284 | |
b0e1d01d JH |
285 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
286 | const struct sr_datafeed_packet *packet) | |
287 | { | |
288 | assert(sdi); | |
289 | assert(packet); | |
290 | ||
291 | switch (packet->type) { | |
292 | case SR_DF_HEADER: | |
293 | { | |
294 | lock_guard<mutex> lock(_signals_mutex); | |
295 | _signals.clear(); | |
296 | break; | |
3868e5fa | 297 | } |
f556bc6a | 298 | |
b0e1d01d JH |
299 | case SR_DF_META_LOGIC: |
300 | assert(packet->payload); | |
301 | feed_in_meta_logic(sdi, | |
302 | *(const sr_datafeed_meta_logic*)packet->payload); | |
303 | break; | |
304 | ||
aba1dd16 JH |
305 | case SR_DF_META_ANALOG: |
306 | assert(packet->payload); | |
307 | feed_in_meta_analog(sdi, | |
308 | *(const sr_datafeed_meta_analog*)packet->payload); | |
309 | break; | |
310 | ||
2e2946fe | 311 | case SR_DF_LOGIC: |
28a4c9c5 | 312 | assert(packet->payload); |
9c112671 | 313 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
314 | break; |
315 | ||
aba1dd16 JH |
316 | case SR_DF_ANALOG: |
317 | assert(packet->payload); | |
318 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
319 | break; | |
320 | ||
2953961c | 321 | case SR_DF_END: |
2e2946fe JH |
322 | { |
323 | { | |
324 | lock_guard<mutex> lock(_data_mutex); | |
325 | _cur_logic_snapshot.reset(); | |
aba1dd16 | 326 | _cur_analog_snapshot.reset(); |
2e2946fe | 327 | } |
04abfae9 | 328 | data_updated(); |
2953961c JH |
329 | break; |
330 | } | |
2e2946fe | 331 | } |
2953961c JH |
332 | } |
333 | ||
04abfae9 | 334 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
bc5c1a99 | 335 | const struct sr_datafeed_packet *packet) |
2953961c | 336 | { |
04abfae9 JH |
337 | assert(_session); |
338 | _session->data_feed_in(sdi, packet); | |
2953961c | 339 | } |
51e77110 JH |
340 | |
341 | } // namespace pv |