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