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