2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "sigsession.h"
23 #include "logicdata.h"
24 #include "logicdatasnapshot.h"
25 #include "view/logicsignal.h"
31 using namespace boost;
36 // TODO: This should not be necessary
37 SigSession* SigSession::_session = NULL;
39 SigSession::SigSession() :
40 _capture_state(Stopped)
42 // TODO: This should not be necessary
46 SigSession::~SigSession()
50 if(_sampling_thread.get())
51 _sampling_thread->join();
52 _sampling_thread.reset();
54 // TODO: This should not be necessary
58 void SigSession::load_file(const std::string &name)
60 if (sr_session_load(name.c_str()) == SR_OK) {
61 /* sigrok session file */
62 sr_session_datafeed_callback_add(data_feed_in_proc);
69 SigSession::capture_state SigSession::get_capture_state() const
71 lock_guard<mutex> lock(_state_mutex);
72 return _capture_state;
75 void SigSession::start_capture(struct sr_dev_inst *sdi,
76 uint64_t record_length, uint64_t sample_rate)
81 _sampling_thread.reset(new boost::thread(
82 &SigSession::sample_thread_proc, this, sdi,
83 record_length, sample_rate));
86 void SigSession::stop_capture()
88 if(get_capture_state() == Stopped)
93 // Check that sampling stopped
94 if(_sampling_thread.get())
95 _sampling_thread->join();
96 _sampling_thread.reset();
99 vector< shared_ptr<view::Signal> > SigSession::get_signals()
101 lock_guard<mutex> lock(_signals_mutex);
105 boost::shared_ptr<LogicData> SigSession::get_data()
110 void SigSession::set_capture_state(capture_state state)
112 lock_guard<mutex> lock(_state_mutex);
113 _capture_state = state;
114 capture_state_changed(state);
117 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
118 uint64_t record_length, uint64_t sample_rate)
121 sr_session_datafeed_callback_add(data_feed_in_proc);
123 if (sr_session_dev_add(sdi) != SR_OK) {
124 qDebug() << "Failed to use device.";
125 sr_session_destroy();
129 if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
130 &record_length) != SR_OK) {
131 qDebug() << "Failed to configure time-based sample limit.";
132 sr_session_destroy();
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();
143 if (sr_session_start() != SR_OK) {
144 qDebug() << "Failed to start session.";
148 set_capture_state(Running);
151 sr_session_destroy();
153 set_capture_state(Stopped);
156 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
157 struct sr_datafeed_packet *packet)
159 using view::LogicSignal;
164 switch (packet->type) {
167 lock_guard<mutex> lock(_signals_mutex);
172 case SR_DF_META_LOGIC:
174 assert(packet->payload);
175 const sr_datafeed_meta_logic &meta_logic =
176 *(sr_datafeed_meta_logic*)packet->payload;
179 lock_guard<mutex> lock(_data_mutex);
181 // Create an empty LogiData for coming data snapshots
182 _logic_data.reset(new LogicData(meta_logic));
189 lock_guard<mutex> lock(_signals_mutex);
192 for (int i = 0; i < meta_logic.num_probes; i++)
194 const sr_probe *const probe =
195 (const sr_probe*)g_slist_nth_data(
199 shared_ptr<LogicSignal> signal(
200 new LogicSignal(probe->name,
203 _signals.push_back(signal);
214 lock_guard<mutex> lock(_data_mutex);
215 assert(packet->payload);
216 if(!_cur_logic_snapshot)
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);
226 // Append to the existing data snapshot
227 _cur_logic_snapshot->append_payload(
228 *(sr_datafeed_logic*)packet->payload);
238 lock_guard<mutex> lock(_data_mutex);
239 _cur_logic_snapshot.reset();
247 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
248 struct sr_datafeed_packet *packet)
251 _session->data_feed_in(sdi, packet);