Skip to content

Commit

Permalink
[Python] CommissonWithCode support discoveryType (#31904)
Browse files Browse the repository at this point in the history
* [Linux] fix memory leak

* [Python] call StopDiscovery after DiscoveryNodes

* [Python] CommissionWithCode support DiscoveryType

* fix param error

* add e2e test

* automatically run in CI

* Test different modes using different devices

* fix error manual code
  • Loading branch information
tianfeng-yang authored and pull[bot] committed Jun 28, 2024
1 parent 9892874 commit 1029404
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
10 changes: 4 additions & 6 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissio
PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr,
uint32_t setupPINCode, chip::NodeId nodeid);
PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload,
chip::NodeId nodeid, bool networkOnly);
chip::NodeId nodeid, uint8_t discoveryType);
PyChipError pychip_DeviceController_UnpairDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId remoteDeviceId,
DeviceUnpairingCompleteFunct callback);
PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size);
Expand Down Expand Up @@ -401,13 +401,11 @@ PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommission
}

PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload,
chip::NodeId nodeid, bool networkOnly)
chip::NodeId nodeid, uint8_t discoveryType)
{
chip::Controller::DiscoveryType discoveryType = chip::Controller::DiscoveryType::kAll;
sPairingDelegate.SetExpectingPairingComplete(true);
if (networkOnly)
discoveryType = chip::Controller::DiscoveryType::kDiscoveryNetworkOnly;
return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters, discoveryType));
return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters,
static_cast<chip::Controller::DiscoveryType>(discoveryType)));
}

namespace {
Expand Down
7 changes: 4 additions & 3 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def attestationChallenge(self) -> bytes:


DiscoveryFilterType = discovery.FilterType
DiscoveryType = discovery.DiscoveryType


class ChipDeviceControllerBase():
Expand Down Expand Up @@ -1624,7 +1625,7 @@ def _InitLib(self):
self._dmLib.pychip_DeviceController_ConnectIP.restype = PyChipError

self._dmLib.pychip_DeviceController_ConnectWithCode.argtypes = [
c_void_p, c_char_p, c_uint64, c_bool]
c_void_p, c_char_p, c_uint64, c_uint8]
self._dmLib.pychip_DeviceController_ConnectWithCode.restype = PyChipError

self._dmLib.pychip_DeviceController_UnpairDevice.argtypes = [
Expand Down Expand Up @@ -1937,7 +1938,7 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
return PyChipError(CHIP_ERROR_TIMEOUT)
return self._ChipStack.commissioningEventRes

def CommissionWithCode(self, setupPayload: str, nodeid: int, networkOnly: bool = False) -> PyChipError:
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> PyChipError:
''' Commission with the given nodeid from the setupPayload.
setupPayload may be a QR or manual code.
'''
Expand All @@ -1953,7 +1954,7 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int, networkOnly: bool =

self._ChipStack.CallAsync(
lambda: self._dmLib.pychip_DeviceController_ConnectWithCode(
self.devCtrl, setupPayload, nodeid, networkOnly)
self.devCtrl, setupPayload, nodeid, discoveryType.value)
)
if not self._ChipStack.commissioningCompleteEvent.isSet():
# Error 50 is a timeout
Expand Down
6 changes: 6 additions & 0 deletions src/controller/python/chip/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
from chip.native import PyChipError


class DiscoveryType(enum.IntEnum):
DISCOVERY_NETWORK_ONLY = 0
DISCOVERY_NETWORK_ONLY_WITHOUT_PASE_AUTO_RETRY = 1
DISCOVERY_ALL = 2


class FilterType(enum.IntEnum):
# These must match chip::Dnssd::DiscoveryFilterType values (barring the naming convention)
NONE = 0
Expand Down
4 changes: 2 additions & 2 deletions src/controller/python/test/test_scripts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ def TestCommissioning(self, ip: str, setuppin: int, nodeid: int):
self.logger.info("Commissioning finished.")
return True

def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int):
def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int, discoveryType: int = 2):
self.logger.info("Commissioning device with setup payload {}".format(setupPayload))
if not self.devCtrl.CommissionWithCode(setupPayload, nodeid):
if not self.devCtrl.CommissionWithCode(setupPayload, nodeid, chip.discovery.DiscoveryType(discoveryType)):
self.logger.info(
"Failed to finish commissioning device {}".format(setupPayload))
return False
Expand Down
13 changes: 12 additions & 1 deletion src/controller/python/test/test_scripts/commissioning_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# Network id, for the thread network, current a const value, will be changed to XPANID of the thread network.
TEST_THREAD_NETWORK_ID = "fedcba9876543210"
TEST_DISCRIMINATOR = 3840
TEST_DISCOVERY_TYPE = 2

ENDPOINT_ID = 0
LIGHTING_ENDPOINT_ID = 1
Expand Down Expand Up @@ -104,6 +105,15 @@ def main():
help="Path that contains valid and trusted PAA Root Certificates.",
metavar="<paa-trust-store-path>"
)
optParser.add_option(
"--discovery-type",
action="store",
dest="discoveryType",
default=TEST_DISCOVERY_TYPE,
type=int,
help="Discovery type of commissioning. (0: networkOnly 1: networkOnlyWithoutPASEAutoRetry 2: All<Ble & Network>)",
metavar="<discovery-type>"
)

(options, remainingArgs) = optParser.parse_args(sys.argv[1:])

Expand All @@ -129,7 +139,8 @@ def main():
elif options.setupPayload:
logger.info("Testing commissioning (w/ Setup Payload)")
FailIfNot(test.TestCommissioningWithSetupPayload(setupPayload=options.setupPayload,
nodeid=options.nodeid),
nodeid=options.nodeid,
discoveryType=options.discoveryType),
"Failed to finish commissioning")
else:
TestFail("Must provide device address or setup payload to commissioning the device")
Expand Down
58 changes: 56 additions & 2 deletions src/test_driver/linux-cirque/CommissioningTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
TEST_EXTPANID = "fedcba9876543210"
TEST_DISCRIMINATOR = 3840
TEST_DISCRIMINATOR2 = 3584
TEST_DISCRIMINATOR3 = 1203
TEST_DISCRIMINATOR4 = 2145
TEST_DISCOVERY_TYPE = [0, 1, 2]
MATTER_DEVELOPMENT_PAA_ROOT_CERTS = "credentials/development/paa-root-certs"

DEVICE_CONFIG = {
Expand Down Expand Up @@ -67,6 +70,24 @@
'docker_network': 'Ipv6',
'traffic_control': {'latencyMs': 100},
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
},
'device3': {
'type': 'CHIPEndDevice',
'base_image': '@default',
'capability': ['Thread', 'TrafficControl', 'Mount'],
'rcp_mode': True,
'docker_network': 'Ipv6',
'traffic_control': {'latencyMs': 100},
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
},
'device4': {
'type': 'CHIPEndDevice',
'base_image': '@default',
'capability': ['Thread', 'TrafficControl', 'Mount'],
'rcp_mode': True,
'docker_network': 'Ipv6',
'traffic_control': {'latencyMs': 100},
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
}
}

Expand Down Expand Up @@ -95,6 +116,10 @@ def run_controller_test(self):
servers[0]['nodeid'] = 1
servers[1]['discriminator'] = TEST_DISCRIMINATOR2
servers[1]['nodeid'] = 2
servers[2]['discriminator'] = TEST_DISCRIMINATOR3
servers[2]['nodeid'] = 3
servers[3]['discriminator'] = TEST_DISCRIMINATOR4
servers[3]['nodeid'] = 4

for server in servers:
self.execute_device_cmd(
Expand Down Expand Up @@ -128,13 +153,42 @@ def run_controller_test(self):
"Test failed: non-zero return code")

command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {}").format(
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
os.path.join(
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
servers[1]['discriminator'],
"33331712336",
servers[1]['nodeid'])
servers[1]['nodeid'],
TEST_DISCOVERY_TYPE[2])
ret = self.execute_device_cmd(req_device_id, command)

self.assertEqual(ret['return_code'], '0',
"Test failed: non-zero return code")

command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
os.path.join(
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
servers[2]['discriminator'],
"10054912339",
servers[2]['nodeid'],
TEST_DISCOVERY_TYPE[0])
ret = self.execute_device_cmd(req_device_id, command)

self.assertEqual(ret['return_code'], '0',
"Test failed: non-zero return code")

command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
os.path.join(
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
servers[3]['discriminator'],
"20054912334",
servers[3]['nodeid'],
TEST_DISCOVERY_TYPE[1])
ret = self.execute_device_cmd(req_device_id, command)

self.assertEqual(ret['return_code'], '0',
Expand Down

0 comments on commit 1029404

Please sign in to comment.