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