]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Renamed pv::SigSession::_state_mutex to pv::SigSession::_sampling_mutex
[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
28a4c9c5
JH
23#include "logicdata.h"
24#include "logicdatasnapshot.h"
8d634081 25#include "view/logicsignal.h"
28a4c9c5 26
009e1503
JH
27#include <QDebug>
28
2953961c
JH
29#include <assert.h>
30
28a4c9c5 31using namespace boost;
e3f65ace 32using namespace std;
28a4c9c5 33
51e77110
JH
34namespace pv {
35
2953961c 36// TODO: This should not be necessary
04abfae9 37SigSession* SigSession::_session = NULL;
2953961c 38
5b7cf66c
JH
39SigSession::SigSession() :
40 _capture_state(Stopped)
2953961c
JH
41{
42 // TODO: This should not be necessary
04abfae9 43 _session = this;
2953961c
JH
44}
45
46SigSession::~SigSession()
47{
5b7cf66c
JH
48 stop_capture();
49
333d5bbc 50 if (_sampling_thread.get())
2e2946fe
JH
51 _sampling_thread->join();
52 _sampling_thread.reset();
53
2953961c 54 // TODO: This should not be necessary
04abfae9 55 _session = NULL;
2953961c
JH
56}
57
8a67bd9e 58void SigSession::load_file(const string &name)
2953961c 59{
8a67bd9e
JH
60 stop_capture();
61 _sampling_thread.reset(new boost::thread(
62 &SigSession::load_thread_proc, this, name));
2953961c
JH
63}
64
5b7cf66c
JH
65SigSession::capture_state SigSession::get_capture_state() const
66{
949f8050 67 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
68 return _capture_state;
69}
70
274d4f13 71void SigSession::start_capture(struct sr_dev_inst *sdi,
215f9499 72 uint64_t record_length, uint64_t sample_rate)
2e2946fe 73{
5b7cf66c
JH
74 stop_capture();
75
2e2946fe
JH
76
77 _sampling_thread.reset(new boost::thread(
78 &SigSession::sample_thread_proc, this, sdi,
79 record_length, sample_rate));
80}
81
5b7cf66c
JH
82void SigSession::stop_capture()
83{
333d5bbc 84 if (get_capture_state() == Stopped)
5b7cf66c
JH
85 return;
86
87 sr_session_stop();
88
89 // Check that sampling stopped
333d5bbc 90 if (_sampling_thread.get())
5b7cf66c
JH
91 _sampling_thread->join();
92 _sampling_thread.reset();
5b7cf66c
JH
93}
94
3868e5fa 95vector< shared_ptr<view::Signal> > SigSession::get_signals()
2e2946fe 96{
3868e5fa 97 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
98 return _signals;
99}
100
101boost::shared_ptr<LogicData> SigSession::get_data()
102{
103 return _logic_data;
104}
105
6ac96c2e
JH
106void SigSession::set_capture_state(capture_state state)
107{
949f8050 108 lock_guard<mutex> lock(_sampling_mutex);
6ac96c2e
JH
109 _capture_state = state;
110 capture_state_changed(state);
111}
112
8a67bd9e
JH
113void SigSession::load_thread_proc(const string name)
114{
115 if (sr_session_load(name.c_str()) != SR_OK) {
116 qDebug() << "Failed to load file.";
117 return;
118 }
119
120 sr_session_datafeed_callback_add(data_feed_in_proc);
121
122 if (sr_session_start() != SR_OK) {
123 qDebug() << "Failed to start session.";
124 return;
125 }
126
127 set_capture_state(Running);
128
129 sr_session_run();
130 sr_session_stop();
131
132 set_capture_state(Stopped);
133}
134
2e2946fe
JH
135void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
136 uint64_t record_length, uint64_t sample_rate)
274d4f13
JH
137{
138 sr_session_new();
04abfae9 139 sr_session_datafeed_callback_add(data_feed_in_proc);
274d4f13
JH
140
141 if (sr_session_dev_add(sdi) != SR_OK) {
142 qDebug() << "Failed to use device.";
143 sr_session_destroy();
144 return;
145 }
146
274d4f13 147 if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
215f9499 148 &record_length) != SR_OK) {
274d4f13
JH
149 qDebug() << "Failed to configure time-based sample limit.";
150 sr_session_destroy();
151 return;
152 }
153
154 if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
155 &sample_rate) != SR_OK) {
156 qDebug() << "Failed to configure samplerate.";
157 sr_session_destroy();
158 return;
159 }
160
161 if (sr_session_start() != SR_OK) {
162 qDebug() << "Failed to start session.";
163 return;
164 }
165
6ac96c2e 166 set_capture_state(Running);
5b7cf66c 167
274d4f13
JH
168 sr_session_run();
169 sr_session_destroy();
6ac96c2e
JH
170
171 set_capture_state(Stopped);
274d4f13
JH
172}
173
b0e1d01d
JH
174void SigSession::feed_in_meta_logic(const struct sr_dev_inst *sdi,
175 const sr_datafeed_meta_logic &meta_logic)
2953961c 176{
8d634081
JH
177 using view::LogicSignal;
178
3868e5fa
JH
179 {
180 lock_guard<mutex> lock(_data_mutex);
181
b0e1d01d 182 // Create an empty LogicData for coming data snapshots
2e2946fe
JH
183 _logic_data.reset(new LogicData(meta_logic));
184 assert(_logic_data);
333d5bbc 185 if (!_logic_data)
b0e1d01d 186 return;
3868e5fa
JH
187 }
188
189 {
190 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
191
192 // Add the signals
b0e1d01d 193 for (int i = 0; i < meta_logic.num_probes; i++) {
2e2946fe
JH
194 const sr_probe *const probe =
195 (const sr_probe*)g_slist_nth_data(
196 sdi->probes, i);
b0e1d01d 197 if (probe->enabled) {
2e2946fe
JH
198 shared_ptr<LogicSignal> signal(
199 new LogicSignal(probe->name,
b0e1d01d 200 _logic_data, probe->index));
2e2946fe 201 _signals.push_back(signal);
e3f65ace 202 }
2953961c 203 }
28a4c9c5 204
69dd2b03 205 signals_changed();
2e2946fe 206 }
b0e1d01d
JH
207}
208
9c112671
JH
209void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
210{
211 lock_guard<mutex> lock(_data_mutex);
212 if (!_cur_logic_snapshot)
213 {
214 // Create a new data snapshot
215 _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
216 new LogicDataSnapshot(logic));
217 _logic_data->push_snapshot(_cur_logic_snapshot);
218 }
219 else
220 {
221 // Append to the existing data snapshot
222 _cur_logic_snapshot->append_payload(logic);
223 }
224
225 data_updated();
226}
227
228
b0e1d01d
JH
229void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
230 const struct sr_datafeed_packet *packet)
231{
232 assert(sdi);
233 assert(packet);
234
235 switch (packet->type) {
236 case SR_DF_HEADER:
237 {
238 lock_guard<mutex> lock(_signals_mutex);
239 _signals.clear();
240 break;
3868e5fa 241 }
f556bc6a 242
b0e1d01d
JH
243 case SR_DF_META_LOGIC:
244 assert(packet->payload);
245 feed_in_meta_logic(sdi,
246 *(const sr_datafeed_meta_logic*)packet->payload);
247 break;
248
2e2946fe 249 case SR_DF_LOGIC:
28a4c9c5 250 assert(packet->payload);
9c112671 251 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
252 break;
253
254 case SR_DF_END:
2e2946fe
JH
255 {
256 {
257 lock_guard<mutex> lock(_data_mutex);
258 _cur_logic_snapshot.reset();
259 }
04abfae9 260 data_updated();
2953961c
JH
261 break;
262 }
2e2946fe 263 }
2953961c
JH
264}
265
04abfae9 266void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
bc5c1a99 267 const struct sr_datafeed_packet *packet)
2953961c 268{
04abfae9
JH
269 assert(_session);
270 _session->data_feed_in(sdi, packet);
2953961c 271}
51e77110
JH
272
273} // namespace pv