/* * 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; import android.app.AppGlobals; import android.content.pm.IPackageManager; import android.os.Binder; import android.os.ShellCommand; import java.io.PrintWriter; /** * Interprets and executes 'adb shell cmd wifi [args]'. * * To add new commands: * - onCommand: Add a case "" execute. Return a 0 * if command executed successfully. * - onHelp: add a description string. * * If additional state objects are necessary add them to the * constructor. * * Permissions: currently root permission is required for all * commands. If the requirement needs to be relaxed then modify * the onCommand method to check for specific permissions on * individual commands. */ public class WifiShellCommand extends ShellCommand { private final WifiStateMachine mStateMachine; private final IPackageManager mPM; WifiShellCommand(WifiStateMachine stateMachine) { mStateMachine = stateMachine; mPM = AppGlobals.getPackageManager(); } @Override public int onCommand(String cmd) { checkRootPermission(); final PrintWriter pw = getOutPrintWriter(); try { switch (cmd != null ? cmd : "") { case "set-ipreach-disconnect": { boolean enabled; String nextArg = getNextArgRequired(); if ("enabled".equals(nextArg)) { enabled = true; } else if ("disabled".equals(nextArg)) { enabled = false; } else { pw.println( "Invalid argument to 'set-ipreach-disconnect' - must be 'enabled'" + " or 'disabled'"); return -1; } mStateMachine.setIpReachabilityDisconnectEnabled(enabled); return 0; } case "get-ipreach-disconnect": pw.println("IPREACH_DISCONNECT state is " + mStateMachine.getIpReachabilityDisconnectEnabled()); return 0; case "set-poll-rssi-interval-msecs": int newPollIntervalMsecs; try { newPollIntervalMsecs = Integer.parseInt(getNextArgRequired()); } catch (NumberFormatException e) { pw.println( "Invalid argument to 'set-poll-rssi-interval-msecs' " + "- must be a positive integer"); return -1; } if (newPollIntervalMsecs < 1) { pw.println( "Invalid argument to 'set-poll-rssi-interval-msecs' " + "- must be a positive integer"); return -1; } mStateMachine.setPollRssiIntervalMsecs(newPollIntervalMsecs); return 0; case "get-poll-rssi-interval-msecs": pw.println("WifiStateMachine.mPollRssiIntervalMsecs = " + mStateMachine.getPollRssiIntervalMsecs()); return 0; default: 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 wifi commands"); } @Override public void onHelp() { final PrintWriter pw = getOutPrintWriter(); pw.println("Wi-Fi (wifi) commands:"); pw.println(" help"); pw.println(" Print this help text."); pw.println(" set-ipreach-disconnect enabled|disabled"); pw.println(" Sets whether CMD_IP_REACHABILITY_LOST events should trigger disconnects."); pw.println(" get-ipreach-disconnect"); pw.println(" Gets setting of CMD_IP_REACHABILITY_LOST events triggering disconnects."); pw.println(" set-poll-rssi-interval-msecs "); pw.println(" Sets the interval between RSSI polls to milliseconds."); pw.println(" get-poll-rssi-interval-msecs"); pw.println(" Gets current interval between RSSI polls, in milliseconds."); pw.println(); } }