Skip to content

Commit

Permalink
Update commission method using Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Jun 19, 2024
1 parent 32b98d9 commit 978c85e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ android_library("java") {
"src/chip/devicecontroller/ChipCommandType.java",
"src/chip/devicecontroller/ChipDeviceController.java",
"src/chip/devicecontroller/CommissioningWindowStatus.java",
"src/chip/devicecontroller/CommissionParameters.java",
"src/chip/devicecontroller/ConnectionFailureException.java",
"src/chip/devicecontroller/ControllerParams.java",
"src/chip/devicecontroller/DeviceAttestation.java",
Expand Down
118 changes: 105 additions & 13 deletions src/controller/java/src/chip/devicecontroller/ChipDeviceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Optional;
import java.util.TimeZone;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** Controller to interact with the CHIP device. */
Expand Down Expand Up @@ -156,6 +157,8 @@ public void setICDCheckInDelegate(ICDCheckInDelegate delegate) {
setICDCheckInDelegate(deviceControllerPtr, new ICDCheckInDelegateWrapper(delegate));
}

/* This method was deprecated. Please use {@link ChipDeviceController.pairDevice(BluetoothGatt, int, long, long, CommissionParameters)}. */
@Deprecated
public void pairDevice(
BluetoothGatt bleServer,
int connId,
Expand All @@ -165,6 +168,8 @@ public void pairDevice(
pairDevice(bleServer, connId, deviceId, setupPincode, null, networkCredentials, null);
}

/* This method was deprecated. Please use {@link ChipDeviceController.pairDevice(BluetoothGatt, int, long, long, CommissionParameters)}. */
@Deprecated
public void pairDevice(
BluetoothGatt bleServer,
int connId,
Expand All @@ -176,6 +181,8 @@ public void pairDevice(
bleServer, connId, deviceId, setupPincode, null, networkCredentials, registrationInfo);
}

/* This method was deprecated. Please use {@link ChipDeviceController.pairDevice(BluetoothGatt, int, long, long, CommissionParameters)}. */
@Deprecated
public void pairDevice(
BluetoothGatt bleServer,
int connId,
Expand All @@ -200,7 +207,9 @@ public void pairDevice(
* {@link ICDRegistrationInfo}. If this value is null when commissioning an ICD device, {@link
* CompletionListener.onICDRegistrationInfoRequired} is called to request the
* ICDRegistrationInfo value.
* This method was deprecated. Please use {@link ChipDeviceController.pairDevice(BluetoothGatt, int, long, long, CommissionParameters)}.
*/
@Deprecated
public void pairDevice(
BluetoothGatt bleServer,
int connId,
Expand All @@ -209,6 +218,25 @@ public void pairDevice(
@Nullable byte[] csrNonce,
NetworkCredentials networkCredentials,
@Nullable ICDRegistrationInfo icdRegistrationInfo) {
CommissionParameters params = new CommissionParameters.Builder().setCsrNonce(csrNonce).setNetworkCredentials(networkCredentials).setICDRegistrationInfo(icdRegistrationInfo).build();
pairDevice(bleServer, connId, deviceId, setupPincode, params);
}

/**
* Pair a device connected through BLE.
*
* @param bleServer the BluetoothGatt representing the BLE connection to the device
* @param connId the BluetoothGatt Id representing the BLE connection to the device
* @param deviceId the node ID to assign to the device
* @param setupPincode the pincode for the device
* @param params Parameters representing commissioning arguments. see detailed in {@link CommissionParameters}
*/
public void pairDevice(
BluetoothGatt bleServer,
int connId,
long deviceId,
long setupPincode,
@Nonnull CommissionParameters params) {
if (connectionId == 0) {
connectionId = connId;

Expand All @@ -225,15 +253,16 @@ public void pairDevice(
deviceId,
connectionId,
setupPincode,
csrNonce,
networkCredentials,
icdRegistrationInfo);
params.getCsrNonce(),
params.getNetworkCredentials(),
params.getICDRegistrationInfo());
} else {
Log.e(TAG, "Bluetooth connection already in use.");
completionListener.onError(new Exception("Bluetooth connection already in use."));
}
}

/* This method was deprecated. Please use {@link ChipDeviceController.pairDeviceWithAddress(long, String, int, int, long, CommissionParameters)}. */
public void pairDeviceWithAddress(
long deviceId,
String address,
Expand All @@ -259,7 +288,9 @@ public void pairDeviceWithAddress(
* {@link ICDRegistrationInfo}. If this value is null when commissioning an ICD device, {@link
* CompletionListener.onICDRegistrationInfoRequired} is called to request the
* ICDRegistrationInfo value.
* This method was deprecated. Please use {@link ChipDeviceController.pairDeviceWithAddress(long, String, int, int, long, CommissionParameters)}.
*/
@Deprecated
public void pairDeviceWithAddress(
long deviceId,
String address,
Expand All @@ -279,6 +310,41 @@ public void pairDeviceWithAddress(
icdRegistrationInfo);
}

/**
* Pair a device connected using IP Address.
*
* @param deviceId the node ID to assign to the device
* @param address IP Address of the connecting device
* @param port the port of the connecting device
* @param discriminator the discriminator for connecting device
* @param pinCode the pincode for connecting device
* @param params Parameters representing commissioning arguments. see detailed in {@link CommissionParameters}
*/
public void pairDeviceWithAddress(
long deviceId,
String address,
int port,
int discriminator,
long pinCode,
@Nonnull CommissionParameters params) {
if (params.getNetworkCredentials() != null) {
Log.e(TAG, "Invalid parameter : NetworkCredentials");
completionListener.onError(new Exception("Invalid parameter : NetworkCredentials"));
return;
}
pairDeviceWithAddress(
deviceControllerPtr,
deviceId,
address,
port,
discriminator,
pinCode,
params.getCsrNonce(),
params.getICDRegistrationInfo());
}

/* This method was deprecated. Please use {@link ChipDeviceController.pairDeviceWithCode(long, String, boolean, boolean, CommissionParameters)}. */
@Deprecated
public void pairDeviceWithCode(
long deviceId,
String setupCode,
Expand Down Expand Up @@ -312,7 +378,9 @@ public void pairDeviceWithCode(
* {@link ICDRegistrationInfo}. If this value is null when commissioning an ICD device, {@link
* CompletionListener.onICDRegistrationInfoRequired} is called to request the
* ICDRegistrationInfo value.
* <p>This method was deprecated. Please use {@link ChipDeviceController.pairDeviceWithCode(long, String, boolean, boolean, CommissionParameters)}.
*/
@Deprecated
public void pairDeviceWithCode(
long deviceId,
String setupCode,
Expand All @@ -332,6 +400,33 @@ public void pairDeviceWithCode(
icdRegistrationInfo);
}

/**
* Pair a device connected using the scanned QR code or manual entry code.
*
* @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 params Parameters representing commissioning arguments. see detailed in {@link CommissionParameters}
*/
public void pairDeviceWithCode(
long deviceId,
String setupCode,
boolean discoverOnce,
boolean useOnlyOnNetworkDiscovery,
@Nonnull CommissionParameters params) {
pairDeviceWithCode(
deviceControllerPtr,
deviceId,
setupCode,
discoverOnce,
useOnlyOnNetworkDiscovery,
params.getCsrNonce(),
params.getNetworkCredentials(),
params.getICDRegistrationInfo());
}

public void establishPaseConnection(long deviceId, int connId, long setupPincode) {
if (connectionId == 0) {
connectionId = connId;
Expand Down Expand Up @@ -371,7 +466,9 @@ public void establishPaseConnection(long deviceId, String address, int port, lon
*
* @param deviceId the ID of the node to be commissioned
* @param networkCredentials the credentials (Wi-Fi or Thread) to be provisioned
* <p>This method was deprecated. Please use {@link ChipDeviceController.commissionDevice(long, CommissionParameters)}.
*/
@Deprecated
public void commissionDevice(long deviceId, @Nullable NetworkCredentials networkCredentials) {
commissionDevice(deviceControllerPtr, deviceId, /* csrNonce= */ null, networkCredentials, null);
}
Expand All @@ -384,7 +481,9 @@ public void commissionDevice(long deviceId, @Nullable NetworkCredentials network
* @param deviceId the ID of the node to be commissioned
* @param csrNonce a nonce to be used for the CSR request
* @param networkCredentials the credentials (Wi-Fi or Thread) to be provisioned
* <p>This method was deprecated. Please use {@link ChipDeviceController.commissionDevice(long, CommissionParameters)}.
*/
@Deprecated
public void commissionDevice(
long deviceId, @Nullable byte[] csrNonce, @Nullable NetworkCredentials networkCredentials) {
commissionDevice(deviceControllerPtr, deviceId, csrNonce, networkCredentials, null);
Expand All @@ -396,20 +495,13 @@ public void commissionDevice(
* #establishPaseConnection(long, int, long)}.
*
* @param deviceId the ID of the node to be commissioned
* @param csrNonce a nonce to be used for the CSR request
* @param networkCredentials the credentials (Wi-Fi or Thread) to be provisioned
* @param icdRegistrationInfo the informations for ICD registration. For detailed information
* {@link ICDRegistrationInfo}. If this value is null when commissioning an ICD device, {@link
* CompletionListener.onICDRegistrationInfoRequired} is called to request the
* ICDRegistrationInfo value.
* @param params Parameters representing commissioning arguments. see detailed in {@link CommissionParameters}
*/
public void commissionDevice(
long deviceId,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials,
@Nullable ICDRegistrationInfo icdRegistrationInfo) {
@Nonnull CommissionParameters params) {
commissionDevice(
deviceControllerPtr, deviceId, csrNonce, networkCredentials, icdRegistrationInfo);
deviceControllerPtr, deviceId, params.getCsrNonce(), params.getNetworkCredentials(), params.getICDRegistrationInfo());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package chip.devicecontroller;

import javax.annotation.Nullable;

/** Parameters representing commissioning arguments for {@link ChipDeviceController}. */
public final class CommissionParameters {
@Nullable private final byte[] csrNonce;
@Nullable private final NetworkCredentials networkCredentials;
@Nullable private final ICDRegistrationInfo icdRegistrationInfo;

private CommissionParameters(Builder builder) {
csrNonce = builder.csrNonce;
networkCredentials = builder.networkCredentials;
icdRegistrationInfo = builder.icdRegistrationInfo;
}
/* a nonce to be used for the CSR request */
public byte[] getCsrNonce() {
return csrNonce;
}

/* the credentials (Wi-Fi or Thread) to be provisioned */
public NetworkCredentials getNetworkCredentials() {
return networkCredentials;
}
/* the informations for ICD registration. For detailed information {@link ICDRegistrationInfo}. If this value is null when commissioning an ICD device, {@link CompletionListener.onICDRegistrationInfoRequired} is called to request the ICDRegistrationInfo value. */
public ICDRegistrationInfo getICDRegistrationInfo() {
return icdRegistrationInfo;
}

public static class Builder {
@Nullable private byte[] csrNonce = null;
@Nullable private NetworkCredentials networkCredentials = null;
@Nullable private ICDRegistrationInfo icdRegistrationInfo = null;

public Builder setCsrNonce(byte[] csrNonce) {
this.csrNonce = csrNonce;
return this;
}

public Builder setNetworkCredentials(NetworkCredentials networkCredentials) {
this.networkCredentials = networkCredentials;
return this;
}

public Builder setICDRegistrationInfo(ICDRegistrationInfo icdRegistrationInfo) {
this.icdRegistrationInfo = icdRegistrationInfo;
return this;
}

public CommissionParameters build() {
return new CommissionParameters(this);
}
}
}

0 comments on commit 978c85e

Please sign in to comment.