sigsession.h 11.3 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
#include <libsigrok4DSL/libsigrok.h>
DreamSourceLab's avatar
DreamSourceLab committed
27 28 29 30 31 32 33
#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
34 35 36
#include <map>
#include <set>
#include <string>
DreamSourceLab's avatar
DreamSourceLab committed
37
#include <vector>
38
#include <stdint.h>
DreamSourceLab's avatar
DreamSourceLab committed
39 40 41 42 43 44 45

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

50
#include <libusb.h>
DreamSourceLab's avatar
DreamSourceLab committed
51

Andy Dneg's avatar
Andy Dneg committed
52 53 54
#include "view/mathtrace.h"
#include "data/mathstack.h"

DreamSourceLab's avatar
DreamSourceLab committed
55 56 57
struct srd_decoder;
struct srd_channel;

DreamSourceLab's avatar
DreamSourceLab committed
58 59 60 61 62
namespace pv {

class DeviceManager;

namespace data {
DreamSourceLab's avatar
DreamSourceLab committed
63
class SignalData;
64
class Snapshot;
DreamSourceLab's avatar
DreamSourceLab committed
65 66
class Analog;
class AnalogSnapshot;
67 68
class Dso;
class DsoSnapshot;
DreamSourceLab's avatar
DreamSourceLab committed
69 70 71 72
class Logic;
class LogicSnapshot;
class Group;
class GroupSnapshot;
73
class DecoderModel;
Andy Dneg's avatar
Andy Dneg committed
74
class MathStack;
DreamSourceLab's avatar
DreamSourceLab committed
75 76
}

DreamSourceLab's avatar
DreamSourceLab committed
77 78 79 80
namespace device {
class DevInst;
}

DreamSourceLab's avatar
DreamSourceLab committed
81 82
namespace view {
class Signal;
DreamSourceLab's avatar
DreamSourceLab committed
83 84
class GroupSignal;
class DecodeTrace;
Andy Dneg's avatar
Andy Dneg committed
85 86
class SpectrumTrace;
class LissajousTrace;
DreamSourceLab's avatar
DreamSourceLab committed
87
class MathTrace;
DreamSourceLab's avatar
DreamSourceLab committed
88 89 90 91 92 93 94 95 96 97 98 99
}

namespace decoder {
class Decoder;
class DecoderFactory;
}

class SigSession : public QObject
{
        Q_OBJECT

private:
100
    static constexpr float Oversampling = 2.0f;
101
    static const int RefreshTime = 500;
102
    static const int RepeatHoldDiv = 20;
DreamSourceLab's avatar
DreamSourceLab committed
103

104
public:
105
    static const int FeedInterval = 50;
106
    static const int WaitShowTime = 500;
107

DreamSourceLab's avatar
DreamSourceLab committed
108 109 110 111 112 113 114
public:
	enum capture_state {
        Init,
		Stopped,
		Running
	};

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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
130 131 132 133 134
public:
	SigSession(DeviceManager &device_manager);

	~SigSession();

DreamSourceLab's avatar
DreamSourceLab committed
135
    boost::shared_ptr<device::DevInst> get_device() const;
DreamSourceLab's avatar
DreamSourceLab committed
136 137 138 139

	/**
	 * Sets device instance that will be used in the next capture session.
	 */
Andy Dneg's avatar
Andy Dneg committed
140 141 142
    void set_device(boost::shared_ptr<device::DevInst> dev_inst);

    void set_file(QString name);
DreamSourceLab's avatar
DreamSourceLab committed
143

Andy Dneg's avatar
Andy Dneg committed
144
    void close_file(boost::shared_ptr<pv::device::DevInst> dev_inst);
DreamSourceLab's avatar
DreamSourceLab committed
145

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

DreamSourceLab's avatar
DreamSourceLab committed
148 149
    void release_device(device::DevInst *dev_inst);

DreamSourceLab's avatar
DreamSourceLab committed
150 151
	capture_state get_capture_state() const;

152
    uint64_t cur_samplerate() const;
153
    uint64_t cur_snap_samplerate() const;
154 155
    uint64_t cur_samplelimits() const;
    double cur_sampletime() const;
156 157 158 159
    double cur_snap_sampletime() const;
    double cur_view_time() const;

    void set_cur_snap_samplerate(uint64_t samplerate);
160
    void set_cur_samplelimits(uint64_t samplelimits);
161 162
    void set_session_time(QDateTime time);
    QDateTime get_session_time() const;
163
    uint64_t get_trigger_pos() const;
164

DreamSourceLab's avatar
DreamSourceLab committed
165
    void start_capture(bool instant,
DreamSourceLab's avatar
DreamSourceLab committed
166
		boost::function<void (const QString)> error_handler);
167
    void capture_init();
168
    bool get_capture_status(bool &triggered, int &progress);
169
    void container_init();
DreamSourceLab's avatar
DreamSourceLab committed
170

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

DreamSourceLab's avatar
DreamSourceLab committed
173 174 175
	std::vector< boost::shared_ptr<view::Signal> >
		get_signals();

DreamSourceLab's avatar
DreamSourceLab committed
176 177
    std::vector< boost::shared_ptr<view::GroupSignal> >
        get_group_signals();
DreamSourceLab's avatar
DreamSourceLab committed
178

DreamSourceLab's avatar
DreamSourceLab committed
179
#ifdef ENABLE_DECODE
180
    bool add_decoder(srd_decoder *const dec, bool silent);
DreamSourceLab's avatar
DreamSourceLab committed
181

DreamSourceLab's avatar
DreamSourceLab committed
182 183
    std::vector< boost::shared_ptr<view::DecodeTrace> >
        get_decode_signals() const;
DreamSourceLab's avatar
DreamSourceLab committed
184

DreamSourceLab's avatar
DreamSourceLab committed
185
    void remove_decode_signal(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
186

DreamSourceLab's avatar
DreamSourceLab committed
187
    void remove_decode_signal(int index);
DreamSourceLab's avatar
DreamSourceLab committed
188

DreamSourceLab's avatar
DreamSourceLab committed
189
    void rst_decoder(int index);
DreamSourceLab's avatar
DreamSourceLab committed
190

DreamSourceLab's avatar
DreamSourceLab committed
191
    void rst_decoder(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
192

193
    pv::data::DecoderModel* get_decoder_model() const;
194
#endif
195

Andy Dneg's avatar
Andy Dneg committed
196 197 198 199 200 201 202 203
    std::vector< boost::shared_ptr<view::SpectrumTrace> >
        get_spectrum_traces();

    boost::shared_ptr<view::LissajousTrace>
        get_lissajous_trace();

    boost::shared_ptr<view::MathTrace>
        get_math_trace();
DreamSourceLab's avatar
DreamSourceLab committed
204

DreamSourceLab's avatar
DreamSourceLab committed
205
    void init_signals();
DreamSourceLab's avatar
DreamSourceLab committed
206

DreamSourceLab's avatar
DreamSourceLab committed
207
    void add_group();
DreamSourceLab's avatar
DreamSourceLab committed
208

DreamSourceLab's avatar
DreamSourceLab committed
209
    void del_group();
DreamSourceLab's avatar
DreamSourceLab committed
210

211 212
    void start_hotplug_proc(boost::function<void (const QString)> error_handler);
    void stop_hotplug_proc();
DreamSourceLab's avatar
DreamSourceLab committed
213
	void register_hotplug_callback();
214
    void deregister_hotplug_callback();
DreamSourceLab's avatar
DreamSourceLab committed
215

DreamSourceLab's avatar
DreamSourceLab committed
216
    uint16_t get_ch_num(int type);
DreamSourceLab's avatar
DreamSourceLab committed
217
    
DreamSourceLab's avatar
DreamSourceLab committed
218
    bool get_instant();
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
219

220
    bool get_data_lock();
221 222 223
    void data_auto_lock(int lock);
    void data_auto_unlock();
    bool get_data_auto_lock();
Andy Dneg's avatar
Andy Dneg committed
224 225 226 227 228 229 230 231
    void spectrum_rebuild();
    void lissajous_rebuild(bool enable, int xindex, int yindex, double percent);
    void lissajous_disable();
    void math_rebuild(bool enable,
                      boost::shared_ptr<pv::view::DsoSignal> dsoSig1,
                      boost::shared_ptr<pv::view::DsoSignal> dsoSig2,
                      data::MathStack::MathType type);
    void math_disable();
DreamSourceLab's avatar
DreamSourceLab committed
232

233
    bool trigd() const;
234
    uint8_t trigd_ch() const;
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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;

Andy Dneg's avatar
Andy Dneg committed
253 254 255 256
    void set_save_start(uint64_t start);
    void set_save_end(uint64_t end);
    uint64_t get_save_start() const;
    uint64_t get_save_end() const;
257 258
    bool get_saving() const;
    void set_saving(bool saving);
259 260 261 262
    void set_stop_scale(float scale);
    float stop_scale() const;

    void exit_capture();
Andy Dneg's avatar
Andy Dneg committed
263

DreamSourceLab's avatar
DreamSourceLab committed
264 265 266 267
private:
	void set_capture_state(capture_state state);

private:
DreamSourceLab's avatar
DreamSourceLab committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    /**
     * 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
285

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
286
    // data feed
DreamSourceLab's avatar
DreamSourceLab committed
287 288 289
	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
290
    void feed_in_trigger(const ds_trigger_pos &trigger_pos);
DreamSourceLab's avatar
DreamSourceLab committed
291
	void feed_in_logic(const sr_datafeed_logic &logic);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
292
    void feed_in_dso(const sr_datafeed_dso &dso);
DreamSourceLab's avatar
DreamSourceLab committed
293 294
	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
295 296 297
		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
298

DreamSourceLab's avatar
DreamSourceLab committed
299
    // thread for hotplug
DreamSourceLab's avatar
DreamSourceLab committed
300 301 302
    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
303

DreamSourceLab's avatar
DreamSourceLab committed
304 305 306 307 308 309
private:
	DeviceManager &_device_manager;

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

312
    mutable boost::mutex _sampling_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
313
	capture_state _capture_state;
DreamSourceLab's avatar
DreamSourceLab committed
314
    bool _instant;
315
    uint64_t _cur_snap_samplerate;
316
    uint64_t _cur_samplelimits;
DreamSourceLab's avatar
DreamSourceLab committed
317

318
    //mutable boost::mutex _signals_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
319
	std::vector< boost::shared_ptr<view::Signal> > _signals;
DreamSourceLab's avatar
DreamSourceLab committed
320
    std::vector< boost::shared_ptr<view::GroupSignal> > _group_traces;
DreamSourceLab's avatar
DreamSourceLab committed
321
#ifdef ENABLE_DECODE
DreamSourceLab's avatar
DreamSourceLab committed
322
    std::vector< boost::shared_ptr<view::DecodeTrace> > _decode_traces;
323
    pv::data::DecoderModel *_decoder_model;
DreamSourceLab's avatar
DreamSourceLab committed
324
#endif
Andy Dneg's avatar
Andy Dneg committed
325 326 327
    std::vector< boost::shared_ptr<view::SpectrumTrace> > _spectrum_traces;
    boost::shared_ptr<view::LissajousTrace> _lissajous_trace;
    boost::shared_ptr<view::MathTrace> _math_trace;
DreamSourceLab's avatar
DreamSourceLab committed
328

DreamSourceLab's avatar
DreamSourceLab committed
329
    mutable boost::mutex _data_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
330 331
	boost::shared_ptr<data::Logic> _logic_data;
	boost::shared_ptr<data::LogicSnapshot> _cur_logic_snapshot;
332 333
    boost::shared_ptr<data::Dso> _dso_data;
    boost::shared_ptr<data::DsoSnapshot> _cur_dso_snapshot;
DreamSourceLab's avatar
DreamSourceLab committed
334 335 336 337 338 339
	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
340
	std::unique_ptr<boost::thread> _sampling_thread;
DreamSourceLab's avatar
DreamSourceLab committed
341

DreamSourceLab's avatar
DreamSourceLab committed
342
	libusb_hotplug_callback_handle _hotplug_handle;
DreamSourceLab's avatar
DreamSourceLab committed
343
    std::unique_ptr<boost::thread> _hotplug;
DreamSourceLab's avatar
DreamSourceLab committed
344 345 346
    bool _hot_attach;
    bool _hot_detach;

347
    QTimer _feed_timer;
348
    int    _noData_cnt;
349
    bool _data_lock;
350
    bool _data_updated;
351
    int _data_auto_lock;
DreamSourceLab's avatar
DreamSourceLab committed
352

353
    QDateTime _session_time;
354
    uint64_t _trigger_pos;
355
    bool _trigger_flag;
356
    uint8_t _trigger_ch;
357 358 359 360 361 362 363 364 365 366 367
    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;
368

Andy Dneg's avatar
Andy Dneg committed
369 370
    uint64_t _save_start;
    uint64_t _save_end;
371
    bool _saving;
Andy Dneg's avatar
Andy Dneg committed
372 373

    bool _dso_feed;
374
    float _stop_scale;
Andy Dneg's avatar
Andy Dneg committed
375

DreamSourceLab's avatar
DreamSourceLab committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389
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);

390 391
    void receive_header();

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
392 393
    void dso_ch_changed(uint16_t num);

DreamSourceLab's avatar
DreamSourceLab committed
394 395 396
    void frame_began();

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

DreamSourceLab's avatar
DreamSourceLab committed
398 399 400 401
    void frame_ended();

    void device_setted();

DreamSourceLab's avatar
DreamSourceLab committed
402
    void zero_adj();
403
    void progressSaveFileValueChanged(int percent);
DreamSourceLab's avatar
DreamSourceLab committed
404

405 406
    void decode_done();

407
    void show_region(uint64_t start, uint64_t end, bool keep);
408

409 410
    void show_wait_trigger();

411 412 413 414 415 416
    void session_save();

    void session_error();

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

418
    void cur_snap_samplerate_changed();
419 420 421

    void update_capture();

DreamSourceLab's avatar
DreamSourceLab committed
422 423
public slots:
    void reload();
424
    void refresh(int holdtime);
425
    void stop_capture();
426
    void check_update();
427 428 429
    // repeat
    void set_repeating(bool repeat);
    void set_map_zoom(int index);
Andy Dneg's avatar
Andy Dneg committed
430 431
    // OSC auto
    void auto_end();
DreamSourceLab's avatar
DreamSourceLab committed
432

433
private slots:
434
    void data_lock();
435
    void data_unlock();
436
    void nodata_timeout();
437
    void feed_timeout();
438
    void repeat_update();
439

DreamSourceLab's avatar
DreamSourceLab committed
440 441 442 443 444 445 446 447 448
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
449
#endif // DSVIEW_PV_SIGSESSION_H