]> sigrok.org Git - pulseview.git/blob - pv/sigsession.h
Rename 'probe' to 'channel' everywhere.
[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 #include <libsigrok/libsigrok.h>
36
37 struct srd_decoder;
38 struct srd_channel;
39
40 namespace pv {
41
42 class DeviceManager;
43
44 namespace data {
45 class Analog;
46 class AnalogSnapshot;
47 class Logic;
48 class LogicSnapshot;
49 class SignalData;
50 }
51
52 namespace device {
53 class DevInst;
54 }
55
56 namespace view {
57 class DecodeTrace;
58 class LogicSignal;
59 class Signal;
60 }
61
62 class SigSession : public QObject
63 {
64         Q_OBJECT
65
66 public:
67         enum capture_state {
68                 Stopped,
69                 AwaitingTrigger,
70                 Running
71         };
72
73 public:
74         SigSession(DeviceManager &device_manager);
75
76         ~SigSession();
77
78         std::shared_ptr<device::DevInst> get_device() const;
79
80         /**
81          * Sets device instance that will be used in the next capture session.
82          */
83         void set_device(std::shared_ptr<device::DevInst> dev_inst)
84                 throw(QString);
85
86         void set_file(const std::string &name)
87                 throw(QString);
88
89         void set_default_device();
90
91         void release_device(device::DevInst *dev_inst);
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<device::DevInst> dev_inst);
117
118         std::shared_ptr<view::Signal> signal_from_channel(
119                 const sr_channel *channel) const;
120
121         void read_sample_rate(const sr_dev_inst *const sdi);
122
123 private:
124         void sample_thread_proc(std::shared_ptr<device::DevInst> dev_inst,
125                 std::function<void (const QString)> error_handler);
126
127         void feed_in_header(const sr_dev_inst *sdi);
128
129         void feed_in_meta(const sr_dev_inst *sdi,
130                 const sr_datafeed_meta &meta);
131
132         void feed_in_frame_begin();
133
134         void feed_in_logic(const sr_datafeed_logic &logic);
135
136         void feed_in_analog(const sr_datafeed_analog &analog);
137
138         void data_feed_in(const struct sr_dev_inst *sdi,
139                 const struct sr_datafeed_packet *packet);
140
141         static void data_feed_in_proc(const struct sr_dev_inst *sdi,
142                 const struct sr_datafeed_packet *packet, void *cb_data);
143
144 private:
145         DeviceManager &_device_manager;
146
147         /**
148          * The device instance that will be used in the next capture session.
149          */
150         std::shared_ptr<device::DevInst> _dev_inst;
151
152         std::vector< std::shared_ptr<view::DecodeTrace> > _decode_traces;
153
154         mutable std::mutex _sampling_mutex;
155         capture_state _capture_state;
156
157         mutable std::mutex _signals_mutex;
158         std::vector< std::shared_ptr<view::Signal> > _signals;
159
160         mutable std::mutex _data_mutex;
161         std::shared_ptr<data::Logic> _logic_data;
162         std::shared_ptr<data::LogicSnapshot> _cur_logic_snapshot;
163         std::map< const sr_channel*, std::shared_ptr<data::AnalogSnapshot> >
164                 _cur_analog_snapshots;
165
166         std::thread _sampling_thread;
167
168 Q_SIGNALS:
169         void capture_state_changed(int state);
170
171         void signals_changed();
172
173         void frame_began();
174
175         void data_received();
176
177         void frame_ended();
178
179 private:
180         // TODO: This should not be necessary. Multiple concurrent
181         // sessions should should be supported and it should be
182         // possible to associate a pointer with a sr_session.
183         static SigSession *_session;
184
185 public:
186         // TODO: Even more of a hack. The libsigrok API now allows for
187         // multiple sessions. However sr_session_* calls are scattered
188         // around the PV architecture and a single SigSession object is
189         // being used across multiple sequential sessions, which are
190         // created and destroyed in other classes in pv::device. This
191         // is a mess. For now just keep a single sr_session pointer here
192         // which we can use for all those scattered calls.
193         static struct sr_session *_sr_session;
194 };
195
196 } // namespace pv
197
198 #endif // PULSEVIEW_PV_SIGSESSION_H