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