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.h"
25 #include <pv/sigsession.h>
26 #include <pv/data/logic.h>
27 #include <pv/data/logicsnapshot.h>
28 #include <pv/view/signal.h>
30 #include <libsigrok/libsigrok.hpp>
33 using std::dynamic_pointer_cast;
34 using std::lock_guard;
40 using std::shared_ptr;
45 using sigrok::ConfigKey;
50 const size_t StoreSession::BlockSize = 1024 * 1024;
52 StoreSession::StoreSession(const std::string &file_name,
53 const SigSession &session) :
54 _file_name(file_name),
62 StoreSession::~StoreSession()
67 pair<int, int> StoreSession::progress() const
69 return make_pair(_units_stored.load(), _unit_count.load());
72 const QString& StoreSession::error() const
74 lock_guard<mutex> lock(_mutex);
78 bool StoreSession::start()
80 set< shared_ptr<data::SignalData> > data_set =
83 lock_guard<mutex> lock(_session.signals_mutex());
84 const vector< shared_ptr<view::Signal> > &sigs(_session.signals());
86 // Check we have logic data
87 if (data_set.empty() || sigs.empty()) {
88 _error = tr("No data to save.");
92 if (data_set.size() > 1) {
93 _error = tr("PulseView currently only has support for "
94 "storing a single data stream.");
99 shared_ptr<data::Logic> data;
100 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
101 _error = tr("PulseView currently only has support for "
102 "storing a logic data.");
107 const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
108 data->get_snapshots();
110 if (snapshots.empty()) {
111 _error = tr("No snapshots to save.");
115 const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
120 auto context = _session.session()->context();
121 auto output_format = context->output_formats()["srzip"];
122 auto device = _session.device();
123 _output = output_format->create_output(device,
125 Glib::Variant<Glib::ustring>::create(_file_name)}});
126 auto meta = context->create_meta_packet(
127 {{ConfigKey::SAMPLERATE,
128 Glib::Variant<guint64>::create(data->samplerate())}});
129 _output->receive(meta);
130 } catch (Error error) {
131 _error = tr("Error while saving.");
135 _thread = std::thread(&StoreSession::store_proc, this, snapshot);
139 void StoreSession::wait()
141 if (_thread.joinable())
145 void StoreSession::cancel()
150 void StoreSession::store_proc(shared_ptr<data::LogicSnapshot> snapshot)
154 uint64_t start_sample = 0, sample_count;
155 unsigned progress_scale = 0;
157 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
158 uint8_t *const data = new uint8_t[BlockSize];
161 const int unit_size = snapshot->unit_size();
162 assert(unit_size != 0);
164 sample_count = snapshot->get_sample_count();
166 // Qt needs the progress values to fit inside an int. If they would
167 // not, scale the current and max values down until they do.
168 while ((sample_count >> progress_scale) > INT_MAX)
171 _unit_count = sample_count >> progress_scale;
173 const unsigned int samples_per_block = BlockSize / unit_size;
175 while (!_interrupt && start_sample < sample_count)
179 const uint64_t end_sample = min(
180 start_sample + samples_per_block, sample_count);
181 snapshot->get_samples(data, start_sample, end_sample);
183 size_t length = end_sample - start_sample;
186 auto context = _session.session()->context();
187 auto logic = context->create_logic_packet(data, length, unit_size);
188 _output->receive(logic);
189 } catch (Error error) {
190 _error = tr("Error while saving.");
194 start_sample = end_sample;
195 _units_stored = start_sample >> progress_scale;