Implement MathSignal
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_SESSION_HPP
21 #define PULSEVIEW_PV_SESSION_HPP
22
23 #ifdef ENABLE_FLOW
24 #include <atomic>
25 #include <condition_variable>
26 #endif
27
28 #include <deque>
29 #include <functional>
30 #include <map>
31 #include <memory>
32 #include <mutex>
33 #include <set>
34 #include <string>
35 #include <thread>
36 #include <vector>
37
38 #include <QObject>
39 #include <QSettings>
40 #include <QString>
41 #include <QElapsedTimer>
42
43 #ifdef ENABLE_FLOW
44 #include <gstreamermm.h>
45 #include <libsigrokflow/libsigrokflow.hpp>
46 #endif
47
48 #include "metadata_obj.hpp"
49 #include "util.hpp"
50 #include "views/viewbase.hpp"
51
52 using std::deque;
53 using std::function;
54 using std::map;
55 using std::mutex;
56 using std::recursive_mutex;
57 using std::shared_ptr;
58 using std::string;
59 using std::unordered_set;
60
61 #ifdef ENABLE_FLOW
62 using Glib::RefPtr;
63 using Gst::AppSink;
64 using Gst::Element;
65 using Gst::Pipeline;
66 #endif
67
68 struct srd_decoder;
69 struct srd_channel;
70
71 namespace sigrok {
72 class Analog;
73 class Channel;
74 class Device;
75 class InputFormat;
76 class Logic;
77 class Meta;
78 class Option;
79 class OutputFormat;
80 class Packet;
81 class Session;
82 }  // namespace sigrok
83
84 using sigrok::Option;
85
86 namespace pv {
87
88 class DeviceManager;
89
90 namespace data {
91 class Analog;
92 class AnalogSegment;
93 class DecodeSignal;
94 class Logic;
95 class LogicSegment;
96 class SignalBase;
97 class SignalData;
98 class SignalGroup;
99 }
100
101 namespace devices {
102 class Device;
103 }
104
105 namespace toolbars {
106 class MainBar;
107 }
108
109 namespace views {
110 class ViewBase;
111 }
112
113 using pv::views::ViewType;
114
115 class Session : public QObject
116 {
117         Q_OBJECT
118
119 public:
120         enum capture_state {
121                 Stopped,
122                 AwaitingTrigger,
123                 Running
124         };
125
126         static shared_ptr<sigrok::Context> sr_context;
127
128 public:
129         Session(DeviceManager &device_manager, QString name);
130
131         ~Session();
132
133         DeviceManager& device_manager();
134
135         const DeviceManager& device_manager() const;
136
137         shared_ptr<sigrok::Session> session() const;
138
139         shared_ptr<devices::Device> device() const;
140
141         QString name() const;
142         void set_name(QString name);
143
144         QString save_path() const;
145         void set_save_path(QString path);
146
147         const vector< shared_ptr<views::ViewBase> > views() const;
148
149         shared_ptr<views::ViewBase> main_view() const;
150         shared_ptr<pv::toolbars::MainBar> main_bar() const;
151         void set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar);
152
153         /**
154          * Indicates whether the captured data was saved to disk already or not
155          */
156         bool data_saved() const;
157
158         void save_setup(QSettings &settings) const;
159         void save_settings(QSettings &settings) const;
160         void restore_setup(QSettings &settings);
161         void restore_settings(QSettings &settings);
162
163         /**
164          * Attempts to set device instance, may fall back to demo if needed
165          */
166         void select_device(shared_ptr<devices::Device> device);
167
168         /**
169          * Sets device instance that will be used in the next capture session.
170          */
171         void set_device(shared_ptr<devices::Device> device);
172         void set_default_device();
173         bool using_file_device() const;
174
175         void load_init_file(const string &file_name, const string &format,
176                 const string &setup_file_name);
177
178         void load_file(QString file_name, QString setup_file_name = QString(),
179                 shared_ptr<sigrok::InputFormat> format = nullptr,
180                 const map<string, Glib::VariantBase> &options =
181                         map<string, Glib::VariantBase>());
182
183         capture_state get_capture_state() const;
184         void start_capture(function<void (const QString)> error_handler);
185         void stop_capture();
186
187         double get_samplerate() const;
188
189         uint32_t get_highest_segment_id() const;
190         uint64_t get_segment_sample_count(uint32_t segment_id) const;
191
192         vector<util::Timestamp> get_triggers(uint32_t segment_id) const;
193
194         void register_view(shared_ptr<views::ViewBase> view);
195         void deregister_view(shared_ptr<views::ViewBase> view);
196         bool has_view(shared_ptr<views::ViewBase> view);
197
198         const vector< shared_ptr<data::SignalBase> > signalbases() const;
199         uint32_t get_signal_count(data::SignalBase::ChannelType type) const;
200         uint32_t get_next_signal_index(data::SignalBase::ChannelType type);
201
202         void add_generated_signal(shared_ptr<data::SignalBase> signal);
203         void remove_generated_signal(shared_ptr<data::SignalBase> signal);
204
205 #ifdef ENABLE_DECODE
206         shared_ptr<data::DecodeSignal> add_decode_signal();
207
208         void remove_decode_signal(shared_ptr<data::DecodeSignal> signal);
209 #endif
210
211         bool all_segments_complete(uint32_t segment_id) const;
212
213         MetadataObjManager* metadata_obj_manager();
214
215 private:
216         void set_capture_state(capture_state state);
217
218         void update_signals();
219
220         shared_ptr<data::SignalBase> signalbase_from_channel(
221                 shared_ptr<sigrok::Channel> channel) const;
222
223         static map<string, Glib::VariantBase> input_format_options(
224                 vector<string> user_spec,
225                 map<string, shared_ptr<Option>> fmt_opts);
226
227         void sample_thread_proc(function<void (const QString)> error_handler);
228
229         void free_unused_memory();
230
231         void signal_new_segment();
232         void signal_segment_completed();
233
234 #ifdef ENABLE_FLOW
235         bool on_gst_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message);
236
237         Gst::FlowReturn on_gst_new_sample();
238 #endif
239
240         void feed_in_header();
241         void feed_in_meta(shared_ptr<sigrok::Meta> meta);
242         void feed_in_trigger();
243         void feed_in_frame_begin();
244         void feed_in_frame_end();
245         void feed_in_logic(shared_ptr<sigrok::Logic> logic);
246         void feed_in_analog(shared_ptr<sigrok::Analog> analog);
247
248         void data_feed_in(shared_ptr<sigrok::Device> device,
249                 shared_ptr<sigrok::Packet> packet);
250
251 Q_SIGNALS:
252         void capture_state_changed(int state);
253         void device_changed();
254
255         void signals_changed();
256
257         void name_changed();
258
259         void trigger_event(int segment_id, util::Timestamp location);
260
261         void new_segment(int new_segment_id);
262         void segment_completed(int segment_id);
263
264         void data_received();
265
266         void add_view(ViewType type, Session *session);
267
268 public Q_SLOTS:
269         void on_data_saved();
270
271 #ifdef ENABLE_DECODE
272         void on_new_decoders_selected(vector<const srd_decoder*> decoders);
273 #endif
274
275 private:
276         bool shutting_down_;
277
278         DeviceManager &device_manager_;
279         shared_ptr<devices::Device> device_;
280         QString default_name_, name_, save_path_;
281
282         vector< shared_ptr<views::ViewBase> > views_;
283         shared_ptr<pv::views::ViewBase> main_view_;
284
285         shared_ptr<pv::toolbars::MainBar> main_bar_;
286
287         mutable mutex sampling_mutex_; //!< Protects access to capture_state_
288         capture_state capture_state_;
289
290         vector< shared_ptr<data::SignalBase> > signalbases_;
291         unordered_set< shared_ptr<data::SignalData> > all_signal_data_;
292         deque<data::SignalGroup*> signal_groups_;
293         map<uint8_t, uint32_t> next_index_list_; // signal type -> index counter
294
295         /// trigger_list_ contains pairs of <segment_id, timestamp> values
296         vector< std::pair<uint32_t, util::Timestamp> > trigger_list_;
297
298         mutable recursive_mutex data_mutex_;
299         shared_ptr<data::Logic> logic_data_;
300         uint64_t cur_samplerate_;
301         shared_ptr<data::LogicSegment> cur_logic_segment_;
302         map< shared_ptr<sigrok::Channel>, shared_ptr<data::AnalogSegment> >
303                 cur_analog_segments_;
304         int32_t highest_segment_id_;
305         vector<uint64_t> segment_sample_count_;
306
307         std::thread sampling_thread_;
308
309         bool out_of_memory_;
310         bool data_saved_;
311         bool frame_began_;
312
313         QElapsedTimer acq_time_;
314
315         MetadataObjManager metadata_obj_manager_;
316
317 #ifdef ENABLE_FLOW
318         RefPtr<Pipeline> pipeline_;
319         RefPtr<Element> source_;
320         RefPtr<AppSink> sink_;
321
322         mutable mutex pipeline_done_mutex_;
323         mutable condition_variable pipeline_done_cond_;
324         atomic<bool> pipeline_done_interrupt_;
325 #endif
326 };
327
328 } // namespace pv
329
330 #endif // PULSEVIEW_PV_SESSION_HPP