Skip to content

Commit

Permalink
added checksum calculation. Need to test still
Browse files Browse the repository at this point in the history
  • Loading branch information
PWRxPSYCHO committed Jan 2, 2025
1 parent 6077785 commit 75b2ec1
Showing 1 changed file with 63 additions and 41 deletions.
104 changes: 63 additions & 41 deletions klv_parser/klv/misb_0601_19/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

_OUT_OF_RANGE_STR = "Out of Range"
_NA_OFF_EARTH = "N/A (Off-Earth)"
_RESERVED = "Reserved"

_OFFSET = 900
_UINT_RANGE = 65535
_INT_RANGE = 65534


class Metadata:
Expand All @@ -12,8 +17,6 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet

def format_data(self):
if self.key_type in {}:
raise NotImplementedError("Each UasDatalinkLocalSet must implement its own format_data() function")
return self.data

def data_to_signed_int(self, data):
Expand Down Expand Up @@ -72,7 +75,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _OUT_OF_RANGE_STR
return 40 / 65534 * self.data_to_signed_int(self.data)
return 40 / _INT_RANGE * self.data_to_signed_int(self.data)


# Item 7
Expand All @@ -84,7 +87,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _OUT_OF_RANGE_STR
return (1000 / 65534) * self.data_to_signed_int(self.data)
return (100 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 8
Expand Down Expand Up @@ -136,7 +139,7 @@ def __init__(self, data, length, id):

def format_data(self):
if self.data.hex() == 80000000:
return "Reserved"
return _RESERVED
return (180 / 4294967294) * self.data_to_signed_int(self.data)


Expand All @@ -148,7 +151,7 @@ def __init__(self, data, length, id):

def format_data(self):
if self.data.hex() == 80000000:
return "Reserved"
return _RESERVED
return (360 / 4294967294) * self.data_to_signed_int(self.data)


Expand All @@ -159,7 +162,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.SENSOR_TRUE_ALTITUDE

def format_data(self):
return (199000 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (199000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 16
Expand All @@ -169,7 +172,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.SENSOR_HORIZONTAL_FOV

def format_data(self):
return (180 / 65535) * self.data_to_unsigned_int(self.data)
return (180 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 17
Expand All @@ -179,7 +182,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.SENSOR_VERTICAL_FOV

def format_data(self):
return (180 / 65535) * self.data_to_unsigned_int(self.data)
return (180 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 18
Expand All @@ -200,7 +203,7 @@ def __init__(self, data, length, id):

def format_data(self):
if self.data.hex() == 80000000:
return "Reserved"
return _RESERVED
return (360 / 4294967294) * self.data_to_signed_int(self.data)


Expand Down Expand Up @@ -231,7 +234,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.TARGET_WIDTH

def format_data(self):
return (10000 / 65535) * self.data_to_unsigned_int(self.data)
return (10000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 23
Expand Down Expand Up @@ -266,7 +269,7 @@ def __init__(self, data, length, id):

# Default LS_dec
def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 26
Expand All @@ -278,7 +281,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 27
Expand All @@ -290,7 +293,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 28
Expand All @@ -302,7 +305,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 29
Expand All @@ -314,7 +317,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 30
Expand All @@ -326,7 +329,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 31
Expand All @@ -338,7 +341,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 32
Expand All @@ -350,7 +353,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 33
Expand All @@ -362,7 +365,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data.hex() == 8000:
return _NA_OFF_EARTH
return (0.15 / 65534) * self.data_to_signed_int(self.data)
return (0.15 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 34
Expand All @@ -379,7 +382,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.WIND_DIRECTION

def format_data(self):
return (360 / 65535) * self.data_to_unsigned_int(self.data)
return (360 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 36
Expand All @@ -399,7 +402,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.STATIC_PRESSURE

def format_data(self):
return (5000 / 65535) * self.data_to_unsigned_int(self.data)
return (5000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 38
Expand All @@ -409,7 +412,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.DENSITY_ALTITUDE

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 39
Expand Down Expand Up @@ -453,7 +456,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.TARGET_LOCATION_ELEVATION

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 43
Expand Down Expand Up @@ -483,7 +486,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.TARGET_ERROR_ESTIMATE_CE90

def format_data(self):
return (4095 / 65535) * self.data_to_unsigned_int(self.data)
return (4095 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 46
Expand All @@ -493,7 +496,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.TARGET_ERROR_ESTIMATE_LE90

def format_data(self):
return (4095 / 65535) * self.data_to_unsigned_int(self.data)
return (4095 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 47
Expand Down Expand Up @@ -524,7 +527,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.DIFFERENTIAL_PRESSURE

def format_data(self):
return (5000 / 65535) * self.data_to_unsigned_int(self.data)
return (5000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 50
Expand All @@ -536,7 +539,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data == 8000:
return _OUT_OF_RANGE_STR
return (40 / 65534) * self.data_to_signed_int(self.data)
return (40 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 51
Expand All @@ -548,7 +551,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data == 8000:
return _OUT_OF_RANGE_STR
return (360 / 65534) * self.data_to_signed_int(self.data)
return (360 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 52
Expand All @@ -560,7 +563,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data == 8000:
return _OUT_OF_RANGE_STR
return (40 / 65534) * self.data_to_signed_int(self.data)
return (40 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 53
Expand All @@ -570,7 +573,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.PLATFORM_SIDESLIP_ANGLE

def format_data(self):
return (5000 / 65535) * self.data_to_unsigned_int(self.data)
return (5000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 54
Expand All @@ -580,7 +583,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.AIRFIELD_ELEVATION

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 55
Expand Down Expand Up @@ -620,7 +623,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.PLATFORM_FUEL_REMAINING

def format_data(self):
return (10000 / 65535) * self.data_to_unsigned_int(self.data)
return (10000 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 59
Expand Down Expand Up @@ -674,7 +677,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.PLATFORM_MAGNETIC_HEADING

def format_data(self):
return (360 / 65535) * self.data_to_unsigned_int(self.data)
return (360 / _UINT_RANGE) * self.data_to_unsigned_int(self.data)


# Item 65
Expand Down Expand Up @@ -718,7 +721,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.ALTERNATE_PLATFORM_ALTITUDE

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 70
Expand All @@ -735,7 +738,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.ALTERNATE_PLATFORM_HEADING

def format_data(self):
return (360 / 65535) * self.data_to_signed_int(self.data)
return (360 / _UINT_RANGE) * self.data_to_signed_int(self.data)


# Item 72
Expand Down Expand Up @@ -775,7 +778,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.SENSOR_ELLIPSOID_HEIGHT

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 76
Expand All @@ -785,7 +788,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.ALTERNATE_PLATFORM_ELLIPSOID_HEIGHT

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 77
Expand All @@ -802,7 +805,7 @@ def __init__(self, data, length, id):
self.key_type = UasDatalinkLocalSet.FRAME_CENTER_HEIGHT_ABOVE_ELLIPSOID

def format_data(self):
return (19900 / 65535) * self.data_to_unsigned_int(self.data) - 900
return (19900 / _UINT_RANGE) * self.data_to_unsigned_int(self.data) - _OFFSET


# Item 79
Expand All @@ -814,7 +817,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data == 8000:
return _OUT_OF_RANGE_STR
return (654 / 65534) * self.data_to_signed_int(self.data)
return (654 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 80
Expand All @@ -826,7 +829,7 @@ def __init__(self, data, length, id):
def format_data(self):
if self.data == 8000:
return "Out of Range`"
return (654 / 65534) * self.data_to_signed_int(self.data)
return (654 / _INT_RANGE) * self.data_to_signed_int(self.data)


# Item 81
Expand Down Expand Up @@ -1403,6 +1406,25 @@ def __init__(self, data, length, id):
super().__init__(data, length, id)
self.key_type = UasDatalinkLocalSet.CHECKSUM

def format_data(self):
return self.data_to_unsigned_int(self.data)

def calculate_checksum(self):
bcc = 0
for i in range(self.length):
bcc += self.data[i] << (8 * ((i + 1) % 2))
return bcc

def validate_checksum(self):
if len(self.data) < 3:
raise ValueError("Checksum data is too short")

received_checksum = self.data

calculated_checksum = self.calculate_checksum()

return calculated_checksum == received_checksum


class_map = {
UasDatalinkLocalSet.CHECKSUM: Checksum,
Expand Down

0 comments on commit 75b2ec1

Please sign in to comment.