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/data/signalbase.hpp>
40 #include <pv/devices/device.hpp>
41 #include <pv/view/signal.hpp>
43 #include <libsigrokcxx/libsigrokcxx.hpp>
45 using boost::shared_lock;
46 using boost::shared_mutex;
49 using std::dynamic_pointer_cast;
51 using std::lock_guard;
58 using std::shared_ptr;
61 using std::unordered_set;
64 using Glib::VariantBase;
66 using sigrok::ConfigKey;
68 using sigrok::OutputFormat;
69 using sigrok::OutputFlag;
73 const size_t StoreSession::BlockSize = 1024 * 1024;
75 StoreSession::StoreSession(const std::string &file_name,
76 const shared_ptr<OutputFormat> &output_format,
77 const map<string, VariantBase> &options,
78 const std::pair<uint64_t, uint64_t> sample_range,
79 const Session &session) :
80 file_name_(file_name),
81 output_format_(output_format),
83 sample_range_(sample_range),
91 StoreSession::~StoreSession()
96 pair<int, int> StoreSession::progress() const
98 return make_pair(units_stored_.load(), unit_count_.load());
101 const QString& StoreSession::error() const
103 lock_guard<mutex> lock(mutex_);
107 bool StoreSession::start()
109 const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals());
111 shared_ptr<data::Segment> any_segment;
112 shared_ptr<data::LogicSegment> lsegment;
113 vector< shared_ptr<data::SignalBase> > achannel_list;
114 vector< shared_ptr<data::AnalogSegment> > asegment_list;
116 for (shared_ptr<view::Signal> signal : sigs) {
117 if (!signal->enabled())
120 shared_ptr<data::SignalData> data = signal->data();
122 if (dynamic_pointer_cast<data::Logic>(data)) {
123 // All logic channels share the same data segments
124 shared_ptr<data::Logic> ldata = dynamic_pointer_cast<data::Logic>(data);
126 const deque< shared_ptr<data::LogicSegment> > &lsegments =
127 ldata->logic_segments();
129 if (lsegments.empty()) {
130 error_ = tr("Can't save logic channel without data.");
134 lsegment = lsegments.front();
135 any_segment = lsegment;
138 if (dynamic_pointer_cast<data::Analog>(data)) {
139 // Each analog channel has its own segments
140 shared_ptr<data::Analog> adata =
141 dynamic_pointer_cast<data::Analog>(data);
143 const deque< shared_ptr<data::AnalogSegment> > &asegments =
144 adata->analog_segments();
146 if (asegments.empty()) {
147 error_ = tr("Can't save analog channel without data.");
151 asegment_list.push_back(asegments.front());
152 any_segment = asegments.front();
154 achannel_list.push_back(signal->channel());
159 error_ = tr("No channels enabled.");
163 // Check whether the user wants to export a certain sample range
166 if (sample_range_.first == sample_range_.second) {
168 sample_count_ = any_segment->get_sample_count();
170 if (sample_range_.first > sample_range_.second) {
171 start_sample_ = sample_range_.second;
172 end_sample = min(sample_range_.first, any_segment->get_sample_count());
173 sample_count_ = end_sample - start_sample_;
175 start_sample_ = sample_range_.first;
176 end_sample = min(sample_range_.second, any_segment->get_sample_count());
177 sample_count_ = end_sample - start_sample_;
183 const auto context = session_.device_manager().context();
184 auto device = session_.device()->device();
186 map<string, Glib::VariantBase> options = options_;
188 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
189 output_stream_.open(file_name_, ios_base::binary |
190 ios_base::trunc | ios_base::out);
192 output_ = output_format_->create_output(file_name_, device, options);
193 auto meta = context->create_meta_packet(
194 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
195 any_segment->samplerate())}});
196 output_->receive(meta);
197 } catch (Error error) {
198 error_ = tr("Error while saving: ") + error.what();
202 thread_ = std::thread(&StoreSession::store_proc, this,
203 achannel_list, asegment_list, lsegment);
207 void StoreSession::wait()
209 if (thread_.joinable())
213 void StoreSession::cancel()
218 void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list,
219 vector< shared_ptr<data::AnalogSegment> > asegment_list,
220 shared_ptr<data::LogicSegment> lsegment)
222 unsigned progress_scale = 0;
224 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
225 uint8_t *const ldata = new uint8_t[BlockSize];
230 unsigned int lsamples_per_block = INT_MAX;
231 unsigned int asamples_per_block = INT_MAX;
233 if (!asegment_list.empty()) {
234 // We assume all analog channels use the sample unit size
235 aunit_size = asegment_list.front()->unit_size();
236 asamples_per_block = BlockSize / aunit_size;
239 lunit_size = lsegment->unit_size();
240 lsamples_per_block = BlockSize / lunit_size;
243 // Qt needs the progress values to fit inside an int. If they would
244 // not, scale the current and max values down until they do.
245 while ((sample_count_ >> progress_scale) > INT_MAX)
248 unit_count_ = sample_count_ >> progress_scale;
250 const unsigned int samples_per_block =
251 std::min(asamples_per_block, lsamples_per_block);
253 while (!interrupt_ && sample_count_) {
256 const uint64_t packet_len =
257 std::min((uint64_t)samples_per_block, sample_count_);
260 const auto context = session_.device_manager().context();
262 for (unsigned int i = 0; i < achannel_list.size(); i++) {
263 shared_ptr<sigrok::Channel> achannel = (achannel_list.at(i))->channel();
264 shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i);
267 asegment->get_samples(start_sample_, start_sample_ + packet_len);
269 // The srzip format currently only supports packets with one
270 // analog channel. See zip_append_analog() in srzip.c
271 auto analog = context->create_analog_packet(
272 vector<shared_ptr<sigrok::Channel> >{achannel},
273 (float *)adata, packet_len,
274 sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT,
275 vector<const sigrok::QuantityFlag *>());
276 const string adata_str = output_->receive(analog);
278 if (output_stream_.is_open())
279 output_stream_ << adata_str;
285 lsegment->get_samples(ldata, start_sample_, start_sample_ + packet_len);
287 const size_t length = packet_len * lunit_size;
288 auto logic = context->create_logic_packet(ldata, length, lunit_size);
289 const string ldata_str = output_->receive(logic);
291 if (output_stream_.is_open())
292 output_stream_ << ldata_str;
294 } catch (Error error) {
295 error_ = tr("Error while saving: ") + error.what();
299 sample_count_ -= packet_len;
300 start_sample_ += packet_len;
301 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
304 // Zeroing the progress variables indicates completion
305 units_stored_ = unit_count_ = 0;
310 output_stream_.close();