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()
41 // TODO: This should not be necessary
45 SigSession::~SigSession()
47 if(_sampling_thread.get())
48 _sampling_thread->join();
49 _sampling_thread.reset();
51 // TODO: This should not be necessary
55 void SigSession::load_file(const std::string &name)
57 if (sr_session_load(name.c_str()) == SR_OK) {
58 /* sigrok session file */
59 sr_session_datafeed_callback_add(data_feed_in_proc);
66 void SigSession::start_capture(struct sr_dev_inst *sdi,
67 uint64_t record_length, uint64_t sample_rate)
69 // Check sampling isn't already active
70 if(_sampling_thread.get())
71 _sampling_thread->join();
73 _sampling_thread.reset(new boost::thread(
74 &SigSession::sample_thread_proc, this, sdi,
75 record_length, sample_rate));
78 vector< shared_ptr<view::Signal> >& SigSession::get_signals()
83 boost::shared_ptr<LogicData> SigSession::get_data()
88 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
89 uint64_t record_length, uint64_t sample_rate)
92 sr_session_datafeed_callback_add(data_feed_in_proc);
94 if (sr_session_dev_add(sdi) != SR_OK) {
95 qDebug() << "Failed to use device.";
100 if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
101 &record_length) != SR_OK) {
102 qDebug() << "Failed to configure time-based sample limit.";
103 sr_session_destroy();
107 if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
108 &sample_rate) != SR_OK) {
109 qDebug() << "Failed to configure samplerate.";
110 sr_session_destroy();
114 if (sr_session_start() != SR_OK) {
115 qDebug() << "Failed to start session.";
120 sr_session_destroy();
123 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
124 struct sr_datafeed_packet *packet)
126 using view::LogicSignal;
131 switch (packet->type) {
134 lock_guard<mutex> lock(_data_mutex);
139 case SR_DF_META_LOGIC:
141 assert(packet->payload);
143 lock_guard<mutex> lock(_data_mutex);
145 const sr_datafeed_meta_logic &meta_logic =
146 *(sr_datafeed_meta_logic*)packet->payload;
148 // Create an empty LogiData for coming data snapshots
149 _logic_data.reset(new LogicData(meta_logic));
155 for (int i = 0; i < meta_logic.num_probes; i++)
157 const sr_probe *const probe =
158 (const sr_probe*)g_slist_nth_data(
162 shared_ptr<LogicSignal> signal(
163 new LogicSignal(probe->name,
166 _signals.push_back(signal);
176 lock_guard<mutex> lock(_data_mutex);
177 assert(packet->payload);
178 if(!_cur_logic_snapshot)
180 // Create a new data snapshot
181 _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
182 new LogicDataSnapshot(
183 *(sr_datafeed_logic*)packet->payload));
184 _logic_data->push_snapshot(_cur_logic_snapshot);
188 // Append to the existing data snapshot
189 _cur_logic_snapshot->append_payload(
190 *(sr_datafeed_logic*)packet->payload);
199 lock_guard<mutex> lock(_data_mutex);
200 _cur_logic_snapshot.reset();
208 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
209 struct sr_datafeed_packet *packet)
212 _session->data_feed_in(sdi, packet);