Skip to content

Commit

Permalink
Add on-network-long commissioing test in java matter controler example
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Nov 22, 2022
1 parent 669cd47 commit 40e3d2f
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ private static void registerCommandsPairing(
}

public static void main(String[] args) {
ChipDeviceController controller =
new ChipDeviceController(
ControllerParams.newBuilder()
.setUdpListenPort(0)
.setControllerVendorId(0xFFF1)
.build());

CredentialsIssuer credentialsIssuer = new CredentialsIssuer();
CommandManager commandManager = new CommandManager();

Expand All @@ -104,5 +97,6 @@ public static void main(String[] args) {
} catch (Exception e) {
System.out.println("Run command failed with exception: " + e.getMessage());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,21 @@ public void setValue(String value) {
String str = (String) mValue;
isValidArgument = value.equals(str);
break;
case NUMBER_INT16:
AtomicInteger numShort = (AtomicInteger) mValue;
numShort.set(Integer.parseInt(value));
isValidArgument = (numShort.intValue() >= mMin && numShort.intValue() <= mMax);
break;
case NUMBER_INT32:
AtomicInteger num = (AtomicInteger) mValue;
num.set(Integer.parseInt(value));
isValidArgument = (num.intValue() >= mMin && num.intValue() <= mMax);
break;
case NUMBER_INT64:
AtomicLong numLong = (AtomicLong) mValue;
numLong.set(Long.parseLong(value));
isValidArgument = (numLong.intValue() >= mMin && numLong.intValue() <= mMax);
break;
case ADDRESS:
try {
IPAddress ipAddress = (IPAddress) mValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,9 @@ public void initArgument(int argIndex, String argValue) {
mArgs.get(argIndex).setValue(argValue);
}

public Object getArgument(int argIndex) {
return mArgs.get(argIndex);
}

public abstract void run() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public final void run(String[] args) {
}
}

String[] temp = Arrays.copyOfRange(args, 1, args.length);
String[] temp = Arrays.copyOfRange(args, 2, args.length);

try {
command.initArguments(args.length - 1, temp);
command.initArguments(args.length - 2, temp);
command.run();
} catch (IllegalArgumentException e) {
System.out.println("Arguments init failed with exception: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void setAddress(InetAddress address) {

@Override
public String toString() {
return address.toString();
return address.getHostAddress();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class MatterCommand extends Command {
private final AtomicLong mCommissionerNodeId = new AtomicLong();
private final AtomicBoolean mUseMaxSizedCerts = new AtomicBoolean();;
private final AtomicBoolean mOnlyAllowTrustedCdKeys = new AtomicBoolean();;
private String TestResult = null;

public MatterCommand(String commandName, CredentialsIssuer credIssuerCmds) {
this(commandName, credIssuerCmds, null);
Expand All @@ -47,7 +48,8 @@ public MatterCommand(String commandName, CredentialsIssuer credIssuerCmds) {
public MatterCommand(String commandName, CredentialsIssuer credIssuerCmds, String helpText) {
super(commandName, helpText);
this.mCredIssuerCmds = Optional.ofNullable(credIssuerCmds);

//TODO: Add support to enable the below optional arguments
/*
addArgument(
"paa-trust-store-path",
mPaaTrustStorePath,
Expand Down Expand Up @@ -79,12 +81,14 @@ public MatterCommand(String commandName, CredentialsIssuer credIssuerCmds, Strin
mOnlyAllowTrustedCdKeys,
"Only allow trusted CD verifying keys (disallow test keys). If not provided or 0 (\"false\"), untrusted CD "
+ "verifying keys are allowed. If 1 (\"true\"), test keys are disallowed.");
*/
}

/////////// Command Interface /////////
@Override
public void run() throws Exception {
maybeSetUpStack();
// TODO: setup chip storage from Java, currently it is using example one from chip-tool
// maybeSetUpStack();
runCommand();
maybeTearDownStack();
}
Expand All @@ -100,4 +104,42 @@ private void maybeSetUpStack() throws Exception {
private void maybeTearDownStack() {
// ToDo:We need to call DeviceController::Shutdown()
}


public void SetTestResult(String result)
{
TestResult = result;
}

public void ExpectSuccess(long timeout)
{
ExpectResult("Success", timeout);
}

private void ExpectResult(String expectedResult, long timeout)
{
long start = System.currentTimeMillis();
while (TestResult == null)
try
{
if (System.currentTimeMillis() > (start + timeout))
{
throw new RuntimeException("timeout!");
}
Thread.sleep(100);
}
catch (Exception ex) { }

if (!TestResult.equals(expectedResult)) {
if (!expectedResult.equals("Success"))
{
System.out.format("%s command failed:%n Expected: %s%n Got: %s%n", getName(), expectedResult, TestResult);
throw new RuntimeException(getName());
}
else
{
System.out.format("%s command failed: %s%n", getName(), TestResult);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.matter.controller.commands.pairing;

import com.matter.controller.commands.common.CredentialsIssuer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import chip.devicecontroller.ChipDeviceController;
import chip.devicecontroller.ControllerParams;

public final class PairOnNetworkLongCommand extends PairingCommand {
public PairOnNetworkLongCommand(CredentialsIssuer credsIssue) {
Expand All @@ -13,5 +17,25 @@ public PairOnNetworkLongCommand(CredentialsIssuer credsIssue) {
}

@Override
protected void runCommand() {}
protected void runCommand() {
ChipDeviceController controller =
new ChipDeviceController(
ControllerParams.newBuilder()
.setUdpListenPort(0)
.setControllerVendorId(0xFFF1)
.build());

controller.pairDeviceWithAddress(
getNodeId(),
GetRemoteAddr().toString(),
5540,
getDiscriminator(),
getSetupPINCode(),
null
);

controller.setCompletionListener(this);
ExpectSuccess(getTimeoutMillis());
controller.shutdownCommissioning();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import chip.devicecontroller.ChipDeviceController;

public abstract class PairingCommand extends MatterCommand {
public abstract class PairingCommand extends MatterCommand implements ChipDeviceController.CompletionListener{
private PairingModeType mPairingMode = PairingModeType.NONE;
private PairingNetworkType mNetworkType = PairingNetworkType.NONE;
private DiscoveryFilterType mFilterType = DiscoveryFilterType.NONE;
private final IPAddress mRemoteAddr;
private final AtomicLong mNodeId = new AtomicLong();
private final AtomicLong mDiscoveryFilterCode = new AtomicLong();
private final AtomicInteger mTimeout = new AtomicInteger();
private final AtomicLong mTimeoutMillis = new AtomicLong();
private final AtomicBoolean mDiscoverOnce = new AtomicBoolean();
private final AtomicBoolean mUseOnlyOnNetworkDiscovery = new AtomicBoolean();
private final AtomicInteger mRemotePort = new AtomicInteger();
Expand All @@ -45,6 +46,115 @@ public abstract class PairingCommand extends MatterCommand {
private final StringBuffer mPassword = new StringBuffer();
private final StringBuffer mOnboardingPayload = new StringBuffer();
private final StringBuffer mDiscoveryFilterInstanceName = new StringBuffer();

public long getNodeId()
{
return mNodeId.get();
}

public int getSetupPINCode()
{
return mSetupPINCode.get();
}

public int getDiscriminator()
{
return mDiscriminator.get();
}

public long getTimeoutMillis()
{
return mTimeoutMillis.get();
}

@Override
public void onConnectDeviceComplete()
{
System.out.println("onConnectDeviceComplete");
}

@Override
public void onStatusUpdate(int status)
{
System.out.println("onStatusUpdate with status: " + status);
}

@Override
public void onPairingComplete(int errorCode)
{
System.out.println("onPairingComplete with error code: " + errorCode);
if (errorCode != 0) {
SetTestResult("Failure");
throw new RuntimeException("onPairingComplete");
}
}

@Override
public void onPairingDeleted(int errorCode)
{
System.out.println("onPairingDeleted with error code: " + errorCode);
}

@Override
public void onCommissioningComplete(long nodeId, int errorCode)
{
System.out.println("onCommissioningComplete with error code: " + errorCode);
if (errorCode == 0) {
SetTestResult("Success");
}
else
{
SetTestResult("Failure");
throw new RuntimeException("onCommissioningComplete");
}
}

@Override
public void onReadCommissioningInfo(
int vendorId, int productId, int wifiEndpointId, int threadEndpointId)
{
System.out.println("onReadCommissioningInfo");
}

@Override
public void onCommissioningStatusUpdate(long nodeId, String stage, int errorCode)
{
System.out.println("onCommissioningStatusUpdate");
}

@Override
public void onNotifyChipConnectionClosed()
{
System.out.println("onNotifyChipConnectionClosed");
}

@Override
public void onCloseBleComplete()
{
System.out.println("onCloseBleComplete");
}

@Override
public void onError(Throwable error)
{
SetTestResult(error.toString());
System.out.println("onError with error: " + error.toString());
throw new RuntimeException(error.toString());
}

@Override
public void onOpCSRGenerationComplete(byte[] csr)
{
System.out.println("onOpCSRGenerationComplete");
for(int i=0; i< csr.length ; i++) {
System.out.print(csr[i] +" ");
}
}

public IPAddress GetRemoteAddr()
{
return mRemoteAddr;
}

public PairingCommand(
String commandName,
Expand All @@ -66,7 +176,7 @@ public PairingCommand(
this.mFilterType = filterType;

try {
this.mRemoteAddr = new IPAddress(InetAddress.getByName("0.0.0.0"));
this.mRemoteAddr = new IPAddress(InetAddress.getByName("127.0.0.1"));
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -144,6 +254,7 @@ public PairingCommand(
break;
}

addArgument("timeout", (short) 0, Short.MAX_VALUE, mTimeout, null);
addArgument("timeout", (long) 0, Long.MAX_VALUE, mTimeoutMillis, null);

}
}
Loading

0 comments on commit 40e3d2f

Please sign in to comment.