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