Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix device inspection to properly show all endpoints. #94

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions app/src/main/java/com/google/homesampleapp/chip/ClustersHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ClustersHelper @Inject constructor(private val chipClient: ChipClient) {
// Convenience functions

/** Fetches MatterDeviceInfo for each endpoint supported by the device. */
suspend fun fetchDeviceMatterInfo(nodeId: Long, endpoint: Int): List<DeviceMatterInfo> {
suspend fun fetchDeviceMatterInfo(nodeId: Long): List<DeviceMatterInfo> {
Timber.d("fetchDeviceMatterInfo(): nodeId [${nodeId}]")
val matterDeviceInfoList = arrayListOf<DeviceMatterInfo>()
val connectedDevicePtr =
Expand All @@ -54,10 +54,46 @@ class ClustersHelper @Inject constructor(private val chipClient: ChipClient) {
Timber.e("Can't get connectedDevicePointer.")
return emptyList()
}
fetchDeviceMatterInfo(nodeId, connectedDevicePtr, 0, matterDeviceInfoList)
return matterDeviceInfoList
}

val partsListAttribute = readDescriptorClusterPartsListAttribute(connectedDevicePtr, endpoint)
/** Fetches MatterDeviceInfo for a specific endpoint. */
suspend fun fetchDeviceMatterInfo(
nodeId: Long,
connectedDevicePtr: Long,
endpointInt: Int,
matterDeviceInfoList: ArrayList<DeviceMatterInfo>
) {
Timber.d("fetchDeviceMatterInfo(): nodeId [${nodeId}] endpoint [$endpointInt]")

val partsListAttribute =
readDescriptorClusterPartsListAttribute(connectedDevicePtr, endpointInt)
Timber.d("partsListAttribute [${partsListAttribute}]")

// DeviceListAttribute
val deviceListAttribute =
readDescriptorClusterDeviceListAttribute(connectedDevicePtr, endpointInt)
val types = arrayListOf<Long>()
deviceListAttribute.forEach { types.add(it.deviceType) }

// ServerListAttribute
val serverListAttribute =
readDescriptorClusterServerListAttribute(connectedDevicePtr, endpointInt)
val serverClusters = arrayListOf<Any>()
serverListAttribute.forEach { serverClusters.add(it) }

// ClientListAttribute
val clientListAttribute =
readDescriptorClusterClientListAttribute(connectedDevicePtr, endpointInt)
val clientClusters = arrayListOf<Any>()
clientListAttribute.forEach { clientClusters.add(it) }

// Build the DeviceMatterInfo
val deviceMatterInfo = DeviceMatterInfo(endpointInt, types, serverClusters, clientClusters)
matterDeviceInfoList.add(deviceMatterInfo)

// Recursive call for the parts supported by the endpoint.
// For each part (endpoint)
partsListAttribute?.forEach { part ->
Timber.d("part [$part] is [${part.javaClass}]")
Expand All @@ -67,30 +103,8 @@ class ClustersHelper @Inject constructor(private val chipClient: ChipClient) {
else -> return@forEach
}
Timber.d("Processing part [$part]")

// DeviceListAttribute
val deviceListAttribute =
readDescriptorClusterDeviceListAttribute(connectedDevicePtr, endpointInt)
val types = arrayListOf<Long>()
deviceListAttribute.forEach { types.add(it.deviceType) }

// ServerListAttribute
val serverListAttribute =
readDescriptorClusterServerListAttribute(connectedDevicePtr, endpointInt)
val serverClusters = arrayListOf<Any>()
serverListAttribute.forEach { serverClusters.add(it) }

// ClientListAttribute
val clientListAttribute =
readDescriptorClusterClientListAttribute(connectedDevicePtr, endpointInt)
val clientClusters = arrayListOf<Any>()
clientListAttribute.forEach { clientClusters.add(it) }

// Build the DeviceMatterInfo
val deviceMatterInfo = DeviceMatterInfo(endpointInt, types, serverClusters, clientClusters)
matterDeviceInfoList.add(deviceMatterInfo)
fetchDeviceMatterInfo(nodeId, connectedDevicePtr, endpointInt, matterDeviceInfoList)
}
return matterDeviceInfoList
}

// -----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -298,7 +312,10 @@ class ClustersHelper @Inject constructor(private val chipClient: ChipClient) {
}
}

private fun getBasicClusterForDevice(devicePtr: Long, endpoint: Int): ChipClusters.ApplicationBasicCluster {
private fun getBasicClusterForDevice(
devicePtr: Long,
endpoint: Int
): ChipClusters.ApplicationBasicCluster {
return ChipClusters.ApplicationBasicCluster(devicePtr, endpoint)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,27 @@ constructor(
// Introspect the device and update its deviceType.
// TODO: Need to get capabilities information and store that in the devices repository.
// (e.g on/off on which endpoint).
val deviceMatterInfoList = clustersHelper.fetchDeviceMatterInfo(deviceId, 0)
val deviceMatterInfoList = clustersHelper.fetchDeviceMatterInfo(deviceId)
Timber.d("*** MATTER DEVICE INFO ***")
deviceMatterInfoList.forEachIndexed { index, deviceMatterInfo ->
Timber.d("Processing [[${index}] ${deviceMatterInfo}]")
if (index == 0) {
var gotDeviceType = false
deviceMatterInfoList.forEach { deviceMatterInfo ->
Timber.d("Processing endpoint [$deviceMatterInfo.endpoint]")
// Endpoint 0 is the Root Node, so we disregard it.
if (deviceMatterInfo.endpoint != 0) {
if (gotDeviceType) {
// TODO: Handle this properly once we have specific examples to learn from.
Timber.w(
"The device has more than one endpoint. We're simply using the first one to define the device type.")
return@forEach
}
if (deviceMatterInfo.types.size > 1) {
// TODO: Handle this properly
Timber.w("The device has more than one type. We're simply using the first one.")
// TODO: Handle this properly once we have specific examples to learn from.
Timber.w(
"The endpoint has more than one type. We're simply using the first one to define the device type.")
}
devicesRepository.updateDeviceType(
deviceId, convertToAppDeviceType(deviceMatterInfo.types.first()))
gotDeviceType = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,13 @@ class InspectViewModel @Inject constructor(private val clustersHelper: ClustersH
viewModelScope.launch {
try {
// Introspect the device.
val deviceMatterInfoList = clustersHelper.fetchDeviceMatterInfo(deviceId, 0)
val deviceMatterInfoList = clustersHelper.fetchDeviceMatterInfo(deviceId)
_instrospectionInfo.postValue(deviceMatterInfoList)
for (deviceMatterInfo in deviceMatterInfoList) {
Timber.d("[${deviceMatterInfo}]")
val deviceMatterInfoListEndpoint =
clustersHelper.fetchDeviceMatterInfo(deviceId, deviceMatterInfo.endpoint)
if (deviceMatterInfoListEndpoint.isEmpty()) {
Timber.d("No parts in ${deviceMatterInfo.endpoint}")
} else {
for (deviceMatterInfoEndpoint in deviceMatterInfoListEndpoint) {
Timber.d(
"*** FIXME: MATTER DEVICE INFO ENDPOINT [${deviceMatterInfoEndpoint.endpoint}***")
Timber.d("[${deviceMatterInfoEndpoint}]")
}
if (deviceMatterInfoList.isEmpty()) {
Timber.d("deviceMatterInfoList is empty")
} else {
for (deviceMatterInfo in deviceMatterInfoList) {
Timber.d("[${deviceMatterInfo}]")
}
}
} catch (e: Exception) {
Expand Down