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