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