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