San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.res.Resources; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.net.Uri; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 27 | import android.net.InterfaceConfiguration; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 28 | import android.net.INetworkManagementEventObserver; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 29 | import android.net.LinkAddress; |
| 30 | import android.net.NetworkUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 31 | import android.net.wifi.WifiConfiguration; |
| 32 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 33 | import android.os.INetworkManagementService; |
| 34 | import android.os.Handler; |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 35 | import android.os.SystemProperties; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 36 | import android.text.TextUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 37 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 38 | import android.util.Slog; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 39 | import java.util.ArrayList; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 40 | import java.util.NoSuchElementException; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 41 | import java.util.StringTokenizer; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 42 | import android.provider.Settings; |
| 43 | import android.content.ContentResolver; |
| 44 | import android.database.ContentObserver; |
| 45 | |
| 46 | import java.io.File; |
| 47 | import java.io.FileReader; |
| 48 | import java.lang.IllegalStateException; |
| 49 | |
| 50 | import java.net.InetAddress; |
| 51 | import java.net.UnknownHostException; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 52 | import java.util.concurrent.CountDownLatch; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * @hide |
| 56 | */ |
| 57 | class NetworkManagementService extends INetworkManagementService.Stub { |
| 58 | |
| 59 | private static final String TAG = "NetworkManagmentService"; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 60 | private static final boolean DBG = false; |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 61 | private static final String NETD_TAG = "NetdConnector"; |
| 62 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 63 | class NetdResponseCode { |
| 64 | public static final int InterfaceListResult = 110; |
| 65 | public static final int TetherInterfaceListResult = 111; |
| 66 | public static final int TetherDnsFwdTgtListResult = 112; |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 67 | public static final int TtyListResult = 113; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 68 | |
| 69 | public static final int TetherStatusResult = 210; |
| 70 | public static final int IpFwdStatusResult = 211; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 71 | public static final int InterfaceGetCfgResult = 213; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 72 | public static final int SoftapStatusResult = 214; |
| 73 | public static final int UsbRNDISStatusResult = 215; |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 74 | public static final int InterfaceRxCounterResult = 216; |
| 75 | public static final int InterfaceTxCounterResult = 217; |
| 76 | public static final int InterfaceRxThrottleResult = 218; |
| 77 | public static final int InterfaceTxThrottleResult = 219; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 78 | |
| 79 | public static final int InterfaceChange = 600; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Binder context for this service |
| 84 | */ |
| 85 | private Context mContext; |
| 86 | |
| 87 | /** |
| 88 | * connector object for communicating with netd |
| 89 | */ |
| 90 | private NativeDaemonConnector mConnector; |
| 91 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 92 | private Thread mThread; |
| 93 | private final CountDownLatch mConnectedSignal = new CountDownLatch(1); |
| 94 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 95 | private ArrayList<INetworkManagementEventObserver> mObservers; |
| 96 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Constructs a new NetworkManagementService instance |
| 99 | * |
| 100 | * @param context Binder context for this service |
| 101 | */ |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 102 | private NetworkManagementService(Context context) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 103 | mContext = context; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 104 | mObservers = new ArrayList<INetworkManagementEventObserver>(); |
| 105 | |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 106 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 107 | return; |
| 108 | } |
| 109 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 110 | mConnector = new NativeDaemonConnector( |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 111 | new NetdCallbackReceiver(), "netd", 10, NETD_TAG); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 112 | mThread = new Thread(mConnector, NETD_TAG); |
| 113 | } |
| 114 | |
| 115 | public static NetworkManagementService create(Context context) throws InterruptedException { |
| 116 | NetworkManagementService service = new NetworkManagementService(context); |
| 117 | if (DBG) Slog.d(TAG, "Creating NetworkManagementService"); |
| 118 | service.mThread.start(); |
| 119 | if (DBG) Slog.d(TAG, "Awaiting socket connection"); |
| 120 | service.mConnectedSignal.await(); |
| 121 | if (DBG) Slog.d(TAG, "Connected"); |
| 122 | return service; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 123 | } |
| 124 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 125 | public void registerObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 126 | Slog.d(TAG, "Registering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 127 | mObservers.add(obs); |
| 128 | } |
| 129 | |
| 130 | public void unregisterObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 131 | Slog.d(TAG, "Unregistering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 132 | mObservers.remove(mObservers.indexOf(obs)); |
| 133 | } |
| 134 | |
| 135 | /** |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 136 | * Notify our observers of an interface link status change |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 137 | */ |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 138 | private void notifyInterfaceLinkStatusChanged(String iface, boolean link) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 139 | for (INetworkManagementEventObserver obs : mObservers) { |
| 140 | try { |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 141 | obs.interfaceLinkStatusChanged(iface, link); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 142 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 143 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Notify our observers of an interface addition. |
| 150 | */ |
| 151 | private void notifyInterfaceAdded(String iface) { |
| 152 | for (INetworkManagementEventObserver obs : mObservers) { |
| 153 | try { |
| 154 | obs.interfaceAdded(iface); |
| 155 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 156 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Notify our observers of an interface removal. |
| 163 | */ |
| 164 | private void notifyInterfaceRemoved(String iface) { |
| 165 | for (INetworkManagementEventObserver obs : mObservers) { |
| 166 | try { |
| 167 | obs.interfaceRemoved(iface); |
| 168 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 169 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Let us know the daemon is connected |
| 176 | */ |
| 177 | protected void onConnected() { |
| 178 | if (DBG) Slog.d(TAG, "onConnected"); |
| 179 | mConnectedSignal.countDown(); |
| 180 | } |
| 181 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 182 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 183 | // |
| 184 | // Netd Callback handling |
| 185 | // |
| 186 | |
| 187 | class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks { |
| 188 | public void onDaemonConnected() { |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 189 | NetworkManagementService.this.onConnected(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 190 | new Thread() { |
| 191 | public void run() { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 192 | } |
| 193 | }.start(); |
| 194 | } |
| 195 | public boolean onEvent(int code, String raw, String[] cooked) { |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 196 | if (code == NetdResponseCode.InterfaceChange) { |
| 197 | /* |
| 198 | * a network interface change occured |
| 199 | * Format: "NNN Iface added <name>" |
| 200 | * "NNN Iface removed <name>" |
| 201 | * "NNN Iface changed <name> <up/down>" |
| 202 | */ |
| 203 | if (cooked.length < 4 || !cooked[1].equals("Iface")) { |
| 204 | throw new IllegalStateException( |
| 205 | String.format("Invalid event from daemon (%s)", raw)); |
| 206 | } |
| 207 | if (cooked[2].equals("added")) { |
| 208 | notifyInterfaceAdded(cooked[3]); |
| 209 | return true; |
| 210 | } else if (cooked[2].equals("removed")) { |
| 211 | notifyInterfaceRemoved(cooked[3]); |
| 212 | return true; |
| 213 | } else if (cooked[2].equals("changed") && cooked.length == 5) { |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 214 | notifyInterfaceLinkStatusChanged(cooked[3], cooked[4].equals("up")); |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | throw new IllegalStateException( |
| 218 | String.format("Invalid event from daemon (%s)", raw)); |
| 219 | } |
| 220 | return false; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 224 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 225 | // |
| 226 | // INetworkManagementService members |
| 227 | // |
| 228 | |
| 229 | public String[] listInterfaces() throws IllegalStateException { |
| 230 | mContext.enforceCallingOrSelfPermission( |
| 231 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 232 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 233 | try { |
| 234 | return mConnector.doListCommand("interface list", NetdResponseCode.InterfaceListResult); |
| 235 | } catch (NativeDaemonConnectorException e) { |
| 236 | throw new IllegalStateException( |
| 237 | "Cannot communicate with native daemon to list interfaces"); |
| 238 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | public InterfaceConfiguration getInterfaceConfig(String iface) throws IllegalStateException { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 242 | String rsp; |
| 243 | try { |
| 244 | rsp = mConnector.doCommand("interface getcfg " + iface).get(0); |
| 245 | } catch (NativeDaemonConnectorException e) { |
| 246 | throw new IllegalStateException( |
| 247 | "Cannot communicate with native daemon to get interface config"); |
| 248 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 249 | Slog.d(TAG, String.format("rsp <%s>", rsp)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 250 | |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 251 | // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz [flag1 flag2 flag3] |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 252 | StringTokenizer st = new StringTokenizer(rsp); |
| 253 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 254 | InterfaceConfiguration cfg; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 255 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 256 | try { |
| 257 | int code = Integer.parseInt(st.nextToken(" ")); |
| 258 | if (code != NetdResponseCode.InterfaceGetCfgResult) { |
| 259 | throw new IllegalStateException( |
| 260 | String.format("Expected code %d, but got %d", |
| 261 | NetdResponseCode.InterfaceGetCfgResult, code)); |
| 262 | } |
| 263 | } catch (NumberFormatException nfe) { |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 264 | throw new IllegalStateException( |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 265 | String.format("Invalid response from daemon (%s)", rsp)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 266 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 267 | |
| 268 | cfg = new InterfaceConfiguration(); |
| 269 | cfg.hwAddr = st.nextToken(" "); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 270 | InetAddress addr = null; |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 271 | int prefixLength = 0; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 272 | try { |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 273 | addr = InetAddress.getByName(st.nextToken(" ")); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 274 | } catch (UnknownHostException uhe) { |
| 275 | Slog.e(TAG, "Failed to parse ipaddr", uhe); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | try { |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 279 | prefixLength = Integer.parseInt(st.nextToken(" ")); |
| 280 | } catch (NumberFormatException nfe) { |
| 281 | Slog.e(TAG, "Failed to parse prefixLength", nfe); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 282 | } |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 283 | |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 284 | cfg.addr = new LinkAddress(addr, prefixLength); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 285 | cfg.interfaceFlags = st.nextToken("]").trim() +"]"; |
| 286 | } catch (NoSuchElementException nsee) { |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 287 | throw new IllegalStateException( |
| 288 | String.format("Invalid response from daemon (%s)", rsp)); |
| 289 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 290 | Slog.d(TAG, String.format("flags <%s>", cfg.interfaceFlags)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 291 | return cfg; |
| 292 | } |
| 293 | |
| 294 | public void setInterfaceConfig( |
| 295 | String iface, InterfaceConfiguration cfg) throws IllegalStateException { |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 296 | LinkAddress linkAddr = cfg.addr; |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 297 | if (linkAddr == null || linkAddr.getAddress() == null) { |
| 298 | throw new IllegalStateException("Null LinkAddress given"); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 299 | } |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame^] | 300 | String cmd = String.format("interface setcfg %s %s %d %s", iface, |
| 301 | linkAddr.getAddress().getHostAddress(), |
| 302 | linkAddr.getNetworkPrefixLength(), |
| 303 | cfg.interfaceFlags); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 304 | try { |
| 305 | mConnector.doCommand(cmd); |
| 306 | } catch (NativeDaemonConnectorException e) { |
| 307 | throw new IllegalStateException( |
Robert Greenwalt | 81d5ad5 | 2010-12-16 11:27:38 -0800 | [diff] [blame] | 308 | "Unable to communicate with native daemon to interface setcfg - " + e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 309 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | public void shutdown() { |
| 313 | if (mContext.checkCallingOrSelfPermission( |
| 314 | android.Manifest.permission.SHUTDOWN) |
| 315 | != PackageManager.PERMISSION_GRANTED) { |
| 316 | throw new SecurityException("Requires SHUTDOWN permission"); |
| 317 | } |
| 318 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 319 | Slog.d(TAG, "Shutting down"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
| 323 | mContext.enforceCallingOrSelfPermission( |
| 324 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 325 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 326 | ArrayList<String> rsp; |
| 327 | try { |
| 328 | rsp = mConnector.doCommand("ipfwd status"); |
| 329 | } catch (NativeDaemonConnectorException e) { |
| 330 | throw new IllegalStateException( |
| 331 | "Unable to communicate with native daemon to ipfwd status"); |
| 332 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 333 | |
| 334 | for (String line : rsp) { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 335 | String[] tok = line.split(" "); |
| 336 | if (tok.length < 3) { |
| 337 | Slog.e(TAG, "Malformed response from native daemon: " + line); |
| 338 | return false; |
| 339 | } |
| 340 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 341 | int code = Integer.parseInt(tok[0]); |
| 342 | if (code == NetdResponseCode.IpFwdStatusResult) { |
| 343 | // 211 Forwarding <enabled/disabled> |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 344 | return "enabled".equals(tok[2]); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 345 | } else { |
| 346 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 347 | } |
| 348 | } |
| 349 | throw new IllegalStateException("Got an empty response"); |
| 350 | } |
| 351 | |
| 352 | public void setIpForwardingEnabled(boolean enable) throws IllegalStateException { |
| 353 | mContext.enforceCallingOrSelfPermission( |
| 354 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 355 | mConnector.doCommand(String.format("ipfwd %sable", (enable ? "en" : "dis"))); |
| 356 | } |
| 357 | |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 358 | public void startTethering(String[] dhcpRange) |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 359 | throws IllegalStateException { |
| 360 | mContext.enforceCallingOrSelfPermission( |
| 361 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 362 | // cmd is "tether start first_start first_stop second_start second_stop ..." |
| 363 | // an odd number of addrs will fail |
| 364 | String cmd = "tether start"; |
| 365 | for (String d : dhcpRange) { |
| 366 | cmd += " " + d; |
| 367 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 368 | |
| 369 | try { |
| 370 | mConnector.doCommand(cmd); |
| 371 | } catch (NativeDaemonConnectorException e) { |
| 372 | throw new IllegalStateException("Unable to communicate to native daemon"); |
| 373 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | public void stopTethering() throws IllegalStateException { |
| 377 | mContext.enforceCallingOrSelfPermission( |
| 378 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 379 | try { |
| 380 | mConnector.doCommand("tether stop"); |
| 381 | } catch (NativeDaemonConnectorException e) { |
| 382 | throw new IllegalStateException("Unable to communicate to native daemon to stop tether"); |
| 383 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | public boolean isTetheringStarted() throws IllegalStateException { |
| 387 | mContext.enforceCallingOrSelfPermission( |
| 388 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 389 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 390 | ArrayList<String> rsp; |
| 391 | try { |
| 392 | rsp = mConnector.doCommand("tether status"); |
| 393 | } catch (NativeDaemonConnectorException e) { |
| 394 | throw new IllegalStateException( |
| 395 | "Unable to communicate to native daemon to get tether status"); |
| 396 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 397 | |
| 398 | for (String line : rsp) { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 399 | String[] tok = line.split(" "); |
| 400 | if (tok.length < 3) { |
| 401 | throw new IllegalStateException("Malformed response for tether status: " + line); |
| 402 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 403 | int code = Integer.parseInt(tok[0]); |
| 404 | if (code == NetdResponseCode.TetherStatusResult) { |
| 405 | // XXX: Tethering services <started/stopped> <TBD>... |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 406 | return "started".equals(tok[2]); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 407 | } else { |
| 408 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 409 | } |
| 410 | } |
| 411 | throw new IllegalStateException("Got an empty response"); |
| 412 | } |
| 413 | |
| 414 | public void tetherInterface(String iface) throws IllegalStateException { |
| 415 | mContext.enforceCallingOrSelfPermission( |
| 416 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 417 | try { |
| 418 | mConnector.doCommand("tether interface add " + iface); |
| 419 | } catch (NativeDaemonConnectorException e) { |
| 420 | throw new IllegalStateException( |
| 421 | "Unable to communicate to native daemon for adding tether interface"); |
| 422 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | public void untetherInterface(String iface) { |
| 426 | mContext.enforceCallingOrSelfPermission( |
| 427 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 428 | try { |
| 429 | mConnector.doCommand("tether interface remove " + iface); |
| 430 | } catch (NativeDaemonConnectorException e) { |
| 431 | throw new IllegalStateException( |
| 432 | "Unable to communicate to native daemon for removing tether interface"); |
| 433 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | public String[] listTetheredInterfaces() throws IllegalStateException { |
| 437 | mContext.enforceCallingOrSelfPermission( |
| 438 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 439 | try { |
| 440 | return mConnector.doListCommand( |
| 441 | "tether interface list", NetdResponseCode.TetherInterfaceListResult); |
| 442 | } catch (NativeDaemonConnectorException e) { |
| 443 | throw new IllegalStateException( |
| 444 | "Unable to communicate to native daemon for listing tether interfaces"); |
| 445 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | public void setDnsForwarders(String[] dns) throws IllegalStateException { |
| 449 | mContext.enforceCallingOrSelfPermission( |
| 450 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 451 | try { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 452 | String cmd = "tether dns set"; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 453 | for (String s : dns) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 454 | cmd += " " + InetAddress.getByName(s).getHostAddress(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 455 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 456 | try { |
| 457 | mConnector.doCommand(cmd); |
| 458 | } catch (NativeDaemonConnectorException e) { |
| 459 | throw new IllegalStateException( |
| 460 | "Unable to communicate to native daemon for setting tether dns"); |
| 461 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 462 | } catch (UnknownHostException e) { |
| 463 | throw new IllegalStateException("Error resolving dns name", e); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | public String[] getDnsForwarders() throws IllegalStateException { |
| 468 | mContext.enforceCallingOrSelfPermission( |
| 469 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 470 | try { |
| 471 | return mConnector.doListCommand( |
| 472 | "tether dns list", NetdResponseCode.TetherDnsFwdTgtListResult); |
| 473 | } catch (NativeDaemonConnectorException e) { |
| 474 | throw new IllegalStateException( |
| 475 | "Unable to communicate to native daemon for listing tether dns"); |
| 476 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | public void enableNat(String internalInterface, String externalInterface) |
| 480 | throws IllegalStateException { |
| 481 | mContext.enforceCallingOrSelfPermission( |
| 482 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 483 | try { |
| 484 | mConnector.doCommand( |
| 485 | String.format("nat enable %s %s", internalInterface, externalInterface)); |
| 486 | } catch (NativeDaemonConnectorException e) { |
| 487 | throw new IllegalStateException( |
| 488 | "Unable to communicate to native daemon for enabling NAT interface"); |
| 489 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | public void disableNat(String internalInterface, String externalInterface) |
| 493 | throws IllegalStateException { |
| 494 | mContext.enforceCallingOrSelfPermission( |
| 495 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 496 | try { |
| 497 | mConnector.doCommand( |
| 498 | String.format("nat disable %s %s", internalInterface, externalInterface)); |
| 499 | } catch (NativeDaemonConnectorException e) { |
| 500 | throw new IllegalStateException( |
| 501 | "Unable to communicate to native daemon for disabling NAT interface"); |
| 502 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 503 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 504 | |
| 505 | public String[] listTtys() throws IllegalStateException { |
| 506 | mContext.enforceCallingOrSelfPermission( |
| 507 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 508 | try { |
| 509 | return mConnector.doListCommand("list_ttys", NetdResponseCode.TtyListResult); |
| 510 | } catch (NativeDaemonConnectorException e) { |
| 511 | throw new IllegalStateException( |
| 512 | "Unable to communicate to native daemon for listing TTYs"); |
| 513 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 516 | public void attachPppd(String tty, String localAddr, String remoteAddr, String dns1Addr, |
| 517 | String dns2Addr) throws IllegalStateException { |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 518 | try { |
| 519 | mContext.enforceCallingOrSelfPermission( |
| 520 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 521 | mConnector.doCommand(String.format("pppd attach %s %s %s %s %s", tty, |
| 522 | InetAddress.getByName(localAddr).getHostAddress(), |
| 523 | InetAddress.getByName(remoteAddr).getHostAddress(), |
| 524 | InetAddress.getByName(dns1Addr).getHostAddress(), |
| 525 | InetAddress.getByName(dns2Addr).getHostAddress())); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 526 | } catch (UnknownHostException e) { |
| 527 | throw new IllegalStateException("Error resolving addr", e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 528 | } catch (NativeDaemonConnectorException e) { |
| 529 | throw new IllegalStateException("Error communicating to native daemon to attach pppd", e); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | public void detachPppd(String tty) throws IllegalStateException { |
| 534 | mContext.enforceCallingOrSelfPermission( |
| 535 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 536 | try { |
| 537 | mConnector.doCommand(String.format("pppd detach %s", tty)); |
| 538 | } catch (NativeDaemonConnectorException e) { |
| 539 | throw new IllegalStateException("Error communicating to native daemon to detach pppd", e); |
| 540 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 541 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 542 | |
| 543 | public void startUsbRNDIS() throws IllegalStateException { |
| 544 | mContext.enforceCallingOrSelfPermission( |
| 545 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 546 | try { |
| 547 | mConnector.doCommand("usb startrndis"); |
| 548 | } catch (NativeDaemonConnectorException e) { |
| 549 | throw new IllegalStateException( |
| 550 | "Error communicating to native daemon for starting RNDIS", e); |
| 551 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | public void stopUsbRNDIS() throws IllegalStateException { |
| 555 | mContext.enforceCallingOrSelfPermission( |
| 556 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 557 | try { |
| 558 | mConnector.doCommand("usb stoprndis"); |
| 559 | } catch (NativeDaemonConnectorException e) { |
| 560 | throw new IllegalStateException("Error communicating to native daemon", e); |
| 561 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | public boolean isUsbRNDISStarted() throws IllegalStateException { |
| 565 | mContext.enforceCallingOrSelfPermission( |
| 566 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 567 | ArrayList<String> rsp; |
| 568 | try { |
| 569 | rsp = mConnector.doCommand("usb rndisstatus"); |
| 570 | } catch (NativeDaemonConnectorException e) { |
| 571 | throw new IllegalStateException( |
| 572 | "Error communicating to native daemon to check RNDIS status", e); |
| 573 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 574 | |
| 575 | for (String line : rsp) { |
| 576 | String []tok = line.split(" "); |
| 577 | int code = Integer.parseInt(tok[0]); |
| 578 | if (code == NetdResponseCode.UsbRNDISStatusResult) { |
Mike Lockwood | 51cb9d5 | 2010-03-03 15:12:00 -0500 | [diff] [blame] | 579 | if (tok[3].equals("started")) |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 580 | return true; |
| 581 | return false; |
| 582 | } else { |
| 583 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 584 | } |
| 585 | } |
| 586 | throw new IllegalStateException("Got an empty response"); |
| 587 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 588 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 589 | public void startAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 590 | throws IllegalStateException { |
| 591 | mContext.enforceCallingOrSelfPermission( |
| 592 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 593 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 594 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 595 | try { |
| 596 | mConnector.doCommand(String.format("softap stop " + wlanIface)); |
| 597 | mConnector.doCommand(String.format("softap fwreload " + wlanIface + " AP")); |
| 598 | mConnector.doCommand(String.format("softap start " + wlanIface)); |
| 599 | if (wifiConfig == null) { |
| 600 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
| 601 | } else { |
| 602 | /** |
| 603 | * softap set arg1 arg2 arg3 [arg4 arg5 arg6 arg7 arg8] |
| 604 | * argv1 - wlan interface |
| 605 | * argv2 - softap interface |
| 606 | * argv3 - SSID |
| 607 | * argv4 - Security |
| 608 | * argv5 - Key |
| 609 | * argv6 - Channel |
| 610 | * argv7 - Preamble |
| 611 | * argv8 - Max SCB |
| 612 | */ |
| 613 | String str = String.format("softap set " + wlanIface + " " + softapIface + |
| 614 | " %s %s %s", convertQuotedString(wifiConfig.SSID), |
| 615 | wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ? |
| 616 | "wpa2-psk" : "open", |
| 617 | convertQuotedString(wifiConfig.preSharedKey)); |
| 618 | mConnector.doCommand(str); |
| 619 | } |
| 620 | mConnector.doCommand(String.format("softap startap")); |
| 621 | } catch (NativeDaemonConnectorException e) { |
| 622 | throw new IllegalStateException("Error communicating to native daemon to start softap", e); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 623 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Irfan Sheriff | a6e559e | 2010-05-24 14:55:42 -0700 | [diff] [blame] | 626 | private String convertQuotedString(String s) { |
Irfan Sheriff | 7baec0f | 2010-05-26 17:16:47 -0700 | [diff] [blame] | 627 | if (s == null) { |
| 628 | return s; |
| 629 | } |
| 630 | /* Replace \ with \\, then " with \" and add quotes at end */ |
| 631 | return '"' + s.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"") + '"'; |
Irfan Sheriff | a6e559e | 2010-05-24 14:55:42 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 634 | public void stopAccessPoint() throws IllegalStateException { |
| 635 | mContext.enforceCallingOrSelfPermission( |
| 636 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 637 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 638 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 639 | try { |
| 640 | mConnector.doCommand("softap stopap"); |
| 641 | } catch (NativeDaemonConnectorException e) { |
| 642 | throw new IllegalStateException("Error communicating to native daemon to stop soft AP", |
| 643 | e); |
| 644 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 647 | public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
| 648 | throws IllegalStateException { |
| 649 | mContext.enforceCallingOrSelfPermission( |
| 650 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 651 | mContext.enforceCallingOrSelfPermission( |
| 652 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 653 | try { |
| 654 | if (wifiConfig == null) { |
| 655 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
| 656 | } else { |
| 657 | String str = String.format("softap set " + wlanIface + " " + softapIface |
| 658 | + " %s %s %s", convertQuotedString(wifiConfig.SSID), |
| 659 | wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ? "wpa2-psk" : "open", |
| 660 | convertQuotedString(wifiConfig.preSharedKey)); |
| 661 | mConnector.doCommand(str); |
| 662 | } |
| 663 | } catch (NativeDaemonConnectorException e) { |
| 664 | throw new IllegalStateException("Error communicating to native daemon to set soft AP", |
| 665 | e); |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 668 | |
| 669 | private long getInterfaceCounter(String iface, boolean rx) { |
| 670 | mContext.enforceCallingOrSelfPermission( |
| 671 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 672 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 673 | String rsp; |
| 674 | try { |
| 675 | rsp = mConnector.doCommand( |
| 676 | String.format("interface read%scounter %s", (rx ? "rx" : "tx"), iface)).get(0); |
| 677 | } catch (NativeDaemonConnectorException e1) { |
| 678 | Slog.e(TAG, "Error communicating with native daemon", e1); |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | String[] tok = rsp.split(" "); |
| 683 | if (tok.length < 2) { |
| 684 | Slog.e(TAG, String.format("Malformed response for reading %s interface", |
| 685 | (rx ? "rx" : "tx"))); |
| 686 | return -1; |
| 687 | } |
| 688 | |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 689 | int code; |
| 690 | try { |
| 691 | code = Integer.parseInt(tok[0]); |
| 692 | } catch (NumberFormatException nfe) { |
| 693 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| 694 | return -1; |
| 695 | } |
| 696 | if ((rx && code != NetdResponseCode.InterfaceRxCounterResult) || ( |
| 697 | !rx && code != NetdResponseCode.InterfaceTxCounterResult)) { |
| 698 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| 699 | return -1; |
| 700 | } |
| 701 | return Long.parseLong(tok[1]); |
| 702 | } catch (Exception e) { |
| 703 | Slog.e(TAG, String.format( |
| 704 | "Failed to read interface %s counters", (rx ? "rx" : "tx")), e); |
| 705 | } |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | public long getInterfaceRxCounter(String iface) { |
| 710 | return getInterfaceCounter(iface, true); |
| 711 | } |
| 712 | |
| 713 | public long getInterfaceTxCounter(String iface) { |
| 714 | return getInterfaceCounter(iface, false); |
| 715 | } |
| 716 | |
San Mehat | f0db6e1 | 2010-04-07 15:22:10 -0700 | [diff] [blame] | 717 | public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) { |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 718 | mContext.enforceCallingOrSelfPermission( |
| 719 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 720 | try { |
| 721 | mConnector.doCommand(String.format( |
| 722 | "interface setthrottle %s %d %d", iface, rxKbps, txKbps)); |
| 723 | } catch (NativeDaemonConnectorException e) { |
| 724 | Slog.e(TAG, "Error communicating with native daemon to set throttle", e); |
| 725 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | private int getInterfaceThrottle(String iface, boolean rx) { |
| 729 | mContext.enforceCallingOrSelfPermission( |
| 730 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 731 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 732 | String rsp; |
| 733 | try { |
| 734 | rsp = mConnector.doCommand( |
| 735 | String.format("interface getthrottle %s %s", iface, |
| 736 | (rx ? "rx" : "tx"))).get(0); |
| 737 | } catch (NativeDaemonConnectorException e) { |
| 738 | Slog.e(TAG, "Error communicating with native daemon to getthrottle", e); |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | String[] tok = rsp.split(" "); |
| 743 | if (tok.length < 2) { |
| 744 | Slog.e(TAG, "Malformed response to getthrottle command"); |
| 745 | return -1; |
| 746 | } |
| 747 | |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 748 | int code; |
| 749 | try { |
| 750 | code = Integer.parseInt(tok[0]); |
| 751 | } catch (NumberFormatException nfe) { |
| 752 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| 753 | return -1; |
| 754 | } |
| 755 | if ((rx && code != NetdResponseCode.InterfaceRxThrottleResult) || ( |
| 756 | !rx && code != NetdResponseCode.InterfaceTxThrottleResult)) { |
| 757 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| 758 | return -1; |
| 759 | } |
| 760 | return Integer.parseInt(tok[1]); |
| 761 | } catch (Exception e) { |
| 762 | Slog.e(TAG, String.format( |
| 763 | "Failed to read interface %s throttle value", (rx ? "rx" : "tx")), e); |
| 764 | } |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | public int getInterfaceRxThrottle(String iface) { |
| 769 | return getInterfaceThrottle(iface, true); |
| 770 | } |
| 771 | |
| 772 | public int getInterfaceTxThrottle(String iface) { |
| 773 | return getInterfaceThrottle(iface, false); |
| 774 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 775 | } |