Implement multi-session handling
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifndef PULSEVIEW_PV_SESSION_HPP
22 #define PULSEVIEW_PV_SESSION_HPP
23
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <thread>
30 #include <unordered_set>
31 #include <vector>
32
33 #ifdef _WIN32
34 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
35 #define NOGDI
36 #define NORESOURCE
37 #endif
38 #include <boost/thread/shared_mutex.hpp>
39
40 #include <QObject>
41 #include <QSettings>
42 #include <QString>
43
44 #include "util.hpp"
45
46 struct srd_decoder;
47 struct srd_channel;
48
49 namespace sigrok {
50 class Analog;
51 class Channel;
52 class Device;
53 class Logic;
54 class Meta;
55 class Packet;
56 class Session;
57 }
58
59 namespace pv {
60
61 class DeviceManager;
62
63 namespace data {
64 class Analog;
65 class AnalogSegment;
66 class Logic;
67 class LogicSegment;
68 class SignalBase;
69 class SignalData;
70 }
71
72 namespace devices {
73 class Device;
74 }
75
76 namespace toolbars {
77 class MainBar;
78 }
79
80 namespace view {
81 class View;
82 }
83
84 class Session : public QObject
85 {
86         Q_OBJECT
87
88 public:
89         enum capture_state {
90                 Stopped,
91                 AwaitingTrigger,
92                 Running
93         };
94
95 public:
96         Session(DeviceManager &device_manager, QString name);
97
98         ~Session();
99
100         DeviceManager& device_manager();
101
102         const DeviceManager& device_manager() const;
103
104         std::shared_ptr<sigrok::Session> session() const;
105
106         std::shared_ptr<devices::Device> device() const;
107
108         QString name() const;
109
110         void set_name(QString name);
111
112         std::shared_ptr<pv::view::View> main_view() const;
113
114         void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);
115
116         std::shared_ptr<pv::toolbars::MainBar> main_bar() const;
117
118         void save_settings(QSettings &settings) const;
119
120         void restore_settings(QSettings &settings);
121
122         /**
123          * Sets device instance that will be used in the next capture session.
124          */
125         void set_device(std::shared_ptr<devices::Device> device);
126
127         void set_default_device();
128
129         capture_state get_capture_state() const;
130
131         void start_capture(std::function<void (const QString)> error_handler);
132
133         void stop_capture();
134
135         double get_samplerate() const;
136
137         void register_view(std::shared_ptr<pv::view::View> view);
138
139         void deregister_view(std::shared_ptr<pv::view::View> view);
140
141         bool has_view(std::shared_ptr<pv::view::View> view);
142
143         const std::unordered_set< std::shared_ptr<data::SignalBase> >
144                 signalbases() const;
145
146 #ifdef ENABLE_DECODE
147         bool add_decoder(srd_decoder *const dec);
148
149         void remove_decode_signal(std::shared_ptr<data::SignalBase> signalbase);
150 #endif
151
152 private:
153         void set_capture_state(capture_state state);
154
155         void update_signals();
156
157         std::shared_ptr<data::SignalBase> signalbase_from_channel(
158                 std::shared_ptr<sigrok::Channel> channel) const;
159
160 private:
161         void sample_thread_proc(std::function<void (const QString)> error_handler);
162
163         void feed_in_header();
164
165         void feed_in_meta(std::shared_ptr<sigrok::Meta> meta);
166
167         void feed_in_trigger();
168
169         void feed_in_frame_begin();
170
171         void feed_in_logic(std::shared_ptr<sigrok::Logic> logic);
172
173         void feed_in_analog(std::shared_ptr<sigrok::Analog> analog);
174
175         void data_feed_in(std::shared_ptr<sigrok::Device> device,
176                 std::shared_ptr<sigrok::Packet> packet);
177
178 private:
179         DeviceManager &device_manager_;
180         std::shared_ptr<devices::Device> device_;
181         QString default_name_, name_;
182
183         std::unordered_set< std::shared_ptr<pv::view::View> > views_;
184         std::shared_ptr<pv::view::View> main_view_;
185
186         std::shared_ptr<pv::toolbars::MainBar> main_bar_;
187
188         mutable std::mutex sampling_mutex_; //!< Protects access to capture_state_.
189         capture_state capture_state_;
190
191         std::unordered_set< std::shared_ptr<data::SignalBase> > signalbases_;
192         std::unordered_set< std::shared_ptr<data::SignalData> > all_signal_data_;
193
194         mutable std::recursive_mutex data_mutex_;
195         std::shared_ptr<data::Logic> logic_data_;
196         uint64_t cur_samplerate_;
197         std::shared_ptr<data::LogicSegment> cur_logic_segment_;
198         std::map< std::shared_ptr<sigrok::Channel>, std::shared_ptr<data::AnalogSegment> >
199                 cur_analog_segments_;
200
201         std::thread sampling_thread_;
202
203         bool out_of_memory_;
204
205 Q_SIGNALS:
206         void capture_state_changed(int state);
207         void device_selected();
208
209         void signals_changed();
210
211         void name_changed();
212
213         void trigger_event(util::Timestamp location);
214
215         void frame_began();
216
217         void data_received();
218
219         void frame_ended();
220 };
221
222 } // namespace pv
223
224 #endif // PULSEVIEW_PV_SESSION_HPP