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