From db186e65a10020bd810b18babe4d85e6c5b860e5 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 16 Oct 2024 16:17:01 -0700 Subject: [PATCH] Fewer prints. Ignore unknown sessions and failure to decrypt --- circuitmatter/__init__.py | 17 +++++++++++------ circuitmatter/clusters/general/on_off.py | 2 +- circuitmatter/data_model.py | 1 - circuitmatter/exchange.py | 2 -- circuitmatter/nonvolatile.py | 12 ++---------- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/circuitmatter/__init__.py b/circuitmatter/__init__.py index 25e848c..01b5331 100644 --- a/circuitmatter/__init__.py +++ b/circuitmatter/__init__.py @@ -218,13 +218,18 @@ def process_packet(self, address, data): message.decode(data) message.source_ipaddress = address if message.secure_session: + secure_session_context = None + if message.session_id < len(self.manager.secure_session_contexts): + secure_session_context = self.manager.secure_session_contexts[ + message.session_id + ] + if secure_session_context is None: + print("Failed to find session. Ignoring.") + return # Decrypt the payload - secure_session_context = self.manager.secure_session_contexts[ - message.session_id - ] - ok = secure_session_context.decrypt_and_verify(message) - if not ok: - raise RuntimeError("Failed to decrypt message") + if not secure_session_context.decrypt_and_verify(message): + print("Failed to decrypt message. Ignoring.") + return message.parse_protocol_header() self.manager.mark_duplicate(message) diff --git a/circuitmatter/clusters/general/on_off.py b/circuitmatter/clusters/general/on_off.py index 417d228..caa9f84 100644 --- a/circuitmatter/clusters/general/on_off.py +++ b/circuitmatter/clusters/general/on_off.py @@ -11,7 +11,7 @@ class StartUpOnOffEnum(data_model.Enum8): class OnOff(data_model.Cluster): CLUSTER_ID = 0x0006 - OnOff = data_model.BoolAttribute(0x0000, default=False) + OnOff = data_model.BoolAttribute(0x0000, default=False, N_nonvolatile=True) GlobalSceneControl = data_model.BoolAttribute(0x4000, default=True) OnTime = data_model.NumberAttribute(0x4001, signed=False, bits=16, default=0) OffWaitTime = data_model.NumberAttribute(0x4002, signed=False, bits=16, default=0) diff --git a/circuitmatter/data_model.py b/circuitmatter/data_model.py index ad9ac3e..1e4c00e 100644 --- a/circuitmatter/data_model.py +++ b/circuitmatter/data_model.py @@ -313,7 +313,6 @@ def restore(self, nonvolatile): nonvolatile[ATTRIBUTES_KEY] = {} for field_name, descriptor in self._attributes(): if descriptor.nonvolatile: - print(field_name, nonvolatile[ATTRIBUTES_KEY]) if hex(descriptor.id) in nonvolatile[ATTRIBUTES_KEY]: # Update our live value self._attribute_values[descriptor.id] = descriptor.from_json( diff --git a/circuitmatter/exchange.py b/circuitmatter/exchange.py index df9b6c1..136016a 100644 --- a/circuitmatter/exchange.py +++ b/circuitmatter/exchange.py @@ -107,12 +107,10 @@ def receive(self, message) -> bool: ): # Drop messages that have the wrong acknowledgement counter. return True - print("acknowledged", message.acknowledged_message_counter) self.pending_retransmission = None self.next_retransmission_time = None if message.protocol_id not in self.protocols: - print("protocol mismatch") # Drop messages that don't match the protocols we're waiting for. return True diff --git a/circuitmatter/nonvolatile.py b/circuitmatter/nonvolatile.py index 52b93c9..9c0fbb0 100644 --- a/circuitmatter/nonvolatile.py +++ b/circuitmatter/nonvolatile.py @@ -11,7 +11,8 @@ def __init__(self, filename=None, root=None, state=None): self.persisted = {} self._state: dict if self.root is None and filename: - self.rollback() + with open(self.filename, "r") as state_file: + self._state = json.load(state_file) elif state is not None: self._state = state else: @@ -51,19 +52,10 @@ def __iter__(self): def commit(self): if not self.dirty: - print("not dirty") return if self.root: - print("root commit") self.root.commit() return - print("commit") - print(self._state) with open(self.filename, "w") as state_file: json.dump(self._state, state_file, indent=1) self.dirty = False - - def rollback(self): - print("rollback") - with open(self.filename, "r") as state_file: - self._state = json.load(state_file)