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