Skip to content

Commit

Permalink
[Android CHIPTool] Wi-Fi vs Thread provisioning.
Browse files Browse the repository at this point in the history
Add distinct buttons to Android app to provision 
Wi-Fi vs Thread network during CHIP device setup.
  • Loading branch information
vidhis88 committed Dec 3, 2020
1 parent 69a35c0 commit 4043326
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.google.chip.chiptool.commissioner.CommissionerActivity
import com.google.chip.chiptool.echoclient.EchoClientFragment
import com.google.chip.chiptool.provisioning.DeviceProvisioningFragment
import com.google.chip.chiptool.provisioning.EnterWifiNetworkFragment
import com.google.chip.chiptool.provisioning.ProvisionNetworkType
import com.google.chip.chiptool.setuppayloadscanner.BarcodeFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceDetailsFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceInfo
Expand All @@ -38,8 +39,9 @@ import com.google.chip.chiptool.setuppayloadscanner.QrCodeInfo
class CHIPToolActivity :
AppCompatActivity(),
BarcodeFragment.Callback,
SelectActionFragment.Callback,
CHIPDeviceDetailsFragment.Callback {
SelectActionFragment.Callback {

private var provisionNetworkType: ProvisionNetworkType? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -51,24 +53,43 @@ class CHIPToolActivity :
.beginTransaction()
.add(R.id.fragment_container, fragment, fragment.javaClass.simpleName)
.commit()
} else {
provisionNetworkType =
ProvisionNetworkType.fromName(savedInstanceState.getString(ARG_PROVISION_NETWORK_TYPE))
}

if (intent?.action == NfcAdapter.ACTION_NDEF_DISCOVERED)
onNfcIntent(intent)
}

override fun onStartRendezvousOverBle(deviceInfo: CHIPDeviceInfo) {
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo))
override fun onSaveInstanceState(outState: Bundle) {
outState.putString(ARG_PROVISION_NETWORK_TYPE, provisionNetworkType?.name)

super.onSaveInstanceState(outState)
}

override fun onCHIPDeviceInfoReceived(deviceInfo: CHIPDeviceInfo) {
showFragment(CHIPDeviceDetailsFragment.newInstance(deviceInfo))
if (provisionNetworkType == null) {
showFragment(CHIPDeviceDetailsFragment.newInstance(deviceInfo))
} else {
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo, provisionNetworkType!!))
}
}

override fun handleScanQrCodeClicked() {
showFragment(BarcodeFragment.newInstance())
}

override fun onProvisionWifiCredentialsClicked() {
provisionNetworkType = ProvisionNetworkType.WIFI
showFragment(BarcodeFragment.newInstance())
}

override fun onProvisionThreadCredentialsClicked() {
provisionNetworkType = ProvisionNetworkType.THREAD
showFragment(BarcodeFragment.newInstance())
}

override fun handleCommissioningClicked() {
var intent = Intent(this, CommissionerActivity::class.java)
startActivityForResult(intent, REQUEST_CODE_COMMISSIONING)
Expand Down Expand Up @@ -129,6 +150,8 @@ class CHIPToolActivity :
}

companion object {
private const val ARG_PROVISION_NETWORK_TYPE = "provision_network_type"

var REQUEST_CODE_COMMISSIONING = 0xB003
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ class SelectActionFragment : Fragment() {
): View {
return inflater.inflate(R.layout.select_action_fragment, container, false).apply {
scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
commissioningBtn.setOnClickListener { getCallback()?.handleCommissioningClicked() }
provisionWifiCredentialsBtn.setOnClickListener {
getCallback()?.onProvisionWifiCredentialsClicked()
}
provisionThreadCredentialsBtn.setOnClickListener {
getCallback()?.onProvisionThreadCredentialsClicked()
}
otCommissioningBtn.setOnClickListener { getCallback()?.handleCommissioningClicked() }
echoClientBtn.setOnClickListener { getCallback()?.handleEchoClientClicked() }
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
Expand All @@ -48,6 +54,10 @@ class SelectActionFragment : Fragment() {
interface Callback {
/** Notifies listener of Scan QR code button click. */
fun handleScanQrCodeClicked()
/** Notifies listener of provision-Wifi-credentials button click. */
fun onProvisionWifiCredentialsClicked()
/** Notifies listener of provision-Thread-credentials button click. */
fun onProvisionThreadCredentialsClicked()
/** Notifies listener of Commissioning button click. */
fun handleCommissioningClicked()
/** Notifies listener of Echo client button click. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
Expand Down Expand Up @@ -106,6 +107,17 @@ class DeviceProvisioningFragment : Fragment() {
}
}

private fun handleWifiNetworkProvisioning() {
childFragmentManager.beginTransaction()
.add(R.id.fragment_container, EnterWifiNetworkFragment.newInstance())
.commit()
}

private fun handleThreadNetworkProvisioning() {
// TODO: Send ThreadNetworkInfo object
ChipClient.getDeviceController().sendThreadCredentials(null)
}

inner class ConnectionCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {
showMessage(R.string.rendezvous_over_ble_success_text)
Expand All @@ -116,9 +128,14 @@ class DeviceProvisioningFragment : Fragment() {
}

override fun onNetworkCredentialsRequested() {
childFragmentManager.beginTransaction()
.add(R.id.fragment_container, EnterWifiNetworkFragment.newInstance())
.commit()
val provisionNetworkType =
ProvisionNetworkType.fromName(arguments?.getString(ARG_PROVISION_NETWORK_TYPE))

when (provisionNetworkType) {
ProvisionNetworkType.WIFI -> handleWifiNetworkProvisioning()
ProvisionNetworkType.THREAD -> handleThreadNetworkProvisioning()
else -> Log.e(TAG, "Invalid ProvisionNetworkType provided")
}
}

override fun onPairingComplete(code: Int) {
Expand All @@ -141,10 +158,17 @@ class DeviceProvisioningFragment : Fragment() {
companion object {
private const val TAG = "DeviceProvisioningFragment"
private const val ARG_DEVICE_INFO = "device_info"
private const val ARG_PROVISION_NETWORK_TYPE = "provision_network_type"

fun newInstance(deviceInfo: CHIPDeviceInfo): DeviceProvisioningFragment {
fun newInstance(
deviceInfo: CHIPDeviceInfo,
networkType: ProvisionNetworkType
): DeviceProvisioningFragment {
return DeviceProvisioningFragment().apply {
arguments = Bundle(1).apply { putParcelable(ARG_DEVICE_INFO, deviceInfo) }
arguments = Bundle(2).apply {
putParcelable(ARG_DEVICE_INFO, deviceInfo)
putString(ARG_PROVISION_NETWORK_TYPE, networkType.name)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2020 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.provisioning

import android.os.Bundle
Expand All @@ -11,6 +28,9 @@ import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.enter_wifi_network_fragment.*
import kotlinx.android.synthetic.main.enter_wifi_network_fragment.view.*

/**
* Fragment to collect Wi-Fi network information from user and send it to device being provisioned.
*/
class EnterWifiNetworkFragment : Fragment() {

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2020 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.provisioning

/** Network type - Wi-Fi vs Thread, to be provisioned. */
enum class ProvisionNetworkType {
THREAD,
WIFI,
;

companion object {
fun fromName(name: String?): ProvisionNetworkType? {
return when (name) {
THREAD.name -> THREAD
WIFI.name -> WIFI
else -> null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.chip.chiptool.R
import com.google.chip.chiptool.util.FragmentUtil
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.ble_rendezvous_btn
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.discriminatorTv
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.productIdTv
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.setupCodeTv
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.softap_rendezvous_btn
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.vendorIdTv
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.vendorTagsContainer
import kotlinx.android.synthetic.main.chip_device_info_fragment.view.vendorTagsLabelTv
Expand Down Expand Up @@ -71,26 +68,10 @@ class CHIPDeviceDetailsFragment : Fragment() {
vendorTagsContainer.addView(tv)
}
}

ble_rendezvous_btn.setOnClickListener { onRendezvousBleClicked() }

// TODO: Until Rendezvous over hotspot is ready to implement
softap_rendezvous_btn.visibility = View.GONE
}
}



private fun onRendezvousBleClicked() {
FragmentUtil.getHost(this, Callback::class.java)?.onStartRendezvousOverBle(deviceInfo)
}

interface Callback {
fun onStartRendezvousOverBle(deviceInfo: CHIPDeviceInfo)
}

companion object {
private const val TAG = "CHIPDeviceDetailsFragment"
private const val ARG_DEVICE_INFO = "device_info"

@JvmStatic fun newInstance(deviceInfo: CHIPDeviceInfo): CHIPDeviceDetailsFragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,5 @@
android:orientation="vertical"
android:layout_marginBottom="8dp"
android:layout_below="@id/vendorTagsLabelTv"/>

<TextView
android:id="@+id/ble_rendezvous_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/vendorTagsContainer"
android:layout_alignParentStart="true"
android:background="@android:color/darker_gray"
android:layout_marginBottom="8dp"
android:padding="12dp"
android:textSize="20sp"
android:text="@string/rendezvous_over_ble_btn_text"/>

<TextView
android:id="@+id/softap_rendezvous_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ble_rendezvous_btn"
android:layout_alignParentStart="true"
android:background="@android:color/darker_gray"
android:layout_marginBottom="8dp"
android:padding="12dp"
android:textSize="20sp"
android:text="@string/rendezvous_over_soft_ap_btn_text"/>
</RelativeLayout>
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
android:text="@string/scan_qr_code_btn_text" />

<Button
android:id="@+id/commissioningBtn"
android:id="@+id/provisionWifiCredentialsBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/provision_wifi_credentials_btn_text" />

<Button
android:id="@+id/provisionThreadCredentialsBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/provision_thread_credentials_btn_text" />

<Button
android:id="@+id/otCommissioningBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/commissioner_commissioning" />
android:text="@string/commissioner_openthread_commissioning" />

<Button
android:id="@+id/echoClientBtn"
Expand Down
4 changes: 3 additions & 1 deletion src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<string name="echo_status_sending_message">Sending message…</string>
<string name="echo_status_response">Received echo response:\n\n%1$s</string>
<string name="scan_qr_code_btn_text">Scan QR Code</string>
<string name="provision_wifi_credentials_btn_text">Provision CHIP device with Wi-Fi</string>
<string name="provision_thread_credentials_btn_text">Provision CHIP device with Thread</string>
<string name="echo_client_btn_text">Echo Client</string>
<string name="on_off_level_btn_text">Light On/Off &amp; Level Cluster</string>
<string name="enter_ip_address_hint_text">Enter IP Address (eg. 192.168.10.2)</string>
Expand All @@ -39,7 +41,7 @@
<string name="rendezvous_over_ble_pairing_text">Pairing</string>
<string name="rendezvous_over_ble_success_text">Secure channel established. Provisioning</string>

<string name="commissioner_commissioning">Commissioning</string>
<string name="commissioner_openthread_commissioning">Open Thread Commissioning</string>
<string name="commissioner_name">Commissioner</string>
<string name="commissioner_scan_device">Scan Device</string>
<string name="commissioner_cancel">Cancel</string>
Expand Down
Loading

0 comments on commit 4043326

Please sign in to comment.