]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
StoreSession: Added an OutputFormat parameter
[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;
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 49using sigrok::Error;
4764bc4d 50using sigrok::OutputFormat;
e8d00928 51
0fbda3c2
JH
52namespace pv {
53
54const size_t StoreSession::BlockSize = 1024 * 1024;
55
56StoreSession::StoreSession(const std::string &file_name,
4764bc4d 57 const shared_ptr<OutputFormat> &output_format, const Session &session) :
8dbbc7f0 58 file_name_(file_name),
4764bc4d 59 output_format_(output_format),
8dbbc7f0
JH
60 session_(session),
61 interrupt_(false),
62 units_stored_(0),
63 unit_count_(0)
0fbda3c2
JH
64{
65}
66
67StoreSession::~StoreSession()
68{
69 wait();
70}
71
e40b2f29 72pair<int, int> StoreSession::progress() const
0fbda3c2 73{
8dbbc7f0 74 return make_pair(units_stored_.load(), unit_count_.load());
0fbda3c2
JH
75}
76
77const QString& StoreSession::error() const
78{
8dbbc7f0
JH
79 lock_guard<mutex> lock(mutex_);
80 return error_;
0fbda3c2
JH
81}
82
83bool StoreSession::start()
84{
85 set< shared_ptr<data::SignalData> > data_set =
8dbbc7f0 86 session_.get_data();
c3a740dd 87
8dbbc7f0
JH
88 shared_lock<shared_mutex> lock(session_.signals_mutex());
89 const vector< shared_ptr<view::Signal> > &sigs(session_.signals());
0fbda3c2
JH
90
91 // Check we have logic data
92 if (data_set.empty() || sigs.empty()) {
8dbbc7f0 93 error_ = tr("No data to save.");
0fbda3c2
JH
94 return false;
95 }
96
97 if (data_set.size() > 1) {
8dbbc7f0 98 error_ = tr("PulseView currently only has support for "
0fbda3c2
JH
99 "storing a single data stream.");
100 return false;
101 }
102
103 // Get the logic data
0fbda3c2
JH
104 shared_ptr<data::Logic> data;
105 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
8dbbc7f0 106 error_ = tr("PulseView currently only has support for "
0fbda3c2
JH
107 "storing a logic data.");
108 return false;
109 }
110
f3d66e52
JH
111 // Get the segment
112 const deque< shared_ptr<data::LogicSegment> > &segments =
113 data->logic_segments();
0fbda3c2 114
f3d66e52
JH
115 if (segments.empty()) {
116 error_ = tr("No segments to save.");
0fbda3c2
JH
117 return false;
118 }
119
f3d66e52
JH
120 const shared_ptr<data::LogicSegment> segment(segments.front());
121 assert(segment);
0fbda3c2 122
0fbda3c2 123 // Begin storing
e8d00928 124 try {
8dbbc7f0 125 auto context = session_.session()->context();
8dbbc7f0 126 auto device = session_.device();
4764bc4d 127 output_ = output_format_->create_output(device,
7223eb62 128 {{"filename",
8dbbc7f0 129 Glib::Variant<Glib::ustring>::create(file_name_)}});
7223eb62 130 auto meta = context->create_meta_packet(
ff008de6 131 {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
f3d66e52 132 segment->samplerate())}});
8dbbc7f0 133 output_->receive(meta);
e8d00928 134 } catch (Error error) {
8dbbc7f0 135 error_ = tr("Error while saving.");
0fbda3c2
JH
136 return false;
137 }
138
f3d66e52 139 thread_ = std::thread(&StoreSession::store_proc, this, segment);
0fbda3c2
JH
140 return true;
141}
142
143void StoreSession::wait()
144{
8dbbc7f0
JH
145 if (thread_.joinable())
146 thread_.join();
0fbda3c2
JH
147}
148
149void StoreSession::cancel()
150{
8dbbc7f0 151 interrupt_ = true;
0fbda3c2
JH
152}
153
f3d66e52 154void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
0fbda3c2 155{
f3d66e52 156 assert(segment);
0fbda3c2 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
f3d66e52 165 const int unit_size = segment->unit_size();
0fbda3c2
JH
166 assert(unit_size != 0);
167
f3d66e52 168 sample_count = segment->get_sample_count();
e40b2f29
MC
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
8dbbc7f0 175 unit_count_ = sample_count >> progress_scale;
0fbda3c2
JH
176
177 const unsigned int samples_per_block = BlockSize / unit_size;
178
8dbbc7f0 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);
f3d66e52 185 segment->get_samples(data, start_sample, end_sample);
0fbda3c2 186
e8d00928
ML
187 size_t length = end_sample - start_sample;
188
189 try {
8dbbc7f0 190 auto context = session_.session()->context();
7223eb62 191 auto logic = context->create_logic_packet(data, length, unit_size);
8dbbc7f0 192 output_->receive(logic);
e8d00928 193 } catch (Error error) {
8dbbc7f0 194 error_ = tr("Error while saving.");
0fbda3c2
JH
195 break;
196 }
197
198 start_sample = end_sample;
8dbbc7f0 199 units_stored_ = start_sample >> progress_scale;
0fbda3c2
JH
200 }
201
e0e560a5
JH
202 // Zeroing the progress variables indicates completion
203 units_stored_ = unit_count_ = 0;
204
0fbda3c2
JH
205 progress_updated();
206
8dbbc7f0 207 output_.reset();
7223eb62 208
0fbda3c2
JH
209 delete[] data;
210}
211
212} // pv