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