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