|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.matter.controller.commands.pairing |
| 19 | + |
| 20 | +import chip.devicecontroller.ChipDeviceController |
| 21 | +import chip.devicecontroller.ExtendableInvokeCallback |
| 22 | +import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCallback |
| 23 | +import chip.devicecontroller.model.InvokeElement |
| 24 | +import chip.devicecontroller.model.InvokeResponseData |
| 25 | +import chip.devicecontroller.model.NoInvokeResponseData |
| 26 | +import chip.devicecontroller.model.Status |
| 27 | +import com.matter.controller.commands.common.CredentialsIssuer |
| 28 | +import java.util.logging.Level |
| 29 | +import java.util.logging.Logger |
| 30 | +import kotlin.UShort |
| 31 | +import matter.tlv.AnonymousTag |
| 32 | +import matter.tlv.ContextSpecificTag |
| 33 | +import matter.tlv.TlvWriter |
| 34 | + |
| 35 | +class PairOnNetworkLongImExtendableInvokeCommand( |
| 36 | + controller: ChipDeviceController, |
| 37 | + credsIssue: CredentialsIssuer? |
| 38 | +) : |
| 39 | + PairingCommand( |
| 40 | + controller, |
| 41 | + "onnetwork-long-im-batch-invoke", |
| 42 | + credsIssue, |
| 43 | + PairingModeType.ON_NETWORK, |
| 44 | + PairingNetworkType.NONE, |
| 45 | + DiscoveryFilterType.LONG_DISCRIMINATOR |
| 46 | + ) { |
| 47 | + private var devicePointer: Long = 0 |
| 48 | + |
| 49 | + private fun setDevicePointer(devicePointer: Long) { |
| 50 | + this.devicePointer = devicePointer |
| 51 | + } |
| 52 | + |
| 53 | + private inner class InternalInvokeCallback : ExtendableInvokeCallback { |
| 54 | + private var responseCount = 0 |
| 55 | + |
| 56 | + override fun onError(e: Exception) { |
| 57 | + logger.log(Level.INFO, "Batch Invoke receive onError" + e.message) |
| 58 | + setFailure("invoke failure") |
| 59 | + } |
| 60 | + |
| 61 | + override fun onResponse(invokeResponseData: InvokeResponseData) { |
| 62 | + logger.log(Level.INFO, "Batch Invoke receive OnResponse on $invokeResponseData") |
| 63 | + val clusterId = invokeResponseData.getClusterId().getId() |
| 64 | + val commandId = invokeResponseData.getCommandId().getId() |
| 65 | + val tlvData = invokeResponseData.getTlvByteArray() |
| 66 | + val jsonData = invokeResponseData.getJsonString() |
| 67 | + val status = invokeResponseData.getStatus() |
| 68 | + |
| 69 | + if (clusterId == CLUSTER_ID_IDENTIFY && commandId == IDENTIFY_COMMAND) { |
| 70 | + if (tlvData != null || jsonData != null) { |
| 71 | + setFailure("invoke failure with problematic payload") |
| 72 | + } |
| 73 | + if ( |
| 74 | + status != null && status.status != Status.Code.Success && status.clusterStatus.isPresent() |
| 75 | + ) { |
| 76 | + setFailure("invoke failure with incorrect status") |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (clusterId == CLUSTER_ID_TEST && commandId == TEST_ADD_ARGUMENT_RSP_COMMAND) { |
| 81 | + if (tlvData == null || jsonData == null) { |
| 82 | + setFailure("invoke failure with problematic payload") |
| 83 | + } |
| 84 | + |
| 85 | + if (!jsonData.equals("""{"0:UINT":2}""")) { |
| 86 | + setFailure("invoke failure with problematic json") |
| 87 | + } |
| 88 | + |
| 89 | + if (status != null) { |
| 90 | + setFailure("invoke failure with incorrect status") |
| 91 | + } |
| 92 | + } |
| 93 | + responseCount++ |
| 94 | + } |
| 95 | + |
| 96 | + override fun onNoResponse(noInvokeResponseData: NoInvokeResponseData) { |
| 97 | + logger.log(Level.INFO, "Batch Invoke receive onNoResponse on $noInvokeResponseData") |
| 98 | + } |
| 99 | + |
| 100 | + override fun onDone() { |
| 101 | + if (responseCount == TEST_COMMONDS_NUM) { |
| 102 | + setSuccess() |
| 103 | + } else { |
| 104 | + setFailure("invoke failure") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private inner class InternalGetConnectedDeviceCallback : GetConnectedDeviceCallback { |
| 110 | + override fun onDeviceConnected(devicePointer: Long) { |
| 111 | + setDevicePointer(devicePointer) |
| 112 | + logger.log(Level.INFO, "onDeviceConnected") |
| 113 | + } |
| 114 | + |
| 115 | + override fun onConnectionFailure(nodeId: Long, error: Exception) { |
| 116 | + logger.log(Level.INFO, "onConnectionFailure") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + override fun runCommand() { |
| 121 | + val number: UShort = 1u |
| 122 | + val tlvWriter1 = TlvWriter() |
| 123 | + tlvWriter1.startStructure(AnonymousTag) |
| 124 | + tlvWriter1.put(ContextSpecificTag(0), number) |
| 125 | + tlvWriter1.endStructure() |
| 126 | + |
| 127 | + val element1: InvokeElement = |
| 128 | + InvokeElement.newInstance( |
| 129 | + /* endpointId= */ 0, |
| 130 | + CLUSTER_ID_IDENTIFY, |
| 131 | + IDENTIFY_COMMAND, |
| 132 | + tlvWriter1.getEncoded(), |
| 133 | + null |
| 134 | + ) |
| 135 | + |
| 136 | + val tlvWriter2 = TlvWriter() |
| 137 | + tlvWriter2.startStructure(AnonymousTag) |
| 138 | + tlvWriter2.put(ContextSpecificTag(0), number) |
| 139 | + tlvWriter2.put(ContextSpecificTag(1), number) |
| 140 | + tlvWriter2.endStructure() |
| 141 | + |
| 142 | + val element2: InvokeElement = |
| 143 | + InvokeElement.newInstance( |
| 144 | + /* endpointId= */ 1, |
| 145 | + CLUSTER_ID_TEST, |
| 146 | + TEST_ADD_ARGUMENT_COMMAND, |
| 147 | + tlvWriter2.getEncoded(), |
| 148 | + null |
| 149 | + ) |
| 150 | + |
| 151 | + val invokeList = listOf(element1, element2) |
| 152 | + currentCommissioner() |
| 153 | + .pairDeviceWithAddress( |
| 154 | + getNodeId(), |
| 155 | + getRemoteAddr().address.hostAddress, |
| 156 | + MATTER_PORT, |
| 157 | + getDiscriminator(), |
| 158 | + getSetupPINCode(), |
| 159 | + null |
| 160 | + ) |
| 161 | + currentCommissioner().setCompletionListener(this) |
| 162 | + waitCompleteMs(getTimeoutMillis()) |
| 163 | + currentCommissioner() |
| 164 | + .getConnectedDevicePointer(getNodeId(), InternalGetConnectedDeviceCallback()) |
| 165 | + clear() |
| 166 | + currentCommissioner() |
| 167 | + .extendableInvoke(InternalInvokeCallback(), devicePointer, invokeList, 0, 0) |
| 168 | + waitCompleteMs(getTimeoutMillis()) |
| 169 | + } |
| 170 | + |
| 171 | + companion object { |
| 172 | + private val logger = |
| 173 | + Logger.getLogger(PairOnNetworkLongImExtendableInvokeCommand::class.java.name) |
| 174 | + |
| 175 | + private const val MATTER_PORT = 5540 |
| 176 | + private const val CLUSTER_ID_IDENTIFY = 0x0003L |
| 177 | + private const val IDENTIFY_COMMAND = 0L |
| 178 | + private const val CLUSTER_ID_TEST = 0xFFF1FC05L |
| 179 | + private const val TEST_ADD_ARGUMENT_COMMAND = 0X04L |
| 180 | + private const val TEST_ADD_ARGUMENT_RSP_COMMAND = 0X01L |
| 181 | + private const val TEST_COMMONDS_NUM = 2 |
| 182 | + } |
| 183 | +} |
0 commit comments