Don't use std:: in the code directly (where possible).
[pulseview.git] / pv / session.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012-14 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_SESSION_HPP
21 #define PULSEVIEW_PV_SESSION_HPP
22
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <set>
27 #include <string>
28 #include <thread>
29 #include <unordered_set>
30 #include <vector>
31
32 #ifdef _WIN32
33 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
34 #define NOGDI
35 #define NORESOURCE
36 #endif
37 #include <boost/thread/shared_mutex.hpp>
38
39 #include <QObject>
40 #include <QSettings>
41 #include <QString>
42
43 #include "util.hpp"
44 #include "views/viewbase.hpp"
45
46 using std::function;
47 using std::list;
48 using std::map;
49 using std::mutex;
50 using std::recursive_mutex;
51 using std::shared_ptr;
52 using std::string;
53 using std::unordered_set;
54
55 struct srd_decoder;
56 struct srd_channel;
57
58 namespace sigrok {
59 class Analog;
60 class Channel;
61 class Device;
62 class InputFormat;
63 class Logic;
64 class Meta;
65 class OutputFormat;
66 class Packet;
67 class Session;
68 }
69
70 namespace pv {
71
72 class DeviceManager;
73
74 namespace data {
75 class Analog;
76 class AnalogSegment;
77 class Logic;
78 class LogicSegment;
79 class SignalBase;
80 class SignalData;
81 }
82
83 namespace devices {
84 class Device;
85 }
86
87 namespace toolbars {
88 class MainBar;
89 }
90
91 namespace views {
92 class ViewBase;
93 }
94
95 class Session : public QObject
96 {
97         Q_OBJECT
98
99 public:
100         enum capture_state {
101                 Stopped,
102                 AwaitingTrigger,
103                 Running
104         };
105
106 public:
107         Session(DeviceManager &device_manager, QString name);
108
109         ~Session();
110
111         DeviceManager& device_manager();
112
113         const DeviceManager& device_manager() const;
114
115         shared_ptr<sigrok::Session> session() const;
116
117         shared_ptr<devices::Device> device() const;
118
119         QString name() const;
120
121         void set_name(QString name);
122
123         const list< shared_ptr<views::ViewBase> > views() const;
124
125         shared_ptr<views::ViewBase> main_view() const;
126
127         shared_ptr<pv::toolbars::MainBar> main_bar() const;
128
129         void set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar);
130
131         /**
132          * Indicates whether the captured data was saved to disk already or not
133          */
134         bool data_saved() const;
135
136         void save_settings(QSettings &settings) const;
137
138         void restore_settings(QSettings &settings);
139
140         /**
141          * Attempts to set device instance, may fall back to demo if needed
142          */
143         void select_device(shared_ptr<devices::Device> device);
144
145         /**
146          * Sets device instance that will be used in the next capture session.
147          */
148         void set_device(shared_ptr<devices::Device> device);
149
150         void set_default_device();
151
152         void load_init_file(const string &file_name, const string &format);
153
154         void load_file(QString file_name,
155                 shared_ptr<sigrok::InputFormat> format = nullptr,
156                 const map<string, Glib::VariantBase> &options =
157                         map<string, Glib::VariantBase>());
158
159         capture_state get_capture_state() const;
160
161         void start_capture(function<void (const QString)> error_handler);
162
163         void stop_capture();
164
165         double get_samplerate() const;
166
167         void register_view(shared_ptr<views::ViewBase> view);
168
169         void deregister_view(shared_ptr<views::ViewBase> view);
170
171         bool has_view(shared_ptr<views::ViewBase> view);
172
173         const unordered_set< shared_ptr<data::SignalBase> >
174                 signalbases() const;
175
176 #ifdef ENABLE_DECODE
177         bool add_decoder(srd_decoder *const dec);
178
179         void remove_decode_signal(shared_ptr<data::SignalBase> signalbase);
180 #endif
181
182 private:
183         void set_capture_state(capture_state state);
184
185         void update_signals();
186
187         shared_ptr<data::SignalBase> signalbase_from_channel(
188                 shared_ptr<sigrok::Channel> channel) const;
189
190 private:
191         void sample_thread_proc(function<void (const QString)> error_handler);
192
193         void free_unused_memory();
194
195         void feed_in_header();
196
197         void feed_in_meta(shared_ptr<sigrok::Meta> meta);
198
199         void feed_in_trigger();
200
201         void feed_in_frame_begin();
202
203         void feed_in_logic(shared_ptr<sigrok::Logic> logic);
204
205         void feed_in_analog(shared_ptr<sigrok::Analog> analog);
206
207         void data_feed_in(shared_ptr<sigrok::Device> device,
208                 shared_ptr<sigrok::Packet> packet);
209
210 private:
211         DeviceManager &device_manager_;
212         shared_ptr<devices::Device> device_;
213         QString default_name_, name_;
214
215         list< shared_ptr<views::ViewBase> > views_;
216         shared_ptr<pv::views::ViewBase> main_view_;
217
218         shared_ptr<pv::toolbars::MainBar> main_bar_;
219
220         mutable mutex sampling_mutex_; //!< Protects access to capture_state_.
221         capture_state capture_state_;
222
223         unordered_set< shared_ptr<data::SignalBase> > signalbases_;
224         unordered_set< shared_ptr<data::SignalData> > all_signal_data_;
225
226         mutable recursive_mutex data_mutex_;
227         shared_ptr<data::Logic> logic_data_;
228         uint64_t cur_samplerate_;
229         shared_ptr<data::LogicSegment> cur_logic_segment_;
230         map< shared_ptr<sigrok::Channel>, shared_ptr<data::AnalogSegment> >
231                 cur_analog_segments_;
232
233         std::thread sampling_thread_;
234
235         bool out_of_memory_;
236         bool data_saved_;
237
238 Q_SIGNALS:
239         void capture_state_changed(int state);
240         void device_changed();
241
242         void signals_changed();
243
244         void name_changed();
245
246         void trigger_event(util::Timestamp location);
247
248         void frame_began();
249
250         void data_received();
251
252         void frame_ended();
253
254         void add_view(const QString &title, views::ViewType type,
255                 Session *session);
256
257 public Q_SLOTS:
258         void on_data_saved();
259 };
260
261 } // namespace pv
262
263 #endif // PULSEVIEW_PV_SESSION_HPP