Session: Renamed set_file to set_session_file
[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 AnalogSegment;
57 class Logic;
58 class LogicSegment;
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         /**
98          * Sets a sigrok session file as the capture device.
99          * @param name the path to the file.
100          */
101         void set_session_file(const std::string &name);
102
103         void set_default_device();
104
105         capture_state get_capture_state() const;
106
107         void start_capture(std::function<void (const QString)> error_handler);
108
109         void stop_capture();
110
111         std::set< std::shared_ptr<data::SignalData> > get_data() const;
112
113         boost::shared_mutex& signals_mutex() const;
114
115         const std::vector< std::shared_ptr<view::Signal> >& signals() const;
116
117 #ifdef ENABLE_DECODE
118         bool add_decoder(srd_decoder *const dec);
119
120         std::vector< std::shared_ptr<view::DecodeTrace> >
121                 get_decode_signals() const;
122
123         void remove_decode_signal(view::DecodeTrace *signal);
124 #endif
125
126 private:
127         void set_capture_state(capture_state state);
128
129         void update_signals(std::shared_ptr<sigrok::Device> device);
130
131         std::shared_ptr<view::Signal> signal_from_channel(
132                 std::shared_ptr<sigrok::Channel> channel) const;
133
134         void read_sample_rate(std::shared_ptr<sigrok::Device>);
135
136 private:
137         void sample_thread_proc(std::shared_ptr<sigrok::Device> device,
138                 std::function<void (const QString)> error_handler);
139
140         void feed_in_header(std::shared_ptr<sigrok::Device> device);
141
142         void feed_in_meta(std::shared_ptr<sigrok::Device> device,
143                 std::shared_ptr<sigrok::Meta> meta);
144
145         void feed_in_frame_begin();
146
147         void feed_in_logic(std::shared_ptr<sigrok::Logic> logic);
148
149         void feed_in_analog(std::shared_ptr<sigrok::Analog> analog);
150
151         void data_feed_in(std::shared_ptr<sigrok::Device> device,
152                 std::shared_ptr<sigrok::Packet> packet);
153
154 private:
155         DeviceManager &device_manager_;
156         std::shared_ptr<sigrok::Session> session_;
157
158         /**
159          * The device instance that will be used in the next capture session.
160          */
161         std::shared_ptr<sigrok::Device> device_;
162
163         std::vector< std::shared_ptr<view::DecodeTrace> > decode_traces_;
164
165         mutable std::mutex sampling_mutex_;
166         capture_state capture_state_;
167
168         mutable boost::shared_mutex signals_mutex_;
169         std::vector< std::shared_ptr<view::Signal> > signals_;
170
171         mutable std::mutex data_mutex_;
172         std::shared_ptr<data::Logic> logic_data_;
173         uint64_t cur_samplerate_;
174         std::shared_ptr<data::LogicSegment> cur_logic_segment_;
175         std::map< std::shared_ptr<sigrok::Channel>, std::shared_ptr<data::AnalogSegment> >
176                 cur_analog_segments_;
177
178         std::thread sampling_thread_;
179
180 Q_SIGNALS:
181         void capture_state_changed(int state);
182         void device_selected();
183
184         void signals_changed();
185
186         void frame_began();
187
188         void data_received();
189
190         void frame_ended();
191 };
192
193 } // namespace pv
194
195 #endif // PULSEVIEW_PV_SIGSESSION_H