blob: 41eef69602f905401c5206db5d875b0c0ea2a37e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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.android.server.wifi.aware;
import android.os.Binder;
import android.os.ShellCommand;
import android.text.TextUtils;
import android.util.Log;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
/**
* Interprets and executes 'adb shell cmd wifiaware [args]'.
*/
public class WifiAwareShellCommand extends ShellCommand {
private static final String TAG = "WifiAwareShellCommand";
private Map<String, DelegatedShellCommand> mDelegatedCommands = new HashMap<>();
/**
* Register an delegated command interpreter for the specified 'command'. Each class can
* interpret and execute their own commands.
*/
public void register(String command, DelegatedShellCommand shellCommand) {
if (mDelegatedCommands.containsKey(command)) {
Log.e(TAG, "register: overwriting existing command -- '" + command + "'");
}
mDelegatedCommands.put(command, shellCommand);
}
@Override
public int onCommand(String cmd) {
checkRootPermission();
final PrintWriter pw = getErrPrintWriter();
try {
if ("reset".equals(cmd)) {
for (DelegatedShellCommand dsc: mDelegatedCommands.values()) {
dsc.onReset();
}
} else {
DelegatedShellCommand delegatedCmd = null;
if (!TextUtils.isEmpty(cmd)) {
delegatedCmd = mDelegatedCommands.get(cmd);
}
if (delegatedCmd != null) {
return delegatedCmd.onCommand(this);
} else {
return handleDefaultCommands(cmd);
}
}
} catch (Exception e) {
pw.println("Exception: " + e);
}
return -1;
}
private void checkRootPermission() {
final int uid = Binder.getCallingUid();
if (uid == 0) {
// Root can do anything.
return;
}
throw new SecurityException("Uid " + uid + " does not have access to wifiaware commands");
}
@Override
public void onHelp() {
final PrintWriter pw = getOutPrintWriter();
pw.println("Wi-Fi Aware (wifiaware) commands:");
pw.println(" help");
pw.println(" Print this help text.");
pw.println(" reset");
pw.println(" Reset parameters to default values.");
for (Map.Entry<String, DelegatedShellCommand> sce: mDelegatedCommands.entrySet()) {
sce.getValue().onHelp(sce.getKey(), this);
}
pw.println();
}
/**
* Interface that delegated command targets must implement. They are passed the parent shell
* command (the real command interpreter) from which they can obtain arguments.
*/
public interface DelegatedShellCommand {
/**
* Execute the specified command. Use the parent shell to obtain arguments. Note that the
* first argument (which specified the delegated shell) has already been extracted.
*/
int onCommand(ShellCommand parentShell);
/**
* Reset all parameters to their default values.
*/
void onReset();
/**
* Print out help for the delegated command. The name of the delegated command is passed
* as a first argument as an assist (prevents hard-coding of that string in multiple
* places).
*/
void onHelp(String command, ShellCommand parentShell);
}
}
|