|
| 1 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.espressif.matter |
| 16 | + |
| 17 | +import android.util.Log |
| 18 | +import chip.devicecontroller.ChipClusters |
| 19 | +import chip.devicecontroller.ChipStructs |
| 20 | +import chip.devicecontroller.ChipStructs.AccessControlClusterAccessControlEntryStruct |
| 21 | +import kotlinx.coroutines.GlobalScope |
| 22 | +import kotlinx.coroutines.future.future |
| 23 | +import java.util.concurrent.CompletableFuture |
| 24 | +import kotlin.coroutines.resume |
| 25 | +import kotlin.coroutines.resumeWithException |
| 26 | +import kotlin.coroutines.suspendCoroutine |
| 27 | + |
| 28 | +class AccessControlClusterHelper constructor(private val chipClient: ChipClient) { |
| 29 | + |
| 30 | + companion object { |
| 31 | + const val TAG = "AccessControlClusterHelper" |
| 32 | + } |
| 33 | + |
| 34 | + suspend fun readAclAttribute( |
| 35 | + deviceId: Long, |
| 36 | + endpoint: Int |
| 37 | + ): MutableList<ChipStructs.AccessControlClusterAccessControlEntryStruct>? { |
| 38 | + Log.d(TAG, "readAclAttribute : $deviceId") |
| 39 | + val connectedDevicePtr = |
| 40 | + try { |
| 41 | + chipClient.getConnectedDevicePointer(deviceId) |
| 42 | + } catch (e: IllegalStateException) { |
| 43 | + Log.e(TAG, "Can't get connectedDevicePointer.") |
| 44 | + return null |
| 45 | + } |
| 46 | + return suspendCoroutine { continuation -> |
| 47 | + getAccessControlClusterForDevice(connectedDevicePtr, endpoint) |
| 48 | + .readAclAttribute( |
| 49 | + object : ChipClusters.AccessControlCluster.AclAttributeCallback { |
| 50 | + |
| 51 | + override fun onSuccess(valueList: MutableList<ChipStructs.AccessControlClusterAccessControlEntryStruct>?) { |
| 52 | + Log.d(TAG, "readAclAttribute success: [$valueList]") |
| 53 | + continuation.resume(valueList) |
| 54 | + } |
| 55 | + |
| 56 | + override fun onError(ex: Exception) { |
| 57 | + Log.e(TAG, "readAclAttribute command failure") |
| 58 | + continuation.resumeWithException(ex) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fun readAclAttributeAsync( |
| 65 | + deviceId: Long, |
| 66 | + endpoint: Int |
| 67 | + ): CompletableFuture<MutableList<ChipStructs.AccessControlClusterAccessControlEntryStruct>?> = |
| 68 | + GlobalScope.future { readAclAttribute(deviceId, endpoint) } |
| 69 | + |
| 70 | + suspend fun writeAclAttribute( |
| 71 | + deviceId: Long, |
| 72 | + endpoint: Int, |
| 73 | + entries: ArrayList<AccessControlClusterAccessControlEntryStruct> |
| 74 | + ) { |
| 75 | + Log.d(TAG, "writeAclAttribute : $deviceId") |
| 76 | + val connectedDevicePtr = |
| 77 | + try { |
| 78 | + chipClient.getConnectedDevicePointer(deviceId) |
| 79 | + } catch (e: IllegalStateException) { |
| 80 | + Log.e(TAG, "Can't get connectedDevicePointer.") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + return suspendCoroutine { continuation -> |
| 85 | + getAccessControlClusterForDevice(connectedDevicePtr, endpoint) |
| 86 | + .writeAclAttribute( |
| 87 | + object : ChipClusters.DefaultClusterCallback { |
| 88 | + |
| 89 | + override fun onSuccess() { |
| 90 | + Log.d(TAG, "writeAclAttribute success") |
| 91 | + continuation.resume(Unit) |
| 92 | + } |
| 93 | + |
| 94 | + override fun onError(ex: Exception) { |
| 95 | + Log.e(TAG, "writeAclAttribute command failure") |
| 96 | + continuation.resumeWithException(ex) |
| 97 | + } |
| 98 | + }, entries |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fun writeAclAttributeAsync( |
| 104 | + deviceId: Long, |
| 105 | + endpoint: Int, |
| 106 | + level: ArrayList<ChipStructs.AccessControlClusterAccessControlEntryStruct> |
| 107 | + ): CompletableFuture<Unit> = |
| 108 | + GlobalScope.future { writeAclAttribute(deviceId, endpoint, level) } |
| 109 | + |
| 110 | + private fun getAccessControlClusterForDevice( |
| 111 | + devicePtr: Long, |
| 112 | + endpoint: Int |
| 113 | + ): ChipClusters.AccessControlCluster { |
| 114 | + return ChipClusters.AccessControlCluster(devicePtr, endpoint) |
| 115 | + } |
| 116 | +} |
0 commit comments