]> sigrok.org Git - pulseview.git/blob - pv/session.hpp
a764c7c3f1f52367fde2c803952dd697180e482d
[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 #include "views/viewbase.hpp"
46
47 struct srd_decoder;
48 struct srd_channel;
49
50 namespace sigrok {
51 class Analog;
52 class Channel;
53 class Device;
54 class InputFormat;
55 class Logic;
56 class Meta;
57 class OutputFormat;
58 class Packet;
59 class Session;
60 }
61
62 namespace pv {
63
64 class DeviceManager;
65
66 namespace data {
67 class Analog;
68 class AnalogSegment;
69 class Logic;
70 class LogicSegment;
71 class SignalBase;
72 class SignalData;
73 }
74
75 namespace devices {
76 class Device;
77 }
78
79 namespace toolbars {
80 class MainBar;
81 }
82
83 namespace views {
84 class ViewBase;
85 }
86
87 class Session : public QObject
88 {
89         Q_OBJECT
90
91 public:
92         enum capture_state {
93                 Stopped,
94                 AwaitingTrigger,
95                 Running
96         };
97
98 public:
99         Session(DeviceManager &device_manager, QString name);
100
101         ~Session();
102
103         DeviceManager& device_manager();
104
105         const DeviceManager& device_manager() const;
106
107         std::shared_ptr<sigrok::Session> session() const;
108
109         std::shared_ptr<devices::Device> device() const;
110
111         QString name() const;
112
113         void set_name(QString name);
114
115         const std::list< std::shared_ptr<views::ViewBase> > views() const;
116
117         std::shared_ptr<views::ViewBase> main_view() const;
118
119         std::shared_ptr<pv::toolbars::MainBar> main_bar() const;
120
121         void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);
122
123         void save_settings(QSettings &settings) const;
124
125         void restore_settings(QSettings &settings);
126
127         /**
128          * Attempts to set device instance, may fall back to demo if needed
129          */
130         void select_device(std::shared_ptr<devices::Device> device);
131
132         /**
133          * Sets device instance that will be used in the next capture session.
134          */
135         void set_device(std::shared_ptr<devices::Device> device);
136
137         void set_default_device();
138
139         void load_init_file(const std::string &file_name,
140                 const std::string &format);
141
142         void load_file(QString file_name,
143                 std::shared_ptr<sigrok::InputFormat> format = nullptr,
144                 const std::map<std::string, Glib::VariantBase> &options =
145                         std::map<std::string, Glib::VariantBase>());
146
147         capture_state get_capture_state() const;
148
149         void start_capture(std::function<void (const QString)> error_handler);
150
151         void stop_capture();
152
153         double get_samplerate() const;
154
155         void register_view(std::shared_ptr<views::ViewBase> view);
156
157         void deregister_view(std::shared_ptr<views::ViewBase> view);
158
159         bool has_view(std::shared_ptr<views::ViewBase> view);
160
161         const std::unordered_set< std::shared_ptr<data::SignalBase> >
162                 signalbases() const;
163
164 #ifdef ENABLE_DECODE
165         bool add_decoder(srd_decoder *const dec);
166
167         void remove_decode_signal(std::shared_ptr<data::SignalBase> signalbase);
168 #endif
169
170 private:
171         void set_capture_state(capture_state state);
172
173         void update_signals();
174
175         std::shared_ptr<data::SignalBase> signalbase_from_channel(
176                 std::shared_ptr<sigrok::Channel> channel) const;
177
178 private:
179         void sample_thread_proc(std::function<void (const QString)> error_handler);
180
181         void feed_in_header();
182
183         void feed_in_meta(std::shared_ptr<sigrok::Meta> meta);
184
185         void feed_in_trigger();
186
187         void feed_in_frame_begin();
188
189         void feed_in_logic(std::shared_ptr<sigrok::Logic> logic);
190
191         void feed_in_analog(std::shared_ptr<sigrok::Analog> analog);
192
193         void data_feed_in(std::shared_ptr<sigrok::Device> device,
194                 std::shared_ptr<sigrok::Packet> packet);
195
196 private:
197         DeviceManager &device_manager_;
198         std::shared_ptr<devices::Device> device_;
199         QString default_name_, name_;
200
201         std::list< std::shared_ptr<views::ViewBase> > views_;
202         std::shared_ptr<pv::views::ViewBase> main_view_;
203
204         std::shared_ptr<pv::toolbars::MainBar> main_bar_;
205
206         mutable std::mutex sampling_mutex_; //!< Protects access to capture_state_.
207         capture_state capture_state_;
208
209         std::unordered_set< std::shared_ptr<data::SignalBase> > signalbases_;
210         std::unordered_set< std::shared_ptr<data::SignalData> > all_signal_data_;
211
212         mutable std::recursive_mutex data_mutex_;
213         std::shared_ptr<data::Logic> logic_data_;
214         uint64_t cur_samplerate_;
215         std::shared_ptr<data::LogicSegment> cur_logic_segment_;
216         std::map< std::shared_ptr<sigrok::Channel>, std::shared_ptr<data::AnalogSegment> >
217                 cur_analog_segments_;
218
219         std::thread sampling_thread_;
220
221         bool out_of_memory_;
222
223 Q_SIGNALS:
224         void capture_state_changed(int state);
225         void device_changed();
226
227         void signals_changed();
228
229         void name_changed();
230
231         void trigger_event(util::Timestamp location);
232
233         void frame_began();
234
235         void data_received();
236
237         void frame_ended();
238
239         void add_view(const QString &title, views::ViewType type,
240                 Session *session);
241 };
242
243 } // namespace pv
244
245 #endif // PULSEVIEW_PV_SESSION_HPP