]> sigrok.org Git - pulseview.git/blob - pv/sigsession.h
a624d1d216aaf6e0e280cfde4bf514a61ee118c4
[pulseview.git] / pv / sigsession.h
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 <QObject>
33 #include <QString>
34
35 struct srd_decoder;
36 struct srd_channel;
37
38 namespace sigrok {
39         class Analog;
40         class Channel;
41         class Device;
42         class Logic;
43         class Meta;
44         class Packet;
45         class Session;
46 }
47
48 namespace pv {
49
50 class DeviceManager;
51
52 namespace data {
53 class Analog;
54 class AnalogSnapshot;
55 class Logic;
56 class LogicSnapshot;
57 class SignalData;
58 }
59
60 namespace view {
61 class DecodeTrace;
62 class LogicSignal;
63 class Signal;
64 }
65
66 class SigSession : public QObject
67 {
68         Q_OBJECT
69
70 public:
71         enum capture_state {
72                 Stopped,
73                 AwaitingTrigger,
74                 Running
75         };
76
77 public:
78         SigSession(DeviceManager &device_manager);
79
80         ~SigSession();
81
82         std::shared_ptr<sigrok::Device> get_device() const;
83
84         /**
85          * Sets device instance that will be used in the next capture session.
86          */
87         void set_device(std::shared_ptr<sigrok::Device> device);
88
89         void set_file(const std::string &name);
90
91         void set_default_device();
92
93         capture_state get_capture_state() const;
94
95         void start_capture(std::function<void (const QString)> error_handler);
96
97         void stop_capture();
98
99         std::set< std::shared_ptr<data::SignalData> > get_data() const;
100
101         std::vector< std::shared_ptr<view::Signal> >
102                 get_signals() const;
103
104 #ifdef ENABLE_DECODE
105         bool add_decoder(srd_decoder *const dec);
106
107         std::vector< std::shared_ptr<view::DecodeTrace> >
108                 get_decode_signals() const;
109
110         void remove_decode_signal(view::DecodeTrace *signal);
111 #endif
112
113 private:
114         void set_capture_state(capture_state state);
115
116         void update_signals(std::shared_ptr<sigrok::Device> device);
117
118         std::shared_ptr<view::Signal> signal_from_channel(
119                 std::shared_ptr<sigrok::Channel> channel) const;
120
121         void read_sample_rate(std::shared_ptr<sigrok::Device>);
122
123 private:
124         void sample_thread_proc(std::shared_ptr<sigrok::Device> device,
125                 std::function<void (const QString)> error_handler);
126
127         void feed_in_header(std::shared_ptr<sigrok::Device> device);
128
129         void feed_in_meta(std::shared_ptr<sigrok::Device> device,
130                 std::shared_ptr<sigrok::Meta> meta);
131
132         void feed_in_frame_begin();
133
134         void feed_in_logic(std::shared_ptr<sigrok::Logic> logic);
135
136         void feed_in_analog(std::shared_ptr<sigrok::Analog> analog);
137
138         void data_feed_in(std::shared_ptr<sigrok::Device> device,
139                 std::shared_ptr<sigrok::Packet> packet);
140
141 private:
142         DeviceManager &_device_manager;
143
144         /**
145          * The device instance that will be used in the next capture session.
146          */
147         std::shared_ptr<sigrok::Device> _device;
148
149         std::vector< std::shared_ptr<view::DecodeTrace> > _decode_traces;
150
151         mutable std::mutex _sampling_mutex;
152         capture_state _capture_state;
153
154         mutable std::mutex _signals_mutex;
155         std::vector< std::shared_ptr<view::Signal> > _signals;
156
157         mutable std::mutex _data_mutex;
158         std::shared_ptr<data::Logic> _logic_data;
159         std::shared_ptr<data::LogicSnapshot> _cur_logic_snapshot;
160         std::map< std::shared_ptr<sigrok::Channel>, std::shared_ptr<data::AnalogSnapshot> >
161                 _cur_analog_snapshots;
162
163         std::thread _sampling_thread;
164
165 Q_SIGNALS:
166         void capture_state_changed(int state);
167
168         void signals_changed();
169
170         void frame_began();
171
172         void data_received();
173
174         void frame_ended();
175
176 public:
177         // Hack. The libsigrok API now allows for multiple sessions. However,
178         // sigrok::Session calls are scattered around the PV architecture and a
179         // single SigSession object is being used across multiple sequential
180         // sessions. This is a mess. For now just keep a single sigrok::Session
181         // pointer here which we can use for all those scattered calls.
182         static std::shared_ptr<sigrok::Session> _sr_session;
183 };
184
185 } // namespace pv
186
187 #endif // PULSEVIEW_PV_SIGSESSION_H