|
16 | 16 |
|
17 | 17 | import ctypes
|
18 | 18 | import enum
|
| 19 | +import functools |
19 | 20 | import glob
|
20 | 21 | import os
|
21 | 22 | import platform
|
@@ -146,21 +147,31 @@ def __ne__(self, other):
|
146 | 147 | return not self == other
|
147 | 148 |
|
148 | 149 |
|
149 |
| -PostAttributeChangeCallback = ctypes.CFUNCTYPE( |
| 150 | +c_PostAttributeChangeCallback = ctypes.CFUNCTYPE( |
150 | 151 | None,
|
151 | 152 | ctypes.c_uint16,
|
152 | 153 | ctypes.c_uint16,
|
153 | 154 | ctypes.c_uint16,
|
154 | 155 | ctypes.c_uint8,
|
155 | 156 | ctypes.c_uint16,
|
156 |
| - # TODO: This should be a pointer to uint8_t, but ctypes does not provide |
157 |
| - # such a type. The best approximation is c_char_p, however, this |
158 |
| - # requires the caller to pass NULL-terminate C-string which might |
159 |
| - # not be the case here. |
160 |
| - ctypes.c_char_p, |
| 157 | + ctypes.POINTER(ctypes.c_char), |
161 | 158 | )
|
162 | 159 |
|
163 | 160 |
|
| 161 | +def PostAttributeChangeCallback(func): |
| 162 | + @functools.wraps(func) |
| 163 | + def wrapper( |
| 164 | + endpoint: int, |
| 165 | + clusterId: int, |
| 166 | + attributeId: int, |
| 167 | + xx_type: int, |
| 168 | + size: int, |
| 169 | + value: ctypes.POINTER(ctypes.c_char), |
| 170 | + ): |
| 171 | + return func(endpoint, clusterId, attributeId, xx_type, size, value[:size]) |
| 172 | + return c_PostAttributeChangeCallback(wrapper) |
| 173 | + |
| 174 | + |
164 | 175 | def FindNativeLibraryPath(library: Library) -> str:
|
165 | 176 | """Find the native CHIP dll/so path."""
|
166 | 177 |
|
@@ -234,7 +245,7 @@ def _GetLibraryHandle(lib: Library, expectAlreadyInitialized: bool) -> _Handle:
|
234 | 245 | [ctypes.POINTER(PyChipError), ctypes.c_char_p, ctypes.c_uint32])
|
235 | 246 | elif lib == Library.SERVER:
|
236 | 247 | setter.Set("pychip_server_native_init", PyChipError, [])
|
237 |
| - setter.Set("pychip_server_set_callbacks", None, [PostAttributeChangeCallback]) |
| 248 | + setter.Set("pychip_server_set_callbacks", None, [c_PostAttributeChangeCallback]) |
238 | 249 |
|
239 | 250 | handle = _nativeLibraryHandles[lib]
|
240 | 251 | if expectAlreadyInitialized and not handle.initialized:
|
|
0 commit comments