Skip to content

Commit 6a20783

Browse files
authored
[java-matter-controller] Add the initial version of command parser (#23242)
* [java-matter-controller] Add the initial version of command parser * Address review comments * Address round two review comments * Address round three review comments * Address round four review comments
1 parent 3518760 commit 6a20783

13 files changed

+746
-5
lines changed

examples/java-matter-controller/BUILD.gn

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ android_binary("java-matter-controller") {
2929
"${chip_root}/third_party/java_deps:annotation",
3030
]
3131

32-
sources = [ "java/src/com/matter/controller/Main.java" ]
32+
sources = [
33+
"java/src/com/matter/controller/Main.java",
34+
"java/src/com/matter/controller/Off.java",
35+
"java/src/com/matter/controller/On.java",
36+
"java/src/com/matter/controller/commands/common/Argument.java",
37+
"java/src/com/matter/controller/commands/common/ArgumentType.java",
38+
"java/src/com/matter/controller/commands/common/Command.java",
39+
"java/src/com/matter/controller/commands/common/CredentialsIssuer.java",
40+
"java/src/com/matter/controller/commands/common/IPAddress.java",
41+
"java/src/com/matter/controller/commands/common/MatterCommand.java",
42+
"java/src/com/matter/controller/config/PersistentStorage.java",
43+
"java/src/com/matter/controller/config/PersistentStorageOpCertStore.java",
44+
"java/src/com/matter/controller/config/PersistentStorageOperationalKeystore.java",
45+
]
3346

3447
javac_flags = [ "-Xlint:deprecation" ]
3548
}

examples/java-matter-controller/java/src/com/matter/controller/Main.java

+66-4
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,85 @@
1515
* limitations under the License.
1616
*
1717
*/
18+
1819
package com.matter.controller;
1920

2021
import chip.devicecontroller.ChipDeviceController;
2122
import chip.devicecontroller.ControllerParams;
23+
import com.matter.controller.commands.common.Command;
24+
import com.matter.controller.commands.common.CredentialsIssuer;
25+
import java.util.Arrays;
2226

2327
public class Main {
28+
private static void ShowUsage(Command[] commands) {
29+
StringBuffer arguments = new StringBuffer();
30+
StringBuffer attributes = new StringBuffer();
31+
32+
for (Command command : commands) {
33+
arguments.append(" ");
34+
arguments.append(command.getName());
35+
36+
int argumentsCount = command.getArgumentsCount();
37+
for (int j = 0; j < argumentsCount; j++) {
38+
arguments.append(" ");
39+
arguments.append(command.getArgumentName(j));
40+
}
41+
42+
arguments.append("\n");
43+
44+
if ("read".equals(command.getName()) && command.getAttribute().isPresent()) {
45+
attributes.append(" " + command.getAttribute().get() + "\n");
46+
}
47+
}
48+
49+
System.out.println(
50+
String.format(
51+
"Usage: \n"
52+
+ " java_matter_controller command [params]\n\n"
53+
+ " Supported commands and their parameters:\n%s\n"
54+
+ " Supported attribute names for the 'read' command:\n%s",
55+
arguments, attributes));
56+
}
57+
58+
private static void runCommand(CredentialsIssuer credIssuerCmds, String[] args) {
59+
60+
// TODO::Start list of available commands, this hard coded list need to be replaced by command
61+
// registration mechanism.
62+
Command[] commands = {new On(credIssuerCmds), new Off(credIssuerCmds)};
63+
// End list of available commands
64+
65+
if (args.length == 0) {
66+
ShowUsage(commands);
67+
return;
68+
}
69+
70+
for (Command cmd : commands) {
71+
if (cmd.getName().equals(args[0])) {
72+
String[] temp = Arrays.copyOfRange(args, 1, args.length);
73+
74+
try {
75+
cmd.initArguments(args.length - 1, temp);
76+
cmd.run();
77+
} catch (IllegalArgumentException e) {
78+
System.out.println("Arguments init failed with exception: " + e.getMessage());
79+
} catch (Exception e) {
80+
System.out.println("Run command failed with exception: " + e.getMessage());
81+
}
82+
break;
83+
}
84+
}
85+
}
86+
2487
public static void main(String[] args) {
2588
ChipDeviceController controller =
2689
new ChipDeviceController(
2790
ControllerParams.newBuilder()
2891
.setUdpListenPort(0)
2992
.setControllerVendorId(0xFFF1)
3093
.build());
31-
System.out.println("Hello Matter Controller!");
3294

33-
for (String s : args) {
34-
System.out.println(s);
35-
}
95+
CredentialsIssuer credentialsIssuer = new CredentialsIssuer();
96+
97+
runCommand(credentialsIssuer, args);
3698
}
3799
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022 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+
19+
package com.matter.controller;
20+
21+
import com.matter.controller.commands.common.CredentialsIssuer;
22+
import com.matter.controller.commands.common.MatterCommand;
23+
import java.util.concurrent.atomic.AtomicLong;
24+
25+
public final class Off extends MatterCommand {
26+
private final AtomicLong mNodeId = new AtomicLong();
27+
private final AtomicLong mFabricId = new AtomicLong();
28+
29+
public Off(CredentialsIssuer credIssuerCmds) {
30+
super("off", credIssuerCmds);
31+
addArgument("nodeid", 0L, Long.MAX_VALUE, mNodeId, null);
32+
addArgument("fabricid", 0L, Long.MAX_VALUE, mFabricId, null);
33+
}
34+
35+
@Override
36+
protected final void runCommand() {}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022 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+
19+
package com.matter.controller;
20+
21+
import com.matter.controller.commands.common.CredentialsIssuer;
22+
import com.matter.controller.commands.common.MatterCommand;
23+
import java.util.concurrent.atomic.AtomicLong;
24+
25+
public final class On extends MatterCommand {
26+
private final AtomicLong mNodeId = new AtomicLong();
27+
private final AtomicLong mFabricId = new AtomicLong();
28+
29+
public On(CredentialsIssuer credIssuerCmds) {
30+
super("on", credIssuerCmds);
31+
addArgument("nodeid", 0, Long.MAX_VALUE, mNodeId, null);
32+
addArgument("fabricid", 0, Long.MAX_VALUE, mFabricId, null);
33+
}
34+
35+
@Override
36+
protected final void runCommand() {}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2022 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+
19+
package com.matter.controller.commands.common;
20+
21+
import java.net.InetAddress;
22+
import java.net.UnknownHostException;
23+
import java.util.Optional;
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
import javax.annotation.Nullable;
28+
29+
public final class Argument {
30+
private final String mName;
31+
private final ArgumentType mType;
32+
private final long mMin;
33+
private final long mMax;
34+
private final Object mValue;
35+
private final Optional<String> mDesc;
36+
37+
public Argument(String name, IPAddress value) {
38+
this.mName = name;
39+
this.mType = ArgumentType.ADDRESS;
40+
this.mMin = 0;
41+
this.mMax = 0;
42+
this.mValue = value;
43+
this.mDesc = Optional.empty();
44+
}
45+
46+
public Argument(String name, StringBuffer value, @Nullable String desc) {
47+
this.mName = name;
48+
this.mType = ArgumentType.STRING;
49+
this.mMin = 0;
50+
this.mMax = 0;
51+
this.mValue = value;
52+
this.mDesc = Optional.ofNullable(desc);
53+
}
54+
55+
public Argument(String name, AtomicBoolean value, @Nullable String desc) {
56+
this.mName = name;
57+
this.mType = ArgumentType.BOOL;
58+
this.mMin = 0;
59+
this.mMax = 0;
60+
this.mValue = value;
61+
this.mDesc = Optional.ofNullable(desc);
62+
}
63+
64+
public Argument(String name, short min, short max, AtomicInteger value, @Nullable String desc) {
65+
this.mName = name;
66+
this.mType = ArgumentType.NUMBER_INT16;
67+
this.mMin = min;
68+
this.mMax = max;
69+
this.mValue = value;
70+
this.mDesc = Optional.ofNullable(desc);
71+
}
72+
73+
public Argument(String name, int min, int max, AtomicInteger value, @Nullable String desc) {
74+
this.mName = name;
75+
this.mType = ArgumentType.NUMBER_INT32;
76+
this.mMin = min;
77+
this.mMax = max;
78+
this.mValue = value;
79+
this.mDesc = Optional.ofNullable(desc);
80+
}
81+
82+
public Argument(String name, long min, long max, AtomicLong value, @Nullable String desc) {
83+
this.mName = name;
84+
this.mType = ArgumentType.NUMBER_INT64;
85+
this.mMin = min;
86+
this.mMax = max;
87+
this.mValue = value;
88+
this.mDesc = Optional.ofNullable(desc);
89+
}
90+
91+
public String getName() {
92+
return mName;
93+
}
94+
95+
public ArgumentType getType() {
96+
return mType;
97+
}
98+
99+
public Object getValue() {
100+
return mValue;
101+
}
102+
103+
public Optional<String> getDesc() {
104+
return mDesc;
105+
}
106+
107+
public void setValue(String value) {
108+
boolean isValidArgument = false;
109+
110+
switch (mType) {
111+
case ATTRIBUTE:
112+
String str = (String) mValue;
113+
isValidArgument = value.equals(str);
114+
break;
115+
case NUMBER_INT32:
116+
AtomicInteger num = (AtomicInteger) mValue;
117+
num.set(Integer.parseInt(value));
118+
isValidArgument = (num.intValue() >= mMin && num.intValue() <= mMax);
119+
break;
120+
case ADDRESS:
121+
try {
122+
IPAddress ipAddress = (IPAddress) mValue;
123+
ipAddress.setAddress(InetAddress.getByName(value));
124+
} catch (UnknownHostException e) {
125+
isValidArgument = true;
126+
}
127+
break;
128+
}
129+
130+
if (!isValidArgument) {
131+
throw new IllegalArgumentException("Invalid argument " + mName + ": " + value);
132+
}
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022 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+
19+
package com.matter.controller.commands.common;
20+
21+
public enum ArgumentType {
22+
NUMBER_INT8,
23+
NUMBER_INT16,
24+
NUMBER_INT32,
25+
NUMBER_INT64,
26+
FLOAT,
27+
DOUBLE,
28+
BOOL,
29+
STRING,
30+
CHARSTRING,
31+
OCTETSTRING,
32+
ATTRIBUTE,
33+
ADDRESS,
34+
COMPLEX,
35+
CUSTOM,
36+
VECTOR_BOOL,
37+
VECTOR16,
38+
VECTOR32,
39+
VECTOR_CUSTOM,
40+
}

0 commit comments

Comments
 (0)