Skip to content

Commit

Permalink
[Android] Add Discovered Device information (project-chip#32472)
Browse files Browse the repository at this point in the history
* Add Android Discovered Device infor

* Restyled by whitespace

* Restyled by google-java-format

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
joonhaengHeo and restyled-commits authored Mar 8, 2024
1 parent 0aa2442 commit 3dc2320
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ android_library("java") {
"src/chip/devicecontroller/ChipCommandType.java",
"src/chip/devicecontroller/ChipDeviceController.java",
"src/chip/devicecontroller/ChipDeviceControllerException.java",
"src/chip/devicecontroller/CommissioningWindowStatus.java",
"src/chip/devicecontroller/ConnectionFailureException.java",
"src/chip/devicecontroller/ControllerParams.java",
"src/chip/devicecontroller/DeviceAttestationDelegate.java",
Expand All @@ -469,6 +470,7 @@ android_library("java") {
"src/chip/devicecontroller/OTAProviderDelegate.java",
"src/chip/devicecontroller/OpenCommissioningCallback.java",
"src/chip/devicecontroller/OperationalKeyConfig.java",
"src/chip/devicecontroller/PairingHintBitmap.java",
"src/chip/devicecontroller/PaseVerifierParams.java",
"src/chip/devicecontroller/ReportCallback.java",
"src/chip/devicecontroller/ReportCallbackJni.java",
Expand Down
36 changes: 33 additions & 3 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,19 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl
jclass discoveredDeviceCls = env->FindClass("chip/devicecontroller/DiscoveredDevice");
jmethodID constructor = env->GetMethodID(discoveredDeviceCls, "<init>", "()V");

jfieldID discrminatorID = env->GetFieldID(discoveredDeviceCls, "discriminator", "J");
jfieldID ipAddressID = env->GetFieldID(discoveredDeviceCls, "ipAddress", "Ljava/lang/String;");
jfieldID portID = env->GetFieldID(discoveredDeviceCls, "port", "I");
jfieldID discrminatorID = env->GetFieldID(discoveredDeviceCls, "discriminator", "J");
jfieldID ipAddressID = env->GetFieldID(discoveredDeviceCls, "ipAddress", "Ljava/lang/String;");
jfieldID portID = env->GetFieldID(discoveredDeviceCls, "port", "I");
jfieldID deviceTypeID = env->GetFieldID(discoveredDeviceCls, "deviceType", "J");
jfieldID vendorIdID = env->GetFieldID(discoveredDeviceCls, "vendorId", "I");
jfieldID productIdID = env->GetFieldID(discoveredDeviceCls, "productId", "I");
jfieldID rotatingIdID = env->GetFieldID(discoveredDeviceCls, "rotatingId", "[B");
jfieldID instanceNameID = env->GetFieldID(discoveredDeviceCls, "instanceName", "Ljava/lang/String;");
jfieldID deviceNameID = env->GetFieldID(discoveredDeviceCls, "deviceName", "Ljava/lang/String;");
jfieldID pairingInstructionID = env->GetFieldID(discoveredDeviceCls, "pairingInstruction", "Ljava/lang/String;");

jmethodID setCommissioningModeID = env->GetMethodID(discoveredDeviceCls, "setCommissioningMode", "(I)V");
jmethodID setPairingHintID = env->GetMethodID(discoveredDeviceCls, "setPairingHint", "(I)V");

jobject discoveredObj = env->NewObject(discoveredDeviceCls, constructor);

Expand All @@ -1911,6 +1921,26 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl

env->SetObjectField(discoveredObj, ipAddressID, jniipAdress);
env->SetIntField(discoveredObj, portID, static_cast<jint>(data->resolutionData.port));
env->SetLongField(discoveredObj, deviceTypeID, static_cast<jlong>(data->commissionData.deviceType));
env->SetIntField(discoveredObj, vendorIdID, static_cast<jint>(data->commissionData.vendorId));
env->SetIntField(discoveredObj, productIdID, static_cast<jint>(data->commissionData.productId));

jbyteArray jRotatingId;
CHIP_ERROR err = JniReferences::GetInstance().N2J_ByteArray(
env, data->commissionData.rotatingId, static_cast<jsize>(data->commissionData.rotatingIdLen), jRotatingId);

if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "jRotatingId N2J_ByteArray error : %" CHIP_ERROR_FORMAT, err.Format());
return nullptr;
}
env->SetObjectField(discoveredObj, rotatingIdID, static_cast<jobject>(jRotatingId));
env->SetObjectField(discoveredObj, instanceNameID, env->NewStringUTF(data->commissionData.instanceName));
env->SetObjectField(discoveredObj, deviceNameID, env->NewStringUTF(data->commissionData.deviceName));
env->SetObjectField(discoveredObj, pairingInstructionID, env->NewStringUTF(data->commissionData.pairingInstruction));

env->CallVoidMethod(discoveredObj, setCommissioningModeID, static_cast<jint>(data->commissionData.commissioningMode));
env->CallVoidMethod(discoveredObj, setPairingHintID, static_cast<jint>(data->commissionData.pairingHint));

return discoveredObj;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

public enum CommissioningWindowStatus {
WindowNotOpen(0),
EnhancedWindowOpen(1),
BasicWindowOpen(2);

private final int value;

CommissioningWindowStatus(int value) {
this.value = value;
}

public static CommissioningWindowStatus value(int value) {
for (CommissioningWindowStatus status : CommissioningWindowStatus.values()) {
if (status.value == value) {
return status;
}
}
throw new IllegalArgumentException("Invalid value: " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,66 @@
*/
package chip.devicecontroller;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class DiscoveredDevice {
public long discriminator;
public String ipAddress;
public int port;
public long deviceType;
public int vendorId;
public int productId;
public Set<PairingHintBitmap> pairingHint;
public CommissioningWindowStatus commissioningMode;
public byte[] rotatingId;
public String instanceName;
public String deviceName;
public String pairingInstruction;

// For use in JNI.
private void setCommissioningMode(int value) {
this.commissioningMode = CommissioningWindowStatus.value(value);
}

private void setPairingHint(int value) {
this.pairingHint = new HashSet<>();
for (PairingHintBitmap mode : PairingHintBitmap.values()) {
int bitmask = 1 << mode.getBitIndex();
if ((value & bitmask) != 0) {
pairingHint.add(mode);
}
}
}

@Override
public String toString() {
return "DiscoveredDevice : {"
+ "\n\tdiscriminator : "
+ discriminator
+ "\n\tipAddress : "
+ ipAddress
+ "\n\tport : "
+ port
+ "\n\tdeviceType : "
+ deviceType
+ "\n\tvendorId : "
+ vendorId
+ "\n\tproductId : "
+ productId
+ "\n\tpairingHint : "
+ pairingHint
+ "\n\tcommissioningMode : "
+ commissioningMode
+ "\n\trotatingId : "
+ (rotatingId != null ? Arrays.toString(rotatingId) : "null")
+ "\n\tinstanceName : "
+ instanceName
+ "\n\tdeviceName : "
+ deviceName
+ "\n\tpairingInstruction : "
+ pairingInstruction
+ "\n}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;

public enum PairingHintBitmap {
PowerCycle(0, false),
DeviceManufacturerURL(1, false),
Administrator(2, false),
SettingsMenuOnTheNode(3, false),
CustomInstruction(4, true),
DeviceManual(5, false),
PressResetButton(6, false),
PressResetButtonWithApplicationOfPower(7, false),
PressResetButtonForNseconds(8, true),
PressResetButtonUntilLightBlinks(9, true),
PressResetButtonForNsecondsWithApplicationOfPower(10, true),
PressResetButtonUntilLightBlinksWithApplicationOfPower(11, true),
PressResetButtonNTimes(12, true),
PressSetupButton(13, false),
PressSetupButtonWithApplicationOfPower(14, false),
PressSetupButtonForNseconds(15, true),
PressSetupButtonUntilLightBlinks(16, true),
PressSetupButtonForNsecondsWithApplicationOfPower(17, true),
PressSetupButtonUntilLightBlinksWithApplicationOfPower(18, true),
PressSetupButtonNtimes(19, true);

private final int bitIndex;
private final boolean isRequirePairingInstruction;

PairingHintBitmap(int bitIndex, boolean isRequirePairingInstruction) {
this.bitIndex = bitIndex;
this.isRequirePairingInstruction = isRequirePairingInstruction;
}

public int getBitIndex() {
return bitIndex;
}

public boolean getRequirePairingInstruction() {
return isRequirePairingInstruction;
}
}

0 comments on commit 3dc2320

Please sign in to comment.