Skip to content

Commit

Permalink
wip: further CPy compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalbert committed Dec 29, 2024
1 parent 7b4cb0b commit 0e54df3
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 190 deletions.
5 changes: 3 additions & 2 deletions blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
"""Simple LED on and off as a light."""

import os
import cm_fake_random

import board
import digitalio

import circuitmatter as cm
import cm_fake_random
from circuitmatter.device_types.lighting import on_off

try:
print("removing matter-device-state.json if present")
os.remove("matter-device-state.json")
except FileNotFoundError:
except OSError:
pass


class LED(on_off.OnOffLight):
def __init__(self, name, led):
super().__init__(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum

import cm_enum as enum
from circuitmatter import tlv
from circuitmatter.data_model import (
BoolAttribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum

import cm_enum as enum
from circuitmatter import tlv
from circuitmatter.data_model import (
BitmapAttribute,
Expand Down
3 changes: 1 addition & 2 deletions circuitmatter/clusters/general/level_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum

import cm_enum as enum
from circuitmatter import data_model, tlv


Expand Down
3 changes: 1 addition & 2 deletions circuitmatter/clusters/general/on_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum

import cm_enum as enum
from circuitmatter import data_model, tlv


Expand Down
3 changes: 1 addition & 2 deletions circuitmatter/clusters/lighting/color_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum

import cm_enum as enum
from circuitmatter import data_model, tlv


Expand Down
10 changes: 2 additions & 8 deletions circuitmatter/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
#
# SPDX-License-Identifier: MIT

try:
import enum
except ImportError:
class enum:
class IntEnum:
pass

import hashlib
import struct

from cm_sha import sha256
import cm_ecdsa as ecdsa
import cm_enum as enum
import cm_hmac as hmac
from cm_sha import sha256

from . import tlv

Expand Down
15 changes: 9 additions & 6 deletions circuitmatter/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
# SPDX-License-Identifier: MIT

import binascii
import enum
import inspect
import random
import struct
import traceback
import typing
from collections.abc import Iterable
from typing import Union

try:
from collections.abc import Iterable
from typing import Union
except ImportError:
pass

import cm_enum as enum

from . import interaction_model, tlv

Expand Down Expand Up @@ -232,7 +235,7 @@ def __str__(self):

class ListAttribute(Attribute):
def __init__(self, _id, element_type, **kwargs):
if inspect.isclass(element_type) and issubclass(element_type, enum.Enum):
if isinstance(element_type, type) and issubclass(element_type, enum.Enum):
element_type = tlv.EnumMember(None, element_type)
self.tlv_type = tlv.ArrayMember(None, element_type)
self._element_type = element_type
Expand Down
3 changes: 0 additions & 3 deletions circuitmatter/device_types/lighting/dimmable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#
# SPDX-License-Identifier: MIT

import abc

from circuitmatter.clusters.general import level_control

from .on_off import OnOffLight
Expand Down Expand Up @@ -42,6 +40,5 @@ def brightness(self):
return self._level_control.CurrentLevel / self._level_control.max_level

@brightness.setter
@abc.abstractmethod
def brightness(self, value):
raise NotImplementedError()
4 changes: 0 additions & 4 deletions circuitmatter/device_types/lighting/on_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#
# SPDX-License-Identifier: MIT

import abc

from circuitmatter.clusters.general import on_off
from circuitmatter.clusters.general.identify import Identify

Expand Down Expand Up @@ -44,12 +42,10 @@ def _off(self, session):
return
self._on_off.OnOff = False

@abc.abstractmethod
def on(self):
"""Called when the light is turned on"""
pass

@abc.abstractmethod
def off(self):
"""Called when the light is turned off"""
pass
5 changes: 2 additions & 3 deletions circuitmatter/device_types/simple_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#
# SPDX-License-Identifier: MIT

from abc import ABC

from circuitmatter.clusters.system_model import binding, descriptor, user_label


class SimpleDevice(ABC):
# abstract
class SimpleDevice:
def __init__(self, name):
self.name = name
self.servers = []
Expand Down
2 changes: 1 addition & 1 deletion circuitmatter/interaction_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: MIT

import enum
import cm_enum as enum

from . import protocol, tlv

Expand Down
9 changes: 7 additions & 2 deletions circuitmatter/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#
# SPDX-License-Identifier: MIT

import enum
import struct
from typing import Optional

try:
from typing import Optional
except ImportError:
pass

import cm_enum as enum

from . import tlv
from .protocol import ProtocolId
Expand Down
3 changes: 1 addition & 2 deletions circuitmatter/pase.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import hashlib
import struct

from cryptography.hazmat.primitives.ciphers.aead import AESCCM

from cm_aesccm import AESCCM
from cm_ecdsa.curves import NIST256p
from cm_ecdsa.ellipticcurve import AbstractPoint, Point, PointJacobi

Expand Down
7 changes: 1 addition & 6 deletions circuitmatter/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
#
# SPDX-License-Identifier: MIT

try:
import enum
except ImportError:
class enum:
class IntEnum:
pass
import cm_enum as enum


class SecureProtocolOpcode(enum.IntEnum):
Expand Down
16 changes: 4 additions & 12 deletions circuitmatter/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
import struct
import time

try:
import enum
except ImportError:
class enum:
class IntEnum:
pass

import cryptography
from cryptography.hazmat.primitives.ciphers.aead import AESCCM

import cm_enum as enum
from cm_aesccm import AESCCM, InvalidTag
from cm_sha import sha256

from . import case, crypto, protocol, tlv
Expand Down Expand Up @@ -285,7 +277,7 @@ def decrypt_and_verify(self, message):
decrypted_payload = cipher.decrypt(
self._nonce, bytes(message.payload), bytes(message.header)
)
except cryptography.exceptions.InvalidTag:
except InvalidTag:
return False

message.decrypted = True
Expand Down Expand Up @@ -684,7 +676,7 @@ def reply_to_sigma3(self, exchange, sigma3) -> SecureChannelProtocolCode:
)
try:
decrypted = s3k_cipher.decrypt(b"NCASE_Sigma3N", sigma3.encrypted3, b"")
except cryptography.exceptions.InvalidTag:
except InvalidTag:
return SecureChannelProtocolCode.INVALID_PARAMETER
sigma3_tbe = case.Sigma3TbeData.decode(decrypted)

Expand Down
23 changes: 9 additions & 14 deletions circuitmatter/tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@

from __future__ import annotations

try:
import enum
except ImportError:
class enum:
class IntEnum:
pass
pass

import math
import struct

try:
from collections.abc import Iterable
from typing import (
AnyStr,
Generic,
Iterable,
Literal,
TypeVar,
overload,
)
except ImportError:
pass

import cm_enum as enum

# As a byte string to save space.
TAG_LENGTH = b"\x00\x01\x02\x04\x02\x04\x06\x08"
INT_SIZE = "BHIQ"
Expand All @@ -39,15 +33,16 @@ def _mro(cls):
return [object]
return [cls] + _mro_merge([_mro(base) for base in cls.__bases__])


def _mro_merge(mros):
if not any(mros): # all lists are empty
if not any(mros): # all lists are empty
return [] # base case
for candidate, *_ in mros:
if all(candidate not in tail for _, *tail in mros):
return [candidate] + _mro_merge([tail if head is candidate else [head, *tail]
for head, *tail in mros])
else:
raise TypeError("No legal mro")
return [candidate] + _mro_merge([
tail if head is candidate else [head] + tail for head, *tail in mros
])
raise TypeError("No legal mro")


class ElementType(enum.IntEnum):
Expand Down
7 changes: 5 additions & 2 deletions circuitmatter/hm_aesccm.py → cm_aesccm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# SPDX-License-Identifier: MIT

try:
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
except ImportError:
# Provide an exception raised by cryptography.
class InvalidTag(Exception):
pass

# Use a CircuitPython implementation.
from hm_aesccm_aesio import *
from cm_aesccm_aesio import new

def AESCCM(key, *, tag_length=16):
return new(key, mac_len=tag_length)
Loading

0 comments on commit 0e54df3

Please sign in to comment.