Skip to content

Commit

Permalink
[Python] TLV schema tag should start from 1 (#12347)
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing authored and pull[bot] committed Jul 10, 2023
1 parent 7346ecc commit 1674236
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 195 deletions.
10 changes: 10 additions & 0 deletions src/controller/python/chip/ChipReplStartup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ def ReplInit():
def matterhelp(classOrObj=None):
if (classOrObj == None):
inspect(builtins.devCtrl, methods=True, help=True, private=False)
inspect(mattersetlog)
inspect(mattersetdebug)
else:
inspect(classOrObj, methods=True, help=True, private=False)


def mattersetlog(level):
logging.getLogger().setLevel(level)


def mattersetdebug(enableDebugMode: bool = True):
''' Enables debug mode that is utilized by some Matter modules
to better facilitate debugging of failures (e.g throwing exceptions instead
of returning well-formatted results).
'''
builtins.enableDebugMode = enableDebugMode
3 changes: 3 additions & 0 deletions src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from ctypes import *
from .ChipUtility import ChipUtility
from .exceptions import *
import builtins

__all__ = [
"DeviceStatusStruct",
Expand Down Expand Up @@ -160,6 +161,8 @@ def Wait(self):
@_singleton
class ChipStack(object):
def __init__(self, installDefaultLogHandler=True, bluetoothAdapter=0):
builtins.enableDebugMode = False

self.networkLock = Lock()
self.completeEvent = Event()
self.commissioningCompleteEvent = Event()
Expand Down
29 changes: 24 additions & 5 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sys
import logging
import threading
import builtins


@dataclass
Expand Down Expand Up @@ -97,6 +98,16 @@ class AttributeReadResult(AttributeStatus):
Data: Any = None


@dataclass
class ValueDecodeFailure:
''' Encapsulates a failure to decode a TLV value into a cluster object.
Some exceptions have custom fields, so run str(ReasonException) to get more info.
'''

TLVValue: Any = None
Reason: Exception = None


_AttributeIndex = {}


Expand Down Expand Up @@ -199,21 +210,29 @@ def _handleAttributeData(self, path: AttributePathWithListIndex, status: int, da
attributeType = _AttributeIndex.get(str(AttributePath(
ClusterId=path.ClusterId, AttributeId=path.AttributeId)), None)
attributeValue = None
tlvData = chip.tlv.TLVReader(data).get().get("Any", {})
if attributeType is None:
attributeValue = chip.tlv.TLVReader(data).get().get("Any", {})
attributeValue = ValueDecodeFailure(
tlvData, LookupError("attribute schema not found"))
else:
try:
attributeValue = attributeType.FromTLV(data)
except:
attributeValue = attributeType(attributeType.FromTLV(data))
except Exception as ex:
logging.error(
f"Error convering TLV to Cluster Object for path: Endpoint = {path.EndpointId}/Cluster = {path.ClusterId}/Attribute = {path.AttributeId}")
logging.error(
f"Failed Cluster Object: {str(attributeType)}")
raise
logging.error(ex)
attributeValue = ValueDecodeFailure(
tlvData, ex)

# If we're in debug mode, raise the exception so that we can better debug what's happening.
if (builtins.enableDebugMode):
raise

with self._resLock:
self._res[path] = AttributeReadResult(
Path=path, Status=imStatus, Data=attributeType(attributeValue))
Path=path, Status=imStatus, Data=attributeValue)
if self._subscription_handler is not None:
self._subscription_handler.OnUpdate(
path, attributeType(attributeValue))
Expand Down
Loading

0 comments on commit 1674236

Please sign in to comment.