]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
SigSession: Replaced do { } while(0) with { }
[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
7223eb62 45using sigrok::ConfigKey;
e8d00928
ML
46using sigrok::Error;
47
0fbda3c2
JH
48namespace pv {
49
50const size_t StoreSession::BlockSize = 1024 * 1024;
51
52StoreSession::StoreSession(const std::string &file_name,
53 const SigSession &session) :
54 _file_name(file_name),
55 _session(session),
3b68d03d 56 _interrupt(false),
0fbda3c2
JH
57 _units_stored(0),
58 _unit_count(0)
59{
60}
61
62StoreSession::~StoreSession()
63{
64 wait();
65}
66
e40b2f29 67pair<int, int> StoreSession::progress() const
0fbda3c2 68{
b101c41e 69 return make_pair(_units_stored.load(), _unit_count.load());
0fbda3c2
JH
70}
71
72const QString& StoreSession::error() const
73{
74 lock_guard<mutex> lock(_mutex);
75 return _error;
76}
77
78bool StoreSession::start()
79{
80 set< shared_ptr<data::SignalData> > data_set =
81 _session.get_data();
82 const vector< shared_ptr<view::Signal> > sigs =
83 _session.get_signals();
84
85 // Check we have logic data
86 if (data_set.empty() || sigs.empty()) {
87 _error = tr("No data to save.");
88 return false;
89 }
90
91 if (data_set.size() > 1) {
92 _error = tr("PulseView currently only has support for "
93 "storing a single data stream.");
94 return false;
95 }
96
97 // Get the logic data
98 //shared_ptr<data::SignalData
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.");
103 return false;
104 }
105
106 // Get the snapshot
107 const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
108 data->get_snapshots();
109
110 if (snapshots.empty()) {
111 _error = tr("No snapshots to save.");
112 return false;
113 }
114
115 const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
116 assert(snapshot);
117
0fbda3c2 118 // Begin storing
e8d00928 119 try {
7223eb62
ML
120 auto context = _session._sr_session->context();
121 auto output_format = context->output_formats()["srzip"];
122 auto device = _session.get_device();
123 _output = output_format->create_output(device,
124 {{"filename",
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);
e8d00928 130 } catch (Error error) {
0fbda3c2
JH
131 _error = tr("Error while saving.");
132 return false;
133 }
134
3b68d03d 135 _thread = std::thread(&StoreSession::store_proc, this, snapshot);
0fbda3c2
JH
136 return true;
137}
138
139void StoreSession::wait()
140{
6d483b8b
MC
141 if (_thread.joinable())
142 _thread.join();
0fbda3c2
JH
143}
144
145void StoreSession::cancel()
146{
3b68d03d 147 _interrupt = true;
0fbda3c2
JH
148}
149
150void StoreSession::store_proc(shared_ptr<data::LogicSnapshot> snapshot)
151{
152 assert(snapshot);
153
e40b2f29
MC
154 uint64_t start_sample = 0, sample_count;
155 unsigned progress_scale = 0;
0fbda3c2
JH
156
157 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
158 uint8_t *const data = new uint8_t[BlockSize];
159 assert(data);
160
161 const int unit_size = snapshot->unit_size();
162 assert(unit_size != 0);
163
e40b2f29
MC
164 sample_count = snapshot->get_sample_count();
165
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)
169 progress_scale ++;
170
171 _unit_count = sample_count >> progress_scale;
0fbda3c2
JH
172
173 const unsigned int samples_per_block = BlockSize / unit_size;
174
e40b2f29 175 while (!_interrupt && start_sample < sample_count)
0fbda3c2
JH
176 {
177 progress_updated();
178
179 const uint64_t end_sample = min(
e40b2f29 180 start_sample + samples_per_block, sample_count);
0fbda3c2
JH
181 snapshot->get_samples(data, start_sample, end_sample);
182
e8d00928
ML
183 size_t length = end_sample - start_sample;
184
185 try {
7223eb62
ML
186 auto context = _session._sr_session->context();
187 auto logic = context->create_logic_packet(data, length, unit_size);
188 _output->receive(logic);
e8d00928 189 } catch (Error error) {
0fbda3c2
JH
190 _error = tr("Error while saving.");
191 break;
192 }
193
194 start_sample = end_sample;
e40b2f29 195 _units_stored = start_sample >> progress_scale;
0fbda3c2
JH
196 }
197
198 progress_updated();
199
7223eb62
ML
200 _output.reset();
201
0fbda3c2
JH
202 delete[] data;
203}
204
205} // pv