sigsession.h 7.18 KB
Newer Older
DreamSourceLab's avatar
DreamSourceLab committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * This file is part of the DSLogic-gui project.
 * DSLogic-gui is based on PulseView.
 *
 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
 * Copyright (C) 2013 DreamSourceLab <dreamsourcelab@dreamsourcelab.com>
 *
 * 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
 */


#ifndef DSLOGIC_PV_SIGSESSION_H
#define DSLOGIC_PV_SIGSESSION_H

#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 38 39 40 41 42 43 44 45 46
#include <vector>

#include <QObject>
#include <QString>
#include <QLine>
#include <QVector>
#include <QMap>
#include <QVariant>

#include <libsigrok4DSLogic/libsigrok.h>
47
#include <libusb.h>
DreamSourceLab's avatar
DreamSourceLab committed
48

DreamSourceLab's avatar
DreamSourceLab committed
49 50 51
struct srd_decoder;
struct srd_channel;

DreamSourceLab's avatar
DreamSourceLab committed
52 53 54 55 56
namespace pv {

class DeviceManager;

namespace data {
DreamSourceLab's avatar
DreamSourceLab committed
57
class SignalData;
DreamSourceLab's avatar
DreamSourceLab committed
58 59
class Analog;
class AnalogSnapshot;
60 61
class Dso;
class DsoSnapshot;
DreamSourceLab's avatar
DreamSourceLab committed
62 63 64 65 66 67
class Logic;
class LogicSnapshot;
class Group;
class GroupSnapshot;
}

DreamSourceLab's avatar
DreamSourceLab committed
68 69 70 71
namespace device {
class DevInst;
}

DreamSourceLab's avatar
DreamSourceLab committed
72 73
namespace view {
class Signal;
DreamSourceLab's avatar
DreamSourceLab committed
74 75
class GroupSignal;
class DecodeTrace;
DreamSourceLab's avatar
DreamSourceLab committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

namespace decoder {
class Decoder;
class DecoderFactory;
}

class SigSession : public QObject
{
        Q_OBJECT

private:
    static const float Oversampling;

public:
	enum capture_state {
        Init,
		Stopped,
		Running
	};

public:
	SigSession(DeviceManager &device_manager);

	~SigSession();

DreamSourceLab's avatar
DreamSourceLab committed
102
    boost::shared_ptr<device::DevInst> get_device() const;
DreamSourceLab's avatar
DreamSourceLab committed
103 104 105 106

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

DreamSourceLab's avatar
DreamSourceLab committed
110 111
    void set_file(const std::string &name)
        throw(QString);
DreamSourceLab's avatar
DreamSourceLab committed
112 113 114

    void save_file(const std::string &name);

DreamSourceLab's avatar
DreamSourceLab committed
115 116 117 118
    void set_default_device();

    void release_device(device::DevInst *dev_inst);

DreamSourceLab's avatar
DreamSourceLab committed
119 120
	capture_state get_capture_state() const;

DreamSourceLab's avatar
DreamSourceLab committed
121
    void start_capture(bool instant,
DreamSourceLab's avatar
DreamSourceLab committed
122 123 124 125
		boost::function<void (const QString)> error_handler);

	void stop_capture();

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

DreamSourceLab's avatar
DreamSourceLab committed
128 129 130
	std::vector< boost::shared_ptr<view::Signal> >
		get_signals();

DreamSourceLab's avatar
DreamSourceLab committed
131 132
    std::vector< boost::shared_ptr<view::GroupSignal> >
        get_group_signals();
DreamSourceLab's avatar
DreamSourceLab committed
133

DreamSourceLab's avatar
DreamSourceLab committed
134 135
#ifdef ENABLE_DECODE
    bool add_decoder(srd_decoder *const dec);
DreamSourceLab's avatar
DreamSourceLab committed
136

DreamSourceLab's avatar
DreamSourceLab committed
137 138
    std::vector< boost::shared_ptr<view::DecodeTrace> >
        get_decode_signals() const;
DreamSourceLab's avatar
DreamSourceLab committed
139

DreamSourceLab's avatar
DreamSourceLab committed
140
    void remove_decode_signal(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
141

DreamSourceLab's avatar
DreamSourceLab committed
142
    void remove_decode_signal(int index);
DreamSourceLab's avatar
DreamSourceLab committed
143

DreamSourceLab's avatar
DreamSourceLab committed
144
    void rst_decoder(int index);
DreamSourceLab's avatar
DreamSourceLab committed
145

DreamSourceLab's avatar
DreamSourceLab committed
146
    void rst_decoder(view::DecodeTrace *signal);
DreamSourceLab's avatar
DreamSourceLab committed
147

DreamSourceLab's avatar
DreamSourceLab committed
148
#endif
DreamSourceLab's avatar
DreamSourceLab committed
149

DreamSourceLab's avatar
DreamSourceLab committed
150
    void init_signals();
DreamSourceLab's avatar
DreamSourceLab committed
151

DreamSourceLab's avatar
DreamSourceLab committed
152
    void add_group();
DreamSourceLab's avatar
DreamSourceLab committed
153

DreamSourceLab's avatar
DreamSourceLab committed
154
    void del_group();
DreamSourceLab's avatar
DreamSourceLab committed
155

DreamSourceLab's avatar
DreamSourceLab committed
156
    void* get_buf(int& unit_size, uint64_t& length);
DreamSourceLab's avatar
DreamSourceLab committed
157

158 159 160 161
    void start_hotplug_proc(boost::function<void (const QString)> error_handler);
    void stop_hotplug_proc();
    void register_hotplug_callback();
    void deregister_hotplug_callback();
DreamSourceLab's avatar
DreamSourceLab committed
162 163 164

    void set_adv_trigger(bool adv_trigger);

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
165
    uint16_t get_dso_ch_num();
DreamSourceLab's avatar
DreamSourceLab committed
166 167
    
    void set_sample_rate(uint64_t sample_rate);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
168

DreamSourceLab's avatar
DreamSourceLab committed
169 170 171
private:
	void set_capture_state(capture_state state);

DreamSourceLab's avatar
DreamSourceLab committed
172 173
    void read_sample_rate(const sr_dev_inst *const sdi);

DreamSourceLab's avatar
DreamSourceLab committed
174
private:
DreamSourceLab's avatar
DreamSourceLab committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    /**
     * 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
192

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
193
    // data feed
DreamSourceLab's avatar
DreamSourceLab committed
194 195 196
	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
197
    void feed_in_trigger(const ds_trigger_pos &trigger_pos);
DreamSourceLab's avatar
DreamSourceLab committed
198
	void feed_in_logic(const sr_datafeed_logic &logic);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
199
    void feed_in_dso(const sr_datafeed_dso &dso);
DreamSourceLab's avatar
DreamSourceLab committed
200 201
	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
202 203 204
		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
205

DreamSourceLab's avatar
DreamSourceLab committed
206 207 208
    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
209

DreamSourceLab's avatar
DreamSourceLab committed
210 211 212 213 214 215
private:
	DeviceManager &_device_manager;

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

	mutable boost::mutex _sampling_mutex;
	capture_state _capture_state;
DreamSourceLab's avatar
DreamSourceLab committed
220
    bool _instant;
DreamSourceLab's avatar
DreamSourceLab committed
221 222 223

	mutable boost::mutex _signals_mutex;
	std::vector< boost::shared_ptr<view::Signal> > _signals;
DreamSourceLab's avatar
DreamSourceLab committed
224
    std::vector< boost::shared_ptr<view::GroupSignal> > _group_traces;
DreamSourceLab's avatar
DreamSourceLab committed
225
#ifdef ENABLE_DECODE
DreamSourceLab's avatar
DreamSourceLab committed
226
    std::vector< boost::shared_ptr<view::DecodeTrace> > _decode_traces;
DreamSourceLab's avatar
DreamSourceLab committed
227
#endif
DreamSourceLab's avatar
DreamSourceLab committed
228

DreamSourceLab's avatar
DreamSourceLab committed
229
    mutable boost::mutex _data_mutex;
DreamSourceLab's avatar
DreamSourceLab committed
230 231
	boost::shared_ptr<data::Logic> _logic_data;
	boost::shared_ptr<data::LogicSnapshot> _cur_logic_snapshot;
232 233
    boost::shared_ptr<data::Dso> _dso_data;
    boost::shared_ptr<data::DsoSnapshot> _cur_dso_snapshot;
DreamSourceLab's avatar
DreamSourceLab committed
234 235 236 237 238 239 240 241
	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;

	std::auto_ptr<boost::thread> _sampling_thread;

242 243
    libusb_hotplug_callback_handle _hotplug_handle;
    std::auto_ptr<boost::thread> _hotplug;
DreamSourceLab's avatar
DreamSourceLab committed
244 245 246 247 248 249 250 251 252 253 254 255
    bool _hot_attach;
    bool _hot_detach;

    bool _adv_trigger;

signals:
	void capture_state_changed(int state);

	void signals_changed();

	void data_updated();

DreamSourceLab's avatar
DreamSourceLab committed
256
    void sample_rate_changed(uint64_t sample_rate);
DreamSourceLab's avatar
DreamSourceLab committed
257 258 259 260 261 262 263 264 265 266

    void receive_data(quint64 length);

    void device_attach();
    void device_detach();

    void test_data_error();

    void receive_trigger(quint64 trigger_pos);

DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
267 268
    void dso_ch_changed(uint16_t num);

DreamSourceLab's avatar
DreamSourceLab committed
269 270 271
    void frame_began();

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

DreamSourceLab's avatar
DreamSourceLab committed
273 274 275 276 277 278
    void frame_ended();

    void device_setted();

public slots:
    void reload();
DreamSourceLab's avatar
DreamSourceLab committed
279 280 281 282 283 284 285 286 287 288 289

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

#endif // DSLOGIC_PV_SIGSESSION_H