]>
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/logic.hpp> | |
36 | #include <pv/data/logicsegment.hpp> | |
37 | #include <pv/devices/device.hpp> | |
38 | #include <pv/view/signal.hpp> | |
39 | ||
40 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
41 | ||
42 | using boost::shared_lock; | |
43 | using boost::shared_mutex; | |
44 | ||
45 | using std::deque; | |
46 | using std::dynamic_pointer_cast; | |
47 | using std::ios_base; | |
48 | using std::lock_guard; | |
49 | using std::make_pair; | |
50 | using std::map; | |
51 | using std::min; | |
52 | using std::mutex; | |
53 | using std::pair; | |
54 | using std::set; | |
55 | using std::shared_ptr; | |
56 | using std::string; | |
57 | using std::thread; | |
58 | using std::unordered_set; | |
59 | using std::vector; | |
60 | ||
61 | using Glib::VariantBase; | |
62 | ||
63 | using sigrok::ConfigKey; | |
64 | using sigrok::Error; | |
65 | using sigrok::OutputFormat; | |
66 | using sigrok::OutputFlag; | |
67 | ||
68 | namespace pv { | |
69 | ||
70 | const size_t StoreSession::BlockSize = 1024 * 1024; | |
71 | ||
72 | StoreSession::StoreSession(const std::string &file_name, | |
73 | const shared_ptr<OutputFormat> &output_format, | |
74 | const map<string, VariantBase> &options, const Session &session) : | |
75 | file_name_(file_name), | |
76 | output_format_(output_format), | |
77 | options_(options), | |
78 | session_(session), | |
79 | interrupt_(false), | |
80 | units_stored_(0), | |
81 | unit_count_(0) | |
82 | { | |
83 | } | |
84 | ||
85 | StoreSession::~StoreSession() | |
86 | { | |
87 | wait(); | |
88 | } | |
89 | ||
90 | pair<int, int> StoreSession::progress() const | |
91 | { | |
92 | return make_pair(units_stored_.load(), unit_count_.load()); | |
93 | } | |
94 | ||
95 | const QString& StoreSession::error() const | |
96 | { | |
97 | lock_guard<mutex> lock(mutex_); | |
98 | return error_; | |
99 | } | |
100 | ||
101 | bool StoreSession::start() | |
102 | { | |
103 | const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals()); | |
104 | ||
105 | // Add enabled channels to the data set | |
106 | set< shared_ptr<data::SignalData> > data_set; | |
107 | ||
108 | for (shared_ptr<view::Signal> signal : sigs) | |
109 | if (signal->enabled()) | |
110 | data_set.insert(signal->data()); | |
111 | ||
112 | // Check we have logic data | |
113 | if (data_set.empty() || sigs.empty()) { | |
114 | error_ = tr("No data to save."); | |
115 | return false; | |
116 | } | |
117 | ||
118 | if (data_set.size() > 1) { | |
119 | error_ = tr("PulseView currently only has support for " | |
120 | "storing a single data stream."); | |
121 | return false; | |
122 | } | |
123 | ||
124 | // Get the logic data | |
125 | shared_ptr<data::Logic> data; | |
126 | if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) { | |
127 | error_ = tr("PulseView currently only has support for " | |
128 | "storing logic data."); | |
129 | return false; | |
130 | } | |
131 | ||
132 | // Get the segment | |
133 | const deque< shared_ptr<data::LogicSegment> > &segments = | |
134 | data->logic_segments(); | |
135 | ||
136 | if (segments.empty()) { | |
137 | error_ = tr("No segments to save."); | |
138 | return false; | |
139 | } | |
140 | ||
141 | const shared_ptr<data::LogicSegment> segment(segments.front()); | |
142 | assert(segment); | |
143 | ||
144 | // Begin storing | |
145 | try { | |
146 | const auto context = session_.device_manager().context(); | |
147 | auto device = session_.device()->device(); | |
148 | ||
149 | map<string, Glib::VariantBase> options = options_; | |
150 | ||
151 | if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING)) | |
152 | output_stream_.open(file_name_, ios_base::binary | | |
153 | ios_base::trunc | ios_base::out); | |
154 | ||
155 | output_ = output_format_->create_output(file_name_, device, options); | |
156 | auto meta = context->create_meta_packet( | |
157 | {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create( | |
158 | segment->samplerate())}}); | |
159 | output_->receive(meta); | |
160 | } catch (Error error) { | |
161 | error_ = tr("Error while saving."); | |
162 | return false; | |
163 | } | |
164 | ||
165 | thread_ = std::thread(&StoreSession::store_proc, this, segment); | |
166 | return true; | |
167 | } | |
168 | ||
169 | void StoreSession::wait() | |
170 | { | |
171 | if (thread_.joinable()) | |
172 | thread_.join(); | |
173 | } | |
174 | ||
175 | void StoreSession::cancel() | |
176 | { | |
177 | interrupt_ = true; | |
178 | } | |
179 | ||
180 | void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment) | |
181 | { | |
182 | assert(segment); | |
183 | ||
184 | uint64_t start_sample = 0, sample_count; | |
185 | unsigned progress_scale = 0; | |
186 | ||
187 | /// TODO: Wrap this in a std::unique_ptr when we transition to C++11 | |
188 | uint8_t *const data = new uint8_t[BlockSize]; | |
189 | assert(data); | |
190 | ||
191 | const int unit_size = segment->unit_size(); | |
192 | assert(unit_size != 0); | |
193 | ||
194 | sample_count = segment->get_sample_count(); | |
195 | ||
196 | // Qt needs the progress values to fit inside an int. If they would | |
197 | // not, scale the current and max values down until they do. | |
198 | while ((sample_count >> progress_scale) > INT_MAX) | |
199 | progress_scale ++; | |
200 | ||
201 | unit_count_ = sample_count >> progress_scale; | |
202 | ||
203 | const unsigned int samples_per_block = BlockSize / unit_size; | |
204 | ||
205 | while (!interrupt_ && start_sample < sample_count) | |
206 | { | |
207 | progress_updated(); | |
208 | ||
209 | const uint64_t end_sample = min( | |
210 | start_sample + samples_per_block, sample_count); | |
211 | segment->get_samples(data, start_sample, end_sample); | |
212 | ||
213 | size_t length = (end_sample - start_sample) * unit_size; | |
214 | ||
215 | try { | |
216 | const auto context = session_.device_manager().context(); | |
217 | auto logic = context->create_logic_packet(data, length, unit_size); | |
218 | const string data = output_->receive(logic); | |
219 | if (output_stream_.is_open()) | |
220 | output_stream_ << data; | |
221 | } catch (Error error) { | |
222 | error_ = tr("Error while saving."); | |
223 | break; | |
224 | } | |
225 | ||
226 | start_sample = end_sample; | |
227 | units_stored_ = start_sample >> progress_scale; | |
228 | } | |
229 | ||
230 | // Zeroing the progress variables indicates completion | |
231 | units_stored_ = unit_count_ = 0; | |
232 | ||
233 | progress_updated(); | |
234 | ||
235 | output_.reset(); | |
236 | output_stream_.close(); | |
237 | ||
238 | delete[] data; | |
239 | } | |
240 | ||
241 | } // pv |