Skip to content

Commit

Permalink
[Android] Add initial batch command support (#32326)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Mar 5, 2024
1 parent e929072 commit c8fa478
Show file tree
Hide file tree
Showing 24 changed files with 1,242 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/java-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ jobs:
--tool-cluster "im" \
--tool-args "onnetwork-long-im-invoke --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
--factoryreset \
'
- name: Run IM Batch Invoke Test
run: |
scripts/run_in_python_env.sh out/venv \
'./scripts/tests/run_java_test.py \
--app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
--app-args "--discriminator 3840 --interface-id -1" \
--tool-path out/linux-x64-java-matter-controller \
--tool-cluster "im" \
--tool-args "onnetwork-long-im-batch-invoke --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
--factoryreset \
'
- name: Run IM Read Test
run: |
Expand Down
1 change: 1 addition & 0 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ kotlin_binary("java-matter-controller") {
"java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImExtendableInvokeCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImInvokeCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImReadCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private fun getImCommands(
PairOnNetworkLongImSubscribeCommand(controller, credentialsIssuer),
PairOnNetworkLongImWriteCommand(controller, credentialsIssuer),
PairOnNetworkLongImInvokeCommand(controller, credentialsIssuer),
PairOnNetworkLongImExtendableInvokeCommand(controller, credentialsIssuer),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.matter.controller.commands.pairing

import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ExtendableInvokeCallback
import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCallback
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.InvokeResponseData
import chip.devicecontroller.model.NoInvokeResponseData
import chip.devicecontroller.model.Status
import com.matter.controller.commands.common.CredentialsIssuer
import java.util.logging.Level
import java.util.logging.Logger
import kotlin.UShort
import matter.tlv.AnonymousTag
import matter.tlv.ContextSpecificTag
import matter.tlv.TlvWriter

class PairOnNetworkLongImExtendableInvokeCommand(
controller: ChipDeviceController,
credsIssue: CredentialsIssuer?
) :
PairingCommand(
controller,
"onnetwork-long-im-batch-invoke",
credsIssue,
PairingModeType.ON_NETWORK,
PairingNetworkType.NONE,
DiscoveryFilterType.LONG_DISCRIMINATOR
) {
private var devicePointer: Long = 0

private fun setDevicePointer(devicePointer: Long) {
this.devicePointer = devicePointer
}

private inner class InternalInvokeCallback : ExtendableInvokeCallback {
private var responseCount = 0

override fun onError(e: Exception) {
logger.log(Level.INFO, "Batch Invoke receive onError" + e.message)
setFailure("invoke failure")
}

override fun onResponse(invokeResponseData: InvokeResponseData) {
logger.log(Level.INFO, "Batch Invoke receive OnResponse on $invokeResponseData")
val clusterId = invokeResponseData.getClusterId().getId()
val commandId = invokeResponseData.getCommandId().getId()
val tlvData = invokeResponseData.getTlvByteArray()
val jsonData = invokeResponseData.getJsonString()
val status = invokeResponseData.getStatus()

if (clusterId == CLUSTER_ID_IDENTIFY && commandId == IDENTIFY_COMMAND) {
if (tlvData != null || jsonData != null) {
setFailure("invoke failure with problematic payload")
}
if (
status != null && status.status != Status.Code.Success && status.clusterStatus.isPresent()
) {
setFailure("invoke failure with incorrect status")
}
}

if (clusterId == CLUSTER_ID_TEST && commandId == TEST_ADD_ARGUMENT_RSP_COMMAND) {
if (tlvData == null || jsonData == null) {
setFailure("invoke failure with problematic payload")
}

if (!jsonData.equals("""{"0:UINT":2}""")) {
setFailure("invoke failure with problematic json")
}

if (status != null) {
setFailure("invoke failure with incorrect status")
}
}
responseCount++
}

override fun onNoResponse(noInvokeResponseData: NoInvokeResponseData) {
logger.log(Level.INFO, "Batch Invoke receive onNoResponse on $noInvokeResponseData")
}

override fun onDone() {
if (responseCount == TEST_COMMONDS_NUM) {
setSuccess()
} else {
setFailure("invoke failure")
}
}
}

private inner class InternalGetConnectedDeviceCallback : GetConnectedDeviceCallback {
override fun onDeviceConnected(devicePointer: Long) {
setDevicePointer(devicePointer)
logger.log(Level.INFO, "onDeviceConnected")
}

override fun onConnectionFailure(nodeId: Long, error: Exception) {
logger.log(Level.INFO, "onConnectionFailure")
}
}

override fun runCommand() {
val number: UShort = 1u
val tlvWriter1 = TlvWriter()
tlvWriter1.startStructure(AnonymousTag)
tlvWriter1.put(ContextSpecificTag(0), number)
tlvWriter1.endStructure()

val element1: InvokeElement =
InvokeElement.newInstance(
/* endpointId= */ 0,
CLUSTER_ID_IDENTIFY,
IDENTIFY_COMMAND,
tlvWriter1.getEncoded(),
null
)

val tlvWriter2 = TlvWriter()
tlvWriter2.startStructure(AnonymousTag)
tlvWriter2.put(ContextSpecificTag(0), number)
tlvWriter2.put(ContextSpecificTag(1), number)
tlvWriter2.endStructure()

val element2: InvokeElement =
InvokeElement.newInstance(
/* endpointId= */ 1,
CLUSTER_ID_TEST,
TEST_ADD_ARGUMENT_COMMAND,
tlvWriter2.getEncoded(),
null
)

val invokeList = listOf(element1, element2)
currentCommissioner()
.pairDeviceWithAddress(
getNodeId(),
getRemoteAddr().address.hostAddress,
MATTER_PORT,
getDiscriminator(),
getSetupPINCode(),
null
)
currentCommissioner().setCompletionListener(this)
waitCompleteMs(getTimeoutMillis())
currentCommissioner()
.getConnectedDevicePointer(getNodeId(), InternalGetConnectedDeviceCallback())
clear()
currentCommissioner()
.extendableInvoke(InternalInvokeCallback(), devicePointer, invokeList, 0, 0)
waitCompleteMs(getTimeoutMillis())
}

companion object {
private val logger =
Logger.getLogger(PairOnNetworkLongImExtendableInvokeCommand::class.java.name)

private const val MATTER_PORT = 5540
private const val CLUSTER_ID_IDENTIFY = 0x0003L
private const val IDENTIFY_COMMAND = 0L
private const val CLUSTER_ID_TEST = 0xFFF1FC05L
private const val TEST_ADD_ARGUMENT_COMMAND = 0X04L
private const val TEST_ADD_ARGUMENT_RSP_COMMAND = 0X01L
private const val TEST_COMMONDS_NUM = 2
}
}
1 change: 1 addition & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ style:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImInvokeCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImExtendableInvokeCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImWriteCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt"
Expand Down
13 changes: 13 additions & 0 deletions scripts/tests/java/im_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def TestCmdOnnetworkLongImInvoke(self, nodeid, setuppin, discriminator, timeout)
DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
return java_process.wait()

def TestCmdOnnetworkLongImExtendableInvoke(self, nodeid, setuppin, discriminator, timeout):
java_command = self.command + ['im', 'onnetwork-long-im-batch-invoke', nodeid, setuppin, discriminator, timeout]
logging.info(f"Execute: {java_command}")
java_process = subprocess.Popen(
java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
return java_process.wait()

def TestCmdOnnetworkLongImWrite(self, nodeid, setuppin, discriminator, timeout):
java_command = self.command + ['im', 'onnetwork-long-im-write', nodeid, setuppin, discriminator, timeout]
logging.info(f"Execute: {java_command}")
Expand Down Expand Up @@ -101,6 +109,11 @@ def RunTest(self):
code = self.TestCmdOnnetworkLongImInvoke(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
if code != 0:
raise Exception(f"Testing pairing onnetwork-long-im-invoke failed with error {code}")
elif self.command_name == 'onnetwork-long-im-batch-invoke':
logging.info("Testing pairing onnetwork-long-im-batch-invoke")
code = self.TestCmdOnnetworkLongImExtendableInvoke(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
if code != 0:
raise Exception(f"Testing pairing onnetwork-long-im-batch-invoke failed with error {code}")
elif self.command_name == 'onnetwork-long-im-write':
logging.info("Testing pairing onnetwork-long-im-write")
code = self.TestCmdOnnetworkLongImWrite(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
Expand Down
11 changes: 11 additions & 0 deletions src/controller/java/AndroidCallbacks-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ JNI_METHOD(void, InvokeCallbackJni, deleteCallback)(JNIEnv * env, jobject self,
{
deleteInvokeCallback(env, self, callbackHandle);
}

JNI_METHOD(jlong, ExtendableInvokeCallbackJni, newCallback)
(JNIEnv * env, jobject self)
{
return newExtendableInvokeCallback(env, self);
}

JNI_METHOD(void, ExtendableInvokeCallbackJni, deleteCallback)(JNIEnv * env, jobject self, jlong callbackHandle)
{
deleteExtendableInvokeCallback(env, self, callbackHandle);
}
Loading

0 comments on commit c8fa478

Please sign in to comment.