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