Skip to content

Commit b1087be

Browse files
[Python] CommissonWithCode support discoveryType (project-chip#31904)
* [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
1 parent 9ad8ec8 commit b1087be

File tree

6 files changed

+84
-14
lines changed

6 files changed

+84
-14
lines changed

src/controller/python/ChipDeviceController-ScriptBinding.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissio
137137
PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr,
138138
uint32_t setupPINCode, chip::NodeId nodeid);
139139
PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload,
140-
chip::NodeId nodeid, bool networkOnly);
140+
chip::NodeId nodeid, uint8_t discoveryType);
141141
PyChipError pychip_DeviceController_UnpairDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId remoteDeviceId,
142142
DeviceUnpairingCompleteFunct callback);
143143
PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size);
@@ -401,13 +401,11 @@ PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommission
401401
}
402402

403403
PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload,
404-
chip::NodeId nodeid, bool networkOnly)
404+
chip::NodeId nodeid, uint8_t discoveryType)
405405
{
406-
chip::Controller::DiscoveryType discoveryType = chip::Controller::DiscoveryType::kAll;
407406
sPairingDelegate.SetExpectingPairingComplete(true);
408-
if (networkOnly)
409-
discoveryType = chip::Controller::DiscoveryType::kDiscoveryNetworkOnly;
410-
return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters, discoveryType));
407+
return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters,
408+
static_cast<chip::Controller::DiscoveryType>(discoveryType)));
411409
}
412410

413411
namespace {

src/controller/python/chip/ChipDeviceCtrl.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def attestationChallenge(self) -> bytes:
238238

239239

240240
DiscoveryFilterType = discovery.FilterType
241+
DiscoveryType = discovery.DiscoveryType
241242

242243

243244
class ChipDeviceControllerBase():
@@ -1624,7 +1625,7 @@ def _InitLib(self):
16241625
self._dmLib.pychip_DeviceController_ConnectIP.restype = PyChipError
16251626

16261627
self._dmLib.pychip_DeviceController_ConnectWithCode.argtypes = [
1627-
c_void_p, c_char_p, c_uint64, c_bool]
1628+
c_void_p, c_char_p, c_uint64, c_uint8]
16281629
self._dmLib.pychip_DeviceController_ConnectWithCode.restype = PyChipError
16291630

16301631
self._dmLib.pychip_DeviceController_UnpairDevice.argtypes = [
@@ -1937,7 +1938,7 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
19371938
return PyChipError(CHIP_ERROR_TIMEOUT)
19381939
return self._ChipStack.commissioningEventRes
19391940

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

19541955
self._ChipStack.CallAsync(
19551956
lambda: self._dmLib.pychip_DeviceController_ConnectWithCode(
1956-
self.devCtrl, setupPayload, nodeid, networkOnly)
1957+
self.devCtrl, setupPayload, nodeid, discoveryType.value)
19571958
)
19581959
if not self._ChipStack.commissioningCompleteEvent.isSet():
19591960
# Error 50 is a timeout

src/controller/python/chip/discovery/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
from chip.native import PyChipError
2727

2828

29+
class DiscoveryType(enum.IntEnum):
30+
DISCOVERY_NETWORK_ONLY = 0
31+
DISCOVERY_NETWORK_ONLY_WITHOUT_PASE_AUTO_RETRY = 1
32+
DISCOVERY_ALL = 2
33+
34+
2935
class FilterType(enum.IntEnum):
3036
# These must match chip::Dnssd::DiscoveryFilterType values (barring the naming convention)
3137
NONE = 0

src/controller/python/test/test_scripts/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ def TestCommissioning(self, ip: str, setuppin: int, nodeid: int):
341341
self.logger.info("Commissioning finished.")
342342
return True
343343

344-
def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int):
344+
def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int, discoveryType: int = 2):
345345
self.logger.info("Commissioning device with setup payload {}".format(setupPayload))
346-
if not self.devCtrl.CommissionWithCode(setupPayload, nodeid):
346+
if not self.devCtrl.CommissionWithCode(setupPayload, nodeid, chip.discovery.DiscoveryType(discoveryType)):
347347
self.logger.info(
348348
"Failed to finish commissioning device {}".format(setupPayload))
349349
return False

src/controller/python/test/test_scripts/commissioning_test.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# Network id, for the thread network, current a const value, will be changed to XPANID of the thread network.
4040
TEST_THREAD_NETWORK_ID = "fedcba9876543210"
4141
TEST_DISCRIMINATOR = 3840
42+
TEST_DISCOVERY_TYPE = 2
4243

4344
ENDPOINT_ID = 0
4445
LIGHTING_ENDPOINT_ID = 1
@@ -104,6 +105,15 @@ def main():
104105
help="Path that contains valid and trusted PAA Root Certificates.",
105106
metavar="<paa-trust-store-path>"
106107
)
108+
optParser.add_option(
109+
"--discovery-type",
110+
action="store",
111+
dest="discoveryType",
112+
default=TEST_DISCOVERY_TYPE,
113+
type=int,
114+
help="Discovery type of commissioning. (0: networkOnly 1: networkOnlyWithoutPASEAutoRetry 2: All<Ble & Network>)",
115+
metavar="<discovery-type>"
116+
)
107117

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

@@ -129,7 +139,8 @@ def main():
129139
elif options.setupPayload:
130140
logger.info("Testing commissioning (w/ Setup Payload)")
131141
FailIfNot(test.TestCommissioningWithSetupPayload(setupPayload=options.setupPayload,
132-
nodeid=options.nodeid),
142+
nodeid=options.nodeid,
143+
discoveryType=options.discoveryType),
133144
"Failed to finish commissioning")
134145
else:
135146
TestFail("Must provide device address or setup payload to commissioning the device")

src/test_driver/linux-cirque/CommissioningTest.py

+56-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
TEST_EXTPANID = "fedcba9876543210"
3939
TEST_DISCRIMINATOR = 3840
4040
TEST_DISCRIMINATOR2 = 3584
41+
TEST_DISCRIMINATOR3 = 1203
42+
TEST_DISCRIMINATOR4 = 2145
43+
TEST_DISCOVERY_TYPE = [0, 1, 2]
4144
MATTER_DEVELOPMENT_PAA_ROOT_CERTS = "credentials/development/paa-root-certs"
4245

4346
DEVICE_CONFIG = {
@@ -67,6 +70,24 @@
6770
'docker_network': 'Ipv6',
6871
'traffic_control': {'latencyMs': 100},
6972
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
73+
},
74+
'device3': {
75+
'type': 'CHIPEndDevice',
76+
'base_image': '@default',
77+
'capability': ['Thread', 'TrafficControl', 'Mount'],
78+
'rcp_mode': True,
79+
'docker_network': 'Ipv6',
80+
'traffic_control': {'latencyMs': 100},
81+
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
82+
},
83+
'device4': {
84+
'type': 'CHIPEndDevice',
85+
'base_image': '@default',
86+
'capability': ['Thread', 'TrafficControl', 'Mount'],
87+
'rcp_mode': True,
88+
'docker_network': 'Ipv6',
89+
'traffic_control': {'latencyMs': 100},
90+
"mount_pairs": [[CHIP_REPO, CHIP_REPO]],
7091
}
7192
}
7293

@@ -95,6 +116,10 @@ def run_controller_test(self):
95116
servers[0]['nodeid'] = 1
96117
servers[1]['discriminator'] = TEST_DISCRIMINATOR2
97118
servers[1]['nodeid'] = 2
119+
servers[2]['discriminator'] = TEST_DISCRIMINATOR3
120+
servers[2]['nodeid'] = 3
121+
servers[3]['discriminator'] = TEST_DISCRIMINATOR4
122+
servers[3]['nodeid'] = 4
98123

99124
for server in servers:
100125
self.execute_device_cmd(
@@ -128,13 +153,42 @@ def run_controller_test(self):
128153
"Test failed: non-zero return code")
129154

130155
command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
131-
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {}").format(
156+
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
132157
os.path.join(
133158
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
134159
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
135160
servers[1]['discriminator'],
136161
"33331712336",
137-
servers[1]['nodeid'])
162+
servers[1]['nodeid'],
163+
TEST_DISCOVERY_TYPE[2])
164+
ret = self.execute_device_cmd(req_device_id, command)
165+
166+
self.assertEqual(ret['return_code'], '0',
167+
"Test failed: non-zero return code")
168+
169+
command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
170+
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
171+
os.path.join(
172+
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
173+
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
174+
servers[2]['discriminator'],
175+
"10054912339",
176+
servers[2]['nodeid'],
177+
TEST_DISCOVERY_TYPE[0])
178+
ret = self.execute_device_cmd(req_device_id, command)
179+
180+
self.assertEqual(ret['return_code'], '0',
181+
"Test failed: non-zero return code")
182+
183+
command = ("gdb -return-child-result -q -ex run -ex bt --args python3 "
184+
"{} -t 150 --paa-trust-store-path {} --discriminator {} --setup-payload {} --nodeid {} --discovery-type {}").format(
185+
os.path.join(
186+
CHIP_REPO, "src/controller/python/test/test_scripts/commissioning_test.py"),
187+
os.path.join(CHIP_REPO, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
188+
servers[3]['discriminator'],
189+
"20054912334",
190+
servers[3]['nodeid'],
191+
TEST_DISCOVERY_TYPE[1])
138192
ret = self.execute_device_cmd(req_device_id, command)
139193

140194
self.assertEqual(ret['return_code'], '0',

0 commit comments

Comments
 (0)