Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PostAttributeChangeCallback value ctypes typing #36928

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/controller/python/chip/native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ctypes
import enum
import functools
import glob
import os
import platform
Expand Down Expand Up @@ -146,21 +147,31 @@ def __ne__(self, other):
return not self == other


PostAttributeChangeCallback = ctypes.CFUNCTYPE(
c_PostAttributeChangeCallback = ctypes.CFUNCTYPE(
None,
ctypes.c_uint16,
ctypes.c_uint16,
ctypes.c_uint16,
ctypes.c_uint8,
ctypes.c_uint16,
# TODO: This should be a pointer to uint8_t, but ctypes does not provide
# such a type. The best approximation is c_char_p, however, this
# requires the caller to pass NULL-terminate C-string which might
# not be the case here.
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_char),
)


def PostAttributeChangeCallback(func):
@functools.wraps(func)
def wrapper(
endpoint: int,
clusterId: int,
attributeId: int,
xx_type: int,
size: int,
value: ctypes.POINTER(ctypes.c_char),
):
return func(endpoint, clusterId, attributeId, xx_type, size, value[:size])
return c_PostAttributeChangeCallback(wrapper)


def FindNativeLibraryPath(library: Library) -> str:
"""Find the native CHIP dll/so path."""

Expand Down Expand Up @@ -234,7 +245,7 @@ def _GetLibraryHandle(lib: Library, expectAlreadyInitialized: bool) -> _Handle:
[ctypes.POINTER(PyChipError), ctypes.c_char_p, ctypes.c_uint32])
elif lib == Library.SERVER:
setter.Set("pychip_server_native_init", PyChipError, [])
setter.Set("pychip_server_set_callbacks", None, [PostAttributeChangeCallback])
setter.Set("pychip_server_set_callbacks", None, [c_PostAttributeChangeCallback])

handle = _nativeLibraryHandles[lib]
if expectAlreadyInitialized and not handle.initialized:
Expand Down
15 changes: 12 additions & 3 deletions src/controller/python/chip/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
import ctypes

from chip import native
from chip.native import PostAttributeChangeCallback
from chip.native import PostAttributeChangeCallback, c_PostAttributeChangeCallback

__all__ = [
"GetLibraryHandle",
"PostAttributeChangeCallback",
]

def GetLibraryHandle(cb: PostAttributeChangeCallback) -> ctypes.CDLL:
"""Get a memoized handle to the chip native code dll."""

def GetLibraryHandle(cb: c_PostAttributeChangeCallback) -> ctypes.CDLL:
"""Get a memoized handle to the chip native code dll.

Args:
cb: A callback decorated by PostAttributeChangeCallback decorator.
"""

handle = native._GetLibraryHandle(native.Library.SERVER, False)
if not handle.initialized:
Expand Down
Loading