55from collections import deque
66from dataclasses import dataclass , field
77from pathlib import Path
8- from typing import TYPE_CHECKING
8+ from typing import TYPE_CHECKING
99
1010from spacepackets .cfdp import (
1111 ChecksumType ,
@@ -83,7 +83,7 @@ class _DestFileParams(_FileParamsBase):
8383 file_name : Path
8484
8585 @classmethod
86- def empty (cls ) -> _DestFileParams :
86+ def empty (cls ) -> _DestFileParams : # pyright: ignore[reportImplicitOverride]
8787 return cls (
8888 progress = 0 ,
8989 segment_len = 0 ,
@@ -93,7 +93,7 @@ def empty(cls) -> _DestFileParams:
9393 metadata_only = False ,
9494 )
9595
96- def reset (self ) -> None :
96+ def reset (self ) -> None : # pyright: ignore[reportImplicitOverride]
9797 super ().reset ()
9898 self .file_name = Path ()
9999
@@ -146,7 +146,7 @@ def packets_ready(self) -> bool:
146146
147147class LostSegmentTracker :
148148 def __init__ (self ):
149- self .lost_segments = {}
149+ self .lost_segments : dict [ int , int ] = {}
150150
151151 @property
152152 def num_lost_segments (self ) -> int :
@@ -162,7 +162,7 @@ def add_lost_segment(self, lost_seg: tuple[int, int]) -> None:
162162 def coalesce_lost_segments (self ) -> None :
163163 if len (self .lost_segments ) <= 1 :
164164 return
165- merged_segments = []
165+ merged_segments : list [ tuple [ int , int ]] = []
166166 # Initialize to the first entry.
167167 current_start , current_end = next (iter (self .lost_segments .items ()))
168168
@@ -248,16 +248,16 @@ def __init__(self):
248248 condition_code = ConditionCode .NO_ERROR ,
249249 )
250250 self .completion_disposition : CompletionDisposition = CompletionDisposition .COMPLETED
251- self .pdu_conf = PduConfig .empty ()
251+ self .pdu_conf : PduConfig = PduConfig .empty ()
252252 self .fp : _DestFileParams = _DestFileParams .empty ()
253253
254- self .acked_params = _AckedModeParams ()
255- self .positive_ack_params = _PositiveAckProcedureParams ()
254+ self .acked_params : _AckedModeParams = _AckedModeParams ()
255+ self .positive_ack_params : _PositiveAckProcedureParams = _PositiveAckProcedureParams ()
256256
257257
258258class FsmResult :
259259 def __init__ (self , states : DestStateWrapper ):
260- self .states = states
260+ self .states : DestStateWrapper = states
261261
262262
263263def acknowledge_inactive_eof_pdu (eof_pdu : EofPdu , status : TransactionStatus ) -> AckPdu :
@@ -323,12 +323,12 @@ def __init__(
323323 remote_cfg_table : RemoteEntityConfigTable ,
324324 check_timer_provider : CheckTimerProvider ,
325325 ) -> None :
326- self .cfg = cfg
327- self .remote_cfg_table = remote_cfg_table
328- self .states = DestStateWrapper ()
329- self .user = user
330- self .check_timer_provider = check_timer_provider
331- self ._params = _DestFieldWrapper ()
326+ self .cfg : LocalEntityConfig = cfg
327+ self .remote_cfg_table : RemoteEntityConfigTable = remote_cfg_table
328+ self .states : DestStateWrapper = DestStateWrapper ()
329+ self .user : CfdpUserBase = user
330+ self .check_timer_provider : CheckTimerProvider = check_timer_provider
331+ self ._params : _DestFieldWrapper = _DestFieldWrapper ()
332332 self ._pdus_to_be_sent : deque [PduHolder ] = deque ()
333333
334334 @property
@@ -684,6 +684,7 @@ def _common_first_packet_handler(self, pdu: GenericPduPacket) -> bool | None:
684684 return None
685685
686686 def _handle_metadata_packet (self , metadata_pdu : MetadataPdu ) -> None :
687+ assert self ._params .transaction_id is not None
687688 self ._params .checksum_type = metadata_pdu .checksum_type
688689 self ._params .closure_requested = metadata_pdu .closure_requested
689690 self ._params .acked_params .metadata_missing = False
@@ -703,10 +704,11 @@ def _handle_metadata_packet(self, metadata_pdu: MetadataPdu) -> None:
703704 raise NoRemoteEntityConfigFound (metadata_pdu .dest_entity_id )
704705 if not self ._params .fp .metadata_only :
705706 self .states .step = TransactionStep .RECEIVING_FILE_DATA
706- self ._init_vfs_handling (Path (metadata_pdu .source_file_name ).name ) # type: ignore
707+ assert metadata_pdu .source_file_name is not None
708+ self ._init_vfs_handling (Path (metadata_pdu .source_file_name ).name )
707709 else :
708710 self .states .step = TransactionStep .TRANSFER_COMPLETION
709- msgs_to_user_list = None
711+ msgs_to_user_list : None | list [ MessageToUserTlv ] = None
710712 options = metadata_pdu .options_as_tlv ()
711713 if options is not None :
712714 msgs_to_user_list = []
@@ -717,7 +719,7 @@ def _handle_metadata_packet(self, metadata_pdu: MetadataPdu) -> None:
717719 None if metadata_pdu .source_file_name is None else metadata_pdu .file_size
718720 )
719721 params = MetadataRecvParams (
720- transaction_id = self ._params .transaction_id , # type: ignore
722+ transaction_id = self ._params .transaction_id ,
721723 file_size = file_size_for_indication ,
722724 source_id = metadata_pdu .source_entity_id ,
723725 dest_file_name = metadata_pdu .dest_file_name ,
@@ -848,11 +850,11 @@ def _handle_fd_pdu(self, file_data_pdu: FileDataPdu) -> None:
848850 except FileNotFoundError :
849851 if self ._params .finished_params .file_status != FileStatus .FILE_RETAINED :
850852 self ._params .finished_params .file_status = FileStatus .DISCARDED_FILESTORE_REJECTION
851- self ._declare_fault (ConditionCode .FILESTORE_REJECTION )
853+ _ = self ._declare_fault (ConditionCode .FILESTORE_REJECTION )
852854 except PermissionError :
853855 if self ._params .finished_params .file_status != FileStatus .FILE_RETAINED :
854856 self ._params .finished_params .file_status = FileStatus .DISCARDED_FILESTORE_REJECTION
855- self ._declare_fault (ConditionCode .FILESTORE_REJECTION )
857+ _ = self ._declare_fault (ConditionCode .FILESTORE_REJECTION )
856858
857859 def _handle_transfer_completion (self ) -> None :
858860 self ._notice_of_completion ()
@@ -887,7 +889,7 @@ def _lost_segment_handling(self, offset: int, data_len: int) -> None:
887889 self ._params .acked_params .last_end_offset = offset + data_len
888890 if offset + data_len <= self ._params .acked_params .last_start_offset :
889891 # Might be a re-requested FD PDU.
890- self ._params .acked_params .lost_seg_tracker .remove_lost_segment (
892+ _ = self ._params .acked_params .lost_seg_tracker .remove_lost_segment (
891893 (offset , offset + data_len )
892894 )
893895
@@ -973,7 +975,7 @@ def _handle_eof_pdu(self, eof_pdu: EofPdu) -> None:
973975 assert self ._params .transaction_id is not None
974976 self .user .eof_recv_indication (self ._params .transaction_id )
975977 if eof_pdu .condition_code == ConditionCode .NO_ERROR :
976- regular_completion = self ._handle_eof_no_error ()
978+ regular_completion = self ._handle_eof_no_error (eof_pdu . file_checksum )
977979 if not regular_completion :
978980 return
979981 else :
@@ -985,6 +987,7 @@ def _handle_eof_pdu(self, eof_pdu: EofPdu) -> None:
985987 self ._eof_ack_pdu_done ()
986988
987989 def _handle_eof_cancel (self , eof_pdu : EofPdu ) -> None :
990+ assert self ._params .remote_cfg is not None
988991 # This is an EOF (Cancel), perform Cancel Response Procedures according to chapter
989992 # 4.6.6 of the standard. Set remote ID as fault location.
990993 self ._trigger_notice_of_completion_canceled (
@@ -1000,29 +1003,29 @@ def _handle_eof_cancel(self, eof_pdu: EofPdu) -> None:
10001003 # Empty file, no file data PDU.
10011004 self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
10021005 return
1003- if self ._checksum_verify (self ._params .fp .progress , self . _params . fp . crc32 ):
1006+ if self ._checksum_verify (self ._params .fp .progress , eof_pdu . file_checksum ):
10041007 self ._params .finished_params .delivery_code = DeliveryCode .DATA_COMPLETE
10051008 return
10061009 self ._params .finished_params .delivery_code = DeliveryCode .DATA_INCOMPLETE
10071010
1008- def _handle_eof_no_error (self ) -> bool :
1011+ def _handle_eof_no_error (self , crc32 : bytes ) -> bool :
10091012 """Returns whether the transfer can be completed regularly."""
1013+ assert self ._params .fp .file_size is not None
10101014 # CFDP 4.6.1.2.9: Declare file size error if progress exceeds file size
1011- if self ._params .fp .progress > self ._params .fp .file_size : # type: ignore
1015+ if self ._params .fp .progress > self ._params .fp .file_size :
10121016 if self ._declare_fault (ConditionCode .FILE_SIZE_ERROR ) != FaultHandlerCode .IGNORE_ERROR :
10131017 return False
10141018 elif (
1015- self ._params .fp .progress < self ._params .fp .file_size # type: ignore
1019+ self ._params .fp .progress < self ._params .fp .file_size
10161020 ) and self .transmission_mode == TransmissionMode .ACKNOWLEDGED :
10171021 # CFDP 4.6.4.3.1: The end offset of the last received file segment and the file
10181022 # size as stated in the EOF PDU is not the same, so we need to add that segment to
10191023 # the lost segments for the deferred lost segment detection procedure.
10201024 self ._params .acked_params .lost_seg_tracker .add_lost_segment (
10211025 (self ._params .fp .progress , self ._params .fp .file_size ) # type: ignore
10221026 )
1023- if (
1024- self .transmission_mode == TransmissionMode .UNACKNOWLEDGED
1025- and not self ._checksum_verify (self ._params .fp .progress , self ._params .fp .crc32 ) # type: ignore
1027+ if self .transmission_mode == TransmissionMode .UNACKNOWLEDGED and not self ._checksum_verify (
1028+ self ._params .fp .progress , crc32
10261029 ):
10271030 self ._start_check_limit_handling ()
10281031 return False
@@ -1031,14 +1034,15 @@ def _handle_eof_no_error(self) -> bool:
10311034 return True
10321035
10331036 def _start_deferred_lost_segment_handling (self ) -> None :
1037+ assert self ._params .fp .file_size is not None
10341038 if self ._params .acked_params .metadata_missing :
10351039 self .states .step = TransactionStep .WAITING_FOR_METADATA
10361040 else :
10371041 self .states .step = TransactionStep .WAITING_FOR_MISSING_DATA
10381042 self ._params .acked_params .deferred_lost_segment_detection_active = True
10391043 self ._params .acked_params .lost_seg_tracker .coalesce_lost_segments ()
1040- self ._params .acked_params .last_start_offset = self ._params .fp .file_size # type: ignore
1041- self ._params .acked_params .last_end_offset = self ._params .fp .file_size # type: ignore
1044+ self ._params .acked_params .last_start_offset = self ._params .fp .file_size
1045+ self ._params .acked_params .last_end_offset = self ._params .fp .file_size
10421046 self ._deferred_lost_segment_handling ()
10431047
10441048 def _prepare_eof_ack_packet (self ) -> None :
@@ -1084,6 +1088,7 @@ def _start_check_limit_handling(self) -> None:
10841088 self ._params .current_check_count = 0
10851089
10861090 def _notice_of_completion (self ) -> None :
1091+ assert self ._params .transaction_id is not None
10871092 if self ._params .completion_disposition == CompletionDisposition .COMPLETED :
10881093 # TODO: Execute any filestore requests
10891094 pass
@@ -1097,7 +1102,7 @@ def _notice_of_completion(self) -> None:
10971102 self ._params .finished_params .file_status = FileStatus .DISCARDED_DELIBERATELY
10981103 if self .cfg .indication_cfg .transaction_finished_indication_required :
10991104 finished_indic_params = TransactionFinishedParams (
1100- transaction_id = self ._params .transaction_id , # type: ignore
1105+ transaction_id = self ._params .transaction_id ,
11011106 finished_params = self ._params .finished_params ,
11021107 status_report = None ,
11031108 )
0 commit comments