]> sigrok.org Git - pulseview.git/blame_incremental - pv/storesession.cpp
Random simplifications, cosmetics/whitespace/consistency fixes.
[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#ifdef _WIN32
23// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
24#define NOGDI
25#define NORESOURCE
26#endif
27
28#include "storesession.hpp"
29
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>
38
39#include <libsigrokcxx/libsigrokcxx.hpp>
40
41using std::deque;
42using std::ios_base;
43using std::lock_guard;
44using std::make_pair;
45using std::map;
46using std::min;
47using std::mutex;
48using std::pair;
49using std::shared_ptr;
50using std::string;
51using std::unordered_set;
52using std::vector;
53
54using Glib::VariantBase;
55
56using sigrok::ConfigKey;
57using sigrok::Error;
58using sigrok::OutputFormat;
59using sigrok::OutputFlag;
60
61namespace pv {
62
63const size_t StoreSession::BlockSize = 10 * 1024 * 1024;
64
65StoreSession::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),
72 options_(options),
73 sample_range_(sample_range),
74 session_(session),
75 interrupt_(false),
76 units_stored_(0),
77 unit_count_(0)
78{
79}
80
81StoreSession::~StoreSession()
82{
83 wait();
84}
85
86pair<int, int> StoreSession::progress() const
87{
88 return make_pair(units_stored_.load(), unit_count_.load());
89}
90
91const QString& StoreSession::error() const
92{
93 lock_guard<mutex> lock(mutex_);
94 return error_;
95}
96
97bool StoreSession::start()
98{
99 const unordered_set< shared_ptr<data::SignalBase> > sigs(session_.signalbases());
100
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;
105
106 for (shared_ptr<data::SignalBase> signal : sigs) {
107 if (!signal->enabled())
108 continue;
109
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();
113
114 const deque< shared_ptr<data::LogicSegment> > &lsegments =
115 ldata->logic_segments();
116
117 if (lsegments.empty()) {
118 error_ = tr("Can't save logic channel without data.");
119 return false;
120 }
121
122 lsegment = lsegments.front();
123 any_segment = lsegment;
124 }
125
126 if (signal->type() == data::SignalBase::AnalogChannel) {
127 // Each analog channel has its own segments
128 shared_ptr<data::Analog> adata = signal->analog_data();
129
130 const deque< shared_ptr<data::AnalogSegment> > &asegments =
131 adata->analog_segments();
132
133 if (asegments.empty()) {
134 error_ = tr("Can't save analog channel without data.");
135 return false;
136 }
137
138 asegment_list.push_back(asegments.front());
139 any_segment = asegments.front();
140
141 achannel_list.push_back(signal);
142 }
143 }
144
145 if (!any_segment) {
146 error_ = tr("No channels enabled.");
147 return false;
148 }
149
150 // Check whether the user wants to export a certain sample range
151 uint64_t end_sample;
152
153 if (sample_range_.first == sample_range_.second) {
154 start_sample_ = 0;
155 sample_count_ = any_segment->get_sample_count();
156 } else {
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_;
161 } else {
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_;
165 }
166 }
167
168 // Begin storing
169 try {
170 const auto context = session_.device_manager().context();
171 auto device = session_.device()->device();
172
173 map<string, Glib::VariantBase> options = options_;
174
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);
178
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();
186 return false;
187 }
188
189 thread_ = std::thread(&StoreSession::store_proc, this,
190 achannel_list, asegment_list, lsegment);
191 return true;
192}
193
194void StoreSession::wait()
195{
196 if (thread_.joinable())
197 thread_.join();
198}
199
200void StoreSession::cancel()
201{
202 interrupt_ = true;
203}
204
205void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list,
206 vector< shared_ptr<data::AnalogSegment> > asegment_list,
207 shared_ptr<data::LogicSegment> lsegment)
208{
209 unsigned progress_scale = 0;
210
211 int aunit_size = 0;
212 int lunit_size = 0;
213 unsigned int lsamples_per_block = INT_MAX;
214 unsigned int asamples_per_block = INT_MAX;
215
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;
220 }
221 if (lsegment) {
222 lunit_size = lsegment->unit_size();
223 lsamples_per_block = BlockSize / lunit_size;
224 }
225
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)
229 progress_scale++;
230
231 unit_count_ = sample_count_ >> progress_scale;
232
233 const unsigned int samples_per_block =
234 min(asamples_per_block, lsamples_per_block);
235
236 while (!interrupt_ && sample_count_) {
237 progress_updated();
238
239 const uint64_t packet_len =
240 min((uint64_t)samples_per_block, sample_count_);
241
242 try {
243 const auto context = session_.device_manager().context();
244
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);
248
249 const float *adata =
250 asegment->get_samples(start_sample_, start_sample_ + packet_len);
251
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);
258
259 if (output_stream_.is_open())
260 output_stream_ << adata_str;
261
262 delete[] adata;
263 }
264
265 if (lsegment) {
266 const uint8_t* ldata =
267 lsegment->get_samples(start_sample_, start_sample_ + packet_len);
268
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);
272
273 if (output_stream_.is_open())
274 output_stream_ << ldata_str;
275
276 delete[] ldata;
277 }
278 } catch (Error error) {
279 error_ = tr("Error while saving: ") + error.what();
280 break;
281 }
282
283 sample_count_ -= packet_len;
284 start_sample_ += packet_len;
285 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
286 }
287
288 // Zeroing the progress variables indicates completion
289 units_stored_ = unit_count_ = 0;
290
291 store_successful();
292 progress_updated();
293
294 output_.reset();
295 output_stream_.close();
296}
297
298} // pv