]> sigrok.org Git - pulseview.git/blame - pv/storesession.cpp
android/: Whitespace and consistency fixes.
[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
0fbda3c2 30using std::deque;
f9abf97e 31using std::dynamic_pointer_cast;
3b68d03d 32using std::lock_guard;
0fbda3c2
JH
33using std::make_pair;
34using std::min;
3b68d03d 35using std::mutex;
0fbda3c2
JH
36using std::pair;
37using std::set;
f9abf97e 38using std::shared_ptr;
0fbda3c2 39using std::string;
3b68d03d 40using std::thread;
0fbda3c2
JH
41using std::vector;
42
43namespace pv {
44
45const size_t StoreSession::BlockSize = 1024 * 1024;
46
47StoreSession::StoreSession(const std::string &file_name,
48 const SigSession &session) :
49 _file_name(file_name),
50 _session(session),
3b68d03d 51 _interrupt(false),
0fbda3c2
JH
52 _units_stored(0),
53 _unit_count(0)
54{
55}
56
57StoreSession::~StoreSession()
58{
59 wait();
60}
61
e40b2f29 62pair<int, int> StoreSession::progress() const
0fbda3c2 63{
b101c41e 64 return make_pair(_units_stored.load(), _unit_count.load());
0fbda3c2
JH
65}
66
67const QString& StoreSession::error() const
68{
69 lock_guard<mutex> lock(_mutex);
70 return _error;
71}
72
73bool StoreSession::start()
74{
75 set< shared_ptr<data::SignalData> > data_set =
76 _session.get_data();
77 const vector< shared_ptr<view::Signal> > sigs =
78 _session.get_signals();
79
80 // Check we have logic data
81 if (data_set.empty() || sigs.empty()) {
82 _error = tr("No data to save.");
83 return false;
84 }
85
86 if (data_set.size() > 1) {
87 _error = tr("PulseView currently only has support for "
88 "storing a single data stream.");
89 return false;
90 }
91
92 // Get the logic data
93 //shared_ptr<data::SignalData
94 shared_ptr<data::Logic> data;
95 if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
96 _error = tr("PulseView currently only has support for "
97 "storing a logic data.");
98 return false;
99 }
100
101 // Get the snapshot
102 const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
103 data->get_snapshots();
104
105 if (snapshots.empty()) {
106 _error = tr("No snapshots to save.");
107 return false;
108 }
109
110 const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
111 assert(snapshot);
112
113 // Make a list of probes
114 char **const probes = new char*[sigs.size() + 1];
115 for (size_t i = 0; i < sigs.size(); i++) {
116 shared_ptr<view::Signal> sig(sigs[i]);
117 assert(sig);
118 probes[i] = strdup(sig->get_name().toUtf8().constData());
119 }
120 probes[sigs.size()] = NULL;
121
122 // Begin storing
bb3030b3 123 if (sr_session_save_init(SigSession::_sr_session, _file_name.c_str(),
0fbda3c2
JH
124 data->samplerate(), probes) != SR_OK) {
125 _error = tr("Error while saving.");
126 return false;
127 }
128
129 // Delete the probes array
130 for (size_t i = 0; i <= sigs.size(); i++)
131 free(probes[i]);
132 delete[] probes;
133
3b68d03d 134 _thread = std::thread(&StoreSession::store_proc, this, snapshot);
0fbda3c2
JH
135 return true;
136}
137
138void StoreSession::wait()
139{
6d483b8b
MC
140 if (_thread.joinable())
141 _thread.join();
0fbda3c2
JH
142}
143
144void StoreSession::cancel()
145{
3b68d03d 146 _interrupt = true;
0fbda3c2
JH
147}
148
149void StoreSession::store_proc(shared_ptr<data::LogicSnapshot> snapshot)
150{
151 assert(snapshot);
152
e40b2f29
MC
153 uint64_t start_sample = 0, sample_count;
154 unsigned progress_scale = 0;
0fbda3c2
JH
155
156 /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
157 uint8_t *const data = new uint8_t[BlockSize];
158 assert(data);
159
160 const int unit_size = snapshot->unit_size();
161 assert(unit_size != 0);
162
e40b2f29
MC
163 sample_count = snapshot->get_sample_count();
164
165 // Qt needs the progress values to fit inside an int. If they would
166 // not, scale the current and max values down until they do.
167 while ((sample_count >> progress_scale) > INT_MAX)
168 progress_scale ++;
169
170 _unit_count = sample_count >> progress_scale;
0fbda3c2
JH
171
172 const unsigned int samples_per_block = BlockSize / unit_size;
173
e40b2f29 174 while (!_interrupt && start_sample < sample_count)
0fbda3c2
JH
175 {
176 progress_updated();
177
178 const uint64_t end_sample = min(
e40b2f29 179 start_sample + samples_per_block, sample_count);
0fbda3c2
JH
180 snapshot->get_samples(data, start_sample, end_sample);
181
bb3030b3
ML
182 if(sr_session_append(SigSession::_sr_session, _file_name.c_str(), data,
183 unit_size, end_sample - start_sample) != SR_OK)
0fbda3c2
JH
184 {
185 _error = tr("Error while saving.");
186 break;
187 }
188
189 start_sample = end_sample;
e40b2f29 190 _units_stored = start_sample >> progress_scale;
0fbda3c2
JH
191 }
192
b101c41e 193 _unit_count = 0;
0fbda3c2
JH
194 progress_updated();
195
196 delete[] data;
197}
198
199} // pv