Skip to content

Commit

Permalink
[Java] Add API pairDeviceWithCode to test pair with setup code (#26494)
Browse files Browse the repository at this point in the history
* [Java] Add pairDeviceWithCode API

* Address review comments
  • Loading branch information
yufengwangca authored and pull[bot] committed Feb 15, 2024
1 parent 9a34b9d commit 2169445
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,30 @@ jobs:
--tool-args "address-paseonly --nodeid 1 --setup-pin-code 20202021 --address ::1 --port 5540 -t 1000" \
--factoryreset \
'
- name: Run Pairing SetupQRCode Test
timeout-minutes: 10
run: |
scripts/run_in_build_env.sh \
'./scripts/tests/run_java_test.py \
--app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
--app-args "--discriminator 3840 --interface-id -1" \
--tool-path out/linux-x64-java-matter-controller \
--tool-cluster "pairing" \
--tool-args "code --nodeid 1 --setup-payload MT:-24J0AFN00KA0648G00 --discover-once 1 --use-only-onnetwork-discovery 0 -t 1000" \
--factoryreset \
'
- name: Run Pairing ManualCode Test
timeout-minutes: 10
run: |
scripts/run_in_build_env.sh \
'./scripts/tests/run_java_test.py \
--app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
--app-args "--discriminator 3840 --interface-id -1" \
--tool-path out/linux-x64-java-matter-controller \
--tool-cluster "pairing" \
--tool-args "code --nodeid 1 --setup-payload 34970112332 --discover-once 1 --use-only-onnetwork-discovery 0 -t 1000" \
--factoryreset \
'
- name: Uploading core files
uses: actions/upload-artifact@v3
if: ${{ failure() && !env.ACT }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PairCodeCommand(controller: ChipDeviceController, credsIssue: CredentialsI
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
getDiscoverOnce(),
getUseOnlyOnNetworkDiscovery(),
null,
getWifiNetworkCredentials(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PairCodeThreadCommand(controller: ChipDeviceController, credsIssue: Creden
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
getDiscoverOnce(),
getUseOnlyOnNetworkDiscovery(),
null,
getThreadNetworkCredentials(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PairCodeWifiCommand(controller: ChipDeviceController, credsIssue: Credenti
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
getDiscoverOnce(),
getUseOnlyOnNetworkDiscovery(),
null,
getWifiNetworkCredentials(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ abstract class PairingCommand(
PairingModeType.NONE -> {}
PairingModeType.CODE, PairingModeType.CODE_PASE_ONLY -> {
addArgument("payload", onboardingPayload, null, false)
addArgument("discover-once", discoverOnce, null, false)
addArgument("use-only-onnetwork-discovery", useOnlyOnNetworkDiscovery, null, false)
addArgument("discover-once", discoverOnce, null, true)
addArgument("use-only-onnetwork-discovery", useOnlyOnNetworkDiscovery, null, true)
}

PairingModeType.ADDRESS_PASE_ONLY -> {
Expand Down Expand Up @@ -244,6 +244,14 @@ abstract class PairingCommand(
return chunked(2).map { byteStr -> byteStr.toUByte(16).toByte() }.toByteArray()
}

fun getDiscoverOnce(): Boolean {
return discoverOnce.get()
}

fun getUseOnlyOnNetworkDiscovery(): Boolean {
return useOnlyOnNetworkDiscovery.get()
}

companion object {
private val logger = Logger.getLogger(PairingCommand::class.java.name)
}
Expand Down
22 changes: 21 additions & 1 deletion scripts/tests/java/commissioning_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def __init__(self, thread_list: typing.List[threading.Thread], queue: queue.Queu
"session establishment (PASE) with the Commissionee"))
parser.add_argument('-n', '--nodeid', help="The Node ID issued to the device", default='1')
parser.add_argument('-d', '--discriminator', help="Discriminator of the device", default='3840')
parser.add_argument('-u', '--paa-trust-store-path', dest='paa_trust_store_path',
parser.add_argument('-o', '--discover-once', help="Enable to disable PASE auto retry mechanism", default='false')
parser.add_argument('-u', '--use-only-onnetwork-discovery',
help="Enable when the commissionable device is available on the network", default='false')
parser.add_argument('-r', '--paa-trust-store-path', dest='paa_trust_store_path',
help="Path that contains valid and trusted PAA Root Certificates")

args = parser.parse_args(args.split())
Expand All @@ -59,6 +62,8 @@ def __init__(self, thread_list: typing.List[threading.Thread], queue: queue.Queu
self.setup_payload = args.setup_payload
self.setup_pin_code = args.setup_pin_code
self.discriminator = args.discriminator
self.discover_once = args.discover_once
self.use_only_onnetwork_discovery = args.use_only_onnetwork_discovery
self.timeout = args.timeout

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -87,6 +92,15 @@ def TestCmdAddressPaseOnly(self, nodeid, setuppin, address, port, timeout):
DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
return java_process.wait()

def TestCmdCode(self, nodeid, setup_payload, discover_once, use_only_onnetwork_discovery, timeout):
java_command = self.command + ['pairing', 'code', nodeid, setup_payload, timeout,
'--discover-once', discover_once, '--use-only-onnetwork-discovery', use_only_onnetwork_discovery]
logging.info(f"Execute: {java_command}")
java_process = subprocess.Popen(
java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
return java_process.wait()

def RunTest(self):
if self.command_name == 'onnetwork-long':
logging.info("Testing pairing onnetwork-long")
Expand All @@ -103,5 +117,11 @@ def RunTest(self):
code = self.TestCmdAddressPaseOnly(self.nodeid, self.setup_pin_code, self.address, self.port, self.timeout)
if code != 0:
raise Exception(f"Testing pairing address-paseonly failed with error {code}")
elif self.command_name == 'code':
logging.info("Testing pairing setup-code")
code = self.TestCmdCode(self.nodeid, self.setup_payload, self.discover_once,
self.use_only_onnetwork_discovery, self.timeout)
if code != 0:
raise Exception(f"Testing pairing code failed with error {code}")
else:
raise Exception(f"Unsupported command {self.command_name}")
1 change: 1 addition & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * se
DiscoveryType discoveryType)
{
MATTER_TRACE_EVENT_SCOPE("PairDevice", "DeviceCommissioner");

if (mDefaultCommissioner == nullptr)
{
ChipLogError(Controller, "No default commissioner is specified");
Expand Down
1 change: 1 addition & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
* @param[in] rendezvousParams The Rendezvous connection parameters
*/
CHIP_ERROR PairDevice(NodeId remoteDeviceId, RendezvousParameters & rendezvousParams);

/**
* @overload
* @param[in] remoteDeviceId The remote device Id.
Expand Down
17 changes: 15 additions & 2 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,8 @@ JNI_METHOD(void, pairDeviceWithAddress)
}

JNI_METHOD(void, pairDeviceWithCode)
(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jstring setUpCode, jbyteArray csrNonce, jobject networkCredentials)
(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jstring setUpCode, jboolean discoverOnce,
jboolean useOnlyOnNetworkDiscovery, jbyteArray csrNonce, jobject networkCredentials)
{
chip::DeviceLayer::StackLock lock;
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -665,6 +666,18 @@ JNI_METHOD(void, pairDeviceWithCode)
JniUtfString setUpCodeJniString(env, setUpCode);

CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();

auto discoveryType = DiscoveryType::kAll;
if (useOnlyOnNetworkDiscovery)
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnly;
}

if (discoverOnce)
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnlyWithoutPASEAutoRetry;
}

if (csrNonce != nullptr)
{
JniByteArray jniCsrNonce(env, csrNonce);
Expand All @@ -680,7 +693,7 @@ JNI_METHOD(void, pairDeviceWithCode)
{
commissioningParams.SetDeviceAttestationDelegate(wrapper->GetDeviceAttestationDelegateBridge());
}
err = wrapper->Controller()->PairDevice(deviceId, setUpCodeJniString.c_str(), commissioningParams, DiscoveryType::kAll);
err = wrapper->Controller()->PairDevice(deviceId, setUpCodeJniString.c_str(), commissioningParams, discoveryType);

if (err != CHIP_NO_ERROR)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,28 @@ public void pairDeviceWithAddress(
*
* @param deviceId the node ID to assign to the device
* @param setupCode the scanned QR code or manual entry code
* @param discoverOnce the flag to enable/disable PASE auto retry mechanism
* @param useOnlyOnNetworkDiscovery the flag to indicate the commissionable device is available on
* the network
* @param csrNonce the 32-byte CSR nonce to use, or null if we want to use an internally randomly
* generated CSR nonce.
* @param networkCredentials the credentials (Wi-Fi or Thread) to be provisioned
*/
public void pairDeviceWithCode(
long deviceId,
String setupCode,
boolean discoverOnce,
boolean useOnlyOnNetworkDiscovery,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials) {
pairDeviceWithCode(deviceControllerPtr, deviceId, setupCode, csrNonce, networkCredentials);
pairDeviceWithCode(
deviceControllerPtr,
deviceId,
setupCode,
discoverOnce,
useOnlyOnNetworkDiscovery,
csrNonce,
networkCredentials);
}

public void establishPaseConnection(long deviceId, int connId, long setupPincode) {
Expand Down Expand Up @@ -1017,6 +1029,8 @@ private native void pairDeviceWithCode(
long deviceControllerPtr,
long deviceId,
String setupCode,
boolean discoverOnce,
boolean useOnlyOnNetworkDiscovery,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials);

Expand Down

0 comments on commit 2169445

Please sign in to comment.