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