Skip to content

Commit

Permalink
[Python] Rename CallAsync to CallAsyncWithCallback
Browse files Browse the repository at this point in the history
CallAsync continuously calls a callback function during the wait
for the call. Rename the function to reflect that fact.

This frees up CallAsync for an asyncio friendly implementation.
  • Loading branch information
agners committed May 7, 2024
1 parent b791b75 commit 604af30
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __init__(self, deviceProxy: ctypes.c_void_p, dmLib=None):
def __del__(self):
if (self._dmLib is not None and hasattr(builtins, 'chipStack') and builtins.chipStack is not None):
# This destructor is called from any threading context, including on the Matter threading context.
# So, we cannot call chipStack.Call or chipStack.CallAsync which waits for the posted work to
# So, we cannot call chipStack.Call or chipStack.CallAsyncWithCallback which waits for the posted work to
# actually be executed. Instead, we just post/schedule the work and move on.
builtins.chipStack.PostTaskOnChipThread(lambda: self._dmLib.pychip_FreeOperationalDeviceProxy(self._deviceProxy))

Expand Down Expand Up @@ -447,7 +447,7 @@ def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> PyChipError:

self.state = DCState.COMMISSIONING
self._enablePairingCompeleteCallback(True)
self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_ConnectBLE(
self.devCtrl, discriminator, setupPinCode, nodeid)
).raise_on_error()
Expand All @@ -459,7 +459,7 @@ def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> PyChipError:
def UnpairDevice(self, nodeid: int):
self.CheckIsActive()

return self._ChipStack.CallAsync(
return self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_UnpairDevice(
self.devCtrl, nodeid, self.cbHandleDeviceUnpairCompleteFunct)
).raise_on_error()
Expand Down Expand Up @@ -498,7 +498,7 @@ def EstablishPASESessionBLE(self, setupPinCode: int, discriminator: int, nodeid:

self.state = DCState.RENDEZVOUS_ONGOING
self._enablePairingCompeleteCallback(True)
return self._ChipStack.CallAsync(
return self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_EstablishPASESessionBLE(
self.devCtrl, setupPinCode, discriminator, nodeid)
)
Expand All @@ -508,7 +508,7 @@ def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int, po

self.state = DCState.RENDEZVOUS_ONGOING
self._enablePairingCompeleteCallback(True)
return self._ChipStack.CallAsync(
return self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_EstablishPASESessionIP(
self.devCtrl, ipaddr.encode("utf-8"), setupPinCode, nodeid, port)
)
Expand All @@ -518,7 +518,7 @@ def EstablishPASESession(self, setUpCode: str, nodeid: int):

self.state = DCState.RENDEZVOUS_ONGOING
self._enablePairingCompeleteCallback(True)
return self._ChipStack.CallAsync(
return self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_EstablishPASESession(
self.devCtrl, setUpCode.encode("utf-8"), nodeid)
)
Expand Down Expand Up @@ -737,7 +737,7 @@ def OpenCommissioningWindow(self, nodeid: int, timeout: int, iteration: int,
Returns CommissioningParameters
'''
self.CheckIsActive()
self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_OpenCommissioningWindow(
self.devCtrl, self.pairingDelegate, nodeid, timeout, iteration, discriminator, option)
).raise_on_error()
Expand Down Expand Up @@ -1895,7 +1895,7 @@ def Commission(self, nodeid) -> PyChipError:
self._ChipStack.commissioningCompleteEvent.clear()
self.state = DCState.COMMISSIONING

self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_Commission(
self.devCtrl, nodeid)
)
Expand Down Expand Up @@ -2011,7 +2011,7 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
self._ChipStack.commissioningCompleteEvent.clear()

self._enablePairingCompeleteCallback(True)
self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_OnNetworkCommission(
self.devCtrl, self.pairingDelegate, nodeId, setupPinCode, int(filterType), str(filter).encode("utf-8") + b"\x00" if filter is not None else None, discoveryTimeoutMsec)
)
Expand All @@ -2035,7 +2035,7 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: Disc
self._ChipStack.commissioningCompleteEvent.clear()

self._enablePairingCompeleteCallback(True)
self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_ConnectWithCode(
self.devCtrl, setupPayload, nodeid, discoveryType.value)
)
Expand All @@ -2055,7 +2055,7 @@ def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> PyChipErr
self._ChipStack.commissioningCompleteEvent.clear()

self._enablePairingCompeleteCallback(True)
self._ChipStack.CallAsync(
self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_ConnectIP(
self.devCtrl, ipaddr.encode("utf-8"), setupPinCode, nodeid)
)
Expand All @@ -2069,7 +2069,7 @@ def IssueNOCChain(self, csr: Clusters.OperationalCredentials.Commands.CSRRespons
The NOC chain will be provided in TLV cert format."""
self.CheckIsActive()

return self._ChipStack.CallAsync(
return self._ChipStack.CallAsyncWithCallback(
lambda: self._dmLib.pychip_DeviceController_IssueNOCChain(
self.devCtrl, py_object(self), csr.NOCSRElements, len(csr.NOCSRElements), nodeId)
)
Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def Call(self, callFunct, timeoutMs: int = None):
return self.callbackRes
return res

def CallAsync(self, callFunct):
def CallAsyncWithCallback(self, callFunct):
'''Run a Python function on CHIP stack, and wait for the application specific response.
This function is a wrapper of PostTaskOnChipThread, which includes some handling of application specific logics.
Calling this function on CHIP on CHIP mainloop thread will cause deadlock.
Expand Down

0 comments on commit 604af30

Please sign in to comment.