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