Merge "Require command and argument separation, cleanup." into jb-mr2-dev

This commit is contained in:
Jeff Sharkey
2013-02-25 19:13:00 +00:00
committed by Android (Google) Code Review
2 changed files with 26 additions and 72 deletions

View File

@ -206,18 +206,19 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
/** /**
* Make command for daemon, escaping arguments as needed. * Make command for daemon, escaping arguments as needed.
*/ */
private void makeCommand(StringBuilder builder, String cmd, Object... args) private void makeCommand(StringBuilder builder, String cmd, Object... args) {
throws NativeDaemonConnectorException {
// TODO: eventually enforce that cmd doesn't contain arguments
if (cmd.indexOf('\0') >= 0) { if (cmd.indexOf('\0') >= 0) {
throw new IllegalArgumentException("unexpected command: " + cmd); throw new IllegalArgumentException("Unexpected command: " + cmd);
}
if (cmd.indexOf(' ') >= 0) {
throw new IllegalArgumentException("Arguments must be separate from command");
} }
builder.append(cmd); builder.append(cmd);
for (Object arg : args) { for (Object arg : args) {
final String argString = String.valueOf(arg); final String argString = String.valueOf(arg);
if (argString.indexOf('\0') >= 0) { if (argString.indexOf('\0') >= 0) {
throw new IllegalArgumentException("unexpected argument: " + arg); throw new IllegalArgumentException("Unexpected argument: " + arg);
} }
builder.append(' '); builder.append(' ');
@ -240,7 +241,8 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
/** /**
* Issue the given command to the native daemon and return a single expected * Issue the given command to the native daemon and return a single expected
* response. * response. Any arguments must be separated from base command so they can
* be properly escaped.
* *
* @throws NativeDaemonConnectorException when problem communicating with * @throws NativeDaemonConnectorException when problem communicating with
* native daemon, or if the response matches * native daemon, or if the response matches
@ -274,7 +276,8 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
/** /**
* Issue the given command to the native daemon and return any * Issue the given command to the native daemon and return any
* {@link NativeDaemonEvent#isClassContinue()} responses, including the * {@link NativeDaemonEvent#isClassContinue()} responses, including the
* final terminal response. * final terminal response. Any arguments must be separated from base
* command so they can be properly escaped.
* *
* @throws NativeDaemonConnectorException when problem communicating with * @throws NativeDaemonConnectorException when problem communicating with
* native daemon, or if the response matches * native daemon, or if the response matches
@ -287,10 +290,11 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
} }
/** /**
* Issue the given command to the native daemon and return any * Issue the given command to the native daemon and return any {@linke
* {@linke NativeDaemonEvent@isClassContinue()} responses, including the * NativeDaemonEvent@isClassContinue()} responses, including the final
* final terminal response. Note that the timeout does not count time in * terminal response. Note that the timeout does not count time in deep
* deep sleep. * sleep. Any arguments must be separated from base command so they can be
* properly escaped.
* *
* @throws NativeDaemonConnectorException when problem communicating with * @throws NativeDaemonConnectorException when problem communicating with
* native daemon, or if the response matches * native daemon, or if the response matches
@ -352,51 +356,6 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
return events.toArray(new NativeDaemonEvent[events.size()]); return events.toArray(new NativeDaemonEvent[events.size()]);
} }
/**
* Issue a command to the native daemon and return the raw responses.
*
* @deprecated callers should move to {@link #execute(String, Object...)}
* which returns parsed {@link NativeDaemonEvent}.
*/
@Deprecated
public ArrayList<String> doCommand(String cmd) throws NativeDaemonConnectorException {
final ArrayList<String> rawEvents = Lists.newArrayList();
final NativeDaemonEvent[] events = executeForList(cmd);
for (NativeDaemonEvent event : events) {
rawEvents.add(event.getRawEvent());
}
return rawEvents;
}
/**
* Issues a list command and returns the cooked list of all
* {@link NativeDaemonEvent#getMessage()} which match requested code.
*/
@Deprecated
public String[] doListCommand(String cmd, int expectedCode)
throws NativeDaemonConnectorException {
final ArrayList<String> list = Lists.newArrayList();
final NativeDaemonEvent[] events = executeForList(cmd);
for (int i = 0; i < events.length - 1; i++) {
final NativeDaemonEvent event = events[i];
final int code = event.getCode();
if (code == expectedCode) {
list.add(event.getMessage());
} else {
throw new NativeDaemonConnectorException(
"unexpected list response " + code + " instead of " + expectedCode);
}
}
final NativeDaemonEvent finalEvent = events[events.length - 1];
if (!finalEvent.isClassOk()) {
throw new NativeDaemonConnectorException("unexpected final event: " + finalEvent);
}
return list.toArray(new String[list.size()]);
}
/** /**
* Append the given argument to {@link StringBuilder}, escaping as needed, * Append the given argument to {@link StringBuilder}, escaping as needed,
* and surrounding with quotes when it contains spaces. * and surrounding with quotes when it contains spaces.
@ -444,7 +403,8 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
} }
/** /**
* Command builder that handles argument list building. * Command builder that handles argument list building. Any arguments must
* be separated from base command so they can be properly escaped.
*/ */
public static class Command { public static class Command {
private String mCmd; private String mCmd;

View File

@ -25,8 +25,6 @@ import static android.net.NetworkStats.UID_ALL;
import static android.net.TrafficStats.UID_TETHERING; import static android.net.TrafficStats.UID_TETHERING;
import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult; import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult; import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceRxThrottleResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceTxThrottleResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult; import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult; import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult; import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
@ -121,8 +119,6 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public static final int SoftapStatusResult = 214; public static final int SoftapStatusResult = 214;
public static final int InterfaceRxCounterResult = 216; public static final int InterfaceRxCounterResult = 216;
public static final int InterfaceTxCounterResult = 217; public static final int InterfaceTxCounterResult = 217;
public static final int InterfaceRxThrottleResult = 218;
public static final int InterfaceTxThrottleResult = 219;
public static final int QuotaCounterResult = 220; public static final int QuotaCounterResult = 220;
public static final int TetheringStatsResult = 221; public static final int TetheringStatsResult = 221;
public static final int DnsProxyQueryResult = 222; public static final int DnsProxyQueryResult = 222;
@ -836,31 +832,28 @@ public class NetworkManagementService extends INetworkManagementService.Stub
} }
// TODO(BT) Remove // TODO(BT) Remove
public void startReverseTethering(String iface) @Override
throws IllegalStateException { public void startReverseTethering(String iface) {
if (DBG) Slog.d(TAG, "startReverseTethering in");
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
// cmd is "tether start first_start first_stop second_start second_stop ..." // cmd is "tether start first_start first_stop second_start second_stop ..."
// an odd number of addrs will fail // an odd number of addrs will fail
String cmd = "tether start-reverse";
cmd += " " + iface;
if (DBG) Slog.d(TAG, "startReverseTethering cmd: " + cmd);
try { try {
mConnector.doCommand(cmd); mConnector.execute("tether", "start-reverse", iface);
} catch (NativeDaemonConnectorException e) { } catch (NativeDaemonConnectorException e) {
throw new IllegalStateException("Unable to communicate to native daemon"); throw e.rethrowAsParcelableException();
} }
BluetoothTetheringDataTracker.getInstance().startReverseTether(iface); BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
} }
// TODO(BT) Remove // TODO(BT) Remove
public void stopReverseTethering() throws IllegalStateException { @Override
public void stopReverseTethering() {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try { try {
mConnector.doCommand("tether stop-reverse"); mConnector.execute("tether", "stop-reverse");
} catch (NativeDaemonConnectorException e) { } catch (NativeDaemonConnectorException e) {
throw new IllegalStateException("Unable to communicate to native daemon to stop tether"); throw e.rethrowAsParcelableException();
} }
BluetoothTetheringDataTracker.getInstance().stopReverseTether(); BluetoothTetheringDataTracker.getInstance().stopReverseTether();
} }
@ -1506,6 +1499,7 @@ public class NetworkManagementService extends INetworkManagementService.Stub
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override
public void monitor() { public void monitor() {
if (mConnector != null) { if (mConnector != null) {
mConnector.monitor(); mConnector.monitor();