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