Skip to content

Commit

Permalink
Implement QR Code Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed May 9, 2024
1 parent 550b1c1 commit e62d12e
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

package com.google.chip.chiptool.setuppayloadscanner

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -27,6 +32,10 @@ import androidx.fragment.app.Fragment
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.ChipDeviceInfoFragmentBinding
import com.google.chip.chiptool.util.FragmentUtil
import java.net.URLEncoder
import matter.onboardingpayload.OnboardingPayload
import matter.onboardingpayload.OnboardingPayloadException
import matter.onboardingpayload.QRCodeOnboardingPayloadGenerator

/** Show the [CHIPDeviceInfo]. */
class CHIPDeviceDetailsFragment : Fragment() {
Expand All @@ -35,6 +44,8 @@ class CHIPDeviceDetailsFragment : Fragment() {
private val binding
get() = _binding!!

private lateinit var onBoardingPayload: OnboardingPayload

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -43,17 +54,13 @@ class CHIPDeviceDetailsFragment : Fragment() {
_binding = ChipDeviceInfoFragmentBinding.inflate(inflater, container, false)
deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO))

binding.versionTv.text = "${deviceInfo.version}"
binding.vendorIdTv.text = "${deviceInfo.vendorId}"
binding.productIdTv.text = "${deviceInfo.productId}"
binding.setupCodeTv.text = "${deviceInfo.setupPinCode}"
binding.discriminatorTv.text = "${deviceInfo.discriminator}"
binding.discoveryCapabilitiesTv.text =
requireContext()
.getString(
R.string.chip_device_info_discovery_capabilities_text,
deviceInfo.discoveryCapabilities
)
binding.versionEd.setText(deviceInfo.version.toString())
binding.vendorIdEd.setText(deviceInfo.vendorId.toString())
binding.productIdEd.setText(deviceInfo.productId.toString())
binding.setupCodeEd.setText(deviceInfo.setupPinCode.toString())
binding.discriminatorEd.setText(deviceInfo.discriminator.toString())
binding.serialNumberEd.setText(deviceInfo.serialNumber)
binding.discoveryCapabilitiesTv.text = "${deviceInfo.discoveryCapabilities}"

if (deviceInfo.optionalQrCodeInfoMap.isEmpty()) {
binding.vendorTagsLabelTv.visibility = View.GONE
Expand All @@ -70,7 +77,7 @@ class CHIPDeviceDetailsFragment : Fragment() {
}
}

binding.commissioningFlowTv.text = "${deviceInfo.commissioningFlow}"
binding.commissioningFlowEd.setText(deviceInfo.commissioningFlow.toString())

// commissioningFlow = 2 (Custom), read device info from Ledger
if (deviceInfo.commissioningFlow == 2) {
Expand All @@ -81,6 +88,59 @@ class CHIPDeviceDetailsFragment : Fragment() {
}
}

onBoardingPayload = deviceInfo.toSetupPayload()
binding.qrCodeTv.text =
QRCodeOnboardingPayloadGenerator(onBoardingPayload)
.payloadBase38RepresentationWithAutoTLVBuffer()

binding.discoveryCapabilitiesEd.setText(onBoardingPayload.getRendezvousInformation().toString())
binding.discoveryCapabilitiesEd.addTextChangedListener(
object : TextWatcher {
override fun onTextChanged(value: CharSequence?, p1: Int, p2: Int, p3: Int) {
if (value.isNullOrEmpty()) {
return
}
onBoardingPayload.setRendezvousInformation(value.toString().toLong())
binding.discoveryCapabilitiesTv.text = "${onBoardingPayload.discoveryCapabilities}"
}

override fun afterTextChanged(p0: Editable?) {}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
}
)
binding.showQRCodeBtn.setOnClickListener {
onBoardingPayload.apply {
version = binding.versionEd.text.toString().toInt()
vendorId = binding.vendorIdEd.text.toString().toInt()
productId = binding.productIdEd.text.toString().toInt()
commissioningFlow = binding.commissioningFlowEd.text.toString().toInt()
setupPinCode = binding.setupCodeEd.text.toString().toLong()
discriminator = binding.discriminatorEd.text.toString().toInt()
val serialNumber = binding.serialNumberEd.text.toString()
try {
removeSerialNumber()
} catch (e: OnboardingPayloadException) {
Log.d(TAG, "Serial Number not set!")
}
if (serialNumber.isNotEmpty()) {
addSerialNumber(binding.serialNumberEd.text.toString())
}
}
val qrCode =
QRCodeOnboardingPayloadGenerator(onBoardingPayload)
.payloadBase38RepresentationWithAutoTLVBuffer()
Log.d(TAG, "QR Code : $qrCode")
binding.qrCodeTv.text = qrCode
}

binding.showQRCodeUriBtn.setOnClickListener {
val qrCodeEncode = URLEncoder.encode(binding.qrCodeTv.text.toString(), "UTF-8")
val qrCodeUri = getString(R.string.chip_device_info_qrcode_uri, qrCodeEncode)
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(qrCodeUri))
startActivity(intent)
}

return binding.root
}

Expand All @@ -96,6 +156,7 @@ class CHIPDeviceDetailsFragment : Fragment() {
}

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

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import matter.onboardingpayload.DiscoveryCapability
import matter.onboardingpayload.OnboardingPayload
import matter.onboardingpayload.OnboardingPayloadException

/** Class to hold the CHIP device information. */
@Parcelize
Expand All @@ -35,12 +36,37 @@ data class CHIPDeviceInfo(
val optionalQrCodeInfoMap: Map<Int, QrCodeInfo> = mapOf(),
val discoveryCapabilities: MutableSet<DiscoveryCapability> = mutableSetOf(),
val isShortDiscriminator: Boolean = false,
val serialNumber: String = "",
val ipAddress: String? = null,
val port: Int = 5540
) : Parcelable {

fun toSetupPayload(): OnboardingPayload {
val onboardingPayload =
OnboardingPayload(
version,
vendorId,
productId,
commissioningFlow,
discoveryCapabilities,
discriminator,
isShortDiscriminator,
setupPinCode
)
if (serialNumber.isNotEmpty()) {
onboardingPayload.addSerialNumber(serialNumber)
}
return onboardingPayload
}

companion object {
fun fromSetupPayload(setupPayload: OnboardingPayload): CHIPDeviceInfo {
val serialNumber =
try {
setupPayload.getSerialNumber()
} catch (e: OnboardingPayloadException) {
""
}
return CHIPDeviceInfo(
setupPayload.version,
setupPayload.vendorId,
Expand All @@ -52,7 +78,8 @@ data class CHIPDeviceInfo(
QrCodeInfo(info.tag, info.type, info.data, info.int32)
},
setupPayload.discoveryCapabilities,
setupPayload.hasShortDiscriminator
setupPayload.hasShortDiscriminator,
serialNumber
)
}
}
Expand Down
Loading

0 comments on commit e62d12e

Please sign in to comment.