]>
Commit | Line | Data |
---|---|---|
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 | ||
2acdb232 | 22 | #include "storesession.hpp" |
0fbda3c2 | 23 | |
8962d7b3 SA |
24 | #include <QSettings> |
25 | ||
c51ae0b4 SA |
26 | #include <pv/data/analog.hpp> |
27 | #include <pv/data/analogsegment.hpp> | |
2acdb232 | 28 | #include <pv/data/logic.hpp> |
f3d66e52 | 29 | #include <pv/data/logicsegment.hpp> |
bf0edd2b | 30 | #include <pv/data/signalbase.hpp> |
aca9aa83 | 31 | #include <pv/devicemanager.hpp> |
da30ecb7 | 32 | #include <pv/devices/device.hpp> |
8962d7b3 | 33 | #include <pv/globalsettings.hpp> |
aca9aa83 | 34 | #include <pv/session.hpp> |
0fbda3c2 | 35 | |
fe3a1c21 | 36 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 37 | |
0fbda3c2 | 38 | using std::deque; |
c1035a14 | 39 | using std::ios_base; |
3b68d03d | 40 | using std::lock_guard; |
0fbda3c2 | 41 | using std::make_pair; |
c1035a14 | 42 | using std::map; |
0fbda3c2 | 43 | using std::min; |
3b68d03d | 44 | using std::mutex; |
0fbda3c2 | 45 | using std::pair; |
f9abf97e | 46 | using std::shared_ptr; |
0fbda3c2 JH |
47 | using std::string; |
48 | using std::vector; | |
49 | ||
e93f5538 JH |
50 | using Glib::VariantBase; |
51 | ||
7223eb62 | 52 | using sigrok::ConfigKey; |
e8d00928 | 53 | using sigrok::Error; |
4764bc4d | 54 | using sigrok::OutputFormat; |
542f1e6c | 55 | using sigrok::OutputFlag; |
e8d00928 | 56 | |
0fbda3c2 JH |
57 | namespace pv { |
58 | ||
9cc264b4 | 59 | const size_t StoreSession::BlockSize = 10 * 1024 * 1024; |
0fbda3c2 | 60 | |
6f925ba9 | 61 | StoreSession::StoreSession(const string &file_name, |
e93f5538 | 62 | const shared_ptr<OutputFormat> &output_format, |
d2fc6be9 | 63 | const map<string, VariantBase> &options, |
6f925ba9 | 64 | const pair<uint64_t, uint64_t> sample_range, |
d2fc6be9 | 65 | const Session &session) : |
8dbbc7f0 | 66 | file_name_(file_name), |
4764bc4d | 67 | output_format_(output_format), |
e93f5538 | 68 | options_(options), |
d2fc6be9 | 69 | sample_range_(sample_range), |
8dbbc7f0 JH |
70 | session_(session), |
71 | interrupt_(false), | |
72 | units_stored_(0), | |
73 | unit_count_(0) | |
0fbda3c2 JH |
74 | { |
75 | } | |
76 | ||
77 | StoreSession::~StoreSession() | |
78 | { | |
79 | wait(); | |
80 | } | |
81 | ||
e40b2f29 | 82 | pair<int, int> StoreSession::progress() const |
0fbda3c2 | 83 | { |
8dbbc7f0 | 84 | return make_pair(units_stored_.load(), unit_count_.load()); |
0fbda3c2 JH |
85 | } |
86 | ||
87 | const QString& StoreSession::error() const | |
88 | { | |
8dbbc7f0 JH |
89 | lock_guard<mutex> lock(mutex_); |
90 | return error_; | |
0fbda3c2 JH |
91 | } |
92 | ||
93 | bool StoreSession::start() | |
94 | { | |
56c4de9a | 95 | const vector< shared_ptr<data::SignalBase> > sigs(session_.signalbases()); |
de6e819c | 96 | |
c51ae0b4 SA |
97 | shared_ptr<data::Segment> any_segment; |
98 | shared_ptr<data::LogicSegment> lsegment; | |
bf0edd2b | 99 | vector< shared_ptr<data::SignalBase> > achannel_list; |
c51ae0b4 | 100 | vector< shared_ptr<data::AnalogSegment> > asegment_list; |
de6e819c | 101 | |
f4ab4b5c | 102 | for (const shared_ptr<data::SignalBase>& signal : sigs) { |
c51ae0b4 SA |
103 | if (!signal->enabled()) |
104 | continue; | |
0fbda3c2 | 105 | |
472a80c5 | 106 | if (signal->type() == data::SignalBase::LogicChannel) { |
c51ae0b4 | 107 | // All logic channels share the same data segments |
47e9e7bb | 108 | shared_ptr<data::Logic> ldata = signal->logic_data(); |
0fbda3c2 | 109 | |
c51ae0b4 SA |
110 | const deque< shared_ptr<data::LogicSegment> > &lsegments = |
111 | ldata->logic_segments(); | |
0fbda3c2 | 112 | |
c51ae0b4 SA |
113 | if (lsegments.empty()) { |
114 | error_ = tr("Can't save logic channel without data."); | |
115 | return false; | |
116 | } | |
0fbda3c2 | 117 | |
c51ae0b4 SA |
118 | lsegment = lsegments.front(); |
119 | any_segment = lsegment; | |
120 | } | |
121 | ||
472a80c5 | 122 | if (signal->type() == data::SignalBase::AnalogChannel) { |
c51ae0b4 | 123 | // Each analog channel has its own segments |
47e9e7bb | 124 | shared_ptr<data::Analog> adata = signal->analog_data(); |
c51ae0b4 SA |
125 | |
126 | const deque< shared_ptr<data::AnalogSegment> > &asegments = | |
127 | adata->analog_segments(); | |
128 | ||
129 | if (asegments.empty()) { | |
130 | error_ = tr("Can't save analog channel without data."); | |
131 | return false; | |
132 | } | |
133 | ||
134 | asegment_list.push_back(asegments.front()); | |
135 | any_segment = asegments.front(); | |
136 | ||
47e9e7bb | 137 | achannel_list.push_back(signal); |
c51ae0b4 | 138 | } |
0fbda3c2 JH |
139 | } |
140 | ||
c51ae0b4 SA |
141 | if (!any_segment) { |
142 | error_ = tr("No channels enabled."); | |
143 | return false; | |
144 | } | |
0fbda3c2 | 145 | |
d2fc6be9 | 146 | // Check whether the user wants to export a certain sample range |
40ec0302 SA |
147 | uint64_t end_sample; |
148 | ||
d2fc6be9 | 149 | if (sample_range_.first == sample_range_.second) { |
7b7ab1f5 | 150 | // No sample range specified, save everything we have |
d2fc6be9 | 151 | start_sample_ = 0; |
c51ae0b4 | 152 | sample_count_ = any_segment->get_sample_count(); |
d2fc6be9 | 153 | } else { |
3e8a7cc6 SA |
154 | if (sample_range_.first > sample_range_.second) { |
155 | start_sample_ = sample_range_.second; | |
40ec0302 SA |
156 | end_sample = min(sample_range_.first, any_segment->get_sample_count()); |
157 | sample_count_ = end_sample - start_sample_; | |
3e8a7cc6 SA |
158 | } else { |
159 | start_sample_ = sample_range_.first; | |
40ec0302 SA |
160 | end_sample = min(sample_range_.second, any_segment->get_sample_count()); |
161 | sample_count_ = end_sample - start_sample_; | |
3e8a7cc6 | 162 | } |
d2fc6be9 SA |
163 | } |
164 | ||
7b7ab1f5 SA |
165 | // Make sure the sample range is valid |
166 | if (start_sample_ > any_segment->get_sample_count()) { | |
167 | error_ = tr("Can't save range without sample data."); | |
168 | return false; | |
169 | } | |
170 | ||
0fbda3c2 | 171 | // Begin storing |
e8d00928 | 172 | try { |
adb240c0 | 173 | const auto context = session_.device_manager().context(); |
da30ecb7 | 174 | auto device = session_.device()->device(); |
c1035a14 | 175 | |
e93f5538 | 176 | map<string, Glib::VariantBase> options = options_; |
c1035a14 | 177 | |
542f1e6c SA |
178 | if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING)) |
179 | output_stream_.open(file_name_, ios_base::binary | | |
180 | ios_base::trunc | ios_base::out); | |
ad83f8fa SA |
181 | |
182 | output_ = output_format_->create_output(file_name_, device, options); | |
7223eb62 | 183 | auto meta = context->create_meta_packet( |
ff008de6 | 184 | {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create( |
c51ae0b4 | 185 | any_segment->samplerate())}}); |
8dbbc7f0 | 186 | output_->receive(meta); |
b30e50f9 | 187 | |
292b6ebc | 188 | Glib::DateTime start_time; |
b30e50f9 SA |
189 | auto header = context->create_header_packet(start_time); |
190 | output_->receive(header); | |
30677c13 | 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); | |
8962d7b3 SA |
198 | |
199 | // Save session setup if we're saving to srzip and the user wants it | |
200 | GlobalSettings settings; | |
201 | bool save_with_setup = settings.value(GlobalSettings::Key_General_SaveWithSetup).toBool(); | |
202 | ||
203 | if ((output_format_->name() == "srzip") && (save_with_setup)) { | |
204 | QString setup_file_name = QString::fromStdString(file_name_); | |
205 | setup_file_name.truncate(setup_file_name.lastIndexOf('.')); | |
206 | setup_file_name.append(".pvs"); | |
207 | ||
208 | QSettings settings_storage(setup_file_name, QSettings::IniFormat); | |
209 | session_.save_setup(settings_storage); | |
210 | } | |
211 | ||
0fbda3c2 JH |
212 | return true; |
213 | } | |
214 | ||
215 | void StoreSession::wait() | |
216 | { | |
8dbbc7f0 JH |
217 | if (thread_.joinable()) |
218 | thread_.join(); | |
0fbda3c2 JH |
219 | } |
220 | ||
221 | void StoreSession::cancel() | |
222 | { | |
8dbbc7f0 | 223 | interrupt_ = true; |
0fbda3c2 JH |
224 | } |
225 | ||
bf0edd2b | 226 | void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_list, |
c51ae0b4 SA |
227 | vector< shared_ptr<data::AnalogSegment> > asegment_list, |
228 | shared_ptr<data::LogicSegment> lsegment) | |
0fbda3c2 | 229 | { |
e40b2f29 | 230 | unsigned progress_scale = 0; |
0fbda3c2 | 231 | |
c51ae0b4 SA |
232 | int aunit_size = 0; |
233 | int lunit_size = 0; | |
234 | unsigned int lsamples_per_block = INT_MAX; | |
235 | unsigned int asamples_per_block = INT_MAX; | |
236 | ||
237 | if (!asegment_list.empty()) { | |
238 | // We assume all analog channels use the sample unit size | |
239 | aunit_size = asegment_list.front()->unit_size(); | |
240 | asamples_per_block = BlockSize / aunit_size; | |
241 | } | |
242 | if (lsegment) { | |
243 | lunit_size = lsegment->unit_size(); | |
244 | lsamples_per_block = BlockSize / lunit_size; | |
245 | } | |
0fbda3c2 | 246 | |
3d79f521 | 247 | // Qt needs the progress values to fit inside an int. If they would |
e40b2f29 | 248 | // not, scale the current and max values down until they do. |
d2fc6be9 | 249 | while ((sample_count_ >> progress_scale) > INT_MAX) |
c063290a | 250 | progress_scale++; |
e40b2f29 | 251 | |
d2fc6be9 | 252 | unit_count_ = sample_count_ >> progress_scale; |
0fbda3c2 | 253 | |
c51ae0b4 | 254 | const unsigned int samples_per_block = |
6f925ba9 | 255 | min(asamples_per_block, lsamples_per_block); |
0fbda3c2 | 256 | |
dd86facc | 257 | const auto context = session_.device_manager().context(); |
2ad82c2e | 258 | while (!interrupt_ && sample_count_) { |
0fbda3c2 JH |
259 | progress_updated(); |
260 | ||
d2fc6be9 | 261 | const uint64_t packet_len = |
6f925ba9 | 262 | min((uint64_t)samples_per_block, sample_count_); |
d2fc6be9 | 263 | |
e8d00928 | 264 | try { |
c51ae0b4 | 265 | for (unsigned int i = 0; i < achannel_list.size(); i++) { |
bf0edd2b | 266 | shared_ptr<sigrok::Channel> achannel = (achannel_list.at(i))->channel(); |
c51ae0b4 SA |
267 | shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i); |
268 | ||
b82243f7 SA |
269 | float *adata = new float[packet_len]; |
270 | asegment->get_samples(start_sample_, start_sample_ + packet_len, adata); | |
c51ae0b4 | 271 | |
c51ae0b4 SA |
272 | auto analog = context->create_analog_packet( |
273 | vector<shared_ptr<sigrok::Channel> >{achannel}, | |
274 | (float *)adata, packet_len, | |
275 | sigrok::Quantity::VOLTAGE, sigrok::Unit::VOLT, | |
276 | vector<const sigrok::QuantityFlag *>()); | |
277 | const string adata_str = output_->receive(analog); | |
278 | ||
279 | if (output_stream_.is_open()) | |
280 | output_stream_ << adata_str; | |
281 | ||
282 | delete[] adata; | |
283 | } | |
284 | ||
285 | if (lsegment) { | |
b82243f7 SA |
286 | const size_t data_size = packet_len * lunit_size; |
287 | uint8_t* ldata = new uint8_t[data_size]; | |
288 | lsegment->get_samples(start_sample_, start_sample_ + packet_len, ldata); | |
c51ae0b4 | 289 | |
b82243f7 | 290 | auto logic = context->create_logic_packet((void*)ldata, data_size, lunit_size); |
c51ae0b4 SA |
291 | const string ldata_str = output_->receive(logic); |
292 | ||
293 | if (output_stream_.is_open()) | |
294 | output_stream_ << ldata_str; | |
038a1427 SA |
295 | |
296 | delete[] ldata; | |
c51ae0b4 | 297 | } |
30677c13 | 298 | } catch (Error& error) { |
c51ae0b4 | 299 | error_ = tr("Error while saving: ") + error.what(); |
0fbda3c2 JH |
300 | break; |
301 | } | |
302 | ||
d2fc6be9 SA |
303 | sample_count_ -= packet_len; |
304 | start_sample_ += packet_len; | |
305 | units_stored_ = unit_count_ - (sample_count_ >> progress_scale); | |
0fbda3c2 JH |
306 | } |
307 | ||
f9319755 R |
308 | try { |
309 | auto dfend = context->create_end_packet(); | |
310 | const string ldata_str = output_->receive(dfend); | |
311 | if (output_stream_.is_open()) | |
312 | output_stream_ << ldata_str; | |
313 | } catch (Error& error) { | |
314 | error_ = tr("Error while saving: ") + error.what(); | |
315 | } | |
dd86facc | 316 | |
e0e560a5 JH |
317 | // Zeroing the progress variables indicates completion |
318 | units_stored_ = unit_count_ = 0; | |
319 | ||
5ccfc97e | 320 | store_successful(); |
0fbda3c2 JH |
321 | progress_updated(); |
322 | ||
8dbbc7f0 | 323 | output_.reset(); |
c1035a14 | 324 | output_stream_.close(); |
0fbda3c2 JH |
325 | } |
326 | ||
870ea3db | 327 | } // namespace pv |