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