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