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