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
23 #include "storesession.hpp"
25 #include <pv/session.hpp>
26 #include <pv/data/logic.hpp>
27 #include <pv/data/logicsegment.hpp>
28 #include <pv/view/signal.hpp>
30 #include <libsigrok/libsigrok.hpp>
32 using boost::shared_lock;
33 using boost::shared_mutex;
36 using std::dynamic_pointer_cast;
37 using std::lock_guard;
43 using std::shared_ptr;
48 using sigrok::ConfigKey;
53 const size_t StoreSession::BlockSize = 1024 * 1024;
55 StoreSession::StoreSession(const std::string &file_name,
56 const Session &session) :
57 file_name_(file_name),
65 StoreSession::~StoreSession()
70 pair<int, int> StoreSession::progress() const
72 return make_pair(units_stored_.load(), unit_count_.load());
75 const QString& StoreSession::error() const
77 lock_guard<mutex> lock(mutex_);
81 bool StoreSession::start()
83 set< shared_ptr<data::SignalData> > data_set =
86 shared_lock<shared_mutex> lock(session_.signals_mutex());
87 const vector< shared_ptr<view::Signal> > &sigs(session_.signals());
89 // Check we have logic data
90 if (data_set.empty() || sigs.empty()) {
91 error_ = tr("No data to save.");
95 if (data_set.size() > 1) {
96 error_ = tr("PulseView currently only has support for "
97 "storing a single data stream.");
101 // Get the logic data
102 shared_ptr<data::Logic> data;
103 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
104 error_ = tr("PulseView currently only has support for "
105 "storing a logic data.");
110 const deque< shared_ptr<data::LogicSegment> > &segments =
111 data->logic_segments();
113 if (segments.empty()) {
114 error_ = tr("No segments to save.");
118 const shared_ptr<data::LogicSegment> segment(segments.front());
123 auto context = session_.session()->context();
124 auto output_format = context->output_formats()["srzip"];
125 auto device = session_.device();
126 output_ = output_format->create_output(device,
128 Glib::Variant<Glib::ustring>::create(file_name_)}});
129 auto meta = context->create_meta_packet(
130 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
131 segment->samplerate())}});
132 output_->receive(meta);
133 } catch (Error error) {
134 error_ = tr("Error while saving.");
138 thread_ = std::thread(&StoreSession::store_proc, this, segment);
142 void StoreSession::wait()
144 if (thread_.joinable())
148 void StoreSession::cancel()
153 void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
157 uint64_t start_sample = 0, sample_count;
158 unsigned progress_scale = 0;
160 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
161 uint8_t *const data = new uint8_t[BlockSize];
164 const int unit_size = segment->unit_size();
165 assert(unit_size != 0);
167 sample_count = segment->get_sample_count();
169 // Qt needs the progress values to fit inside an int. If they would
170 // not, scale the current and max values down until they do.
171 while ((sample_count >> progress_scale) > INT_MAX)
174 unit_count_ = sample_count >> progress_scale;
176 const unsigned int samples_per_block = BlockSize / unit_size;
178 while (!interrupt_ && start_sample < sample_count)
182 const uint64_t end_sample = min(
183 start_sample + samples_per_block, sample_count);
184 segment->get_samples(data, start_sample, end_sample);
186 size_t length = end_sample - start_sample;
189 auto context = session_.session()->context();
190 auto logic = context->create_logic_packet(data, length, unit_size);
191 output_->receive(logic);
192 } catch (Error error) {
193 error_ = tr("Error while saving.");
197 start_sample = end_sample;
198 units_stored_ = start_sample >> progress_scale;
201 // Zeroing the progress variables indicates completion
202 units_stored_ = unit_count_ = 0;