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