]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
Change Glib::Variant<uint64_t> to Glib::Variant<guint64>.
[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
0fbda3c2 32using std::deque;
f9abf97e 33using std::dynamic_pointer_cast;
3b68d03d 34using std::lock_guard;
0fbda3c2
JH
35using std::make_pair;
36using std::min;
3b68d03d 37using std::mutex;
0fbda3c2
JH
38using std::pair;
39using std::set;
f9abf97e 40using std::shared_ptr;
0fbda3c2 41using std::string;
3b68d03d 42using std::thread;
0fbda3c2
JH
43using std::vector;
44
e8d00928
ML
45using sigrok::Error;
46
0fbda3c2
JH
47namespace pv {
48
49const size_t StoreSession::BlockSize = 1024 * 1024;
50
51StoreSession::StoreSession(const std::string &file_name,
52 const SigSession &session) :
53 _file_name(file_name),
54 _session(session),
3b68d03d 55 _interrupt(false),
0fbda3c2
JH
56 _units_stored(0),
57 _unit_count(0)
58{
59}
60
61StoreSession::~StoreSession()
62{
63 wait();
64}
65
e40b2f29 66pair<int, int> StoreSession::progress() const
0fbda3c2 67{
b101c41e 68 return make_pair(_units_stored.load(), _unit_count.load());
0fbda3c2
JH
69}
70
71const QString& StoreSession::error() const
72{
73 lock_guard<mutex> lock(_mutex);
74 return _error;
75}
76
77bool StoreSession::start()
78{
79 set< shared_ptr<data::SignalData> > data_set =
80 _session.get_data();
81 const vector< shared_ptr<view::Signal> > sigs =
82 _session.get_signals();
83
84 // Check we have logic data
85 if (data_set.empty() || sigs.empty()) {
86 _error = tr("No data to save.");
87 return false;
88 }
89
90 if (data_set.size() > 1) {
91 _error = tr("PulseView currently only has support for "
92 "storing a single data stream.");
93 return false;
94 }
95
96 // Get the logic data
97 //shared_ptr<data::SignalData
98 shared_ptr<data::Logic> data;
99 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
100 _error = tr("PulseView currently only has support for "
101 "storing a logic data.");
102 return false;
103 }
104
105 // Get the snapshot
106 const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
107 data->get_snapshots();
108
109 if (snapshots.empty()) {
110 _error = tr("No snapshots to save.");
111 return false;
112 }
113
114 const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
115 assert(snapshot);
116
6ac6242b
ML
117 // Make a list of channels
118 char **const channels = new char*[sigs.size() + 1];
0fbda3c2
JH
119 for (size_t i = 0; i < sigs.size(); i++) {
120 shared_ptr<view::Signal> sig(sigs[i]);
121 assert(sig);
6ac6242b 122 channels[i] = strdup(sig->get_name().toUtf8().constData());
0fbda3c2 123 }
6ac6242b 124 channels[sigs.size()] = NULL;
0fbda3c2
JH
125
126 // Begin storing
e8d00928
ML
127 try {
128 SigSession::_sr_session->begin_save(_file_name);
129 } catch (Error error) {
0fbda3c2
JH
130 _error = tr("Error while saving.");
131 return false;
132 }
133
6ac6242b 134 // Delete the channels array
0fbda3c2 135 for (size_t i = 0; i <= sigs.size(); i++)
6ac6242b
ML
136 free(channels[i]);
137 delete[] channels;
0fbda3c2 138
3b68d03d 139 _thread = std::thread(&StoreSession::store_proc, this, snapshot);
0fbda3c2
JH
140 return true;
141}
142
143void StoreSession::wait()
144{
6d483b8b
MC
145 if (_thread.joinable())
146 _thread.join();
0fbda3c2
JH
147}
148
149void StoreSession::cancel()
150{
3b68d03d 151 _interrupt = true;
0fbda3c2
JH
152}
153
154void StoreSession::store_proc(shared_ptr<data::LogicSnapshot> snapshot)
155{
156 assert(snapshot);
157
e40b2f29
MC
158 uint64_t start_sample = 0, sample_count;
159 unsigned progress_scale = 0;
0fbda3c2
JH
160
161 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
162 uint8_t *const data = new uint8_t[BlockSize];
163 assert(data);
164
165 const int unit_size = snapshot->unit_size();
166 assert(unit_size != 0);
167
e40b2f29
MC
168 sample_count = snapshot->get_sample_count();
169
170 // Qt needs the progress values to fit inside an int. If they would
171 // not, scale the current and max values down until they do.
172 while ((sample_count >> progress_scale) > INT_MAX)
173 progress_scale ++;
174
175 _unit_count = sample_count >> progress_scale;
0fbda3c2
JH
176
177 const unsigned int samples_per_block = BlockSize / unit_size;
178
e40b2f29 179 while (!_interrupt && start_sample < sample_count)
0fbda3c2
JH
180 {
181 progress_updated();
182
183 const uint64_t end_sample = min(
e40b2f29 184 start_sample + samples_per_block, sample_count);
0fbda3c2
JH
185 snapshot->get_samples(data, start_sample, end_sample);
186
e8d00928
ML
187 size_t length = end_sample - start_sample;
188
189 try {
190 SigSession::_sr_session->append(data, length, unit_size);
191 } catch (Error error) {
0fbda3c2
JH
192 _error = tr("Error while saving.");
193 break;
194 }
195
196 start_sample = end_sample;
e40b2f29 197 _units_stored = start_sample >> progress_scale;
0fbda3c2
JH
198 }
199
b101c41e 200 _unit_count = 0;
0fbda3c2
JH
201 progress_updated();
202
203 delete[] data;
204}
205
206} // pv