Skip to content

Commit 3dc2320

Browse files
[Android] Add Discovered Device information (project-chip#32472)
* Add Android Discovered Device infor * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 0aa2442 commit 3dc2320

File tree

5 files changed

+189
-3
lines changed

5 files changed

+189
-3
lines changed

src/controller/java/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ android_library("java") {
450450
"src/chip/devicecontroller/ChipCommandType.java",
451451
"src/chip/devicecontroller/ChipDeviceController.java",
452452
"src/chip/devicecontroller/ChipDeviceControllerException.java",
453+
"src/chip/devicecontroller/CommissioningWindowStatus.java",
453454
"src/chip/devicecontroller/ConnectionFailureException.java",
454455
"src/chip/devicecontroller/ControllerParams.java",
455456
"src/chip/devicecontroller/DeviceAttestationDelegate.java",
@@ -469,6 +470,7 @@ android_library("java") {
469470
"src/chip/devicecontroller/OTAProviderDelegate.java",
470471
"src/chip/devicecontroller/OpenCommissioningCallback.java",
471472
"src/chip/devicecontroller/OperationalKeyConfig.java",
473+
"src/chip/devicecontroller/PairingHintBitmap.java",
472474
"src/chip/devicecontroller/PaseVerifierParams.java",
473475
"src/chip/devicecontroller/ReportCallback.java",
474476
"src/chip/devicecontroller/ReportCallbackJni.java",

src/controller/java/CHIPDeviceController-JNI.cpp

+33-3
Original file line numberDiff line numberDiff line change
@@ -1897,9 +1897,19 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl
18971897
jclass discoveredDeviceCls = env->FindClass("chip/devicecontroller/DiscoveredDevice");
18981898
jmethodID constructor = env->GetMethodID(discoveredDeviceCls, "<init>", "()V");
18991899

1900-
jfieldID discrminatorID = env->GetFieldID(discoveredDeviceCls, "discriminator", "J");
1901-
jfieldID ipAddressID = env->GetFieldID(discoveredDeviceCls, "ipAddress", "Ljava/lang/String;");
1902-
jfieldID portID = env->GetFieldID(discoveredDeviceCls, "port", "I");
1900+
jfieldID discrminatorID = env->GetFieldID(discoveredDeviceCls, "discriminator", "J");
1901+
jfieldID ipAddressID = env->GetFieldID(discoveredDeviceCls, "ipAddress", "Ljava/lang/String;");
1902+
jfieldID portID = env->GetFieldID(discoveredDeviceCls, "port", "I");
1903+
jfieldID deviceTypeID = env->GetFieldID(discoveredDeviceCls, "deviceType", "J");
1904+
jfieldID vendorIdID = env->GetFieldID(discoveredDeviceCls, "vendorId", "I");
1905+
jfieldID productIdID = env->GetFieldID(discoveredDeviceCls, "productId", "I");
1906+
jfieldID rotatingIdID = env->GetFieldID(discoveredDeviceCls, "rotatingId", "[B");
1907+
jfieldID instanceNameID = env->GetFieldID(discoveredDeviceCls, "instanceName", "Ljava/lang/String;");
1908+
jfieldID deviceNameID = env->GetFieldID(discoveredDeviceCls, "deviceName", "Ljava/lang/String;");
1909+
jfieldID pairingInstructionID = env->GetFieldID(discoveredDeviceCls, "pairingInstruction", "Ljava/lang/String;");
1910+
1911+
jmethodID setCommissioningModeID = env->GetMethodID(discoveredDeviceCls, "setCommissioningMode", "(I)V");
1912+
jmethodID setPairingHintID = env->GetMethodID(discoveredDeviceCls, "setPairingHint", "(I)V");
19031913

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

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

19121922
env->SetObjectField(discoveredObj, ipAddressID, jniipAdress);
19131923
env->SetIntField(discoveredObj, portID, static_cast<jint>(data->resolutionData.port));
1924+
env->SetLongField(discoveredObj, deviceTypeID, static_cast<jlong>(data->commissionData.deviceType));
1925+
env->SetIntField(discoveredObj, vendorIdID, static_cast<jint>(data->commissionData.vendorId));
1926+
env->SetIntField(discoveredObj, productIdID, static_cast<jint>(data->commissionData.productId));
1927+
1928+
jbyteArray jRotatingId;
1929+
CHIP_ERROR err = JniReferences::GetInstance().N2J_ByteArray(
1930+
env, data->commissionData.rotatingId, static_cast<jsize>(data->commissionData.rotatingIdLen), jRotatingId);
1931+
1932+
if (err != CHIP_NO_ERROR)
1933+
{
1934+
ChipLogError(Controller, "jRotatingId N2J_ByteArray error : %" CHIP_ERROR_FORMAT, err.Format());
1935+
return nullptr;
1936+
}
1937+
env->SetObjectField(discoveredObj, rotatingIdID, static_cast<jobject>(jRotatingId));
1938+
env->SetObjectField(discoveredObj, instanceNameID, env->NewStringUTF(data->commissionData.instanceName));
1939+
env->SetObjectField(discoveredObj, deviceNameID, env->NewStringUTF(data->commissionData.deviceName));
1940+
env->SetObjectField(discoveredObj, pairingInstructionID, env->NewStringUTF(data->commissionData.pairingInstruction));
1941+
1942+
env->CallVoidMethod(discoveredObj, setCommissioningModeID, static_cast<jint>(data->commissionData.commissioningMode));
1943+
env->CallVoidMethod(discoveredObj, setPairingHintID, static_cast<jint>(data->commissionData.pairingHint));
19141944

19151945
return discoveredObj;
19161946
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.devicecontroller;
19+
20+
public enum CommissioningWindowStatus {
21+
WindowNotOpen(0),
22+
EnhancedWindowOpen(1),
23+
BasicWindowOpen(2);
24+
25+
private final int value;
26+
27+
CommissioningWindowStatus(int value) {
28+
this.value = value;
29+
}
30+
31+
public static CommissioningWindowStatus value(int value) {
32+
for (CommissioningWindowStatus status : CommissioningWindowStatus.values()) {
33+
if (status.value == value) {
34+
return status;
35+
}
36+
}
37+
throw new IllegalArgumentException("Invalid value: " + value);
38+
}
39+
}

src/controller/java/src/chip/devicecontroller/DiscoveredDevice.java

+58
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,66 @@
1717
*/
1818
package chip.devicecontroller;
1919

20+
import java.util.Arrays;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
2024
public class DiscoveredDevice {
2125
public long discriminator;
2226
public String ipAddress;
2327
public int port;
28+
public long deviceType;
29+
public int vendorId;
30+
public int productId;
31+
public Set<PairingHintBitmap> pairingHint;
32+
public CommissioningWindowStatus commissioningMode;
33+
public byte[] rotatingId;
34+
public String instanceName;
35+
public String deviceName;
36+
public String pairingInstruction;
37+
38+
// For use in JNI.
39+
private void setCommissioningMode(int value) {
40+
this.commissioningMode = CommissioningWindowStatus.value(value);
41+
}
42+
43+
private void setPairingHint(int value) {
44+
this.pairingHint = new HashSet<>();
45+
for (PairingHintBitmap mode : PairingHintBitmap.values()) {
46+
int bitmask = 1 << mode.getBitIndex();
47+
if ((value & bitmask) != 0) {
48+
pairingHint.add(mode);
49+
}
50+
}
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "DiscoveredDevice : {"
56+
+ "\n\tdiscriminator : "
57+
+ discriminator
58+
+ "\n\tipAddress : "
59+
+ ipAddress
60+
+ "\n\tport : "
61+
+ port
62+
+ "\n\tdeviceType : "
63+
+ deviceType
64+
+ "\n\tvendorId : "
65+
+ vendorId
66+
+ "\n\tproductId : "
67+
+ productId
68+
+ "\n\tpairingHint : "
69+
+ pairingHint
70+
+ "\n\tcommissioningMode : "
71+
+ commissioningMode
72+
+ "\n\trotatingId : "
73+
+ (rotatingId != null ? Arrays.toString(rotatingId) : "null")
74+
+ "\n\tinstanceName : "
75+
+ instanceName
76+
+ "\n\tdeviceName : "
77+
+ deviceName
78+
+ "\n\tpairingInstruction : "
79+
+ pairingInstruction
80+
+ "\n}";
81+
}
2482
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.devicecontroller;
19+
20+
public enum PairingHintBitmap {
21+
PowerCycle(0, false),
22+
DeviceManufacturerURL(1, false),
23+
Administrator(2, false),
24+
SettingsMenuOnTheNode(3, false),
25+
CustomInstruction(4, true),
26+
DeviceManual(5, false),
27+
PressResetButton(6, false),
28+
PressResetButtonWithApplicationOfPower(7, false),
29+
PressResetButtonForNseconds(8, true),
30+
PressResetButtonUntilLightBlinks(9, true),
31+
PressResetButtonForNsecondsWithApplicationOfPower(10, true),
32+
PressResetButtonUntilLightBlinksWithApplicationOfPower(11, true),
33+
PressResetButtonNTimes(12, true),
34+
PressSetupButton(13, false),
35+
PressSetupButtonWithApplicationOfPower(14, false),
36+
PressSetupButtonForNseconds(15, true),
37+
PressSetupButtonUntilLightBlinks(16, true),
38+
PressSetupButtonForNsecondsWithApplicationOfPower(17, true),
39+
PressSetupButtonUntilLightBlinksWithApplicationOfPower(18, true),
40+
PressSetupButtonNtimes(19, true);
41+
42+
private final int bitIndex;
43+
private final boolean isRequirePairingInstruction;
44+
45+
PairingHintBitmap(int bitIndex, boolean isRequirePairingInstruction) {
46+
this.bitIndex = bitIndex;
47+
this.isRequirePairingInstruction = isRequirePairingInstruction;
48+
}
49+
50+
public int getBitIndex() {
51+
return bitIndex;
52+
}
53+
54+
public boolean getRequirePairingInstruction() {
55+
return isRequirePairingInstruction;
56+
}
57+
}

0 commit comments

Comments
 (0)