]> sigrok.org Git - pulseview.git/blame_incremental - pv/storesession.cpp
store session: do send end() packet after data export to output file
[pulseview.git] / pv / storesession.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <cassert>
21
22#include "storesession.hpp"
23
24#include <QSettings>
25
26#include <pv/data/analog.hpp>
27#include <pv/data/analogsegment.hpp>
28#include <pv/data/logic.hpp>
29#include <pv/data/logicsegment.hpp>
30#include <pv/data/signalbase.hpp>
31#include <pv/devicemanager.hpp>
32#include <pv/devices/device.hpp>
33#include <pv/globalsettings.hpp>
34#include <pv/session.hpp>
35
36#include <libsigrokcxx/libsigrokcxx.hpp>
37
38using std::deque;
39using std::ios_base;
40using std::lock_guard;
41using std::make_pair;
42using std::map;
43using std::min;
44using std::mutex;
45using std::pair;
46using std::shared_ptr;
47using std::string;
48using std::unordered_set;
49using std::vector;
50
51using Glib::VariantBase;
52
53using sigrok::ConfigKey;
54using sigrok::Error;
55using sigrok::OutputFormat;
56using sigrok::OutputFlag;
57
58namespace pv {
59
60const size_t StoreSession::BlockSize = 10 * 1024 * 1024;
61
62StoreSession::StoreSession(const string &file_name,
63 const shared_ptr<OutputFormat> &output_format,
64 const map<string, VariantBase> &options,
65 const pair<uint64_t, uint64_t> sample_range,
66 const Session &session) :
67 file_name_(file_name),
68 output_format_(output_format),
69 options_(options),
70 sample_range_(sample_range),
71 session_(session),
72 interrupt_(false),
73 units_stored_(0),
74 unit_count_(0)
75{
76}
77
78StoreSession::~StoreSession()
79{
80 wait();
81}
82
83pair<int, int> StoreSession::progress() const
84{
85 return make_pair(units_stored_.load(), unit_count_.load());
86}
87
88const QString& StoreSession::error() const
89{
90 lock_guard<mutex> lock(mutex_);
91 return error_;
92}
93
94bool StoreSession::start()
95{
96 const unordered_set< shared_ptr<data::SignalBase> > sigs(session_.signalbases());
97
98 shared_ptr<data::Segment> any_segment;
99 shared_ptr<data::LogicSegment> lsegment;
100 vector< shared_ptr<data::SignalBase> > achannel_list;
101 vector< shared_ptr<data::AnalogSegment> > asegment_list;
102
103 for (const shared_ptr<data::SignalBase>& signal : sigs) {
104 if (!signal->enabled())
105 continue;
106
107 if (signal->type() == data::SignalBase::LogicChannel) {
108 // All logic channels share the same data segments
109 shared_ptr<data::Logic> ldata = signal->logic_data();
110
111 const deque< shared_ptr<data::LogicSegment> > &lsegments =
112 ldata->logic_segments();
113
114 if (lsegments.empty()) {
115 error_ = tr("Can't save logic channel without data.");
116 return false;
117 }
118
119 lsegment = lsegments.front();
120 any_segment = lsegment;
121 }
122
123 if (signal->type() == data::SignalBase::AnalogChannel) {
124 // Each analog channel has its own segments
125 shared_ptr<data::Analog> adata = signal->analog_data();
126
127 const deque< shared_ptr<data::AnalogSegment> > &asegments =
128 adata->analog_segments();
129
130 if (asegments.empty()) {
131 error_ = tr("Can't save analog channel without data.");
132 return false;
133 }
134
135 asegment_list.push_back(asegments.front());
136 any_segment = asegments.front();
137
138 achannel_list.push_back(signal);
139 }
140 }
141
142 if (!any_segment) {
143 error_ = tr("No channels enabled.");
144 return false;
145 }
146
147 // Check whether the user wants to export a certain sample range
148 uint64_t end_sample;
149
150 if (sample_range_.first == sample_range_.second) {
151 // No sample range specified, save everything we have
152 start_sample_ = 0;
153 sample_count_ = any_segment->get_sample_count();
154 } else {
155 if (sample_range_.first > sample_range_.second) {
156 start_sample_ = sample_range_.second;
157 end_sample = min(sample_range_.first, any_segment->get_sample_count());
158 sample_count_ = end_sample - start_sample_;
159 } else {
160 start_sample_ = sample_range_.first;
161 end_sample = min(sample_range_.second, any_segment->get_sample_count());
162 sample_count_ = end_sample - start_sample_;
163 }
164 }
165
166 // Make sure the sample range is valid
167 if (start_sample_ > any_segment->get_sample_count()) {
168 error_ = tr("Can't save range without sample data.");
169 return false;
170 }
171
172 // Begin storing
173 try {
174 const auto context = session_.device_manager().context();
175 auto device = session_.device()->device();
176
177 map<string, Glib::VariantBase> options = options_;
178
179 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
180 output_stream_.open(file_name_, ios_base::binary |
181 ios_base::trunc | ios_base::out);
182
183 output_ = output_format_->create_output(file_name_, device, options);
184 auto meta = context->create_meta_packet(
185 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
186 any_segment->samplerate())}});
187 output_->receive(meta);
188 } catch (Error& error) {
189 error_ = tr("Error while saving: ") + error.what();
190 return false;
191 }
192
193 thread_ = std::thread(&StoreSession::store_proc, this,
194 achannel_list, asegment_list, lsegment);
195
196 // Save session setup if we're saving to srzip and the user wants it
197 GlobalSettings settings;
198 bool save_with_setup = settings.value(GlobalSettings::Key_General_SaveWithSetup).toBool();
199
200 if ((output_format_->name() == "srzip") && (save_with_setup)) {
201 QString setup_file_name = QString::fromStdString(file_name_);
202 setup_file_name.truncate(setup_file_name.lastIndexOf('.'));
203 setup_file_name.append(".pvs");
204
205 QSettings settings_storage(setup_file_name, QSettings::IniFormat);
206 session_.save_setup(settings_storage);
207 }
208
209 return true;
210}
211
212void StoreSession::wait()
213{
214 if (thread_.joinable())
215 thread_.join();
216}
217
218void StoreSession::cancel()
219{
220 interrupt_ = true;
221}
222
223void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list,
224 vector< shared_ptr<data::AnalogSegment> > asegment_list,
225 shared_ptr<data::LogicSegment> lsegment)
226{
227 unsigned progress_scale = 0;
228
229 int aunit_size = 0;
230 int lunit_size = 0;
231 unsigned int lsamples_per_block = INT_MAX;
232 unsigned int asamples_per_block = INT_MAX;
233
234 if (!asegment_list.empty()) {
235 // We assume all analog channels use the sample unit size
236 aunit_size = asegment_list.front()->unit_size();
237 asamples_per_block = BlockSize / aunit_size;
238 }
239 if (lsegment) {
240 lunit_size = lsegment->unit_size();
241 lsamples_per_block = BlockSize / lunit_size;
242 }
243
244 // Qt needs the progress values to fit inside an int. If they would
245 // not, scale the current and max values down until they do.
246 while ((sample_count_ >> progress_scale) > INT_MAX)
247 progress_scale++;
248
249 unit_count_ = sample_count_ >> progress_scale;
250
251 const unsigned int samples_per_block =
252 min(asamples_per_block, lsamples_per_block);
253
254 const auto context = session_.device_manager().context();
255 while (!interrupt_ && sample_count_) {
256 progress_updated();
257
258 const uint64_t packet_len =
259 min((uint64_t)samples_per_block, sample_count_);
260
261 try {
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);
265
266 float *adata = new float[packet_len];
267 asegment->get_samples(start_sample_, start_sample_ + packet_len, adata);
268
269 auto analog = context->create_analog_packet(
270 vector<shared_ptr<sigrok::Channel> >{achannel},
271 (float *)adata, packet_len,
272 sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT,
273 vector<const sigrok::QuantityFlag *>());
274 const string adata_str = output_->receive(analog);
275
276 if (output_stream_.is_open())
277 output_stream_ << adata_str;
278
279 delete[] adata;
280 }
281
282 if (lsegment) {
283 const size_t data_size = packet_len * lunit_size;
284 uint8_t* ldata = new uint8_t[data_size];
285 lsegment->get_samples(start_sample_, start_sample_ + packet_len, ldata);
286
287 auto logic = context->create_logic_packet((void*)ldata, data_size, lunit_size);
288 const string ldata_str = output_->receive(logic);
289
290 if (output_stream_.is_open())
291 output_stream_ << ldata_str;
292
293 delete[] ldata;
294 }
295 } catch (Error& error) {
296 error_ = tr("Error while saving: ") + error.what();
297 break;
298 }
299
300 sample_count_ -= packet_len;
301 start_sample_ += packet_len;
302 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
303 }
304
305 auto dfend = context->create_end_packet();
306 const string ldata_str = output_->receive(dfend);
307 if (output_stream_.is_open())
308 output_stream_ << ldata_str;
309
310 // Zeroing the progress variables indicates completion
311 units_stored_ = unit_count_ = 0;
312
313 store_successful();
314 progress_updated();
315
316 output_.reset();
317 output_stream_.close();
318}
319
320} // namespace pv