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