diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index ec522e367c60f4..88c773fe6c25f2 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -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", diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java index ae94923f89f2ac..b40e4e14a64201 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java @@ -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. */ @@ -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, @@ -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, @@ -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, @@ -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, @@ -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; @@ -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, @@ -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, @@ -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, @@ -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. + *

This method was deprecated. Please use {@link ChipDeviceController.pairDeviceWithCode(long, String, boolean, boolean, CommissionParameters)}. */ + @Deprecated public void pairDeviceWithCode( long deviceId, String setupCode, @@ -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; @@ -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 + *

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); } @@ -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 + *

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); @@ -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()); } /** diff --git a/src/controller/java/src/chip/devicecontroller/CommissionParameters.java b/src/controller/java/src/chip/devicecontroller/CommissionParameters.java new file mode 100644 index 00000000000000..c391b2d98ef2a2 --- /dev/null +++ b/src/controller/java/src/chip/devicecontroller/CommissionParameters.java @@ -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://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); + } + } +}