Skip to content

Commit 3906307

Browse files
austinh0restyled-commits
authored andcommitted
[Android] Use GetDeviceBeingCommissioned for network commissioning (#11658)
* Use GetDeviceBeingCommissioned for network commissioning * Restyled by clang-format Co-authored-by: Restyled.io <commits@restyled.io>
1 parent cb8641d commit 3906307

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/EnterNetworkFragment.kt

+16-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import android.view.View
2424
import android.view.ViewGroup
2525
import android.widget.Toast
2626
import androidx.fragment.app.Fragment
27-
import androidx.lifecycle.lifecycleScope
2827
import chip.devicecontroller.ChipClusters.NetworkCommissioningCluster
2928
import com.google.chip.chiptool.ChipClient
3029
import com.google.chip.chiptool.R
@@ -37,16 +36,11 @@ import kotlinx.android.synthetic.main.enter_thread_network_fragment.xpanIdEd
3736
import kotlinx.android.synthetic.main.enter_wifi_network_fragment.pwdEd
3837
import kotlinx.android.synthetic.main.enter_wifi_network_fragment.ssidEd
3938
import kotlinx.android.synthetic.main.enter_wifi_network_fragment.view.saveNetworkBtn
40-
import kotlinx.coroutines.CoroutineScope
41-
import kotlinx.coroutines.launch
4239

4340
/**
4441
* Fragment to collect Wi-Fi network information from user and send it to device being provisioned.
4542
*/
4643
class EnterNetworkFragment : Fragment() {
47-
48-
private lateinit var scope: CoroutineScope
49-
5044
private val networkType: ProvisionNetworkType
5145
get() = requireNotNull(
5246
ProvisionNetworkType.fromName(arguments?.getString(ARG_PROVISION_NETWORK_TYPE))
@@ -57,15 +51,13 @@ class EnterNetworkFragment : Fragment() {
5751
container: ViewGroup?,
5852
savedInstanceState: Bundle?
5953
): View? {
60-
scope = viewLifecycleOwner.lifecycleScope
61-
6254
val layoutRes = when (networkType) {
6355
ProvisionNetworkType.WIFI -> R.layout.enter_wifi_network_fragment
6456
ProvisionNetworkType.THREAD -> R.layout.enter_thread_network_fragment
6557
}
6658

6759
if (USE_HARDCODED_WIFI) {
68-
scope.launch { saveHardcodedWifiNetwork() }
60+
saveHardcodedWifiNetwork()
6961
}
7062

7163
return inflater.inflate(layoutRes, container, false).apply {
@@ -75,17 +67,17 @@ class EnterNetworkFragment : Fragment() {
7567

7668
private fun onSaveNetworkClicked() {
7769
if (networkType == ProvisionNetworkType.WIFI) {
78-
scope.launch { saveWifiNetwork() }
70+
saveWifiNetwork()
7971
} else {
80-
scope.launch { saveThreadNetwork() }
72+
saveThreadNetwork()
8173
}
8274
}
8375

84-
private suspend fun saveHardcodedWifiNetwork() {
76+
private fun saveHardcodedWifiNetwork() {
8577
addAndEnableWifiNetwork(HARDCODED_WIFI_SSID, HARDCODED_WIFI_PASSWORD)
8678
}
8779

88-
private suspend fun saveWifiNetwork() {
80+
private fun saveWifiNetwork() {
8981
val ssid = ssidEd?.text
9082
val pwd = pwdEd?.text
9183

@@ -97,15 +89,12 @@ class EnterNetworkFragment : Fragment() {
9789
addAndEnableWifiNetwork(ssid.toString(), pwd.toString())
9890
}
9991

100-
private suspend fun addAndEnableWifiNetwork(ssid: String, password: String) {
92+
private fun addAndEnableWifiNetwork(ssid: String, password: String) {
10193
// Uses UTF-8 as default
10294
val ssidBytes = ssid.toByteArray()
10395
val pwdBytes = password.toByteArray()
10496

105-
val devicePtr =
106-
ChipClient.getConnectedDevicePointer(requireContext(), DeviceIdUtil.getLastDeviceId(requireContext()))
107-
val cluster = NetworkCommissioningCluster(devicePtr, /* endpointId = */ 0)
108-
97+
val cluster = createNetworkCommissioningCluster()
10998
val enableNetworkCallback = object :
11099
NetworkCommissioningCluster.EnableNetworkResponseCallback {
111100
override fun onSuccess(errorCode: Int, debugText: String) {
@@ -157,7 +146,7 @@ class EnterNetworkFragment : Fragment() {
157146
}, ssidBytes, pwdBytes, /* breadcrumb = */ 0L, ADD_NETWORK_TIMEOUT)
158147
}
159148

160-
private suspend fun saveThreadNetwork() {
149+
private fun saveThreadNetwork() {
161150
val channelStr = channelEd.text
162151
val panIdStr = panIdEd.text
163152

@@ -193,9 +182,7 @@ class EnterNetworkFragment : Fragment() {
193182
return
194183
}
195184

196-
val devicePtr =
197-
ChipClient.getConnectedDevicePointer(requireContext(), DeviceIdUtil.getLastDeviceId(requireContext()))
198-
val cluster = NetworkCommissioningCluster(devicePtr, /* endpointId = */ 0)
185+
val cluster = createNetworkCommissioningCluster()
199186

200187
val operationalDataset = makeThreadOperationalDataset(
201188
channelStr.toString().toInt(),
@@ -290,13 +277,20 @@ class EnterNetworkFragment : Fragment() {
290277
return dataset
291278
}
292279

280+
private fun createNetworkCommissioningCluster(): NetworkCommissioningCluster {
281+
val devicePtr = ChipClient.getDeviceController(requireContext())
282+
.getDeviceBeingCommissionedPointer(DeviceIdUtil.getLastDeviceId(requireContext()))
283+
return NetworkCommissioningCluster(devicePtr, NETWORK_COMMISSIONING_CLUSTER_ENDPOINT)
284+
}
285+
293286
private fun String.hexToByteArray(): ByteArray {
294287
return chunked(2).map { byteStr -> byteStr.toUByte(16).toByte() }.toByteArray()
295288
}
296289

297290
companion object {
298291
private const val TAG = "EnterNetworkFragment"
299292
private const val ARG_PROVISION_NETWORK_TYPE = "provision_network_type"
293+
private const val NETWORK_COMMISSIONING_CLUSTER_ENDPOINT = 0
300294

301295
// TODO(#5035): remove hardcoded option when delayed commands work.
302296
private const val USE_HARDCODED_WIFI = false

src/controller/java/CHIPDeviceController-JNI.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,39 @@ JNI_METHOD(void, stopDevicePairing)(JNIEnv * env, jobject self, jlong handle, jl
277277
}
278278
}
279279

280+
JNI_METHOD(jlong, getDeviceBeingCommissionedPointer)(JNIEnv * env, jobject self, jlong handle, jlong nodeId)
281+
{
282+
chip::DeviceLayer::StackLock lock;
283+
CHIP_ERROR err = CHIP_NO_ERROR;
284+
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
285+
286+
CommissioneeDeviceProxy * commissioneeDevice = nullptr;
287+
err = wrapper->Controller()->GetDeviceBeingCommissioned(static_cast<NodeId>(nodeId), &commissioneeDevice);
288+
289+
if (commissioneeDevice == nullptr)
290+
{
291+
ChipLogError(Controller, "Commissionee device was nullptr");
292+
err = CHIP_ERROR_INCORRECT_STATE;
293+
}
294+
295+
if (err != CHIP_NO_ERROR)
296+
{
297+
ChipLogError(Controller, "Failed to get commissionee device: %s", ErrorStr(err));
298+
JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
299+
return 0;
300+
}
301+
302+
return reinterpret_cast<jlong>(commissioneeDevice);
303+
}
304+
280305
JNI_METHOD(void, getConnectedDevicePointer)(JNIEnv * env, jobject self, jlong handle, jlong nodeId, jlong callbackHandle)
281306
{
282307
chip::DeviceLayer::StackLock lock;
283308
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
284309

285310
GetConnectedDeviceCallback * connectedDeviceCallback = reinterpret_cast<GetConnectedDeviceCallback *>(callbackHandle);
286311
VerifyOrReturn(connectedDeviceCallback != nullptr, ChipLogError(Controller, "GetConnectedDeviceCallback handle is nullptr"));
312+
wrapper->Controller()->GetCompressedFabricId();
287313
wrapper->Controller()->GetConnectedDevice(nodeId, &connectedDeviceCallback->mOnSuccess, &connectedDeviceCallback->mOnFailure);
288314
}
289315

src/controller/java/src/chip/devicecontroller/ChipDeviceController.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public void pairDevice(BluetoothGatt bleServer, int connId, long deviceId, long
5252
/**
5353
* Pair a device connected through BLE.
5454
*
55-
* <p>TODO(#7985): Annotate csrNonce as Nullable.
56-
*
5755
* @param bleServer the BluetoothGatt representing the BLE connection to the device
5856
* @param connId the BluetoothGatt Id representing the BLE connection to the device
5957
* @param deviceId the node ID to assign to the device
@@ -100,6 +98,14 @@ public void unpairDevice(long deviceId) {
10098
unpairDevice(deviceControllerPtr, deviceId);
10199
}
102100

101+
/**
102+
* Returns a pointer to a device currently being commissioned. This should be used before the
103+
* device is operationally available.
104+
*/
105+
public long getDeviceBeingCommissionedPointer(long nodeId) {
106+
return getDeviceBeingCommissionedPointer(deviceControllerPtr, nodeId);
107+
}
108+
103109
/**
104110
* Through GetConnectedDeviceCallback, returns a pointer to a connected device or an error.
105111
*
@@ -251,6 +257,8 @@ private native void pairDeviceWithAddress(
251257

252258
private native void unpairDevice(long deviceControllerPtr, long deviceId);
253259

260+
private native long getDeviceBeingCommissionedPointer(long deviceControllerPtr, long nodeId);
261+
254262
private native void getConnectedDevicePointer(
255263
long deviceControllerPtr, long deviceId, long callbackHandle);
256264

0 commit comments

Comments
 (0)