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