Skip to content

Commit

Permalink
Add TLV to Object API
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Aug 14, 2023
1 parent b0b0d58 commit 1fd97b2
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import chip.devicecontroller.model.AttributeWriteRequest
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.NodeState
import chip.jsontlv.toAny
import chip.tlv.AnonymousTag
import chip.tlv.TlvReader
import chip.tlv.TlvWriter
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.BasicClientFragmentBinding
import com.google.chip.chiptool.util.TlvParseUtil
import java.util.Optional
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -55,7 +58,7 @@ class BasicClientFragment : Fragment() {
// TODO : Need to be implement poj-to-tlv
sendWriteAttribute(
BasicInformation.Attribute.NodeLabel,
TlvParseUtil.encode(binding.nodeLabelEd.text.toString())
TlvWriter().put(AnonymousTag, binding.nodeLabelEd.text.toString()).getEncoded()
)
binding.nodeLabelEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}
Expand All @@ -65,7 +68,7 @@ class BasicClientFragment : Fragment() {
// TODO : Need to be implement poj-to-tlv
sendWriteAttribute(
BasicInformation.Attribute.Location,
TlvParseUtil.encode(binding.locationEd.text.toString())
TlvWriter().put(AnonymousTag, binding.locationEd.text.toString()).getEncoded()
)
binding.locationEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}
Expand All @@ -75,7 +78,7 @@ class BasicClientFragment : Fragment() {
// TODO : Need to be implement poj-to-tlv
sendWriteAttribute(
BasicInformation.Attribute.LocalConfigDisabled,
TlvParseUtil.encode(isChecked)
TlvWriter().put(AnonymousTag, isChecked).getEncoded()
)
}
}
Expand Down Expand Up @@ -150,13 +153,13 @@ class BasicClientFragment : Fragment() {
}

override fun onReport(nodeState: NodeState?) {
val value =
val tlv =
nodeState
?.getEndpointState(endpointId)
?.getClusterState(clusterId)
?.getAttributeState(attributeId)
?.value
?: "null"
?.tlv
val value = tlv?.let { TlvReader(it).toAny() }
Log.i(TAG, "[Read Success] $attributeName: $value")
showMessage("[Read Success] $attributeName: $value")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.jsontlv.toAny
import chip.tlv.AnonymousTag
import chip.tlv.TlvReader
import chip.tlv.TlvWriter
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
Expand Down Expand Up @@ -209,13 +211,13 @@ class MultiAdminClientFragment : Fragment() {
deviceController.readAttributePath(
object : ReportCallback {
override fun onReport(nodeState: NodeState?) {
val value =
val tlv =
nodeState
?.getEndpointState(endpointId)
?.getClusterState(clusterId)
?.getAttributeState(attributeId)
?.value
?: "null"
?.tlv
val value = tlv?.let { TlvReader(it).toAny() }
Log.i(TAG, "read $attributeName: $value")
showMessage("read $attributeName: $value")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.jsontlv.toAny
import chip.tlv.AnonymousTag
import chip.tlv.ContextSpecificTag
import chip.tlv.TlvReader
import chip.tlv.TlvWriter
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.OnOffClientFragmentBinding
import com.google.chip.chiptool.util.TlvParseUtil
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
Expand Down Expand Up @@ -116,13 +117,13 @@ class OnOffClientFragment : Fragment() {
}

override fun onReport(nodeState: NodeState?) {
val value =
val tlv =
nodeState
?.getEndpointState(endpointId)
?.getClusterState(clusterId)
?.getAttributeState(attributeId)
?.value
?: "null"
?.tlv
val value = tlv?.let { TlvReader(it).toAny() }
Log.v(TAG, "On/Off attribute value: $value")
showMessage("On/Off attribute value: $value")
}
Expand Down Expand Up @@ -201,7 +202,7 @@ class OnOffClientFragment : Fragment() {
?.tlv
?: return
// TODO : Need to be implement poj-to-tlv
val value = TlvParseUtil.decodeBoolean(tlv)
val value = TlvReader(tlv).getBool(AnonymousTag)
val formatter = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
val time = formatter.format(Calendar.getInstance(Locale.getDefault()).time)
val message = "Subscribed on/off value at $time: ${if (value) "ON" else "OFF"}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipStructs
import chip.devicecontroller.ChipTLVValueDecoder
import chip.devicecontroller.ClusterIDMapping.OperationalCredentials
import chip.devicecontroller.InvokeCallback
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.jsontlv.toAny
import chip.tlv.AnonymousTag
import chip.tlv.ContextSpecificTag
import chip.tlv.TlvReader
import chip.tlv.TlvWriter
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
Expand Down Expand Up @@ -115,44 +115,16 @@ class OpCredClientFragment : Fragment() {
}

override fun onReport(nodeState: NodeState?) {
val value =
nodeState
?.getEndpointState(endpointId)
?.getClusterState(clusterId)
?.getAttributeState(attributeId)
?.value
?: "null"
val tlv =
nodeState
?.getEndpointState(endpointId)
?.getClusterState(clusterId)
?.getAttributeState(attributeId)
?.tlv

if (tlv == null) {
Log.i(TAG, "OpCred $attributeName value: $value")
showMessage("OpCred $attributeName value: $value")
return
}

val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)
when (attribute) {
OperationalCredentials.Attribute.Fabrics -> {
val ret =
ChipTLVValueDecoder.decodeAttributeValue<
List<ChipStructs.OperationalCredentialsClusterFabricDescriptorStruct>
>(
attributePath,
tlv
)
Log.i(TAG, "OpCred $attributeName value: $value")
showMessage(ret.toString())
}
else -> {
Log.i(TAG, "OpCred $attributeName value: $value")
showMessage("OpCred $attributeName value: $value")
}
}
val value = tlv?.let { TlvReader(it).toAny() }
Log.i(TAG, "OpCred $attributeName value: $value")
showMessage("OpCred $attributeName value: $value")
}
},
devicePtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import chip.devicecontroller.ReportCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.NodeState
import chip.tlv.AnonymousTag
import chip.tlv.TlvReader
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.SensorClientFragmentBinding
import com.google.chip.chiptool.util.DeviceIdUtil
import com.google.chip.chiptool.util.TlvParseUtil
import com.jjoe64.graphview.LabelFormatter
import com.jjoe64.graphview.Viewport
import com.jjoe64.graphview.series.DataPoint
Expand Down Expand Up @@ -224,7 +225,7 @@ class SensorClientFragment : Fragment() {
// TODO : Need to be implement poj-to-tlv
val value =
try {
TlvParseUtil.decodeInt(tlv)
TlvReader(tlv).getInt(AnonymousTag)
} catch (ex: Exception) {
showMessage(R.string.sensor_client_read_error_text, "value is null")
return
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def copyToSrcAndroid(self):
"CHIPController.jar": "src/controller/java/CHIPController.jar",
"OnboardingPayload.jar": "src/controller/java/OnboardingPayload.jar",
"AndroidPlatform.jar": "src/platform/android/AndroidPlatform.jar",
"libCHIPJson.jar": "src/controller/java/libCHIPJson.jar",
"libCHIPTlv.jar": "src/controller/java/libCHIPTlv.jar",
"CHIPClusters.jar": "src/controller/java/CHIPClusters.jar",
"CHIPClusterID.jar": "src/controller/java/CHIPClusterID.jar",
Expand Down
1 change: 1 addition & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ android_library("java") {
deps = [
":chipcluster",
":chipclusterID",
":jsontlv",
":tlv",
"${chip_root}/third_party/java_deps:annotation",
]
Expand Down
17 changes: 14 additions & 3 deletions src/controller/java/src/chip/jsontlv/JsonToTlv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ import java.util.Base64
* @param json string representing Json encoded data to be converted into TLV format
* @throws IllegalArgumentException if the data was invalid
*/
fun TlvWriter.fromJsonString(json: String): ByteArray {
validateIsJsonObjectAndConvert(JsonParser.parseString(json), AnonymousTag)
return validateTlv().getEncoded()
fun TlvWriter.fromJsonStringToByteArray(json: String): ByteArray {
return putJsonString(AnonymousTag, json).validateTlv().getEncoded()
}

/**
* Converts Json string into TLV writer object.
*
* @param tag the TLV tag to be encoded.
* @param json String representing Json to be converted to TLV.
* @throws IllegalArgumentException if the data was invalid
*/
fun TlvWriter.putJsonString(tag: Tag, json: String): TlvWriter {
validateIsJsonObjectAndConvert(JsonParser.parseString(json), tag)
return this
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/controller/java/src/chip/jsontlv/TlvToJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ fun TlvReader.toJsonString(): String {
}
return getStructJson().toString()
}

/**
* Encodes TLV into kotlin Object. If the TLV reader is positioned TLV Structure, Object will return
* to json format.
*/
fun TlvReader.toAny(tag: Tag = AnonymousTag): Any? {
val element = peekElement()
val value = element.value
value.toAny()?.let {
skipElement()
return it
}
return when (value) {
is ArrayValue -> {
buildList {
enterArray(tag)
while (!isEndOfContainer()) {
add(toAny())
}
exitContainer()
}
}
is StructureValue -> {
skipElement()
getStructJson().toString()
}
else -> {
null
}
}
}
/**
* Encodes TLV Structure into Json Object. The TLV reader should be positioned at the start of a TLV
* Structure (StructureValue element). After this call the TLV reader is positioned at the end of
Expand Down
Loading

0 comments on commit 1fd97b2

Please sign in to comment.