Skip to content

Commit

Permalink
Add discover port parameter (project-chip#31948)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo authored Feb 6, 2024
1 parent 254010c commit 1259831
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AddressCommissioningFragment : Fragment() {
val address = binding.addressEditText.text.toString()
val discriminator = binding.discriminatorEditText.text.toString()
val pincode = binding.pincodeEditText.text.toString()
val port = binding.portEditText.text.toString()

if (address.isEmpty() || discriminator.isEmpty() || pincode.isEmpty()) {
Log.e(TAG, "Address, discriminator, or pincode was empty: $address $discriminator $pincode")
Expand All @@ -62,7 +63,8 @@ class AddressCommissioningFragment : Fragment() {
CHIPDeviceInfo(
discriminator = discriminator.toInt(),
setupPinCode = pincode.toLong(),
ipAddress = address
ipAddress = address,
port = port.toInt()
)
)
}
Expand Down Expand Up @@ -169,7 +171,7 @@ class AddressCommissioningFragment : Fragment() {
val deviceController = ChipClient.getDeviceController(requireContext())
for (i in 0..10) {
val device = deviceController.getDiscoveredDevice(i) ?: break
ipAddressList.add("${device.ipAddress}, ${device.discriminator}")
ipAddressList.add("${device.ipAddress}, ${device.discriminator}, ${device.port}")
}
requireActivity().runOnUiThread {
binding.discoverListSpinner.adapter =
Expand All @@ -179,9 +181,11 @@ class AddressCommissioningFragment : Fragment() {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val address = ipAddressList[position].split(",")[0].trim()
val discriminator = ipAddressList[position].split(",")[1].trim()
val port = ipAddressList[position].split(",")[2].trim()

binding.addressEditText.setText(address)
binding.discriminatorEditText.setText(discriminator)
binding.portEditText.setText(port)
}

override fun onNothingSelected(parent: AdapterView<*>) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class DeviceProvisioningFragment : Fragment() {

private fun pairDeviceWithAddress() {
// IANA CHIP port
val port = 5540
val id = DeviceIdUtil.getNextAvailableId(requireContext())

DeviceIdUtil.setNextAvailableId(requireContext(), id + 1)
Expand All @@ -171,7 +170,7 @@ class DeviceProvisioningFragment : Fragment() {
deviceController.pairDeviceWithAddress(
id,
deviceInfo.ipAddress,
port,
deviceInfo.port,
deviceInfo.discriminator,
deviceInfo.setupPinCode,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data class CHIPDeviceInfo(
val discoveryCapabilities: MutableSet<DiscoveryCapability> = mutableSetOf(),
val isShortDiscriminator: Boolean = false,
val ipAddress: String? = null,
val port: Int = 5540
) : Parcelable {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
android:id="@+id/address_commissioning_fragment_flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:constraint_referenced_ids="addressLabel,addressEditText,discriminatorLabel,discriminatorEditText,pincodeLabel,pincodeEditText"
app:constraint_referenced_ids="addressLabel,addressEditText,discriminatorLabel,discriminatorEditText,pincodeLabel,pincodeEditText,portLabel,portEditText"
app:flow_horizontalStyle="packed"
app:flow_wrapMode="aligned"
app:flow_maxElementsWrap="2"
Expand Down Expand Up @@ -75,6 +75,22 @@
android:inputType="number"
android:text="@string/default_pincode" />

<TextView
android:id="@+id/portLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:text="@string/enter_port_label_text" />

<EditText
android:id="@+id/portEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="number"
android:text="@string/default_port" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions examples/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
<string name="address_commissioning_title_text">Commission with IP address</string>
<string name="default_discriminator">3840</string>
<string name="default_pincode">20202021</string>
<string name="default_port">5540</string>
<string name="enter_device_address_label_text">Device address</string>
<string name="enter_discriminator_label_text">Discriminator</string>
<string name="enter_pincode_label_text">Pincode</string>
<string name="enter_port_label_text">Port</string>
<string name="commission_btn_text">Commission</string>
<string name="dns_discover_btn_text">Discover</string>
<string name="wifi_scan_btn_text">Wi-Fi Scan</string>
Expand Down
8 changes: 4 additions & 4 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl

if (data == nullptr)
{
ChipLogError(Controller, "GetDiscoveredDevice - not found");
return nullptr;
}

Expand All @@ -1918,6 +1919,7 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl

jfieldID discrminatorID = env->GetFieldID(discoveredDeviceCls, "discriminator", "J");
jfieldID ipAddressID = env->GetFieldID(discoveredDeviceCls, "ipAddress", "Ljava/lang/String;");
jfieldID portID = env->GetFieldID(discoveredDeviceCls, "port", "I");

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

Expand All @@ -1926,12 +1928,10 @@ JNI_METHOD(jobject, getDiscoveredDevice)(JNIEnv * env, jobject self, jlong handl
char ipAddress[100];
data->resolutionData.ipAddress[0].ToString(ipAddress, 100);
jstring jniipAdress = env->NewStringUTF(ipAddress);

env->SetObjectField(discoveredObj, ipAddressID, jniipAdress);
env->SetIntField(discoveredObj, portID, static_cast<jint>(data->resolutionData.port));

if (data == nullptr)
{
ChipLogError(Controller, "GetDiscoveredDevice - not found");
}
return discoveredObj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
public class DiscoveredDevice {
public long discriminator;
public String ipAddress;
public int port;
}

0 comments on commit 1259831

Please sign in to comment.