Skip to content

Commit 55bd4b2

Browse files
committed
Start migrating packet spec
1 parent 0f4f83c commit 55bd4b2

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

modules/telemetry/parsing_utils.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import logging
44

55

6-
from modules.telemetry.v1.block import (
7-
PacketHeader,
8-
BlockHeader,
9-
DeviceAddress,
10-
UnsupportedEncodingVersionError,
11-
InvalidHeaderFieldValueError,
12-
)
13-
import modules.telemetry.v1.data_block as v1db
6+
# from modules.telemetry.v1.block import (
7+
# PacketHeader,
8+
# BlockHeader,
9+
# DeviceAddress,
10+
# UnsupportedEncodingVersionError,
11+
# InvalidHeaderFieldValueError,
12+
# )
13+
# import modules.telemetry.v1.data_block as v1db
14+
15+
from modules.telemetry.v2.block import *
1416
from modules.misc.config import Config
1517

1618
MIN_SUPPORTED_VERSION: int = 1
@@ -54,48 +56,51 @@ def parse_rn2483_transmission(data: str, config: Config) -> Optional[ParsedTrans
5456

5557
# Catch unsupported encoding versions by skipping packet
5658
try:
57-
pkt_hdr = PacketHeader.from_hex(data[:32])
58-
except UnsupportedEncodingVersionError as e:
59+
pkt_hdr: PacketHeader = parse_packet_header(data[:PACKET_HEADER_LENGTH])
60+
except InvalidHeaderFieldValueError as e:
5961
logger.error(f"{e}, skipping packet")
6062
return
63+
64+
logger.info(pkt_hdr)
6165

6266
# We can keep unauthorized callsigns but we'll log them as warnings
6367
from_approved_callsign(pkt_hdr, config.approved_callsigns)
6468

65-
if len(pkt_hdr) <= 32: # If this packet nothing more than just the header
69+
if len(pkt_hdr) <= PACKET_HEADER_LENGTH: # If this packet nothing more than just the header
6670
logger.debug(f"{pkt_hdr}")
6771

68-
blocks = data[32:] # Remove the packet header
72+
blocks = data[PACKET_HEADER_LENGTH:] # Remove the packet header
6973

7074
# Parse through all blocks
7175
while blocks != "":
7276
# Parse block header
7377
logger.debug(f"Blocks: {blocks}")
74-
logger.debug(f"Block header: {blocks[:8]}")
78+
logger.debug(f"Block header: {blocks[:BLOCK_HEADER_LENGTH]}")
7579

7680
# Catch invalid block headers field values by skipping packet
7781
try:
78-
block_header = BlockHeader.from_hex(blocks[:8])
82+
block_header = parse_block_header(blocks[:BLOCK_HEADER_LENGTH])
7983
except InvalidHeaderFieldValueError as e:
8084
logger.error(f"{e}, skipping packet")
8185
return
86+
logger.info(block_header)
8287

8388
# Select block contents
84-
block_len = len(block_header) * 2 # Convert length in bytes to length in hex symbols
85-
block_contents = blocks[8:block_len]
86-
logger.debug(f"Block info: {block_header}")
87-
88-
# Check if message is destined for ground station for processing
89-
if block_header.destination in [DeviceAddress.GROUND_STATION, DeviceAddress.MULTICAST]:
90-
cur_block = parse_radio_block(pkt_hdr.version, block_header, block_contents)
91-
if cur_block:
92-
parsed_blocks.append(cur_block) # Append parsed block to list
93-
else:
94-
logger.warning("Invalid destination address")
95-
96-
# Remove the data we processed from the whole set, and move onto the next data block
97-
blocks = blocks[block_len:]
98-
return ParsedTransmission(pkt_hdr, parsed_blocks)
89+
# block_len = len(block_header) * 2 # Convert length in bytes to length in hex symbols
90+
# block_contents = blocks[8:block_len]
91+
# logger.debug(f"Block info: {block_header}")
92+
93+
# # Check if message is destined for ground station for processing
94+
# if block_header.destination in [DeviceAddress.GROUND_STATION, DeviceAddress.MULTICAST]:
95+
# cur_block = parse_radio_block(pkt_hdr.version, block_header, block_contents)
96+
# if cur_block:
97+
# parsed_blocks.append(cur_block) # Append parsed block to list
98+
# else:
99+
# logger.warning("Invalid destination address")
100+
101+
# # Remove the data we processed from the whole set, and move onto the next data block
102+
# blocks = blocks[block_len:]
103+
# return ParsedTransmission(pkt_hdr, parsed_blocks)
99104

100105

101106
def from_approved_callsign(pkt_hdr: PacketHeader, approved_callsigns: dict[str, str]) -> bool:

modules/telemetry/v2/block.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
MIN_SUPPORTED_VERSION: int = 1
1111
MAX_SUPPORTED_VERSION: int = 1
12+
PACKET_HEADER_LENGTH: int = 26
13+
BLOCK_HEADER_LENGTH: int = 2
1214

1315
# Set up logging
1416
logger = logging.getLogger(__name__)
@@ -59,12 +61,15 @@ class BlockHeader:
5961

6062
#Parse packet header
6163
def parse_packet_header(header_bytes: bytes) -> PacketHeader:
62-
callsign_bytes = header_bytes[:9]
63-
callsign = ''
64-
for c in callsign_bytes:
65-
callsign += chr(c)
66-
timestamp, num_blocks, packet_num = struct.unpack("<HBB", header_bytes[9:])
67-
return PacketHeader(callsign, timestamp, num_blocks, packet_num)
64+
try:
65+
callsign_bytes = header_bytes[:18]
66+
callsign = ''
67+
for c in callsign_bytes:
68+
callsign += chr(c)
69+
timestamp, num_blocks, packet_num = struct.unpack("<HBB", header_bytes[18:])
70+
return PacketHeader(callsign.decode("hex"), timestamp, num_blocks, packet_num)
71+
except ValueError as e:
72+
raise InvalidHeaderFieldValueError(e)
6873

6974
#Parse block header
7075
def parse_block_header(header_bytes: bytes) -> BlockHeader:

modules/telemetry/v2/data_block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import struct
77

88
from modules.misc.converter import metres_to_feet, milli_degrees_to_celsius, pascals_to_psi
9-
import * from block
9+
from block import *
1010

1111
@dataclass
1212
class Block(ABC):

0 commit comments

Comments
 (0)