]> sigrok.org Git - pulseview.git/blob - pv/data/signalbase.hpp
SignalBase: Prevent race condition for memory access
[pulseview.git] / pv / data / signalbase.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef PULSEVIEW_PV_DATA_SIGNALBASE_HPP
22 #define PULSEVIEW_PV_DATA_SIGNALBASE_HPP
23
24 #include <atomic>
25 #include <deque>
26 #include <condition_variable>
27 #include <thread>
28 #include <vector>
29
30 #include <QColor>
31 #include <QObject>
32 #include <QSettings>
33 #include <QString>
34 #include <QTimer>
35 #include <QVariant>
36
37 #include <libsigrokcxx/libsigrokcxx.hpp>
38
39 #include "segment.hpp"
40
41 using std::atomic;
42 using std::condition_variable;
43 using std::deque;
44 using std::enable_shared_from_this;
45 using std::map;
46 using std::mutex;
47 using std::pair;
48 using std::shared_ptr;
49 using std::vector;
50
51 namespace sigrok {
52 class Channel;
53 }
54
55 namespace pv {
56 namespace data {
57
58 class Analog;
59 class AnalogSegment;
60 class DecoderStack;
61 class Logic;
62 class LogicSegment;
63 class Segment;
64 class SignalBase;
65 class SignalData;
66
67 class SignalGroup : public QObject
68 {
69         Q_OBJECT
70
71 public:
72         SignalGroup(const QString& name);
73
74         void append_signal(shared_ptr<SignalBase> signal);
75         void remove_signal(shared_ptr<SignalBase> signal);
76         deque<shared_ptr<SignalBase>> signals() const;
77         void clear();
78
79         const QString name() const;
80
81 private:
82         deque<shared_ptr<SignalBase>> signals_;
83         QString name_;
84 };
85
86
87 class SignalBase : public QObject, public enable_shared_from_this<SignalBase>
88 {
89         Q_OBJECT
90
91 public:
92         enum ChannelType {
93                 AnalogChannel = 1, ///< Analog data
94                 LogicChannel,  ///< Logic data
95                 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
96                 MathChannel    ///< Virtual channel generated by math operations
97         };
98
99         enum ConversionType {
100                 NoConversion = 0,
101                 A2LConversionByThreshold = 1,
102                 A2LConversionBySchmittTrigger = 2
103         };
104
105         /**
106          * Conversion presets range from -1 to n, where 1..n are dependent on
107          * the conversion these presets apply to. -1 and 0 have fixed meanings,
108          * however.
109          */
110         enum ConversionPreset {
111                 NoPreset = -1,     ///< Conversion uses custom values
112                 DynamicPreset = 0  ///< Conversion uses calculated values
113         };
114
115         static const QColor AnalogSignalColors[8];
116         static const QColor LogicSignalColors[10];
117
118 private:
119         static const int ColorBGAlpha;
120         static const uint64_t ConversionBlockSize;
121         static const uint32_t ConversionDelay;
122
123 public:
124         SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
125         virtual ~SignalBase();
126
127 public:
128         /**
129          * Returns the underlying SR channel.
130          * Generated channels don't have a SR channel.
131          */
132         shared_ptr<sigrok::Channel> channel() const;
133
134         /**
135          * Returns whether this channel is generated or a channel associated with the device.
136          */
137         bool is_generated() const;
138
139         /**
140          * Returns enabled status of this channel.
141          */
142         bool enabled() const;
143
144         /**
145          * Sets the enabled status of this channel.
146          * @param value Boolean value to set.
147          */
148         void set_enabled(bool value);
149
150         /**
151          * Gets the type of this channel.
152          */
153         ChannelType type() const;
154
155         /**
156          * Gets the index number of this channel, i.e. a unique ID assigned by
157          * the device driver.
158          */
159         unsigned int index() const;
160
161         /**
162          * Sets the index number of this channel, i.e. a unique ID assigned by
163          * the device driver or the logic bit index (see below).
164          * Only use immediately after creating the signal and leave it untouched after.
165          */
166         void set_index(unsigned int index);
167
168         /**
169          * Returns which bit of a given sample for this signal represents the
170          * signal itself. This is relevant for compound signals like logic,
171          * rather meaningless for everything else but provided in case there
172          * is a conversion active that provides a digital signal using bit #0.
173          */
174         unsigned int logic_bit_index() const;
175
176         /**
177          * Sets the signal group this signal belongs to
178          */
179         void set_group(SignalGroup* group);
180
181         /**
182          * Returns the signal group this signal belongs to or nullptr if none
183          */
184         SignalGroup* group() const;
185
186         /**
187          * Gets the name of this signal.
188          */
189         QString name() const;
190
191         /**
192          * Gets the internal name of this signal, i.e. how the device/generator calls it.
193          */
194         QString internal_name() const;
195
196         /**
197          * Sets the internal name of this signal, i.e. how the device/generator calls it.
198          * Only use immediately after creating the signal and leave it untouched after.
199          */
200         void set_internal_name(QString internal_name);
201
202         /**
203          * Produces a string for this signal that can be used for display,
204          * i.e. it contains one or both of the signal/internal names.
205          */
206         QString display_name() const;
207
208         /**
209          * Sets the name of the signal.
210          */
211         virtual void set_name(QString name);
212
213         /**
214          * Get the color of the signal.
215          */
216         QColor color() const;
217
218         /**
219          * Set the color of the signal.
220          */
221         void set_color(QColor color);
222
223         /**
224          * Get the background color of the signal.
225          */
226         QColor bgcolor() const;
227
228         /**
229          * Sets the internal data object.
230          */
231         void set_data(shared_ptr<pv::data::SignalData> data);
232
233         /**
234          * Clears all sample data and removes all associated segments.
235          */
236         void clear_sample_data();
237
238         /**
239          * Get the internal data as analog data object in case of analog type.
240          */
241         shared_ptr<pv::data::Analog> analog_data() const;
242
243         /**
244          * Get the internal data as logic data object in case of logic type.
245          */
246         shared_ptr<pv::data::Logic> logic_data() const;
247
248         /**
249          * Determines whether a given segment is complete (i.e. end-of-frame has
250          * been seen). It only considers the original data, not the converted data.
251          */
252         bool segment_is_complete(uint32_t segment_id) const;
253
254         /**
255          * Determines whether this signal has any sample data at all.
256          */
257         bool has_samples() const;
258
259         /**
260          * Returns the sample rate for this signal.
261          */
262         virtual double get_samplerate() const;
263
264         /**
265          * Queries the kind of conversion performed on this channel.
266          */
267         ConversionType get_conversion_type() const;
268
269         /**
270          * Changes the kind of conversion performed on this channel.
271          *
272          * Restarts the conversion.
273          */
274         void set_conversion_type(ConversionType t);
275
276         /**
277          * Returns all currently known conversion options
278          */
279         map<QString, QVariant> get_conversion_options() const;
280
281         /**
282          * Sets the value of a particular conversion option
283          * Note: it is not checked whether the option is valid for the
284          * currently conversion. If it's not, it will be silently ignored.
285          *
286          * Does not restart the conversion.
287          *
288          * @return true if the value is different from before, false otherwise
289          */
290         bool set_conversion_option(QString key, QVariant value);
291
292         /**
293          * Returns the threshold(s) used for conversions, if applicable.
294          * The resulting thresholds are given for the chosen conversion, so you
295          * can query thresholds also for conversions which aren't currently active.
296          *
297          * If you want the thresholds for the currently active conversion,
298          * call it either with NoConversion or no parameter.
299          *
300          * @param t the type of conversion to obtain the thresholds for, leave
301          *          empty or use NoConversion if you want to query the currently
302          *          used conversion
303          *
304          * @param always_custom ignore the currently selected preset and always
305          *        return the custom values for this conversion, using 0 if those
306          *        aren't set
307          *
308          * @return a list of threshold(s) used by the chosen conversion
309          */
310         vector<double> get_conversion_thresholds(
311                 const ConversionType t = NoConversion, const bool always_custom=false) const;
312
313         /**
314          * Provides all conversion presets available for the currently active
315          * conversion.
316          *
317          * @return a list of description/ID pairs for each preset
318          */
319         vector<pair<QString, int> > get_conversion_presets() const;
320
321         /**
322          * Determines the ID of the currently used conversion preset, which is only
323          * valid for the currently available conversion presets. It is therefore
324          * suggested to call @ref get_conversion_presets right before calling this.
325          *
326          * @return the ID of the currently used conversion preset. -1 if no preset
327          *         is used. In that case, a user setting is used instead.
328          */
329         ConversionPreset get_current_conversion_preset() const;
330
331         /**
332          * Sets the conversion preset to be used.
333          *
334          * Does not restart the conversion.
335          *
336          * @param id the id of the preset to use
337          */
338         void set_conversion_preset(ConversionPreset id);
339
340 #ifdef ENABLE_DECODE
341         bool is_decode_signal() const;
342 #endif
343
344         virtual void save_settings(QSettings &settings) const;
345         virtual void restore_settings(QSettings &settings);
346
347         void start_conversion(bool delayed_start=false);
348
349 private:
350         bool conversion_is_a2l() const;
351
352         uint8_t convert_a2l_threshold(float threshold, float value);
353         uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
354                 float value, uint8_t &state);
355
356         void convert_single_segment_range(shared_ptr<AnalogSegment> asegment,
357                 shared_ptr<LogicSegment> lsegment, uint64_t start_sample, uint64_t end_sample);
358         void convert_single_segment(shared_ptr<AnalogSegment> asegment,
359                 shared_ptr<LogicSegment> lsegment);
360         void conversion_thread_proc();
361
362         void stop_conversion();
363
364 Q_SIGNALS:
365         void enabled_changed(const bool &value);
366
367         void name_changed(const QString &name);
368
369         void color_changed(const QColor &color);
370
371         void conversion_type_changed(const ConversionType t);
372
373         void samples_cleared();
374
375         void samples_added(uint64_t segment_id, uint64_t start_sample,
376                 uint64_t end_sample);
377
378         void min_max_changed(float min, float max);
379
380 private Q_SLOTS:
381         void on_samples_cleared();
382
383         void on_samples_added(SharedPtrToSegment segment, uint64_t start_sample,
384                 uint64_t end_sample);
385
386         void on_min_max_changed(float min, float max);
387
388         void on_capture_state_changed(int state);
389
390         void on_delayed_conversion_start();
391
392 protected:
393         shared_ptr<sigrok::Channel> channel_;
394         ChannelType channel_type_;
395         SignalGroup* group_;
396         shared_ptr<pv::data::SignalData> data_;
397         shared_ptr<pv::data::SignalData> converted_data_;
398         ConversionType conversion_type_;
399         map<QString, QVariant> conversion_options_;
400
401         float min_value_, max_value_;
402
403         std::thread conversion_thread_;
404         atomic<bool> conversion_interrupt_;
405         mutex conversion_input_mutex_;
406         condition_variable conversion_input_cond_;
407         QTimer delayed_conversion_starter_;
408
409         QString internal_name_, name_;
410         QColor color_, bgcolor_;
411         unsigned int index_;
412 };
413
414 } // namespace data
415 } // namespace pv
416
417 #endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP