]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
StoreSession: Observe proper range order
[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>
2acdb232 35#include <pv/data/logic.hpp>
f3d66e52 36#include <pv/data/logicsegment.hpp>
da30ecb7 37#include <pv/devices/device.hpp>
2acdb232 38#include <pv/view/signal.hpp>
0fbda3c2 39
fe3a1c21 40#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 41
aca64cac
JH
42using boost::shared_lock;
43using boost::shared_mutex;
44
0fbda3c2 45using std::deque;
f9abf97e 46using std::dynamic_pointer_cast;
c1035a14 47using std::ios_base;
3b68d03d 48using std::lock_guard;
0fbda3c2 49using std::make_pair;
c1035a14 50using std::map;
0fbda3c2 51using std::min;
3b68d03d 52using std::mutex;
0fbda3c2
JH
53using std::pair;
54using std::set;
f9abf97e 55using std::shared_ptr;
0fbda3c2 56using std::string;
3b68d03d 57using std::thread;
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
70const size_t StoreSession::BlockSize = 1024 * 1024;
71
72StoreSession::StoreSession(const std::string &file_name,
e93f5538 73 const shared_ptr<OutputFormat> &output_format,
d2fc6be9
SA
74 const map<string, VariantBase> &options,
75 const std::pair<uint64_t, uint64_t> sample_range,
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{
bf914698 106 const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals());
de6e819c
SA
107
108 // Add enabled channels to the data set
109 set< shared_ptr<data::SignalData> > data_set;
110
111 for (shared_ptr<view::Signal> signal : sigs)
112 if (signal->enabled())
113 data_set.insert(signal->data());
0fbda3c2
JH
114
115 // Check we have logic data
116 if (data_set.empty() || sigs.empty()) {
8dbbc7f0 117 error_ = tr("No data to save.");
0fbda3c2
JH
118 return false;
119 }
120
121 if (data_set.size() > 1) {
8dbbc7f0 122 error_ = tr("PulseView currently only has support for "
0fbda3c2
JH
123 "storing a single data stream.");
124 return false;
125 }
126
127 // Get the logic data
0fbda3c2
JH
128 shared_ptr<data::Logic> data;
129 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
8dbbc7f0 130 error_ = tr("PulseView currently only has support for "
de6e819c 131 "storing logic data.");
0fbda3c2
JH
132 return false;
133 }
134
f3d66e52
JH
135 // Get the segment
136 const deque< shared_ptr<data::LogicSegment> > &segments =
137 data->logic_segments();
0fbda3c2 138
f3d66e52
JH
139 if (segments.empty()) {
140 error_ = tr("No segments to save.");
0fbda3c2
JH
141 return false;
142 }
143
f3d66e52
JH
144 const shared_ptr<data::LogicSegment> segment(segments.front());
145 assert(segment);
0fbda3c2 146
d2fc6be9
SA
147 // Check whether the user wants to export a certain sample range
148 if (sample_range_.first == sample_range_.second) {
149 start_sample_ = 0;
150 sample_count_ = segment->get_sample_count();
151 } else {
3e8a7cc6
SA
152 if (sample_range_.first > sample_range_.second) {
153 start_sample_ = sample_range_.second;
154 sample_count_ = sample_range_.first - sample_range_.second;
155 } else {
156 start_sample_ = sample_range_.first;
157 sample_count_ = sample_range_.second - sample_range_.first;
158 }
d2fc6be9
SA
159 }
160
0fbda3c2 161 // Begin storing
e8d00928 162 try {
adb240c0 163 const auto context = session_.device_manager().context();
da30ecb7 164 auto device = session_.device()->device();
c1035a14 165
e93f5538 166 map<string, Glib::VariantBase> options = options_;
c1035a14 167
542f1e6c
SA
168 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
169 output_stream_.open(file_name_, ios_base::binary |
170 ios_base::trunc | ios_base::out);
ad83f8fa
SA
171
172 output_ = output_format_->create_output(file_name_, device, options);
7223eb62 173 auto meta = context->create_meta_packet(
ff008de6 174 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
f3d66e52 175 segment->samplerate())}});
8dbbc7f0 176 output_->receive(meta);
e8d00928 177 } catch (Error error) {
8dbbc7f0 178 error_ = tr("Error while saving.");
0fbda3c2
JH
179 return false;
180 }
181
f3d66e52 182 thread_ = std::thread(&StoreSession::store_proc, this, segment);
0fbda3c2
JH
183 return true;
184}
185
186void StoreSession::wait()
187{
8dbbc7f0
JH
188 if (thread_.joinable())
189 thread_.join();
0fbda3c2
JH
190}
191
192void StoreSession::cancel()
193{
8dbbc7f0 194 interrupt_ = true;
0fbda3c2
JH
195}
196
f3d66e52 197void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
0fbda3c2 198{
f3d66e52 199 assert(segment);
0fbda3c2 200
e40b2f29 201 unsigned progress_scale = 0;
0fbda3c2
JH
202
203 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
204 uint8_t *const data = new uint8_t[BlockSize];
205 assert(data);
206
f3d66e52 207 const int unit_size = segment->unit_size();
0fbda3c2
JH
208 assert(unit_size != 0);
209
3d79f521 210 // Qt needs the progress values to fit inside an int. If they would
e40b2f29 211 // not, scale the current and max values down until they do.
d2fc6be9 212 while ((sample_count_ >> progress_scale) > INT_MAX)
e40b2f29
MC
213 progress_scale ++;
214
d2fc6be9 215 unit_count_ = sample_count_ >> progress_scale;
0fbda3c2
JH
216
217 const unsigned int samples_per_block = BlockSize / unit_size;
218
d2fc6be9 219 while (!interrupt_ && sample_count_)
0fbda3c2
JH
220 {
221 progress_updated();
222
d2fc6be9
SA
223 const uint64_t packet_len =
224 std::min((uint64_t)samples_per_block, sample_count_);
225
226 segment->get_samples(data, start_sample_, start_sample_ + packet_len);
0fbda3c2 227
d2fc6be9 228 size_t length = packet_len * unit_size;
e8d00928
ML
229
230 try {
adb240c0 231 const auto context = session_.device_manager().context();
7223eb62 232 auto logic = context->create_logic_packet(data, length, unit_size);
c1035a14
JH
233 const string data = output_->receive(logic);
234 if (output_stream_.is_open())
235 output_stream_ << data;
e8d00928 236 } catch (Error error) {
8dbbc7f0 237 error_ = tr("Error while saving.");
0fbda3c2
JH
238 break;
239 }
240
d2fc6be9
SA
241 sample_count_ -= packet_len;
242 start_sample_ += packet_len;
243 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
0fbda3c2
JH
244 }
245
e0e560a5
JH
246 // Zeroing the progress variables indicates completion
247 units_stored_ = unit_count_ = 0;
248
0fbda3c2
JH
249 progress_updated();
250
8dbbc7f0 251 output_.reset();
c1035a14 252 output_stream_.close();
7223eb62 253
0fbda3c2
JH
254 delete[] data;
255}
256
257} // pv