summaryrefslogtreecommitdiff
path: root/decoders/wiegand
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2018-01-28 20:49:48 +0100
committerGerhard Sittig <gerhard.sittig@gmx.net>2018-01-28 20:49:48 +0100
commit15a8a05595c72d328abdea8e3b23f1b558cc46bf (patch)
treedf65ff2c6163724aa5f49275086963fa3660b6a5 /decoders/wiegand
parent956721de58552b05776c8613449f2907196e61e9 (diff)
downloadlibsigrokdecode-15a8a05595c72d328abdea8e3b23f1b558cc46bf.tar.gz
libsigrokdecode-15a8a05595c72d328abdea8e3b23f1b558cc46bf.zip
graycode, morse, pwm, usb_request, wiegand: cope with absent sample rate
Improve robustness of some more protocol decoders. Few of them never checked for the availability of a sample rate in the first place, others checked for the presence of a spec but would not cope with a value of 0. Some checked the value only after processing it, which could result in runtime errors. This change is motivated by bug #1118.
Diffstat (limited to 'decoders/wiegand')
-rw-r--r--decoders/wiegand/pd.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/decoders/wiegand/pd.py b/decoders/wiegand/pd.py
index ea873ea..c494789 100644
--- a/decoders/wiegand/pd.py
+++ b/decoders/wiegand/pd.py
@@ -19,6 +19,9 @@
import sigrokdecode as srd
+class SamplerateError(Exception):
+ pass
+
class Decoder(srd.Decoder):
api_version = 3
id = 'wiegand'
@@ -51,6 +54,7 @@ class Decoder(srd.Decoder):
self.reset()
def reset(self):
+ self.samplerate = None
self._samples_per_bit = 10
self._d0_prev = None
@@ -73,9 +77,11 @@ class Decoder(srd.Decoder):
def metadata(self, key, value):
'Receive decoder metadata about the data stream.'
if key == srd.SRD_CONF_SAMPLERATE:
- ms_per_sample = 1000 * (1.0 / value)
- ms_per_bit = float(self.options['bitwidth_ms'])
- self._samples_per_bit = int(max(1, int(ms_per_bit / ms_per_sample)))
+ self.samplerate = value
+ if self.samplerate:
+ ms_per_sample = 1000 * (1.0 / self.samplerate)
+ ms_per_bit = float(self.options['bitwidth_ms'])
+ self._samples_per_bit = int(max(1, int(ms_per_bit / ms_per_sample)))
def _update_state(self, state, bit=None):
'Update state and bit values when they change.'
@@ -106,6 +112,8 @@ class Decoder(srd.Decoder):
self._bits = []
def decode(self):
+ if not self.samplerate:
+ raise SamplerateError('Cannot decode without samplerate.')
while True:
# TODO: Come up with more appropriate self.wait() conditions.
(d0, d1) = self.wait()