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