2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
28 #include <boost/thread/locks.hpp>
29 #include <boost/thread/shared_mutex.hpp>
31 #include "storesession.hpp"
33 #include <pv/devicemanager.hpp>
34 #include <pv/session.hpp>
35 #include <pv/data/logic.hpp>
36 #include <pv/data/logicsegment.hpp>
37 #include <pv/devices/device.hpp>
38 #include <pv/view/signal.hpp>
40 #include <libsigrokcxx/libsigrokcxx.hpp>
42 using boost::shared_lock;
43 using boost::shared_mutex;
46 using std::dynamic_pointer_cast;
48 using std::lock_guard;
55 using std::shared_ptr;
58 using std::unordered_set;
61 using Glib::VariantBase;
63 using sigrok::ConfigKey;
65 using sigrok::OutputFormat;
66 using sigrok::OutputFlag;
70 const size_t StoreSession::BlockSize = 1024 * 1024;
72 StoreSession::StoreSession(const std::string &file_name,
73 const shared_ptr<OutputFormat> &output_format,
74 const map<string, VariantBase> &options,
75 const std::pair<uint64_t, uint64_t> sample_range,
76 const Session &session) :
77 file_name_(file_name),
78 output_format_(output_format),
80 sample_range_(sample_range),
88 StoreSession::~StoreSession()
93 pair<int, int> StoreSession::progress() const
95 return make_pair(units_stored_.load(), unit_count_.load());
98 const QString& StoreSession::error() const
100 lock_guard<mutex> lock(mutex_);
104 bool StoreSession::start()
106 const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals());
108 // Add enabled channels to the data set
109 set< shared_ptr<data::SignalData> > data_set;
111 for (shared_ptr<view::Signal> signal : sigs)
112 if (signal->enabled())
113 data_set.insert(signal->data());
115 // Check we have logic data
116 if (data_set.empty() || sigs.empty()) {
117 error_ = tr("No data to save.");
121 if (data_set.size() > 1) {
122 error_ = tr("PulseView currently only has support for "
123 "storing a single data stream.");
127 // Get the logic data
128 shared_ptr<data::Logic> data;
129 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
130 error_ = tr("PulseView currently only has support for "
131 "storing logic data.");
136 const deque< shared_ptr<data::LogicSegment> > &segments =
137 data->logic_segments();
139 if (segments.empty()) {
140 error_ = tr("No segments to save.");
144 const shared_ptr<data::LogicSegment> segment(segments.front());
147 // Check whether the user wants to export a certain sample range
148 if (sample_range_.first == sample_range_.second) {
150 sample_count_ = segment->get_sample_count();
152 if (sample_range_.first > sample_range_.second) {
153 start_sample_ = sample_range_.second;
154 sample_count_ = sample_range_.first - sample_range_.second;
156 start_sample_ = sample_range_.first;
157 sample_count_ = sample_range_.second - sample_range_.first;
163 const auto context = session_.device_manager().context();
164 auto device = session_.device()->device();
166 map<string, Glib::VariantBase> options = options_;
168 if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
169 output_stream_.open(file_name_, ios_base::binary |
170 ios_base::trunc | ios_base::out);
172 output_ = output_format_->create_output(file_name_, device, options);
173 auto meta = context->create_meta_packet(
174 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
175 segment->samplerate())}});
176 output_->receive(meta);
177 } catch (Error error) {
178 error_ = tr("Error while saving.");
182 thread_ = std::thread(&StoreSession::store_proc, this, segment);
186 void StoreSession::wait()
188 if (thread_.joinable())
192 void StoreSession::cancel()
197 void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
201 unsigned progress_scale = 0;
203 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
204 uint8_t *const data = new uint8_t[BlockSize];
207 const int unit_size = segment->unit_size();
208 assert(unit_size != 0);
210 // Qt needs the progress values to fit inside an int. If they would
211 // not, scale the current and max values down until they do.
212 while ((sample_count_ >> progress_scale) > INT_MAX)
215 unit_count_ = sample_count_ >> progress_scale;
217 const unsigned int samples_per_block = BlockSize / unit_size;
219 while (!interrupt_ && sample_count_) {
222 const uint64_t packet_len =
223 std::min((uint64_t)samples_per_block, sample_count_);
225 segment->get_samples(data, start_sample_, start_sample_ + packet_len);
227 size_t length = packet_len * unit_size;
230 const auto context = session_.device_manager().context();
231 auto logic = context->create_logic_packet(data, length, unit_size);
232 const string data = output_->receive(logic);
233 if (output_stream_.is_open())
234 output_stream_ << data;
235 } catch (Error error) {
236 error_ = tr("Error while saving.");
240 sample_count_ -= packet_len;
241 start_sample_ += packet_len;
242 units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
245 // Zeroing the progress variables indicates completion
246 units_stored_ = unit_count_ = 0;
251 output_stream_.close();