]>
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(); | |
d5bf2c6c | 151 | sr_session_destroy(); |
8a67bd9e JH |
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); |
174e27a1 JH |
224 | if (ret != SR_OK) { |
225 | qDebug("Failed to get samplerate\n"); | |
226 | return; | |
227 | } | |
228 | ||
8eb9d311 BV |
229 | sample_rate = g_variant_get_uint64(gvar); |
230 | g_variant_unref(gvar); | |
7297c76e | 231 | |
be73bdfa | 232 | // Create data containers for the coming data snapshots |
3868e5fa | 233 | { |
aba1dd16 | 234 | lock_guard<mutex> data_lock(_data_mutex); |
3868e5fa | 235 | |
be73bdfa JH |
236 | if (logic_probe_count != 0) { |
237 | _logic_data.reset(new data::Logic( | |
8eb9d311 | 238 | logic_probe_count, sample_rate)); |
be73bdfa JH |
239 | assert(_logic_data); |
240 | } | |
241 | ||
242 | if (analog_probe_count != 0) { | |
8eb9d311 | 243 | _analog_data.reset(new data::Analog(sample_rate)); |
be73bdfa JH |
244 | assert(_analog_data); |
245 | } | |
3868e5fa JH |
246 | } |
247 | ||
be73bdfa | 248 | // Make the logic probe list |
3868e5fa JH |
249 | { |
250 | lock_guard<mutex> lock(_signals_mutex); | |
2e2946fe | 251 | |
be73bdfa JH |
252 | _signals.clear(); |
253 | ||
254 | for (const GSList *l = sdi->probes; l; l = l->next) { | |
2e2946fe | 255 | const sr_probe *const probe = |
be73bdfa JH |
256 | (const sr_probe *)l->data; |
257 | assert(probe); | |
258 | if (!probe->enabled) | |
259 | continue; | |
260 | ||
261 | switch(probe->type) { | |
262 | case SR_PROBE_LOGIC: | |
263 | signal = shared_ptr<view::Signal>( | |
264 | new view::LogicSignal(probe->name, | |
b0e1d01d | 265 | _logic_data, probe->index)); |
be73bdfa JH |
266 | break; |
267 | ||
268 | case SR_PROBE_ANALOG: | |
269 | signal = shared_ptr<view::Signal>( | |
270 | new view::AnalogSignal(probe->name, | |
568b90d4 | 271 | _analog_data, probe->index)); |
be73bdfa | 272 | break; |
e3f65ace | 273 | } |
be73bdfa JH |
274 | |
275 | _signals.push_back(signal); | |
2953961c | 276 | } |
28a4c9c5 | 277 | |
69dd2b03 | 278 | signals_changed(); |
2e2946fe | 279 | } |
be73bdfa JH |
280 | } |
281 | ||
282 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
283 | const sr_datafeed_meta &meta) | |
284 | { | |
94fe58ef UH |
285 | (void)sdi; |
286 | ||
be73bdfa JH |
287 | for (const GSList *l = meta.config; l; l = l->next) { |
288 | const sr_config *const src = (const sr_config*)l->data; | |
289 | switch (src->key) { | |
290 | case SR_CONF_SAMPLERATE: | |
291 | /// @todo handle samplerate changes | |
292 | /// samplerate = (uint64_t *)src->value; | |
293 | break; | |
294 | default: | |
295 | // Unknown metadata is not an error. | |
296 | break; | |
297 | } | |
aba1dd16 JH |
298 | } |
299 | } | |
300 | ||
9c112671 JH |
301 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
302 | { | |
303 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
304 | |
305 | if (!_logic_data) | |
9c112671 | 306 | { |
79efbc53 JH |
307 | qDebug() << "Unexpected logic packet"; |
308 | return; | |
309 | } | |
be73bdfa | 310 | |
79efbc53 JH |
311 | if (!_cur_logic_snapshot) |
312 | { | |
9c112671 | 313 | // Create a new data snapshot |
1b1ec774 JH |
314 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
315 | new data::LogicSnapshot(logic)); | |
9c112671 JH |
316 | _logic_data->push_snapshot(_cur_logic_snapshot); |
317 | } | |
318 | else | |
319 | { | |
320 | // Append to the existing data snapshot | |
321 | _cur_logic_snapshot->append_payload(logic); | |
322 | } | |
323 | ||
324 | data_updated(); | |
325 | } | |
326 | ||
aba1dd16 JH |
327 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
328 | { | |
329 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
330 | |
331 | if(!_analog_data) | |
aba1dd16 | 332 | { |
79efbc53 JH |
333 | qDebug() << "Unexpected analog packet"; |
334 | return; // This analog packet was not expected. | |
335 | } | |
be73bdfa | 336 | |
79efbc53 JH |
337 | if (!_cur_analog_snapshot) |
338 | { | |
aba1dd16 | 339 | // Create a new data snapshot |
1b1ec774 JH |
340 | _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>( |
341 | new data::AnalogSnapshot(analog)); | |
aba1dd16 JH |
342 | _analog_data->push_snapshot(_cur_analog_snapshot); |
343 | } | |
344 | else | |
345 | { | |
346 | // Append to the existing data snapshot | |
347 | _cur_analog_snapshot->append_payload(analog); | |
348 | } | |
349 | ||
350 | data_updated(); | |
351 | } | |
9c112671 | 352 | |
b0e1d01d JH |
353 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
354 | const struct sr_datafeed_packet *packet) | |
355 | { | |
356 | assert(sdi); | |
357 | assert(packet); | |
358 | ||
359 | switch (packet->type) { | |
360 | case SR_DF_HEADER: | |
eec446e1 | 361 | feed_in_header(sdi); |
b0e1d01d JH |
362 | break; |
363 | ||
be73bdfa | 364 | case SR_DF_META: |
aba1dd16 | 365 | assert(packet->payload); |
be73bdfa JH |
366 | feed_in_meta(sdi, |
367 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
368 | break; |
369 | ||
2e2946fe | 370 | case SR_DF_LOGIC: |
28a4c9c5 | 371 | assert(packet->payload); |
9c112671 | 372 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
373 | break; |
374 | ||
aba1dd16 JH |
375 | case SR_DF_ANALOG: |
376 | assert(packet->payload); | |
377 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
378 | break; | |
379 | ||
2953961c | 380 | case SR_DF_END: |
2e2946fe JH |
381 | { |
382 | { | |
383 | lock_guard<mutex> lock(_data_mutex); | |
384 | _cur_logic_snapshot.reset(); | |
aba1dd16 | 385 | _cur_analog_snapshot.reset(); |
2e2946fe | 386 | } |
04abfae9 | 387 | data_updated(); |
2953961c JH |
388 | break; |
389 | } | |
2e2946fe | 390 | } |
2953961c JH |
391 | } |
392 | ||
04abfae9 | 393 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 394 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 395 | { |
5045f16d | 396 | (void) cb_data; |
04abfae9 JH |
397 | assert(_session); |
398 | _session->data_feed_in(sdi, packet); | |
2953961c | 399 | } |
51e77110 JH |
400 | |
401 | } // namespace pv |