]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
RowItem: Split appart visual and layout v offsets
[pulseview.git] / pv / storesession.cpp
CommitLineData
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
0fbda3c2
JH
23#include "storesession.h"
24
25#include <pv/sigsession.h>
26#include <pv/data/logic.h>
27#include <pv/data/logicsnapshot.h>
28#include <pv/view/signal.h>
29
e8d00928
ML
30#include <libsigrok/libsigrok.hpp>
31
aca64cac
JH
32using boost::shared_lock;
33using boost::shared_mutex;
34
0fbda3c2 35using std::deque;
f9abf97e 36using std::dynamic_pointer_cast;
3b68d03d 37using std::lock_guard;
0fbda3c2
JH
38using std::make_pair;
39using std::min;
3b68d03d 40using std::mutex;
0fbda3c2
JH
41using std::pair;
42using std::set;
f9abf97e 43using std::shared_ptr;
0fbda3c2 44using std::string;
3b68d03d 45using std::thread;
0fbda3c2
JH
46using std::vector;
47
7223eb62 48using sigrok::ConfigKey;
e8d00928
ML
49using sigrok::Error;
50
0fbda3c2
JH
51namespace pv {
52
53const size_t StoreSession::BlockSize = 1024 * 1024;
54
55StoreSession::StoreSession(const std::string &file_name,
56 const SigSession &session) :
57 _file_name(file_name),
58 _session(session),
3b68d03d 59 _interrupt(false),
0fbda3c2
JH
60 _units_stored(0),
61 _unit_count(0)
62{
63}
64
65StoreSession::~StoreSession()
66{
67 wait();
68}
69
e40b2f29 70pair<int, int> StoreSession::progress() const
0fbda3c2 71{
b101c41e 72 return make_pair(_units_stored.load(), _unit_count.load());
0fbda3c2
JH
73}
74
75const QString& StoreSession::error() const
76{
77 lock_guard<mutex> lock(_mutex);
78 return _error;
79}
80
81bool StoreSession::start()
82{
83 set< shared_ptr<data::SignalData> > data_set =
84 _session.get_data();
c3a740dd 85
aca64cac 86 shared_lock<shared_mutex> lock(_session.signals_mutex());
c3a740dd 87 const vector< shared_ptr<view::Signal> > &sigs(_session.signals());
0fbda3c2
JH
88
89 // Check we have logic data
90 if (data_set.empty() || sigs.empty()) {
91 _error = tr("No data to save.");
92 return false;
93 }
94
95 if (data_set.size() > 1) {
96 _error = tr("PulseView currently only has support for "
97 "storing a single data stream.");
98 return false;
99 }
100
101 // Get the logic data
0fbda3c2
JH
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.");
106 return false;
107 }
108
109 // Get the snapshot
110 const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
111 data->get_snapshots();
112
113 if (snapshots.empty()) {
114 _error = tr("No snapshots to save.");
115 return false;
116 }
117
118 const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
119 assert(snapshot);
120
0fbda3c2 121 // Begin storing
e8d00928 122 try {
1f4caa77 123 auto context = _session.session()->context();
7223eb62 124 auto output_format = context->output_formats()["srzip"];
1f4caa77 125 auto device = _session.device();
7223eb62
ML
126 _output = output_format->create_output(device,
127 {{"filename",
128 Glib::Variant<Glib::ustring>::create(_file_name)}});
129 auto meta = context->create_meta_packet(
130 {{ConfigKey::SAMPLERATE,
131 Glib::Variant<guint64>::create(data->samplerate())}});
132 _output->receive(meta);
e8d00928 133 } catch (Error error) {
0fbda3c2
JH
134 _error = tr("Error while saving.");
135 return false;
136 }
137
3b68d03d 138 _thread = std::thread(&StoreSession::store_proc, this, snapshot);
0fbda3c2
JH
139 return true;
140}
141
142void StoreSession::wait()
143{
6d483b8b
MC
144 if (_thread.joinable())
145 _thread.join();
0fbda3c2
JH
146}
147
148void StoreSession::cancel()
149{
3b68d03d 150 _interrupt = true;
0fbda3c2
JH
151}
152
153void StoreSession::store_proc(shared_ptr<data::LogicSnapshot> snapshot)
154{
155 assert(snapshot);
156
e40b2f29
MC
157 uint64_t start_sample = 0, sample_count;
158 unsigned progress_scale = 0;
0fbda3c2
JH
159
160 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
161 uint8_t *const data = new uint8_t[BlockSize];
162 assert(data);
163
164 const int unit_size = snapshot->unit_size();
165 assert(unit_size != 0);
166
e40b2f29
MC
167 sample_count = snapshot->get_sample_count();
168
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)
172 progress_scale ++;
173
174 _unit_count = sample_count >> progress_scale;
0fbda3c2
JH
175
176 const unsigned int samples_per_block = BlockSize / unit_size;
177
e40b2f29 178 while (!_interrupt && start_sample < sample_count)
0fbda3c2
JH
179 {
180 progress_updated();
181
182 const uint64_t end_sample = min(
e40b2f29 183 start_sample + samples_per_block, sample_count);
0fbda3c2
JH
184 snapshot->get_samples(data, start_sample, end_sample);
185
e8d00928
ML
186 size_t length = end_sample - start_sample;
187
188 try {
1f4caa77 189 auto context = _session.session()->context();
7223eb62
ML
190 auto logic = context->create_logic_packet(data, length, unit_size);
191 _output->receive(logic);
e8d00928 192 } catch (Error error) {
0fbda3c2
JH
193 _error = tr("Error while saving.");
194 break;
195 }
196
197 start_sample = end_sample;
e40b2f29 198 _units_stored = start_sample >> progress_scale;
0fbda3c2
JH
199 }
200
201 progress_updated();
202
7223eb62
ML
203 _output.reset();
204
0fbda3c2
JH
205 delete[] data;
206}
207
208} // pv