Skip to content

Commit

Permalink
Initial commit (#19724)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjerryjohns authored and pull[bot] committed Aug 4, 2023
1 parent 518b1e3 commit 1177266
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
26 changes: 11 additions & 15 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, opCredsContext: ctypes.c_void_p, fabricId: int, fabricIndex:

res = self._ChipStack.Call(
lambda: self._dmLib.pychip_OpCreds_AllocateController(ctypes.c_void_p(
opCredsContext), pointer(devCtrl), fabricIndex, fabricId, nodeId, ctypes.c_char_p(None if len(paaTrustStorePath) is 0 else str.encode(paaTrustStorePath)), useTestCommissioner)
opCredsContext), pointer(devCtrl), fabricIndex, fabricId, nodeId, ctypes.c_char_p(None if len(paaTrustStorePath) == 0 else str.encode(paaTrustStorePath)), useTestCommissioner)
)

if res != 0:
Expand Down Expand Up @@ -546,14 +546,12 @@ async def SendCommand(self, nodeid: int, endpoint: int, payload: ClusterObjects.
future = eventLoop.create_future()

device = self.GetConnectedDeviceSync(nodeid)
res = self._ChipStack.Call(
lambda: ClusterCommand.SendCommand(
future, eventLoop, responseType, device, ClusterCommand.CommandPath(
EndpointId=endpoint,
ClusterId=payload.cluster_id,
CommandId=payload.command_id,
), payload, timedRequestTimeoutMs=timedRequestTimeoutMs)
)
res = ClusterCommand.SendCommand(
future, eventLoop, responseType, device, ClusterCommand.CommandPath(
EndpointId=endpoint,
ClusterId=payload.cluster_id,
CommandId=payload.command_id,
), payload, timedRequestTimeoutMs=timedRequestTimeoutMs)
if res != 0:
future.set_exception(self._ChipStack.ErrorToException(res))
return await future
Expand Down Expand Up @@ -585,10 +583,8 @@ async def WriteAttribute(self, nodeid: int, attributes: typing.List[typing.Tuple
attrs.append(ClusterAttribute.AttributeWriteRequest(
v[0], v[1], v[2], 1, v[1].value))

res = self._ChipStack.Call(
lambda: ClusterAttribute.WriteAttributes(
future, eventLoop, device, attrs, timedRequestTimeoutMs=timedRequestTimeoutMs)
)
res = ClusterAttribute.WriteAttributes(
future, eventLoop, device, attrs, timedRequestTimeoutMs=timedRequestTimeoutMs)
if res != 0:
raise self._ChipStack.ErrorToException(res)
return await future
Expand Down Expand Up @@ -769,8 +765,8 @@ async def Read(self, nodeid: int, attributes: typing.List[typing.Union[
eventPaths = [self._parseEventPathTuple(
v) for v in events] if events else None

res = self._ChipStack.Call(
lambda: ClusterAttribute.Read(future=future, eventLoop=eventLoop, device=device, devCtrl=self, attributes=attributePaths, dataVersionFilters=clusterDataVersionFilters, events=eventPaths, returnClusterObject=returnClusterObject, subscriptionParameters=ClusterAttribute.SubscriptionParameters(reportInterval[0], reportInterval[1]) if reportInterval else None, fabricFiltered=fabricFiltered, keepSubscriptions=keepSubscriptions))
res = ClusterAttribute.Read(future=future, eventLoop=eventLoop, device=device, devCtrl=self, attributes=attributePaths, dataVersionFilters=clusterDataVersionFilters, events=eventPaths, returnClusterObject=returnClusterObject,
subscriptionParameters=ClusterAttribute.SubscriptionParameters(reportInterval[0], reportInterval[1]) if reportInterval else None, fabricFiltered=fabricFiltered, keepSubscriptions=keepSubscriptions)
if res != 0:
raise self._ChipStack.ErrorToException(res)
return await future
Expand Down
33 changes: 18 additions & 15 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,9 @@ def Shutdown(self):
return

handle = chip.native.GetLibraryHandle()
handle.pychip_ReadClient_Abort(
self._readTransaction._pReadClient, self._readTransaction._pReadCallback)
builtins.chipStack.Call(
lambda: handle.pychip_ReadClient_Abort(
self._readTransaction._pReadClient, self._readTransaction._pReadCallback))
self._isDone = True

def __del__(self):
Expand Down Expand Up @@ -853,8 +854,9 @@ def WriteAttributes(future: Future, eventLoop, device, attributes: List[Attribut

transaction = AsyncWriteTransaction(future, eventLoop)
ctypes.pythonapi.Py_IncRef(ctypes.py_object(transaction))
res = handle.pychip_WriteClient_WriteAttributes(
ctypes.py_object(transaction), device, ctypes.c_uint16(0 if timedRequestTimeoutMs is None else timedRequestTimeoutMs), ctypes.c_size_t(len(attributes)), *writeargs)
res = builtins.chipStack.Call(
lambda: handle.pychip_WriteClient_WriteAttributes(
ctypes.py_object(transaction), device, ctypes.c_uint16(0 if timedRequestTimeoutMs is None else timedRequestTimeoutMs), ctypes.c_size_t(len(attributes)), *writeargs))
if res != 0:
ctypes.pythonapi.Py_DecRef(ctypes.py_object(transaction))
return res
Expand Down Expand Up @@ -954,17 +956,18 @@ def Read(future: Future, eventLoop, device, devCtrl, attributes: List[AttributeP
params.IsFabricFiltered = fabricFiltered
params = _ReadParams.build(params)

res = handle.pychip_ReadClient_Read(
ctypes.py_object(transaction),
ctypes.byref(readClientObj),
ctypes.byref(readCallbackObj),
device,
ctypes.c_char_p(params),
ctypes.c_size_t(0 if attributes is None else len(attributes)),
ctypes.c_size_t(
0 if dataVersionFilters is None else len(dataVersionFilters)),
ctypes.c_size_t(0 if events is None else len(events)),
*readargs)
res = builtins.chipStack.Call(
lambda: handle.pychip_ReadClient_Read(
ctypes.py_object(transaction),
ctypes.byref(readClientObj),
ctypes.byref(readCallbackObj),
device,
ctypes.c_char_p(params),
ctypes.c_size_t(0 if attributes is None else len(attributes)),
ctypes.c_size_t(
0 if dataVersionFilters is None else len(dataVersionFilters)),
ctypes.c_size_t(0 if events is None else len(events)),
*readargs))

transaction.SetClientObjPointers(readClientObj, readCallbackObj)

Expand Down
6 changes: 4 additions & 2 deletions src/controller/python/chip/clusters/Command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import inspect
import sys
import builtins


@dataclass
Expand Down Expand Up @@ -157,8 +158,9 @@ def SendCommand(future: Future, eventLoop, responseType: Type, device, commandPa

payloadTLV = payload.ToTLV()
ctypes.pythonapi.Py_IncRef(ctypes.py_object(transaction))
return handle.pychip_CommandSender_SendCommand(ctypes.py_object(
transaction), device, c_uint16(0 if timedRequestTimeoutMs is None else timedRequestTimeoutMs), commandPath.EndpointId, commandPath.ClusterId, commandPath.CommandId, payloadTLV, len(payloadTLV))
return builtins.chipStack.Call(
lambda: handle.pychip_CommandSender_SendCommand(ctypes.py_object(
transaction), device, c_uint16(0 if timedRequestTimeoutMs is None else timedRequestTimeoutMs), commandPath.EndpointId, commandPath.ClusterId, commandPath.CommandId, payloadTLV, len(payloadTLV)))


def Init():
Expand Down
3 changes: 3 additions & 0 deletions src/controller/python/chip/clusters/attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,11 @@ chip::ChipError::StorageType pychip_ReadClient_Read(void * appContext, ReadClien
params.mMaxIntervalCeilingSeconds = pyParams.maxInterval;
params.mKeepSubscriptions = pyParams.keepSubscriptions;
params.mResubscribePolicy = PythonResubscribePolicy;

dataVersionFilters.release();
attributePaths.release();
eventPaths.release();

err = readClient->SendAutoResubscribeRequest(std::move(params));
SuccessOrExit(err);
}
Expand Down
7 changes: 7 additions & 0 deletions src/controller/python/test/test_scripts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,13 @@ def run(self):

# thread changes 5 times, and sleeps for 3 seconds in between. Add an additional 3 seconds of slack. Timeout is in seconds.
changeThread.join(18.0)

#
# Clean-up by shutting down the sub. Otherwise, we're going to get callbacks through OnValueChange on what will soon become an invalid
# execution context above.
#
subscription.Shutdown()

if changeThread.is_alive():
# Thread join timed out
self.logger.error(f"Failed to join change thread")
Expand Down

0 comments on commit 1177266

Please sign in to comment.