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, see <http://www.gnu.org/licenses/>.
23 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
28 #include "storesession.hpp"
30 #include <pv/devicemanager.hpp>
31 #include <pv/session.hpp>
32 #include <pv/data/analog.hpp>
33 #include <pv/data/analogsegment.hpp>
34 #include <pv/data/logic.hpp>
35 #include <pv/data/logicsegment.hpp>
36 #include <pv/data/signalbase.hpp>
37 #include <pv/devices/device.hpp>
39 #include <libsigrokcxx/libsigrokcxx.hpp>
43 using std::lock_guard;
49 using std::shared_ptr;
51 using std::unordered_set;
54 using Glib::VariantBase;
56 using sigrok::ConfigKey;
58 using sigrok::OutputFormat;
59 using sigrok::OutputFlag;
63 const size_t StoreSession::BlockSize = 10 * 1024 * 1024;
65 StoreSession::StoreSession(const string &file_name,
66 const shared_ptr<OutputFormat> &output_format,
67 const map<string, VariantBase> &options,
68 const pair<uint64_t, uint64_t> sample_range,
69 const Session &session) :
70 file_name_(file_name),
71 output_format_(output_format),
73 sample_range_(sample_range),
81 StoreSession::~StoreSession()
86 pair<int, int> StoreSession::progress() const
88 return make_pair(units_stored_.load(), unit_count_.load());
91 const QString& StoreSession::error() const
93 lock_guard<mutex> lock(mutex_);
97 bool StoreSession::start()
99 const unordered_set< shared_ptr<data::SignalBase> > sigs(session_.signalbases());
101 shared_ptr<data::Segment> any_segment;
102 shared_ptr<data::LogicSegment> lsegment;
103 vector< shared_ptr<data::SignalBase> > achannel_list;
104 vector< shared_ptr<data::AnalogSegment> > asegment_list;
106 for (shared_ptr<data::SignalBase> signal : sigs) {
107 if (!signal->enabled())
110 if (signal->type() == data::SignalBase::LogicChannel) {
111 // All logic channels share the same data segments
112 shared_ptr<data::Logic> ldata = signal->logic_data();
114 const deque< shared_ptr<data::LogicSegment> > &lsegments =
115 ldata->logic_segments();
117 if (lsegments.empty()) {
118 error_ = tr("Can't save logic channel without data.");
122 lsegment = lsegments.front();
123 any_segment = lsegment;
126 if (signal->type() == data::SignalBase::AnalogChannel) {
127 // Each analog channel has its own segments
128 shared_ptr<data::Analog> adata = signal->analog_data();
130 const deque< shared_ptr<data::AnalogSegment> > &asegments =
131 adata->analog_segments();
133 if (asegments.empty()) {
134 error_ = tr("Can't save analog channel without data.");
138 asegment_list.push_back(asegments.front());
139 any_segment = asegments.front();
141 achannel_list.push_back(signal);
146 error_ = tr("No channels enabled.");
150 // Check whether the user wants to export a certain sample range
153 if (sample_range_.first == sample_range_.second) {
155 sample_count_ = any_segment->get_sample_count();
157 if (sample_range_.first > sample_range_.second) {
158 start_sample_ = sample_range_.second;
159 end_sample = min(sample_range_.first, any_segment->get_sample_count());
160 sample_count_ = end_sample - start_sample_;
162 start_sample_ = sample_range_.first;
163 end_sample = min(sample_range_.second, any_segment->get_sample_count());
164 sample_count_ = end_sample - start_sample_;
170 const auto context = session_.device_manager().context();
171 auto device = session_.device()->device();
173 map<string, Glib::VariantBase> options = options_;
175 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
176 output_stream_.open(file_name_, ios_base::binary |
177 ios_base::trunc | ios_base::out);
179 output_ = output_format_->create_output(file_name_, device, options);
180 auto meta = context->create_meta_packet(
181 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
182 any_segment->samplerate())}});
183 output_->receive(meta);
184 } catch (Error error) {
185 error_ = tr("Error while saving: ") + error.what();
189 thread_ = std::thread(&StoreSession::store_proc, this,
190 achannel_list, asegment_list, lsegment);
194 void StoreSession::wait()
196 if (thread_.joinable())
200 void StoreSession::cancel()
205 void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list,
206 vector< shared_ptr<data::AnalogSegment> > asegment_list,
207 shared_ptr<data::LogicSegment> lsegment)
209 unsigned progress_scale = 0;
213 unsigned int lsamples_per_block = INT_MAX;
214 unsigned int asamples_per_block = INT_MAX;
216 if (!asegment_list.empty()) {
217 // We assume all analog channels use the sample unit size
218 aunit_size = asegment_list.front()->unit_size();
219 asamples_per_block = BlockSize / aunit_size;
222 lunit_size = lsegment->unit_size();
223 lsamples_per_block = BlockSize / lunit_size;
226 // Qt needs the progress values to fit inside an int. If they would
227 // not, scale the current and max values down until they do.
228 while ((sample_count_ >> progress_scale) > INT_MAX)
231 unit_count_ = sample_count_ >> progress_scale;
233 const unsigned int samples_per_block =
234 min(asamples_per_block, lsamples_per_block);
236 while (!interrupt_ && sample_count_) {
239 const uint64_t packet_len =
240 min((uint64_t)samples_per_block, sample_count_);
243 const auto context = session_.device_manager().context();
245 for (unsigned int i = 0; i < achannel_list.size(); i++) {
246 shared_ptr<sigrok::Channel> achannel = (achannel_list.at(i))->channel();
247 shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i);
250 asegment->get_samples(start_sample_, start_sample_ + packet_len);
252 auto analog = context->create_analog_packet(
253 vector<shared_ptr<sigrok::Channel> >{achannel},
254 (float *)adata, packet_len,
255 sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT,
256 vector<const sigrok::QuantityFlag *>());
257 const string adata_str = output_->receive(analog);
259 if (output_stream_.is_open())
260 output_stream_ << adata_str;
266 const uint8_t* ldata =
267 lsegment->get_samples(start_sample_, start_sample_ + packet_len);
269 const size_t length = packet_len * lunit_size;
270 auto logic = context->create_logic_packet((void*)ldata, length, lunit_size);
271 const string ldata_str = output_->receive(logic);
273 if (output_stream_.is_open())
274 output_stream_ << ldata_str;
278 } catch (Error error) {
279 error_ = tr("Error while saving: ") + error.what();
283 sample_count_ -= packet_len;
284 start_sample_ += packet_len;
285 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
288 // Zeroing the progress variables indicates completion
289 units_stored_ = unit_count_ = 0;
295 output_stream_.close();