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

[java-matter-controller] Add the initial version of command parser #23242

Merged
merged 5 commits into from
Nov 3, 2022
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
15 changes: 14 additions & 1 deletion examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ android_binary("java-matter-controller") {
"${chip_root}/third_party/java_deps:annotation",
]

sources = [ "java/src/com/matter/controller/Main.java" ]
sources = [
"java/src/com/matter/controller/Main.java",
"java/src/com/matter/controller/Off.java",
"java/src/com/matter/controller/On.java",
"java/src/com/matter/controller/commands/common/Argument.java",
"java/src/com/matter/controller/commands/common/ArgumentType.java",
"java/src/com/matter/controller/commands/common/Command.java",
"java/src/com/matter/controller/commands/common/CredentialsIssuer.java",
"java/src/com/matter/controller/commands/common/IPAddress.java",
"java/src/com/matter/controller/commands/common/MatterCommand.java",
"java/src/com/matter/controller/config/PersistentStorage.java",
"java/src/com/matter/controller/config/PersistentStorageOpCertStore.java",
"java/src/com/matter/controller/config/PersistentStorageOperationalKeystore.java",
]

javac_flags = [ "-Xlint:deprecation" ]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,85 @@
* limitations under the License.
*
*/

package com.matter.controller;

import chip.devicecontroller.ChipDeviceController;
import chip.devicecontroller.ControllerParams;
import com.matter.controller.commands.common.Command;
import com.matter.controller.commands.common.CredentialsIssuer;
import java.util.Arrays;

public class Main {
private static void ShowUsage(Command[] commands) {
StringBuffer arguments = new StringBuffer();
StringBuffer attributes = new StringBuffer();

for (Command command : commands) {
arguments.append(" ");
arguments.append(command.getName());

int argumentsCount = command.getArgumentsCount();
for (int j = 0; j < argumentsCount; j++) {
arguments.append(" ");
arguments.append(command.getArgumentName(j));
}

arguments.append("\n");

if ("read".equals(command.getName()) && command.getAttribute().isPresent()) {
attributes.append(" " + command.getAttribute().get() + "\n");
}
}

System.out.println(
String.format(
"Usage: \n"
+ " java_matter_controller command [params]\n\n"
+ " Supported commands and their parameters:\n%s\n"
+ " Supported attribute names for the 'read' command:\n%s",
arguments, attributes));
}

private static void runCommand(CredentialsIssuer credIssuerCmds, String[] args) {

// TODO::Start list of available commands, this hard coded list need to be replaced by command
// registration mechanism.
Command[] commands = {new On(credIssuerCmds), new Off(credIssuerCmds)};
// End list of available commands

if (args.length == 0) {
ShowUsage(commands);
return;
}

for (Command cmd : commands) {
if (cmd.getName().equals(args[0])) {
String[] temp = Arrays.copyOfRange(args, 1, args.length);

try {
cmd.initArguments(args.length - 1, temp);
cmd.run();
} catch (IllegalArgumentException e) {
System.out.println("Arguments init failed with exception: " + e.getMessage());
} catch (Exception e) {
System.out.println("Run command failed with exception: " + e.getMessage());
}
break;
}
}
}

public static void main(String[] args) {
ChipDeviceController controller =
new ChipDeviceController(
ControllerParams.newBuilder()
.setUdpListenPort(0)
.setControllerVendorId(0xFFF1)
.build());
System.out.println("Hello Matter Controller!");

for (String s : args) {
System.out.println(s);
}
CredentialsIssuer credentialsIssuer = new CredentialsIssuer();

runCommand(credentialsIssuer, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 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;

import com.matter.controller.commands.common.CredentialsIssuer;
import com.matter.controller.commands.common.MatterCommand;
import java.util.concurrent.atomic.AtomicLong;

public final class Off extends MatterCommand {
private final AtomicLong mNodeId = new AtomicLong();
private final AtomicLong mFabricId = new AtomicLong();

public Off(CredentialsIssuer credIssuerCmds) {
super("off", credIssuerCmds);
addArgument("nodeid", 0L, Long.MAX_VALUE, mNodeId, null);
addArgument("fabricid", 0L, Long.MAX_VALUE, mFabricId, null);
}

@Override
protected final void runCommand() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 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;

import com.matter.controller.commands.common.CredentialsIssuer;
import com.matter.controller.commands.common.MatterCommand;
import java.util.concurrent.atomic.AtomicLong;

public final class On extends MatterCommand {
private final AtomicLong mNodeId = new AtomicLong();
private final AtomicLong mFabricId = new AtomicLong();

public On(CredentialsIssuer credIssuerCmds) {
super("on", credIssuerCmds);
addArgument("nodeid", 0, Long.MAX_VALUE, mNodeId, null);
addArgument("fabricid", 0, Long.MAX_VALUE, mFabricId, null);
}

@Override
protected final void runCommand() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2022 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.common;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;

public final class Argument {
private final String mName;
private final ArgumentType mType;
private final long mMin;
private final long mMax;
private final Object mValue;
private final Optional<String> mDesc;

public Argument(String name, IPAddress value) {
this.mName = name;
this.mType = ArgumentType.ADDRESS;
this.mMin = 0;
this.mMax = 0;
this.mValue = value;
this.mDesc = Optional.empty();
}

public Argument(String name, StringBuffer value, @Nullable String desc) {
this.mName = name;
this.mType = ArgumentType.STRING;
this.mMin = 0;
this.mMax = 0;
this.mValue = value;
this.mDesc = Optional.ofNullable(desc);
}

public Argument(String name, AtomicBoolean value, @Nullable String desc) {
this.mName = name;
this.mType = ArgumentType.BOOL;
this.mMin = 0;
this.mMax = 0;
this.mValue = value;
this.mDesc = Optional.ofNullable(desc);
}

public Argument(String name, short min, short max, AtomicInteger value, @Nullable String desc) {
this.mName = name;
this.mType = ArgumentType.NUMBER_INT16;
this.mMin = min;
this.mMax = max;
this.mValue = value;
this.mDesc = Optional.ofNullable(desc);
}

public Argument(String name, int min, int max, AtomicInteger value, @Nullable String desc) {
this.mName = name;
this.mType = ArgumentType.NUMBER_INT32;
this.mMin = min;
this.mMax = max;
this.mValue = value;
this.mDesc = Optional.ofNullable(desc);
}

public Argument(String name, long min, long max, AtomicLong value, @Nullable String desc) {
this.mName = name;
this.mType = ArgumentType.NUMBER_INT64;
this.mMin = min;
this.mMax = max;
this.mValue = value;
this.mDesc = Optional.ofNullable(desc);
}

public String getName() {
return mName;
}

public ArgumentType getType() {
return mType;
}

public Object getValue() {
return mValue;
}

public Optional<String> getDesc() {
return mDesc;
}

public void setValue(String value) {
boolean isValidArgument = false;

switch (mType) {
case ATTRIBUTE:
String str = (String) mValue;
isValidArgument = value.equals(str);
break;
case NUMBER_INT32:
AtomicInteger num = (AtomicInteger) mValue;
num.set(Integer.parseInt(value));
isValidArgument = (num.intValue() >= mMin && num.intValue() <= mMax);
break;
case ADDRESS:
try {
IPAddress ipAddress = (IPAddress) mValue;
ipAddress.setAddress(InetAddress.getByName(value));
} catch (UnknownHostException e) {
isValidArgument = true;
}
break;
}

if (!isValidArgument) {
throw new IllegalArgumentException("Invalid argument " + mName + ": " + value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 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.common;

public enum ArgumentType {
NUMBER_INT8,
NUMBER_INT16,
NUMBER_INT32,
NUMBER_INT64,
FLOAT,
DOUBLE,
BOOL,
STRING,
CHARSTRING,
OCTETSTRING,
ATTRIBUTE,
ADDRESS,
COMPLEX,
CUSTOM,
VECTOR_BOOL,
VECTOR16,
VECTOR32,
VECTOR_CUSTOM,
}
Loading