@@ -60,9 +60,12 @@ def datagram_received(self, data: bytes, addr: tuple[str, int]) -> None:
6060 if parsed_data :
6161 # Schedule the user-provided callback, don't await to keep listener responsive
6262 asyncio .create_task (self .callback (parsed_data )) # noqa: RUF006
63- except (AirosEndpointError , AirosListenerError ):
64- # Re-raise discovery-specific errors as-is
65- raise
63+ except (AirosEndpointError , AirosListenerError ) as err :
64+ # These are expected types of malformed packets. Log the specific error
65+ # and then re-raise as AirosDiscoveryError.
66+ log = f"Parsing failed for packet from { host_ip } : { err } "
67+ _LOGGER .exception (log )
68+ raise AirosDiscoveryError (f"Malformed packet from { host_ip } " ) from err
6669 except Exception as err :
6770 # General error during datagram reception (e.g., in callback itself)
6871 log = f"Error processing Airos discovery packet from { host_ip } . Data hex: { data .hex ()} "
@@ -114,12 +117,12 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
114117 if len (data ) < 6 :
115118 log = f"Packet too short for initial fixed header. Length: { len (data )} . Data: { data .hex ()} "
116119 _LOGGER .debug (log )
117- return None
120+ raise AirosEndpointError ( f"Malformed packet: { log } " )
118121
119122 if data [0 ] != 0x01 or data [1 ] != 0x06 :
120123 log = f"Packet does not start with expected Airos header (0x01 0x06). Actual: { data [0 :2 ].hex ()} "
121124 _LOGGER .debug (log )
122- return None
125+ raise AirosEndpointError ( f"Malformed packet: { log } " )
123126
124127 offset : int = 6
125128
@@ -147,7 +150,8 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
147150 else :
148151 log = f"Truncated MAC address TLV (Type 0x06). Expected { expected_length } , got { len (data ) - offset } bytes. Remaining: { data [offset :].hex ()} "
149152 _LOGGER .warning (log )
150- break
153+ log = f"Malformed packet: { log } "
154+ raise AirosEndpointError (log )
151155
152156 elif tlv_type in [
153157 0x02 ,
@@ -164,7 +168,8 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
164168 if (len (data ) - offset ) < 2 :
165169 log = f"Truncated TLV (Type { tlv_type :#x} ), no 2-byte length field. Remaining: { data [offset :].hex ()} "
166170 _LOGGER .warning (log )
167- break
171+ log = f"Malformed packet: { log } "
172+ raise AirosEndpointError (log )
168173
169174 tlv_length : int = struct .unpack_from (">H" , data , offset )[0 ]
170175 offset += 2
@@ -176,7 +181,8 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
176181 _LOGGER .warning (log )
177182 log = f"Data from TLV start: { data [offset - 3 :].hex ()} "
178183 _LOGGER .warning (log )
179- break
184+ log = f"Malformed packet: { log } "
185+ raise AirosEndpointError (log )
180186
181187 tlv_value : bytes = data [offset : offset + tlv_length ]
182188
@@ -250,15 +256,18 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
250256
251257 else :
252258 log = f"Unhandled TLV type: { tlv_type :#x} at offset { offset - 1 } . "
259+ log += f"Cannot determine length, stopping parsing. Remaining: { data [offset - 1 :].hex ()} "
253260 _LOGGER .warning (log )
254- log = f"Cannot determine length, stopping parsing. Remaining: { data [offset - 1 :].hex ()} "
255- _LOGGER .warning (log )
256- break
261+ log = f"Malformed packet: { log } "
262+ raise AirosEndpointError (log )
257263
258264 except (struct .error , IndexError ) as err :
259265 log = f"Parsing error (struct/index) in AirosDiscoveryProtocol: { err } at offset { offset } . Remaining data: { data [offset :].hex ()} "
260- _LOGGER .warning (log )
261- raise AirosEndpointError from err
266+ _LOGGER .debug (log )
267+ log = f"Malformed packet: { log } "
268+ raise AirosEndpointError (log ) from err
269+ except AirosEndpointError : # Catch AirosEndpointError specifically, re-raise it
270+ raise
262271 except Exception as err :
263272 _LOGGER .exception ("Unexpected error during Airos packet parsing" )
264273 raise AirosListenerError from err
0 commit comments