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