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