]> sigrok.org Git - pulseview.git/blame - pv/session.hpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / session.hpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c 3 *
1bc6525b 4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
2953961c
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
2953961c
JH
18 */
19
8e99ef96
JH
20#ifndef PULSEVIEW_PV_SESSION_HPP
21#define PULSEVIEW_PV_SESSION_HPP
2953961c 22
5f9a5209
SA
23#ifdef ENABLE_FLOW
24#include <atomic>
25#include <condition_variable>
26#endif
27
554af71b 28#include <deque>
7671200b 29#include <functional>
708c5523 30#include <map>
f9abf97e 31#include <memory>
3b68d03d 32#include <mutex>
bfc9f61e 33#include <set>
28a4c9c5 34#include <string>
3b68d03d 35#include <thread>
e3f65ace 36#include <vector>
28a4c9c5 37
105ecffd
SA
38#include <glibmm/datetime.h>
39
009e1503 40#include <QObject>
101e7a9b 41#include <QSettings>
f2edb557 42#include <QString>
5322d2d0 43#include <QElapsedTimer>
009e1503 44
5f9a5209
SA
45#ifdef ENABLE_FLOW
46#include <gstreamermm.h>
47#include <libsigrokflow/libsigrokflow.hpp>
48#endif
49
f2739bae 50#include "metadata_obj.hpp"
48257a69 51#include "util.hpp"
f4e57597 52#include "views/viewbase.hpp"
48257a69 53
554af71b 54using std::deque;
6f925ba9 55using std::function;
6f925ba9
UH
56using std::map;
57using std::mutex;
58using std::recursive_mutex;
59using std::shared_ptr;
60using std::string;
61using std::unordered_set;
62
5f9a5209
SA
63#ifdef ENABLE_FLOW
64using Glib::RefPtr;
65using Gst::AppSink;
66using Gst::Element;
67using Gst::Pipeline;
68#endif
69
82c7f640 70struct srd_decoder;
8bd26d8b 71struct srd_channel;
82c7f640 72
e8d00928 73namespace sigrok {
7d80a651
JH
74class Analog;
75class Channel;
76class Device;
fd22c71c 77class InputFormat;
7d80a651
JH
78class Logic;
79class Meta;
724f29f3 80class Option;
fd22c71c 81class OutputFormat;
7d80a651
JH
82class Packet;
83class Session;
870ea3db 84} // namespace sigrok
e8d00928 85
724f29f3
GS
86using sigrok::Option;
87
51e77110
JH
88namespace pv {
89
dc0867ff
JH
90class DeviceManager;
91
1b1ec774
JH
92namespace data {
93class Analog;
f3d66e52 94class AnalogSegment;
ad908057 95class DecodeSignal;
1b1ec774 96class Logic;
f3d66e52 97class LogicSegment;
bf0edd2b 98class SignalBase;
02412f0b 99class SignalData;
554af71b 100class SignalGroup;
1b1ec774 101}
8d634081 102
da30ecb7
JH
103namespace devices {
104class Device;
105}
106
0f8f8c18
SA
107namespace toolbars {
108class MainBar;
109}
110
f4e57597
SA
111namespace views {
112class ViewBase;
8d634081 113}
2953961c 114
2c5b0f46 115using pv::views::ViewType;
f2739bae 116
2b81ae46 117class Session : public QObject
2953961c 118{
009e1503
JH
119 Q_OBJECT
120
5b7cf66c
JH
121public:
122 enum capture_state {
123 Stopped,
2b49eeb0 124 AwaitingTrigger,
5b7cf66c
JH
125 Running
126 };
127
419ec4e1
SA
128 static shared_ptr<sigrok::Context> sr_context;
129
2953961c 130public:
101e7a9b 131 Session(DeviceManager &device_manager, QString name);
2953961c 132
2b81ae46 133 ~Session();
2953961c 134
4cb0b033
JH
135 DeviceManager& device_manager();
136
137 const DeviceManager& device_manager() const;
138
6f925ba9 139 shared_ptr<sigrok::Session> session() const;
1f4caa77 140
6f925ba9 141 shared_ptr<devices::Device> device() const;
dc0867ff 142
101e7a9b 143 QString name() const;
101e7a9b
SA
144 void set_name(QString name);
145
e6d85b89
SA
146 QString save_path() const;
147 void set_save_path(QString path);
2cd6be62 148
2db6e874 149 const vector< shared_ptr<views::ViewBase> > views() const;
36a8185e 150
6f925ba9 151 shared_ptr<views::ViewBase> main_view() const;
6f925ba9 152 shared_ptr<pv::toolbars::MainBar> main_bar() const;
6f925ba9 153 void set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar);
fd22c71c 154
5ccfc97e
SA
155 /**
156 * Indicates whether the captured data was saved to disk already or not
157 */
158 bool data_saved() const;
159
323715c4 160 void save_setup(QSettings &settings) const;
101e7a9b 161 void save_settings(QSettings &settings) const;
323715c4 162 void restore_setup(QSettings &settings);
101e7a9b
SA
163 void restore_settings(QSettings &settings);
164
fd22c71c
SA
165 /**
166 * Attempts to set device instance, may fall back to demo if needed
167 */
6f925ba9 168 void select_device(shared_ptr<devices::Device> device);
fd22c71c 169
d64d1596
JH
170 /**
171 * Sets device instance that will be used in the next capture session.
172 */
6f925ba9 173 void set_device(shared_ptr<devices::Device> device);
6fd0b639 174 void set_default_device();
31d1aca6
SA
175 bool using_file_device() const;
176
96dbf014 177 void load_init_file(const string &file_name, const string &format,
611c8625 178 const string &setup_file_name);
fd22c71c 179
96dbf014 180 void load_file(QString file_name, QString setup_file_name = QString(),
6f925ba9
UH
181 shared_ptr<sigrok::InputFormat> format = nullptr,
182 const map<string, Glib::VariantBase> &options =
183 map<string, Glib::VariantBase>());
fd22c71c 184
5b7cf66c 185 capture_state get_capture_state() const;
6f925ba9 186 void start_capture(function<void (const QString)> error_handler);
5b7cf66c
JH
187 void stop_capture();
188
2220e942 189 double get_samplerate() const;
105ecffd 190 Glib::DateTime get_acquisition_start_time() const;
2220e942 191
4640a84e
SA
192 uint32_t get_highest_segment_id() const;
193 uint64_t get_segment_sample_count(uint32_t segment_id) const;
7f965464 194
7ea2a4ff
SA
195 vector<util::Timestamp> get_triggers(uint32_t segment_id) const;
196
6f925ba9 197 void register_view(shared_ptr<views::ViewBase> view);
6f925ba9 198 void deregister_view(shared_ptr<views::ViewBase> view);
6f925ba9 199 bool has_view(shared_ptr<views::ViewBase> view);
101e7a9b 200
56c4de9a 201 const vector< shared_ptr<data::SignalBase> > signalbases() const;
4640a84e
SA
202 uint32_t get_signal_count(data::SignalBase::ChannelType type) const;
203 uint32_t get_next_signal_index(data::SignalBase::ChannelType type);
204
999869aa
SA
205 void add_generated_signal(shared_ptr<data::SignalBase> signal);
206 void remove_generated_signal(shared_ptr<data::SignalBase> signal);
e3f65ace 207
269528f5 208#ifdef ENABLE_DECODE
e771b42d 209 shared_ptr<data::DecodeSignal> add_decode_signal();
82c7f640 210
ad908057 211 void remove_decode_signal(shared_ptr<data::DecodeSignal> signal);
269528f5 212#endif
c51482b3 213
554af71b
SA
214 bool all_segments_complete(uint32_t segment_id) const;
215
f2739bae
SA
216 MetadataObjManager* metadata_obj_manager();
217
6ac96c2e
JH
218private:
219 void set_capture_state(capture_state state);
220
ffe00119 221 void update_signals();
b087ba7f 222
6f925ba9
UH
223 shared_ptr<data::SignalBase> signalbase_from_channel(
224 shared_ptr<sigrok::Channel> channel) const;
3ddcc083 225
724f29f3
GS
226 static map<string, Glib::VariantBase> input_format_options(
227 vector<string> user_spec,
228 map<string, shared_ptr<Option>> fmt_opts);
229
6f925ba9 230 void sample_thread_proc(function<void (const QString)> error_handler);
2e2946fe 231
5e6967cb
SA
232 void free_unused_memory();
233
4e86ec70 234 void signal_new_segment();
558ad6ce 235 void signal_segment_completed();
4e86ec70 236
5f9a5209
SA
237#ifdef ENABLE_FLOW
238 bool on_gst_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message);
239
240 Gst::FlowReturn on_gst_new_sample();
241#endif
242
da30ecb7 243 void feed_in_header();
6f925ba9 244 void feed_in_meta(shared_ptr<sigrok::Meta> meta);
48257a69 245 void feed_in_trigger();
82f50b10 246 void feed_in_frame_begin();
837bb15a 247 void feed_in_frame_end();
6f925ba9 248 void feed_in_logic(shared_ptr<sigrok::Logic> logic);
6f925ba9 249 void feed_in_analog(shared_ptr<sigrok::Analog> analog);
2953961c 250
6f925ba9
UH
251 void data_feed_in(shared_ptr<sigrok::Device> device,
252 shared_ptr<sigrok::Packet> packet);
2953961c 253
056f443e
SA
254Q_SIGNALS:
255 void capture_state_changed(int state);
256 void device_changed();
257
258 void signals_changed();
259
260 void name_changed();
261
7ea2a4ff 262 void trigger_event(int segment_id, util::Timestamp location);
056f443e
SA
263
264 void new_segment(int new_segment_id);
265 void segment_completed(int segment_id);
266
267 void data_received();
268
2c5b0f46 269 void add_view(ViewType type, Session *session);
ae8dd875 270 void session_error_raised(const QString text, const QString info_text);
056f443e
SA
271
272public Q_SLOTS:
273 void on_data_saved();
274
97378470
SA
275#ifdef ENABLE_DECODE
276 void on_new_decoders_selected(vector<const srd_decoder*> decoders);
277#endif
278
2953961c 279private:
f9a0fd83
SA
280 bool shutting_down_;
281
8dbbc7f0 282 DeviceManager &device_manager_;
6f925ba9 283 shared_ptr<devices::Device> device_;
e6d85b89 284 QString default_name_, name_, save_path_;
d64d1596 285
2db6e874 286 vector< shared_ptr<views::ViewBase> > views_;
6f925ba9 287 shared_ptr<pv::views::ViewBase> main_view_;
0f8f8c18 288
6f925ba9 289 shared_ptr<pv::toolbars::MainBar> main_bar_;
47e9e7bb 290
578d0735 291 mutable mutex sampling_mutex_; //!< Protects access to capture_state_
8dbbc7f0 292 capture_state capture_state_;
5b7cf66c 293
56c4de9a 294 vector< shared_ptr<data::SignalBase> > signalbases_;
6f925ba9 295 unordered_set< shared_ptr<data::SignalData> > all_signal_data_;
554af71b 296 deque<data::SignalGroup*> signal_groups_;
4640a84e 297 map<uint8_t, uint32_t> next_index_list_; // signal type -> index counter
3868e5fa 298
578d0735 299 /// trigger_list_ contains pairs of <segment_id, timestamp> values
7ea2a4ff
SA
300 vector< std::pair<uint32_t, util::Timestamp> > trigger_list_;
301
6f925ba9
UH
302 mutable recursive_mutex data_mutex_;
303 shared_ptr<data::Logic> logic_data_;
ff008de6 304 uint64_t cur_samplerate_;
6f925ba9
UH
305 shared_ptr<data::LogicSegment> cur_logic_segment_;
306 map< shared_ptr<sigrok::Channel>, shared_ptr<data::AnalogSegment> >
f3d66e52 307 cur_analog_segments_;
4e86ec70 308 int32_t highest_segment_id_;
4640a84e 309 vector<uint64_t> segment_sample_count_;
009e1503 310
8dbbc7f0 311 std::thread sampling_thread_;
2e2946fe 312
e7216ae0 313 bool out_of_memory_;
5ccfc97e 314 bool data_saved_;
837bb15a 315 bool frame_began_;
5f9a5209 316
5322d2d0 317 QElapsedTimer acq_time_;
105ecffd 318 Glib::DateTime acq_start_time_;
268fd2b8 319
f2739bae
SA
320 MetadataObjManager metadata_obj_manager_;
321
5f9a5209
SA
322#ifdef ENABLE_FLOW
323 RefPtr<Pipeline> pipeline_;
324 RefPtr<Element> source_;
325 RefPtr<AppSink> sink_;
326
327 mutable mutex pipeline_done_mutex_;
328 mutable condition_variable pipeline_done_cond_;
329 atomic<bool> pipeline_done_interrupt_;
330#endif
2953961c
JH
331};
332
51e77110
JH
333} // namespace pv
334
8e99ef96 335#endif // PULSEVIEW_PV_SESSION_HPP