Skip to content

Commit

Permalink
Add additional checks on some fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
darksidelemm committed Jan 30, 2021
1 parent 975d50a commit 219c4e7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion horusdemodlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.20"
__version__ = "0.1.21"
30 changes: 28 additions & 2 deletions horusdemodlib/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
32: 'horus_binary_v2_32byte'
}

def decode_packet(data:bytes, packet_format:dict = None) -> dict:
def decode_packet(data:bytes, packet_format:dict = None, ignore_crc:bool = False) -> dict:
"""
Attempt to decode a set of bytes based on a provided packet format.
Expand Down Expand Up @@ -113,7 +113,7 @@ def decode_packet(data:bytes, packet_format:dict = None) -> dict:
# Check the Checksum
_crc_ok = check_packet_crc(data, checksum=packet_format['checksum'])

if not _crc_ok:
if (not _crc_ok) and (not ignore_crc):
raise ValueError("Decoder - CRC Failure.")
else:
_output['crc_ok'] = True
Expand Down Expand Up @@ -321,6 +321,32 @@ def parse_ukhas_string(sentence:str) -> dict:
print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}")
assert(_output == 'error')

# Binary packet tests that break various fields
tests = [
# ID Seq---| HH MM SS Lat----------| Lon-----------| Alt---|
['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', ''],
['horus_binary_v1', b'\x01\x12\x00\x18\x00\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'],
['horus_binary_v1', b'\x01\x12\x00\x00\x3c\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'],
['horus_binary_v1', b'\x01\x12\x00\x00\x00\x3c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'],
['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x00\x35\x43\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'],
['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x80\x34\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'],
]

for _test in tests:
_format = _test[0]
_input = _test[1]
_output = _test[2]

try:
_decoded = decode_packet(_input, ignore_crc=True)
print(f"Input ({_format}): {str(_input)} - Output: {_decoded['ukhas_str']}")
print(_decoded)
# Insert assert checks here.

except ValueError as e:
print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}")
assert(_output == 'error')

# RTTY Decoder Tests
tests = [
'$$HORUS,6,06:43:16,0.000000,0.000000,0,0,0,1801,20*1DA2',
Expand Down
16 changes: 15 additions & 1 deletion horusdemodlib/delegates.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ def decode_time_hms(data: bytes) -> str:
raise ValueError(f"time_hms - Input has incorrect length ({len(data)}), should be 3.")

_hour = int(data[0])
if _hour >= 24:
raise ValueError(f"time_hms - Hour value ({_hour}) out of range 0-23.")

_minute = int(data[1])
if _minute >= 60:
raise ValueError(f"time_hms - Minute value ({_minute}) out of range 0-59.")

_second = int(data[2])
if _second >= 60:
raise ValueError(f"time_hms - Second value ({_second}) out of range 0-59.")

_str = f"{_hour:02d}:{_minute:02d}:{_second:02d}"

Expand All @@ -65,7 +73,7 @@ def decode_time_biseconds(data:int) -> str:
raise ValueError("time_biseconds - Invalid input type.")

if (data < 0) or data > 43200:
raise ValueError("time_biseconds - Input of of range (0-43200)")
raise ValueError("time_biseconds - Input out of range (0-43200)")

_str = time.strftime("%H:%M:%S", time.gmtime(data*2))

Expand All @@ -81,6 +89,9 @@ def decode_degree_float(data:float) -> str:
if type(data) != float:
raise ValueError("decimal_degrees - Invalid input type.")

if (data < -180.0) or (data > 180.0):
raise ValueError(f"decimal_degrees - Value ({data}) out of range -180 - 180.")

return (data, f"{data:.5f}")


Expand Down Expand Up @@ -110,6 +121,9 @@ def decode_degree_fixed3(data:bytes) -> str:
_value = struct.unpack('<i', _temp)[0]
_value_degrees = _value * 1e-7

if (_value_degrees < -180.0) or (_value_degrees > 180.0):
raise ValueError(f"degree_fixed3 - Value ({_value_degrees}) out of range -180 - 180.")

return (_value_degrees, f"{_value_degrees:.5f}")


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "horusdemodlib"
version = "0.1.20"
version = "0.1.21"
description = "Project Horus HAB Telemetry Demodulators"
authors = ["Mark Jessop"]
license = "LGPL-2.1-or-later"
Expand Down

0 comments on commit 219c4e7

Please sign in to comment.