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