]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
StoreSession: Supply filename to the output module directly
[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
2acdb232 23#include "storesession.hpp"
0fbda3c2 24
adb240c0 25#include <pv/devicemanager.hpp>
f65cd27b 26#include <pv/session.hpp>
2acdb232 27#include <pv/data/logic.hpp>
f3d66e52 28#include <pv/data/logicsegment.hpp>
da30ecb7 29#include <pv/devices/device.hpp>
2acdb232 30#include <pv/view/signal.hpp>
0fbda3c2 31
fe3a1c21 32#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 33
aca64cac
JH
34using boost::shared_lock;
35using boost::shared_mutex;
36
0fbda3c2 37using std::deque;
f9abf97e 38using std::dynamic_pointer_cast;
c1035a14 39using std::ios_base;
3b68d03d 40using std::lock_guard;
0fbda3c2 41using std::make_pair;
c1035a14 42using std::map;
0fbda3c2 43using std::min;
3b68d03d 44using std::mutex;
0fbda3c2
JH
45using std::pair;
46using std::set;
f9abf97e 47using std::shared_ptr;
0fbda3c2 48using std::string;
3b68d03d 49using std::thread;
78b0af3e 50using std::unordered_set;
0fbda3c2
JH
51using std::vector;
52
e93f5538
JH
53using Glib::VariantBase;
54
7223eb62 55using sigrok::ConfigKey;
e8d00928 56using sigrok::Error;
4764bc4d 57using sigrok::OutputFormat;
e8d00928 58
0fbda3c2
JH
59namespace pv {
60
61const size_t StoreSession::BlockSize = 1024 * 1024;
62
63StoreSession::StoreSession(const std::string &file_name,
e93f5538
JH
64 const shared_ptr<OutputFormat> &output_format,
65 const map<string, VariantBase> &options, const Session &session) :
8dbbc7f0 66 file_name_(file_name),
4764bc4d 67 output_format_(output_format),
e93f5538 68 options_(options),
8dbbc7f0
JH
69 session_(session),
70 interrupt_(false),
71 units_stored_(0),
72 unit_count_(0)
0fbda3c2
JH
73{
74}
75
76StoreSession::~StoreSession()
77{
78 wait();
79}
80
e40b2f29 81pair<int, int> StoreSession::progress() const
0fbda3c2 82{
8dbbc7f0 83 return make_pair(units_stored_.load(), unit_count_.load());
0fbda3c2
JH
84}
85
86const QString& StoreSession::error() const
87{
8dbbc7f0
JH
88 lock_guard<mutex> lock(mutex_);
89 return error_;
0fbda3c2
JH
90}
91
92bool StoreSession::start()
93{
94 set< shared_ptr<data::SignalData> > data_set =
8dbbc7f0 95 session_.get_data();
c3a740dd 96
8dbbc7f0 97 shared_lock<shared_mutex> lock(session_.signals_mutex());
78b0af3e
JH
98 const unordered_set< shared_ptr<view::Signal> > &sigs(
99 session_.signals());
0fbda3c2
JH
100
101 // Check we have logic data
102 if (data_set.empty() || sigs.empty()) {
8dbbc7f0 103 error_ = tr("No data to save.");
0fbda3c2
JH
104 return false;
105 }
106
107 if (data_set.size() > 1) {
8dbbc7f0 108 error_ = tr("PulseView currently only has support for "
0fbda3c2
JH
109 "storing a single data stream.");
110 return false;
111 }
112
113 // Get the logic data
0fbda3c2
JH
114 shared_ptr<data::Logic> data;
115 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
8dbbc7f0 116 error_ = tr("PulseView currently only has support for "
0fbda3c2
JH
117 "storing a logic data.");
118 return false;
119 }
120
f3d66e52
JH
121 // Get the segment
122 const deque< shared_ptr<data::LogicSegment> > &segments =
123 data->logic_segments();
0fbda3c2 124
f3d66e52
JH
125 if (segments.empty()) {
126 error_ = tr("No segments to save.");
0fbda3c2
JH
127 return false;
128 }
129
f3d66e52
JH
130 const shared_ptr<data::LogicSegment> segment(segments.front());
131 assert(segment);
0fbda3c2 132
0fbda3c2 133 // Begin storing
e8d00928 134 try {
adb240c0 135 const auto context = session_.device_manager().context();
da30ecb7 136 auto device = session_.device()->device();
c1035a14 137
e93f5538 138 map<string, Glib::VariantBase> options = options_;
c1035a14 139
ad83f8fa
SA
140 output_stream_.open(file_name_, ios_base::binary |
141 ios_base::trunc | ios_base::out);
142
143 output_ = output_format_->create_output(file_name_, device, options);
7223eb62 144 auto meta = context->create_meta_packet(
ff008de6 145 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
f3d66e52 146 segment->samplerate())}});
8dbbc7f0 147 output_->receive(meta);
e8d00928 148 } catch (Error error) {
8dbbc7f0 149 error_ = tr("Error while saving.");
0fbda3c2
JH
150 return false;
151 }
152
f3d66e52 153 thread_ = std::thread(&StoreSession::store_proc, this, segment);
0fbda3c2
JH
154 return true;
155}
156
157void StoreSession::wait()
158{
8dbbc7f0
JH
159 if (thread_.joinable())
160 thread_.join();
0fbda3c2
JH
161}
162
163void StoreSession::cancel()
164{
8dbbc7f0 165 interrupt_ = true;
0fbda3c2
JH
166}
167
f3d66e52 168void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
0fbda3c2 169{
f3d66e52 170 assert(segment);
0fbda3c2 171
e40b2f29
MC
172 uint64_t start_sample = 0, sample_count;
173 unsigned progress_scale = 0;
0fbda3c2
JH
174
175 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
176 uint8_t *const data = new uint8_t[BlockSize];
177 assert(data);
178
f3d66e52 179 const int unit_size = segment->unit_size();
0fbda3c2
JH
180 assert(unit_size != 0);
181
f3d66e52 182 sample_count = segment->get_sample_count();
e40b2f29
MC
183
184 // Qt needs the progress values to fit inside an int. If they would
185 // not, scale the current and max values down until they do.
186 while ((sample_count >> progress_scale) > INT_MAX)
187 progress_scale ++;
188
8dbbc7f0 189 unit_count_ = sample_count >> progress_scale;
0fbda3c2
JH
190
191 const unsigned int samples_per_block = BlockSize / unit_size;
192
8dbbc7f0 193 while (!interrupt_ && start_sample < sample_count)
0fbda3c2
JH
194 {
195 progress_updated();
196
197 const uint64_t end_sample = min(
e40b2f29 198 start_sample + samples_per_block, sample_count);
f3d66e52 199 segment->get_samples(data, start_sample, end_sample);
0fbda3c2 200
e8d00928
ML
201 size_t length = end_sample - start_sample;
202
203 try {
adb240c0 204 const auto context = session_.device_manager().context();
7223eb62 205 auto logic = context->create_logic_packet(data, length, unit_size);
c1035a14
JH
206 const string data = output_->receive(logic);
207 if (output_stream_.is_open())
208 output_stream_ << data;
e8d00928 209 } catch (Error error) {
8dbbc7f0 210 error_ = tr("Error while saving.");
0fbda3c2
JH
211 break;
212 }
213
214 start_sample = end_sample;
8dbbc7f0 215 units_stored_ = start_sample >> progress_scale;
0fbda3c2
JH
216 }
217
e0e560a5
JH
218 // Zeroing the progress variables indicates completion
219 units_stored_ = unit_count_ = 0;
220
0fbda3c2
JH
221 progress_updated();
222
8dbbc7f0 223 output_.reset();
c1035a14 224 output_stream_.close();
7223eb62 225
0fbda3c2
JH
226 delete[] data;
227}
228
229} // pv