Session: Renamed files to match class name
[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_SIGSESSION_H
22 #define PULSEVIEW_PV_SIGSESSION_H
23
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <thread>
30 #include <vector>
31
32 #include <boost/thread.hpp>
33
34 #include <QObject>
35 #include <QString>
36
37 struct srd_decoder;
38 struct srd_channel;
39
40 namespace sigrok {
41         class Analog;
42         class Channel;
43         class Device;
44         class Logic;
45         class Meta;
46         class Packet;
47         class Session;
48 }
49
50 namespace pv {
51
52 class DeviceManager;
53
54 namespace data {
55 class Analog;
56 class AnalogSnapshot;
57 class Logic;
58 class LogicSnapshot;
59 class SignalData;
60 }
61
62 namespace view {
63 class DecodeTrace;
64 class LogicSignal;
65 class Signal;
66 }
67
68 class Session : public QObject
69 {
70         Q_OBJECT
71
72 public:
73         enum capture_state {
74                 Stopped,
75                 AwaitingTrigger,
76                 Running
77         };
78
79 public:
80         Session(DeviceManager &device_manager);
81
82         ~Session();
83
84         DeviceManager& device_manager();
85
86         const DeviceManager& device_manager() const;
87
88         const std::shared_ptr<sigrok::Session>& session() const;
89
90         std::shared_ptr<sigrok::Device> device() const;
91
92         /**
93          * Sets device instance that will be used in the next capture session.
94          */
95         void set_device(std::shared_ptr<sigrok::Device> device);
96
97         void set_file(const std::string &name);
98
99         void set_default_device();
100
101         capture_state get_capture_state() const;
102
103         void start_capture(std::function<void (const QString)> error_handler);
104
105         void stop_capture();
106
107         std::set< std::shared_ptr<data::SignalData> > get_data() const;
108
109         boost::shared_mutex& signals_mutex() const;
110
111         const std::vector< std::shared_ptr<view::Signal> >& signals() const;
112
113 #ifdef ENABLE_DECODE
114         bool add_decoder(srd_decoder *const dec);
115
116         std::vector< std::shared_ptr<view::DecodeTrace> >
117                 get_decode_signals() const;
118
119         void remove_decode_signal(view::DecodeTrace *signal);
120 #endif
121
122 private:
123         void set_capture_state(capture_state state);
124
125         void update_signals(std::shared_ptr<sigrok::Device> device);
126
127         std::shared_ptr<view::Signal> signal_from_channel(
128                 std::shared_ptr<sigrok::Channel> channel) const;
129
130         void read_sample_rate(std::shared_ptr<sigrok::Device>);
131
132 private:
133         void sample_thread_proc(std::shared_ptr<sigrok::Device> device,
134                 std::function<void (const QString)> error_handler);
135
136         void feed_in_header(std::shared_ptr<sigrok::Device> device);
137
138         void feed_in_meta(std::shared_ptr<sigrok::Device> device,
139                 std::shared_ptr<sigrok::Meta> meta);
140
141         void feed_in_frame_begin();
142
143         void feed_in_logic(std::shared_ptr<sigrok::Logic> logic);
144
145         void feed_in_analog(std::shared_ptr<sigrok::Analog> analog);
146
147         void data_feed_in(std::shared_ptr<sigrok::Device> device,
148                 std::shared_ptr<sigrok::Packet> packet);
149
150 private:
151         DeviceManager &device_manager_;
152         std::shared_ptr<sigrok::Session> session_;
153
154         /**
155          * The device instance that will be used in the next capture session.
156          */
157         std::shared_ptr<sigrok::Device> device_;
158
159         std::vector< std::shared_ptr<view::DecodeTrace> > decode_traces_;
160
161         mutable std::mutex sampling_mutex_;
162         capture_state capture_state_;
163
164         mutable boost::shared_mutex signals_mutex_;
165         std::vector< std::shared_ptr<view::Signal> > signals_;
166
167         mutable std::mutex data_mutex_;
168         std::shared_ptr<data::Logic> logic_data_;
169         std::shared_ptr<data::LogicSnapshot> cur_logic_snapshot_;
170         std::map< std::shared_ptr<sigrok::Channel>, std::shared_ptr<data::AnalogSnapshot> >
171                 cur_analog_snapshots_;
172
173         std::thread sampling_thread_;
174
175 Q_SIGNALS:
176         void capture_state_changed(int state);
177         void device_selected();
178
179         void signals_changed();
180
181         void frame_began();
182
183         void data_received();
184
185         void frame_ended();
186 };
187
188 } // namespace pv
189
190 #endif // PULSEVIEW_PV_SIGSESSION_H