]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
Don't use std:: in the code directly (where possible).
[pulseview.git] / pv / storesession.cpp
CommitLineData
0fbda3c2
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
0fbda3c2
JH
18 */
19
d2344534
JH
20#include <cassert>
21
35750e4d
UH
22#ifdef _WIN32
23// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
24#define NOGDI
25#define NORESOURCE
26#endif
e71eb81c
JH
27#include <boost/thread/locks.hpp>
28#include <boost/thread/shared_mutex.hpp>
29
2acdb232 30#include "storesession.hpp"
0fbda3c2 31
adb240c0 32#include <pv/devicemanager.hpp>
f65cd27b 33#include <pv/session.hpp>
c51ae0b4
SA
34#include <pv/data/analog.hpp>
35#include <pv/data/analogsegment.hpp>
2acdb232 36#include <pv/data/logic.hpp>
f3d66e52 37#include <pv/data/logicsegment.hpp>
bf0edd2b 38#include <pv/data/signalbase.hpp>
da30ecb7 39#include <pv/devices/device.hpp>
0fbda3c2 40
fe3a1c21 41#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 42
aca64cac
JH
43using boost::shared_lock;
44using boost::shared_mutex;
45
0fbda3c2 46using std::deque;
f9abf97e 47using std::dynamic_pointer_cast;
c1035a14 48using std::ios_base;
3b68d03d 49using std::lock_guard;
0fbda3c2 50using std::make_pair;
c1035a14 51using std::map;
0fbda3c2 52using std::min;
3b68d03d 53using std::mutex;
0fbda3c2
JH
54using std::pair;
55using std::set;
f9abf97e 56using std::shared_ptr;
0fbda3c2 57using std::string;
78b0af3e 58using std::unordered_set;
0fbda3c2
JH
59using std::vector;
60
e93f5538
JH
61using Glib::VariantBase;
62
47e9e7bb 63using sigrok::ChannelType;
7223eb62 64using sigrok::ConfigKey;
e8d00928 65using sigrok::Error;
4764bc4d 66using sigrok::OutputFormat;
542f1e6c 67using sigrok::OutputFlag;
e8d00928 68
0fbda3c2
JH
69namespace pv {
70
9cc264b4 71const size_t StoreSession::BlockSize = 10 * 1024 * 1024;
0fbda3c2 72
6f925ba9 73StoreSession::StoreSession(const string &file_name,
e93f5538 74 const shared_ptr<OutputFormat> &output_format,
d2fc6be9 75 const map<string, VariantBase> &options,
6f925ba9 76 const pair<uint64_t, uint64_t> sample_range,
d2fc6be9 77 const Session &session) :
8dbbc7f0 78 file_name_(file_name),
4764bc4d 79 output_format_(output_format),
e93f5538 80 options_(options),
d2fc6be9 81 sample_range_(sample_range),
8dbbc7f0
JH
82 session_(session),
83 interrupt_(false),
84 units_stored_(0),
85 unit_count_(0)
0fbda3c2
JH
86{
87}
88
89StoreSession::~StoreSession()
90{
91 wait();
92}
93
e40b2f29 94pair<int, int> StoreSession::progress() const
0fbda3c2 95{
8dbbc7f0 96 return make_pair(units_stored_.load(), unit_count_.load());
0fbda3c2
JH
97}
98
99const QString& StoreSession::error() const
100{
8dbbc7f0
JH
101 lock_guard<mutex> lock(mutex_);
102 return error_;
0fbda3c2
JH
103}
104
105bool StoreSession::start()
106{
47e9e7bb 107 const unordered_set< shared_ptr<data::SignalBase> > sigs(session_.signalbases());
de6e819c 108
c51ae0b4
SA
109 shared_ptr<data::Segment> any_segment;
110 shared_ptr<data::LogicSegment> lsegment;
bf0edd2b 111 vector< shared_ptr<data::SignalBase> > achannel_list;
c51ae0b4 112 vector< shared_ptr<data::AnalogSegment> > asegment_list;
de6e819c 113
47e9e7bb 114 for (shared_ptr<data::SignalBase> signal : sigs) {
c51ae0b4
SA
115 if (!signal->enabled())
116 continue;
0fbda3c2 117
47e9e7bb 118 if (signal->type() == ChannelType::LOGIC) {
c51ae0b4 119 // All logic channels share the same data segments
47e9e7bb 120 shared_ptr<data::Logic> ldata = signal->logic_data();
0fbda3c2 121
c51ae0b4
SA
122 const deque< shared_ptr<data::LogicSegment> > &lsegments =
123 ldata->logic_segments();
0fbda3c2 124
c51ae0b4
SA
125 if (lsegments.empty()) {
126 error_ = tr("Can't save logic channel without data.");
127 return false;
128 }
0fbda3c2 129
c51ae0b4
SA
130 lsegment = lsegments.front();
131 any_segment = lsegment;
132 }
133
47e9e7bb 134 if (signal->type() == ChannelType::ANALOG) {
c51ae0b4 135 // Each analog channel has its own segments
47e9e7bb 136 shared_ptr<data::Analog> adata = signal->analog_data();
c51ae0b4
SA
137
138 const deque< shared_ptr<data::AnalogSegment> > &asegments =
139 adata->analog_segments();
140
141 if (asegments.empty()) {
142 error_ = tr("Can't save analog channel without data.");
143 return false;
144 }
145
146 asegment_list.push_back(asegments.front());
147 any_segment = asegments.front();
148
47e9e7bb 149 achannel_list.push_back(signal);
c51ae0b4 150 }
0fbda3c2
JH
151 }
152
c51ae0b4
SA
153 if (!any_segment) {
154 error_ = tr("No channels enabled.");
155 return false;
156 }
0fbda3c2 157
d2fc6be9 158 // Check whether the user wants to export a certain sample range
40ec0302
SA
159 uint64_t end_sample;
160
d2fc6be9
SA
161 if (sample_range_.first == sample_range_.second) {
162 start_sample_ = 0;
c51ae0b4 163 sample_count_ = any_segment->get_sample_count();
d2fc6be9 164 } else {
3e8a7cc6
SA
165 if (sample_range_.first > sample_range_.second) {
166 start_sample_ = sample_range_.second;
40ec0302
SA
167 end_sample = min(sample_range_.first, any_segment->get_sample_count());
168 sample_count_ = end_sample - start_sample_;
3e8a7cc6
SA
169 } else {
170 start_sample_ = sample_range_.first;
40ec0302
SA
171 end_sample = min(sample_range_.second, any_segment->get_sample_count());
172 sample_count_ = end_sample - start_sample_;
3e8a7cc6 173 }
d2fc6be9
SA
174 }
175
0fbda3c2 176 // Begin storing
e8d00928 177 try {
adb240c0 178 const auto context = session_.device_manager().context();
da30ecb7 179 auto device = session_.device()->device();
c1035a14 180
e93f5538 181 map<string, Glib::VariantBase> options = options_;
c1035a14 182
542f1e6c
SA
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);
ad83f8fa
SA
186
187 output_ = output_format_->create_output(file_name_, device, options);
7223eb62 188 auto meta = context->create_meta_packet(
ff008de6 189 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
c51ae0b4 190 any_segment->samplerate())}});
8dbbc7f0 191 output_->receive(meta);
e8d00928 192 } catch (Error error) {
c51ae0b4 193 error_ = tr("Error while saving: ") + error.what();
0fbda3c2
JH
194 return false;
195 }
196
c51ae0b4
SA
197 thread_ = std::thread(&StoreSession::store_proc, this,
198 achannel_list, asegment_list, lsegment);
0fbda3c2
JH
199 return true;
200}
201
202void StoreSession::wait()
203{
8dbbc7f0
JH
204 if (thread_.joinable())
205 thread_.join();
0fbda3c2
JH
206}
207
208void StoreSession::cancel()
209{
8dbbc7f0 210 interrupt_ = true;
0fbda3c2
JH
211}
212
bf0edd2b 213void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list,
c51ae0b4
SA
214 vector< shared_ptr<data::AnalogSegment> > asegment_list,
215 shared_ptr<data::LogicSegment> lsegment)
0fbda3c2 216{
e40b2f29 217 unsigned progress_scale = 0;
0fbda3c2 218
c51ae0b4
SA
219 int aunit_size = 0;
220 int lunit_size = 0;
221 unsigned int lsamples_per_block = INT_MAX;
222 unsigned int asamples_per_block = INT_MAX;
223
224 if (!asegment_list.empty()) {
225 // We assume all analog channels use the sample unit size
226 aunit_size = asegment_list.front()->unit_size();
227 asamples_per_block = BlockSize / aunit_size;
228 }
229 if (lsegment) {
230 lunit_size = lsegment->unit_size();
231 lsamples_per_block = BlockSize / lunit_size;
232 }
0fbda3c2 233
3d79f521 234 // Qt needs the progress values to fit inside an int. If they would
e40b2f29 235 // not, scale the current and max values down until they do.
d2fc6be9 236 while ((sample_count_ >> progress_scale) > INT_MAX)
e40b2f29
MC
237 progress_scale ++;
238
d2fc6be9 239 unit_count_ = sample_count_ >> progress_scale;
0fbda3c2 240
c51ae0b4 241 const unsigned int samples_per_block =
6f925ba9 242 min(asamples_per_block, lsamples_per_block);
0fbda3c2 243
2ad82c2e 244 while (!interrupt_ && sample_count_) {
0fbda3c2
JH
245 progress_updated();
246
d2fc6be9 247 const uint64_t packet_len =
6f925ba9 248 min((uint64_t)samples_per_block, sample_count_);
d2fc6be9 249
e8d00928 250 try {
adb240c0 251 const auto context = session_.device_manager().context();
c51ae0b4
SA
252
253 for (unsigned int i = 0; i < achannel_list.size(); i++) {
bf0edd2b 254 shared_ptr<sigrok::Channel> achannel = (achannel_list.at(i))->channel();
c51ae0b4
SA
255 shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i);
256
257 const float *adata =
258 asegment->get_samples(start_sample_, start_sample_ + packet_len);
259
c51ae0b4
SA
260 auto analog = context->create_analog_packet(
261 vector<shared_ptr<sigrok::Channel> >{achannel},
262 (float *)adata, packet_len,
263 sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT,
264 vector<const sigrok::QuantityFlag *>());
265 const string adata_str = output_->receive(analog);
266
267 if (output_stream_.is_open())
268 output_stream_ << adata_str;
269
270 delete[] adata;
271 }
272
273 if (lsegment) {
038a1427
SA
274 const uint8_t* ldata =
275 lsegment->get_samples(start_sample_, start_sample_ + packet_len);
c51ae0b4
SA
276
277 const size_t length = packet_len * lunit_size;
038a1427 278 auto logic = context->create_logic_packet((void*)ldata, length, lunit_size);
c51ae0b4
SA
279 const string ldata_str = output_->receive(logic);
280
281 if (output_stream_.is_open())
282 output_stream_ << ldata_str;
038a1427
SA
283
284 delete[] ldata;
c51ae0b4 285 }
e8d00928 286 } catch (Error error) {
c51ae0b4 287 error_ = tr("Error while saving: ") + error.what();
0fbda3c2
JH
288 break;
289 }
290
d2fc6be9
SA
291 sample_count_ -= packet_len;
292 start_sample_ += packet_len;
293 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
0fbda3c2
JH
294 }
295
e0e560a5
JH
296 // Zeroing the progress variables indicates completion
297 units_stored_ = unit_count_ = 0;
298
5ccfc97e 299 store_successful();
0fbda3c2
JH
300 progress_updated();
301
8dbbc7f0 302 output_.reset();
c1035a14 303 output_stream_.close();
0fbda3c2
JH
304}
305
306} // pv