From: Gerhard Sittig Date: Sun, 28 Jan 2018 19:49:48 +0000 (+0100) Subject: graycode, morse, pwm, usb_request, wiegand: cope with absent sample rate X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=15a8a05595c72d328abdea8e3b23f1b558cc46bf;p=libsigrokdecode.git 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. --- diff --git a/decoders/graycode/pd.py b/decoders/graycode/pd.py index 1211c69..ef5d513 100644 --- a/decoders/graycode/pd.py +++ b/decoders/graycode/pd.py @@ -175,7 +175,7 @@ class Decoder(srd.Decoder): if self.options['edges']: self.turns.set(self.samplenum, self.count.get() // self.options['edges']) - if self.samplerate is not None: + if self.samplerate: period = (curtime - prevtime) / self.samplerate freq = abs(phasedelta_raw) / period diff --git a/decoders/morse/pd.py b/decoders/morse/pd.py index a1e3c23..3048332 100644 --- a/decoders/morse/pd.py +++ b/decoders/morse/pd.py @@ -154,8 +154,6 @@ class Decoder(srd.Decoder): # Annotate symbols, emit symbols, handle timeout via token. timeunit = self.options['timeunit'] - if self.samplerate is None: - self.samplerate = 1.0 self.wait({0: 'r'}) prevtime = self.samplenum # Time of an actual edge. @@ -217,6 +215,15 @@ class Decoder(srd.Decoder): yield None # Pass through flush of 5+ space. def decode(self): + + # Strictly speaking there is no point in running this decoder + # when the sample rate is unknown or zero. But the previous + # implementation already fell back to a rate of 1 in that case. + # We stick with this approach, to not introduce new constraints + # for existing use scenarios. + if not self.samplerate: + self.samplerate = 1.0 + # Annotate letters, group into words. s0 = s1 = None word = '' diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 2301523..9770445 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -20,6 +20,9 @@ import sigrokdecode as srd +class SamplerateError(Exception): + pass + class Decoder(srd.Decoder): api_version = 3 id = 'pwm' @@ -52,6 +55,7 @@ class Decoder(srd.Decoder): self.reset() def reset(self): + self.samplerate = None self.ss_block = self.es_block = None def metadata(self, key, value): @@ -89,6 +93,9 @@ class Decoder(srd.Decoder): self.put(self.ss_block, self.es_block, self.out_binary, data) def decode(self): + if not self.samplerate: + raise SamplerateError('Cannot decode without samplerate.') + num_cycles = 0 average = 0 diff --git a/decoders/usb_request/pd.py b/decoders/usb_request/pd.py index a480284..e77debc 100644 --- a/decoders/usb_request/pd.py +++ b/decoders/usb_request/pd.py @@ -171,7 +171,8 @@ class Decoder(srd.Decoder): def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: self.samplerate = value - self.secs_per_sample = float(1) / float(self.samplerate) + if self.samplerate: + self.secs_per_sample = float(1) / float(self.samplerate) def start(self): self.out_binary = self.register(srd.OUTPUT_BINARY) 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()