Skip to content

Commit

Permalink
pcap_tool tests, configurable extract-embedded-link-control and tests…
Browse files Browse the repository at this point in the history
… for util, tests of GPSInfo and TalkerAlias{header+blocks}
  • Loading branch information
smarek committed Mar 4, 2022
1 parent 39d6426 commit 5808bf3
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[report]
exclude_lines =
if __name__ == .__main__.:
pass
continue
traceback.print_exc()
4 changes: 2 additions & 2 deletions okdmr/dmrlib/etsi/layer2/pdu/embedded_signalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(
colour_code: int,
preemption_and_power_control_indicator: int,
link_control_start_stop: Union[LCSS, int],
emb_parity: int = 0,
emb_parity: Union[int, bool] = 0,
):
"""
Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(
if isinstance(link_control_start_stop, int)
else link_control_start_stop
)
self.emb_parity: int = emb_parity
self.emb_parity: int = emb_parity if isinstance(emb_parity, int) else -1

if self.emb_parity < 0:
# generate parity if not provided
Expand Down
78 changes: 74 additions & 4 deletions okdmr/dmrlib/etsi/layer2/pdu/full_link_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from okdmr.dmrlib.etsi.layer3.elements.talker_alias_data_format import (
TalkerAliasDataFormat,
)
from okdmr.dmrlib.utils.bits_bytes import bytes_to_bits
from okdmr.dmrlib.utils.bits_interface import BitsInterface


Expand All @@ -33,8 +34,8 @@ def __init__(
target_address: int = 0,
# Table 7.3: GPS Info PDU content
position_error: Optional[PositionError] = None,
longitude: int = 0,
latitude: int = 0,
longitude: float = 0,
latitude: float = 0,
# Table 7.4: Talker Alias header Info PDU content
talker_alias_data_format: Optional[TalkerAliasDataFormat] = None,
talker_alias_data_length: int = 0,
Expand All @@ -53,8 +54,8 @@ def __init__(
self.source_address: int = source_address
self.target_address: int = target_address
self.position_error: Optional[PositionError] = position_error
self.longitude: int = longitude
self.latitude: int = latitude
self.longitude: float = longitude
self.latitude: float = latitude
self.talker_alias_data_format: Optional[
TalkerAliasDataFormat
] = talker_alias_data_format
Expand All @@ -75,6 +76,22 @@ def __repr__(self) -> str:
return descr + (
f"[SOURCE: {self.source_address}] [GROUP: {self.group_address}] {repr(self.service_options)}"
)
elif self.full_link_control_opcode == FLCOs.GPSInfo:
return descr + (
f"[{self.position_error}] [LAT: {self.latitude}] [LON: {self.longitude}]"
)
elif self.full_link_control_opcode == FLCOs.TalkerAliasHeader:
return descr + (
f"[{self.talker_alias_data_format}] [Talker Alias Data Length: {self.talker_alias_data_length}] [{self.talker_alias_data.decode('ascii')}]"
)
elif self.full_link_control_opcode in (
FLCOs.TalkerAliasBlock1,
FLCOs.TalkerAliasBlock2,
FLCOs.TalkerAliasBlock3,
):
return descr + (
f"[{self.talker_alias_data.hex()}] [utf16le: {self.talker_alias_data.decode('ascii')}]"
)

raise KeyError(f"FullLinkControl.__repr__ does not support " + descr)

Expand Down Expand Up @@ -108,6 +125,39 @@ def from_bits(bits: bitarray) -> "FullLinkControl":
group_address=ba2int(bits[24:48]),
source_address=ba2int(bits[48:72]),
)
elif flco == FLCOs.GPSInfo:
return FullLinkControl(
protect_flag=pf,
fid=fid,
crc=crc,
flco=flco,
position_error=PositionError.from_bits(bits[20:23]),
longitude=(360 / 2**25) * ba2int(bits[23:48], signed=True),
latitude=(180 / 2**24) * ba2int(bits[48:72], signed=True),
)
elif flco == FLCOs.TalkerAliasHeader:
return FullLinkControl(
protect_flag=pf,
fid=fid,
crc=crc,
flco=flco,
talker_alias_data_format=TalkerAliasDataFormat.from_bits(bits[16:18]),
talker_alias_data_length=ba2int(bits[18:23]),
talker_alias_data_msb=bits[23],
talker_alias_data=bits[24:72].tobytes(),
)
elif flco in (
FLCOs.TalkerAliasBlock1,
FLCOs.TalkerAliasBlock2,
FLCOs.TalkerAliasBlock3,
):
return FullLinkControl(
protect_flag=pf,
fid=fid,
crc=crc,
flco=flco,
talker_alias_data=bits[16:72].tobytes(),
)

raise KeyError(f"Not-implemented FLCO {flco}")

Expand All @@ -130,6 +180,26 @@ def as_bits(self) -> bitarray:
+ int2ba(self.group_address, length=24)
+ int2ba(self.source_address, length=24)
)
elif self.full_link_control_opcode == FLCOs.GPSInfo:
common += (
bitarray([0] * 4)
+ self.position_error.as_bits()
+ int2ba(int(self.longitude / (360 / 2**25)), length=25, signed=True)
+ int2ba(int(self.latitude / (180 / 2**24)), length=24, signed=True)
)
elif self.full_link_control_opcode == FLCOs.TalkerAliasHeader:
common += (
self.talker_alias_data_format.as_bits()
+ int2ba(self.talker_alias_data_length, length=5)
+ bitarray([self.talker_alias_data_msb])
+ bytes_to_bits(self.talker_alias_data)
)
elif self.full_link_control_opcode in (
FLCOs.TalkerAliasBlock1,
FLCOs.TalkerAliasBlock2,
FLCOs.TalkerAliasBlock3,
):
common += bytes_to_bits(self.talker_alias_data)
else:
raise KeyError(
f"as_bits unimplemented FLCO {self.full_link_control_opcode}"
Expand Down
5 changes: 1 addition & 4 deletions okdmr/dmrlib/etsi/layer3/elements/position_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,4 @@ def from_bits(bits: bitarray) -> "PositionError":

@classmethod
def _missing_(cls, value: int) -> Any:
assert (
0b000 <= value <= 0b111
), f"Value for PositionError out of range, got {value}"
return PositionError.PositionErrorNotKnown
raise ValueError(f"Value for PositionError out of range, got {value}")
5 changes: 2 additions & 3 deletions okdmr/dmrlib/etsi/layer3/elements/talker_alias_data_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ def from_bits(bits: bitarray) -> "TalkerAliasDataFormat":

@classmethod
def _missing_(cls, value: int) -> Any:
assert (
0b00 <= value <= 0b11
), f"TalkerAliasDataFormat value out of range, got {value}"
if not (0b00 <= value <= 0b11):
raise ValueError(f"TalkerAliasDataFormat value out of range, got {value}")
Loading

0 comments on commit 5808bf3

Please sign in to comment.