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