sigsession.h 9.63 KB
Newer Older
DreamSourceLab's avatar
DreamSourceLab committed
1
/*
DreamSourceLab's avatar
DreamSourceLab committed
2 3
 * This file is part of the DSView project.
 * DSView is based on PulseView.
DreamSourceLab's avatar
DreamSourceLab committed
4 5
 *
 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6
 * Copyright (C) 2013 DreamSourceLab <support@dreamsourcelab.com>
DreamSourceLab's avatar
DreamSourceLab committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

DreamSourceLab's avatar
DreamSourceLab committed
23 24
#ifndef DSVIEW_PV_SIGSESSION_H
#define DSVIEW_PV_SIGSESSION_H
DreamSourceLab's avatar
DreamSourceLab committed
25 26 27 28 29 30 31 32

#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/thread.hpp>

#include <string>
#include <utility>
DreamSourceLab's avatar
DreamSourceLab committed
33 34 35
#include <map>
#include <set>
#include <string>
DreamSourceLab's avatar
DreamSourceLab committed
36
#include <vector>
37
#include <stdint.h>
DreamSourceLab's avatar
DreamSourceLab committed
38 39 40 41 42 43 44

#include <QObject>
#include <QString>
#include <QLine>
#include <QVector>
#include <QMap>
#include <QVariant>
DreamSourceLab's avatar
DreamSourceLab committed
45
#include <QTimer>
46
#include <QtConcurrent/QtConcurrent>
47
#include <QJsonObject>
DreamSourceLab's avatar
DreamSourceLab committed
48

DreamSourceLab's avatar
DreamSourceLab committed
49
#include <libsigrok4DSL/libsigrok.h>
50
#include <libusb.h>
DreamSourceLab's avatar
DreamSourceLab committed
51

DreamSourceLab's avatar
DreamSourceLab committed
52 53 54
struct srd_decoder;
struct srd_channel;

DreamSourceLab's avatar
DreamSourceLab committed
55 56 57 58 59
namespace pv {

class DeviceManager;

namespace data {
DreamSourceLab's avatar
DreamSourceLab committed
60
class SignalData;
61
class Snapshot;
DreamSourceLab's avatar
DreamSourceLab committed
62 63
class Analog;
class AnalogSnapshot;
64 65
class Dso;
class DsoSnapshot;
DreamSourceLab's avatar
DreamSourceLab committed
66 67 68 69
class Logic;
class LogicSnapshot;
class Group;
class GroupSnapshot;
70
class DecoderModel;
DreamSourceLab's avatar
DreamSourceLab committed
71 72
}

DreamSourceLab's avatar
DreamSourceLab committed
73 74 75 76
namespace device {
class DevInst;
}

DreamSourceLab's avatar
DreamSourceLab committed
77 78
namespace view {
class Signal;
DreamSourceLab's avatar
DreamSourceLab committed
79 80
class GroupSignal;
class DecodeTrace;
DreamSourceLab's avatar
DreamSourceLab committed
81
class MathTrace;
DreamSourceLab's avatar
DreamSourceLab committed
82 83 84 85 86 87 88 89 90 91 92 93
}

namespace decoder {
class Decoder;
class DecoderFactory;
}

class SigSession : public QObject
{
        Q_OBJECT

private:
94
    static constexpr float Oversampling = 2.0f;
95
    static const int RefreshTime = 500;
96
    static const int RepeatHoldDiv = 20;
DreamSourceLab's avatar
DreamSourceLab committed
97

98 99
public:
    static const int ViewTime = 50;
100
    static const int WaitShowTime = 500;
101

DreamSourceLab's avatar
DreamSourceLab committed
102 103 104 105 106 107 108
public:
	enum capture_state {
        Init,
		Stopped,
		Running
	};

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    enum run_mode {
        Single,
        Repetitive
    };

    enum error_state {
        No_err,
        Hw_err,
        Malloc_err,
        Test_data_err,
        Test_timeout_err,
        Pkt_data_err,
        Data_overflow
    };

DreamSourceLab's avatar
DreamSourceLab committed
124 125 126 127 128
public:
	SigSession(DeviceManager &device_manager);

	~SigSession();

DreamSourceLab's avatar
DreamSourceLab committed
129
    boost::shared_ptr<device::DevInst> get_device() const;
DreamSourceLab's avatar
DreamSourceLab committed
130 131 132 133

	/**
	 * Sets device instance that will be used in the next capture session.
	 */
DreamSourceLab's avatar
DreamSourceLab committed
134 135
    void set_device(boost::shared_ptr<device::DevInst> dev_inst)
        throw(QString);
DreamSourceLab's avatar
DreamSourceLab committed
136

137
    void set_file(QString name)
DreamSourceLab's avatar
DreamSourceLab committed
138
        throw(QString);
DreamSourceLab's avatar
DreamSourceLab committed
139

DreamSourceLab's avatar
DreamSourceLab committed
140
    void set_default_device(boost::function<void (const QString)> error_handler);
141

DreamSourceLab's avatar
DreamSourceLab committed
142 143
    void release_device(device::DevInst *dev_inst);

DreamSourceLab's avatar
DreamSourceLab committed
144 145
	capture_state get_capture_state() const;

146 147 148
    uint64_t cur_samplerate() const;
    uint64_t cur_samplelimits() const;
    double cur_sampletime() const;
149 150
    void set_cur_samplerate(uint64_t samplerate);
    void set_cur_samplelimits(uint64_t samplelimits);
151 152
    QDateTime get_trigger_time() const;
    uint64_t get_trigger_pos() const;
153

DreamSourceLab's avatar
DreamSourceLab committed
154
    void start_capture(bool instant,
DreamSourceLab's avatar
DreamSourceLab committed
155
		boost::function<void (const QString)> error_handler);
156
    void capture_init();
157 158
    bool get_capture_status(bool &triggered, int &progress);
    void logic_init();
DreamSourceLab's avatar
DreamSourceLab committed
159

DreamSourceLab's avatar
DreamSourceLab committed
160 161
    std::set< boost::shared_ptr<data::SignalData> > get_data() const;

DreamSourceLab's avatar
DreamSourceLab committed
162 163 164
	std::vector< boost::shared_ptr<view::Signal> >
		get_signals();

DreamSourceLab's avatar
DreamSourceLab committed
165 166
    std::vector< boost::shared_ptr<view::GroupSignal> >
        get_group_signals();
DreamSourceLab's avatar
DreamSourceLab committed
167

DreamSourceLab's avatar
DreamSourceLab committed
168
#ifdef ENABLE_DECODE
169
    bool add_decoder(srd_decoder *const dec, bool silent);
DreamSourceLab's avatar
DreamSourceLab committed
170

DreamSourceLab's avatar
DreamSourceLab committed
171 172
    std::vector< boost::shared_ptr<view::DecodeTrace> >
        get_decode_signals() const;
DreamSourceLab's avatar
DreamSourceLab committed
173

DreamSourceLab's avatar
DreamSourceLab committed
174
    void remove_decode_signal(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
175

DreamSourceLab's avatar
DreamSourceLab committed
176
    void remove_decode_signal(int index);
DreamSourceLab's avatar
DreamSourceLab committed
177

DreamSourceLab's avatar
DreamSourceLab committed
178
    void rst_decoder(int index);
DreamSourceLab's avatar
DreamSourceLab committed
179

DreamSourceLab's avatar
DreamSourceLab committed
180
    void rst_decoder(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
181

182
    pv::data::DecoderModel* get_decoder_model() const;
183
#endif
184

DreamSourceLab's avatar
DreamSourceLab committed
185 186 187
    std::vector< boost::shared_ptr<view::MathTrace> >
        get_math_signals();

DreamSourceLab's avatar
DreamSourceLab committed
188
    void init_signals();
DreamSourceLab's avatar
DreamSourceLab committed
189

DreamSourceLab's avatar
DreamSourceLab committed
190
    void add_group();
DreamSourceLab's avatar
DreamSourceLab committed
191

DreamSourceLab's avatar
DreamSourceLab committed
192
    void del_group();
DreamSourceLab's avatar
DreamSourceLab committed
193

194 195
    void start_hotplug_proc(boost::function<void (const QString)> error_handler);
    void stop_hotplug_proc();
DreamSourceLab's avatar
DreamSourceLab committed
196
	void register_hotplug_callback();
197
    void deregister_hotplug_callback();
DreamSourceLab's avatar
DreamSourceLab committed
198

DreamSourceLab's avatar
DreamSourceLab committed
199
    uint16_t get_ch_num(int type);
DreamSourceLab's avatar
DreamSourceLab committed
200
    
DreamSourceLab's avatar
DreamSourceLab committed
201
    bool get_instant();
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
202

203
    bool get_data_lock();
DreamSourceLab's avatar
DreamSourceLab committed
204 205
    void mathTraces_rebuild();

206 207
    bool trigd() const;

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    boost::shared_ptr<data::Snapshot> get_snapshot(int type);

    error_state get_error() const;
    void set_error(error_state state);
    void clear_error();
    uint64_t get_error_pattern() const;

    run_mode get_run_mode() const;
    void set_run_mode(run_mode mode);
    int get_repeat_intvl() const;
    void set_repeat_intvl(int interval);
    bool isRepeating() const;
    bool repeat_check();
    int get_repeat_hold() const;

    int get_map_zoom() const;

DreamSourceLab's avatar
DreamSourceLab committed
225 226 227 228
private:
	void set_capture_state(capture_state state);

private:
DreamSourceLab's avatar
DreamSourceLab committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    /**
     * Attempts to autodetect the format. Failing that
     * @param filename The filename of the input file.
     * @return A pointer to the 'struct sr_input_format' that should be
     * 	used, or NULL if no input format was selected or
     * 	auto-detected.
     */
    static sr_input_format* determine_input_file_format(
        const std::string &filename);

    static sr_input* load_input_file_format(
        const std::string &filename,
        boost::function<void (const QString)> error_handler,
        sr_input_format *format = NULL);

    void sample_thread_proc(boost::shared_ptr<device::DevInst> dev_inst,
                            boost::function<void (const QString)> error_handler);
DreamSourceLab's avatar
DreamSourceLab committed
246

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
247
    // data feed
DreamSourceLab's avatar
DreamSourceLab committed
248 249 250
	void feed_in_header(const sr_dev_inst *sdi);
	void feed_in_meta(const sr_dev_inst *sdi,
		const sr_datafeed_meta &meta);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
251
    void feed_in_trigger(const ds_trigger_pos &trigger_pos);
DreamSourceLab's avatar
DreamSourceLab committed
252
	void feed_in_logic(const sr_datafeed_logic &logic);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
253
    void feed_in_dso(const sr_datafeed_dso &dso);
DreamSourceLab's avatar
DreamSourceLab committed
254 255
	void feed_in_analog(const sr_datafeed_analog &analog);
	void data_feed_in(const struct sr_dev_inst *sdi,
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
256 257 258
		const struct sr_datafeed_packet *packet);
	static void data_feed_in_proc(const struct sr_dev_inst *sdi,
		const struct sr_datafeed_packet *packet, void *cb_data);
DreamSourceLab's avatar
DreamSourceLab committed
259

DreamSourceLab's avatar
DreamSourceLab committed
260
    // thread for hotplug
DreamSourceLab's avatar
DreamSourceLab committed
261 262 263
    void hotplug_proc(boost::function<void (const QString)> error_handler);
    static int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
                                libusb_hotplug_event event, void *user_data);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
264

DreamSourceLab's avatar
DreamSourceLab committed
265 266 267 268 269 270
private:
	DeviceManager &_device_manager;

	/**
	 * The device instance that will be used in the next capture session.
	 */
DreamSourceLab's avatar
DreamSourceLab committed
271
    boost::shared_ptr<device::DevInst> _dev_inst;
DreamSourceLab's avatar
DreamSourceLab committed
272

273
    mutable boost::mutex _sampling_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
274
	capture_state _capture_state;
DreamSourceLab's avatar
DreamSourceLab committed
275
    bool _instant;
276 277
    uint64_t _cur_samplerate;
    uint64_t _cur_samplelimits;
DreamSourceLab's avatar
DreamSourceLab committed
278

279
    //mutable boost::mutex _signals_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
280
	std::vector< boost::shared_ptr<view::Signal> > _signals;
DreamSourceLab's avatar
DreamSourceLab committed
281
    std::vector< boost::shared_ptr<view::GroupSignal> > _group_traces;
DreamSourceLab's avatar
DreamSourceLab committed
282
#ifdef ENABLE_DECODE
DreamSourceLab's avatar
DreamSourceLab committed
283
    std::vector< boost::shared_ptr<view::DecodeTrace> > _decode_traces;
284
    pv::data::DecoderModel *_decoder_model;
DreamSourceLab's avatar
DreamSourceLab committed
285
#endif
DreamSourceLab's avatar
DreamSourceLab committed
286
    std::vector< boost::shared_ptr<view::MathTrace> > _math_traces;
DreamSourceLab's avatar
DreamSourceLab committed
287

DreamSourceLab's avatar
DreamSourceLab committed
288
    mutable boost::mutex _data_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
289 290
	boost::shared_ptr<data::Logic> _logic_data;
	boost::shared_ptr<data::LogicSnapshot> _cur_logic_snapshot;
291 292
    boost::shared_ptr<data::Dso> _dso_data;
    boost::shared_ptr<data::DsoSnapshot> _cur_dso_snapshot;
DreamSourceLab's avatar
DreamSourceLab committed
293 294 295 296 297 298
	boost::shared_ptr<data::Analog> _analog_data;
	boost::shared_ptr<data::AnalogSnapshot> _cur_analog_snapshot;
    boost::shared_ptr<data::Group> _group_data;
    boost::shared_ptr<data::GroupSnapshot> _cur_group_snapshot;
    int _group_cnt;

DreamSourceLab's avatar
DreamSourceLab committed
299
	std::unique_ptr<boost::thread> _sampling_thread;
DreamSourceLab's avatar
DreamSourceLab committed
300

DreamSourceLab's avatar
DreamSourceLab committed
301
	libusb_hotplug_callback_handle _hotplug_handle;
DreamSourceLab's avatar
DreamSourceLab committed
302
    std::unique_ptr<boost::thread> _hotplug;
DreamSourceLab's avatar
DreamSourceLab committed
303 304 305
    bool _hot_attach;
    bool _hot_detach;

DreamSourceLab's avatar
DreamSourceLab committed
306
    QTimer _view_timer;
307
    int    _noData_cnt;
308
    bool _data_lock;
309
    bool _data_updated;
DreamSourceLab's avatar
DreamSourceLab committed
310

311 312
    QDateTime _trigger_time;
    uint64_t _trigger_pos;
313
    bool _trigger_flag;
314 315 316 317 318 319 320 321 322 323 324
    bool _hw_replied;

    error_state _error;
    uint64_t _error_pattern;

    run_mode _run_mode;
    int _repeat_intvl;
    bool _repeating;
    int _repeat_hold_prg;

    int _map_zoom;
325

DreamSourceLab's avatar
DreamSourceLab committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339
signals:
	void capture_state_changed(int state);

	void signals_changed();

	void data_updated();

    void receive_data(quint64 length);

    void device_attach();
    void device_detach();

    void receive_trigger(quint64 trigger_pos);

340 341
    void receive_header();

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
342 343
    void dso_ch_changed(uint16_t num);

DreamSourceLab's avatar
DreamSourceLab committed
344 345 346
    void frame_began();

    void data_received();
DreamSourceLab's avatar
DreamSourceLab committed
347

DreamSourceLab's avatar
DreamSourceLab committed
348 349 350 351
    void frame_ended();

    void device_setted();

DreamSourceLab's avatar
DreamSourceLab committed
352
    void zero_adj();
353
    void progressSaveFileValueChanged(int percent);
DreamSourceLab's avatar
DreamSourceLab committed
354

355 356
    void decode_done();

357
    void show_region(uint64_t start, uint64_t end, bool keep);
358

359 360
    void show_wait_trigger();

361 362 363 364 365 366
    void session_save();

    void session_error();

    void repeat_hold(int percent);
    void repeat_resume();
367

DreamSourceLab's avatar
DreamSourceLab committed
368 369
public slots:
    void reload();
370
    void refresh(int holdtime);
371
    void stop_capture();
372 373 374
    // repeat
    void set_repeating(bool repeat);
    void set_map_zoom(int index);
DreamSourceLab's avatar
DreamSourceLab committed
375

376
private slots:
377
    void data_unlock();
378
    void check_update();
379
    void nodata_timeout();
380
    void repeat_update();
381

DreamSourceLab's avatar
DreamSourceLab committed
382 383 384 385 386 387 388 389 390
private:
	// TODO: This should not be necessary. Multiple concurrent
	// sessions should should be supported and it should be
	// possible to associate a pointer with a ds_session.
	static SigSession *_session;
};

} // namespace pv

DreamSourceLab's avatar
DreamSourceLab committed
391
#endif // DSVIEW_PV_SIGSESSION_H