2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 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
24 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
28 #include <boost/thread/locks.hpp>
29 #include <boost/thread/shared_mutex.hpp>
31 #include "storesession.hpp"
33 #include <pv/devicemanager.hpp>
34 #include <pv/session.hpp>
35 #include <pv/data/analog.hpp>
36 #include <pv/data/analogsegment.hpp>
37 #include <pv/data/logic.hpp>
38 #include <pv/data/logicsegment.hpp>
39 #include <pv/devices/device.hpp>
40 #include <pv/view/signal.hpp>
42 #include <libsigrokcxx/libsigrokcxx.hpp>
44 using boost::shared_lock;
45 using boost::shared_mutex;
48 using std::dynamic_pointer_cast;
50 using std::lock_guard;
57 using std::shared_ptr;
60 using std::unordered_set;
63 using Glib::VariantBase;
65 using sigrok::ConfigKey;
67 using sigrok::OutputFormat;
68 using sigrok::OutputFlag;
72 const size_t StoreSession::BlockSize = 1024 * 1024;
74 StoreSession::StoreSession(const std::string &file_name,
75 const shared_ptr<OutputFormat> &output_format,
76 const map<string, VariantBase> &options,
77 const std::pair<uint64_t, uint64_t> sample_range,
78 const Session &session) :
79 file_name_(file_name),
80 output_format_(output_format),
82 sample_range_(sample_range),
90 StoreSession::~StoreSession()
95 pair<int, int> StoreSession::progress() const
97 return make_pair(units_stored_.load(), unit_count_.load());
100 const QString& StoreSession::error() const
102 lock_guard<mutex> lock(mutex_);
106 bool StoreSession::start()
108 const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals());
110 shared_ptr<data::Segment> any_segment;
111 shared_ptr<data::LogicSegment> lsegment;
112 vector< shared_ptr<sigrok::Channel> > achannel_list;
113 vector< shared_ptr<data::AnalogSegment> > asegment_list;
115 for (shared_ptr<view::Signal> signal : sigs) {
116 if (!signal->enabled())
119 shared_ptr<data::SignalData> data = signal->data();
121 if (dynamic_pointer_cast<data::Logic>(data)) {
122 // All logic channels share the same data segments
123 shared_ptr<data::Logic> ldata = dynamic_pointer_cast<data::Logic>(data);
125 const deque< shared_ptr<data::LogicSegment> > &lsegments =
126 ldata->logic_segments();
128 if (lsegments.empty()) {
129 error_ = tr("Can't save logic channel without data.");
133 lsegment = lsegments.front();
134 any_segment = lsegment;
137 if (dynamic_pointer_cast<data::Analog>(data)) {
138 // Each analog channel has its own segments
139 shared_ptr<data::Analog> adata =
140 dynamic_pointer_cast<data::Analog>(data);
142 const deque< shared_ptr<data::AnalogSegment> > &asegments =
143 adata->analog_segments();
145 if (asegments.empty()) {
146 error_ = tr("Can't save analog channel without data.");
150 asegment_list.push_back(asegments.front());
151 any_segment = asegments.front();
153 achannel_list.push_back(signal->channel());
158 error_ = tr("No channels enabled.");
162 // Check whether the user wants to export a certain sample range
163 if (sample_range_.first == sample_range_.second) {
165 sample_count_ = any_segment->get_sample_count();
167 if (sample_range_.first > sample_range_.second) {
168 start_sample_ = sample_range_.second;
169 sample_count_ = sample_range_.first - sample_range_.second;
171 start_sample_ = sample_range_.first;
172 sample_count_ = sample_range_.second - sample_range_.first;
178 const auto context = session_.device_manager().context();
179 auto device = session_.device()->device();
181 map<string, Glib::VariantBase> options = options_;
183 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
184 output_stream_.open(file_name_, ios_base::binary |
185 ios_base::trunc | ios_base::out);
187 output_ = output_format_->create_output(file_name_, device, options);
188 auto meta = context->create_meta_packet(
189 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
190 any_segment->samplerate())}});
191 output_->receive(meta);
192 } catch (Error error) {
193 error_ = tr("Error while saving: ") + error.what();
197 thread_ = std::thread(&StoreSession::store_proc, this,
198 achannel_list, asegment_list, lsegment);
202 void StoreSession::wait()
204 if (thread_.joinable())
208 void StoreSession::cancel()
213 void StoreSession::store_proc(vector< shared_ptr<sigrok::Channel> > achannel_list,
214 vector< shared_ptr<data::AnalogSegment> > asegment_list,
215 shared_ptr<data::LogicSegment> lsegment)
217 unsigned progress_scale = 0;
219 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
220 uint8_t *const ldata = new uint8_t[BlockSize];
225 unsigned int lsamples_per_block = INT_MAX;
226 unsigned int asamples_per_block = INT_MAX;
228 if (!asegment_list.empty()) {
229 // We assume all analog channels use the sample unit size
230 aunit_size = asegment_list.front()->unit_size();
231 asamples_per_block = BlockSize / aunit_size;
234 lunit_size = lsegment->unit_size();
235 lsamples_per_block = BlockSize / lunit_size;
238 // Qt needs the progress values to fit inside an int. If they would
239 // not, scale the current and max values down until they do.
240 while ((sample_count_ >> progress_scale) > INT_MAX)
243 unit_count_ = sample_count_ >> progress_scale;
245 const unsigned int samples_per_block =
246 std::min(asamples_per_block, lsamples_per_block);
248 while (!interrupt_ && sample_count_) {
251 const uint64_t packet_len =
252 std::min((uint64_t)samples_per_block, sample_count_);
255 const auto context = session_.device_manager().context();
257 for (unsigned int i = 0; i < achannel_list.size(); i++) {
258 shared_ptr<sigrok::Channel> achannel = achannel_list.at(i);
259 shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i);
262 asegment->get_samples(start_sample_, start_sample_ + packet_len);
264 // The srzip format currently only supports packets with one
265 // analog channel. See zip_append_analog() in srzip.c
266 auto analog = context->create_analog_packet(
267 vector<shared_ptr<sigrok::Channel> >{achannel},
268 (float *)adata, packet_len,
269 sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT,
270 vector<const sigrok::QuantityFlag *>());
271 const string adata_str = output_->receive(analog);
273 if (output_stream_.is_open())
274 output_stream_ << adata_str;
280 lsegment->get_samples(ldata, start_sample_, start_sample_ + packet_len);
282 const size_t length = packet_len * lunit_size;
283 auto logic = context->create_logic_packet(ldata, length, lunit_size);
284 const string ldata_str = output_->receive(logic);
286 if (output_stream_.is_open())
287 output_stream_ << ldata_str;
289 } catch (Error error) {
290 error_ = tr("Error while saving: ") + error.what();
294 sample_count_ -= packet_len;
295 start_sample_ += packet_len;
296 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
299 // Zeroing the progress variables indicates completion
300 units_stored_ = unit_count_ = 0;
305 output_stream_.close();