Skip to content

Commit

Permalink
Decouple NetworkCredentialParcelable from ChipDeviceController (#23327)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored Oct 27, 2022
1 parent dd358ef commit 3b81f3f
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import chip.devicecontroller.NetworkCredentials
import com.google.chip.chiptool.NetworkCredentialsParcelable
import chip.setuppayload.SetupPayload
import chip.setuppayload.SetupPayloadParser
import chip.setuppayload.SetupPayloadParser.UnrecognizedQrCodeException
Expand Down Expand Up @@ -133,7 +133,7 @@ class CHIPToolActivity :
showFragment(AddressCommissioningFragment.newInstance(), false)
}

override fun onNetworkCredentialsEntered(networkCredentials: NetworkCredentials) {
override fun onNetworkCredentialsEntered(networkCredentials: NetworkCredentialsParcelable) {
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo!!, networkCredentials))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2020-2022 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 com.google.chip.chiptool;

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.Nullable;

/** Class for holding WiFi or Thread credentials, but not both. */
public class NetworkCredentialsParcelable implements Parcelable {
@Nullable private WiFiCredentials wifiCredentials;
@Nullable private ThreadCredentials threadCredentials;

private NetworkCredentialsParcelable(
@Nullable WiFiCredentials wifiCredentials, @Nullable ThreadCredentials threadCredentials) {
this.wifiCredentials = wifiCredentials;
this.threadCredentials = threadCredentials;
}

/**
* Return a NetworkCredentialsParcelable object with the given WiFiCredentials and null
* ThreadCredentials.
*/
public static NetworkCredentialsParcelable forWiFi(WiFiCredentials wifiCredentials) {
return new NetworkCredentialsParcelable(wifiCredentials, null);
}

/**
* Return a NetworkCredentialsParcelable object with the given ThreadCredentials and null
* WiFiCredentials.
*/
public static NetworkCredentialsParcelable forThread(ThreadCredentials threadCredentials) {
return new NetworkCredentialsParcelable(null, threadCredentials);
}

public WiFiCredentials getWiFiCredentials() {
return wifiCredentials;
}

public ThreadCredentials getThreadCredentials() {
return threadCredentials;
}

// Begin Parcelable implementation

private NetworkCredentialsParcelable(Parcel in) {
wifiCredentials = in.readParcelable(WiFiCredentials.class.getClassLoader());
threadCredentials = in.readParcelable(ThreadCredentials.class.getClassLoader());
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(wifiCredentials, 0);
out.writeParcelable(threadCredentials, 0);
}

public static final Parcelable.Creator<NetworkCredentialsParcelable> CREATOR =
new Parcelable.Creator<NetworkCredentialsParcelable>() {
public NetworkCredentialsParcelable createFromParcel(Parcel in) {
return new NetworkCredentialsParcelable(in);
}

public NetworkCredentialsParcelable[] newArray(int size) {
return new NetworkCredentialsParcelable[size];
}
};

public static class WiFiCredentials implements Parcelable {
private final String ssid;
private final String password;

public WiFiCredentials(String ssid, String password) {
this.ssid = ssid;
this.password = password;
}

public String getSsid() {
return ssid;
}

public String getPassword() {
return password;
}

// Begin Parcelable implementation

private WiFiCredentials(Parcel in) {
ssid = in.readString();
password = in.readString();
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel out, int flags) {
out.writeString(ssid);
out.writeString(password);
}

public static final Parcelable.Creator<WiFiCredentials> CREATOR =
new Parcelable.Creator<WiFiCredentials>() {
public WiFiCredentials createFromParcel(Parcel in) {
return new WiFiCredentials(in);
}

public WiFiCredentials[] newArray(int size) {
return new WiFiCredentials[size];
}
};
}

public static class ThreadCredentials implements Parcelable {
private final byte[] operationalDataset;

public ThreadCredentials(byte[] operationalDataset) {
this.operationalDataset = operationalDataset;
}

public byte[] getOperationalDataset() {
return operationalDataset;
}

// Begin Parcelable implementation

private ThreadCredentials(Parcel in) {
operationalDataset = new byte[in.readInt()];
in.readByteArray(operationalDataset);
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel out, int flags) {
out.writeInt(operationalDataset.length);
out.writeByteArray(operationalDataset);
}

public static final Parcelable.Creator<ThreadCredentials> CREATOR =
new Parcelable.Creator<ThreadCredentials>() {
public ThreadCredentials createFromParcel(Parcel in) {
return new ThreadCredentials(in);
}

public ThreadCredentials[] newArray(int size) {
return new ThreadCredentials[size];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.NetworkCredentials
import com.google.chip.chiptool.NetworkCredentialsParcelable
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
Expand All @@ -46,7 +47,7 @@ class DeviceProvisioningFragment : Fragment() {

private var gatt: BluetoothGatt? = null

private val networkCredentials: NetworkCredentials?
private val networkCredentialsParcelable: NetworkCredentialsParcelable?
get() = arguments?.getParcelable(ARG_NETWORK_CREDENTIALS)

private lateinit var scope: CoroutineScope
Expand Down Expand Up @@ -120,7 +121,22 @@ class DeviceProvisioningFragment : Fragment() {

val deviceId = DeviceIdUtil.getNextAvailableId(requireContext())
val connId = bluetoothManager.connectionId
deviceController.pairDevice(gatt, connId, deviceId, deviceInfo.setupPinCode, networkCredentials)
val network = NetworkCredentials()
var networkParcelable = checkNotNull(networkCredentialsParcelable)

val wifi = networkParcelable.getWiFiCredentials()
if (wifi != null)
{
network.setWiFiCredentials(wifi.getSsid(), wifi.getPassword())
}

val thread = networkParcelable.getThreadCredentials()
if (thread != null)
{
network.setThreadCredentials(thread.getOperationalDataset())
}

deviceController.pairDevice(gatt, connId, deviceId, deviceInfo.setupPinCode, network)
DeviceIdUtil.setNextAvailableId(requireContext(), deviceId + 1)
}
}
Expand Down Expand Up @@ -191,17 +207,17 @@ class DeviceProvisioningFragment : Fragment() {
private const val STATUS_PAIRING_SUCCESS = 0

/**
* Return a new instance of [DeviceProvisioningFragment]. [networkCredentials] can be null for
* Return a new instance of [DeviceProvisioningFragment]. [networkCredentialsParcelable] can be null for
* IP commissioning.
*/
fun newInstance(
deviceInfo: CHIPDeviceInfo,
networkCredentials: NetworkCredentials?,
networkCredentialsParcelable: NetworkCredentialsParcelable?,
): DeviceProvisioningFragment {
return DeviceProvisioningFragment().apply {
arguments = Bundle(2).apply {
putParcelable(ARG_DEVICE_INFO, deviceInfo)
putParcelable(ARG_NETWORK_CREDENTIALS, networkCredentials)
putParcelable(ARG_NETWORK_CREDENTIALS, networkCredentialsParcelable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.devicecontroller.NetworkCredentials
import com.google.chip.chiptool.R
import com.google.chip.chiptool.util.FragmentUtil
import com.google.chip.chiptool.NetworkCredentialsParcelable
import kotlinx.android.synthetic.main.enter_thread_network_fragment.channelEd
import kotlinx.android.synthetic.main.enter_thread_network_fragment.masterKeyEd
import kotlinx.android.synthetic.main.enter_thread_network_fragment.panIdEd
Expand All @@ -44,7 +44,7 @@ class EnterNetworkFragment : Fragment() {
)

interface Callback {
fun onNetworkCredentialsEntered(networkCredentials: NetworkCredentials)
fun onNetworkCredentialsEntered(networkCredentials: NetworkCredentialsParcelable)
}

override fun onCreateView(
Expand Down Expand Up @@ -79,8 +79,8 @@ class EnterNetworkFragment : Fragment() {
return
}

val networkCredentials = NetworkCredentials.forWiFi(
NetworkCredentials.WiFiCredentials(ssid.toString(), pwd.toString())
val networkCredentials = NetworkCredentialsParcelable.forWiFi(
NetworkCredentialsParcelable.WiFiCredentials(ssid.toString(), pwd.toString())
)
FragmentUtil.getHost(this, Callback::class.java)
?.onNetworkCredentialsEntered(networkCredentials)
Expand Down Expand Up @@ -130,7 +130,7 @@ class EnterNetworkFragment : Fragment() {
)

val networkCredentials =
NetworkCredentials.forThread(NetworkCredentials.ThreadCredentials(operationalDataset))
NetworkCredentialsParcelable.forThread(NetworkCredentialsParcelable.ThreadCredentials(operationalDataset))
FragmentUtil.getHost(this, Callback::class.java)
?.onNetworkCredentialsEntered(networkCredentials)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/android/CHIPTool/chip-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ android {
'../../../setup_payload/java/src',
'../../../controller/java/zap-generated',
'../../../controller/java/src',
'../../../platform/android/java'
'../../../platform/android/java',
'../app/src/main/java/com/google/chip/chiptool'
]
}
}
Expand Down
Loading

0 comments on commit 3b81f3f

Please sign in to comment.