blob: 8d327d01fb2f2c83fe1e0c89a37231f55145090d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package android.net.wifi;
18
repo sync55bc5f32011-06-24 14:23:07 -070019import android.net.wifi.p2p.WifiP2pConfig;
20import android.net.wifi.p2p.WifiP2pGroup;
21import android.net.wifi.p2p.WifiP2pDevice;
22import android.util.Log;
23
24import java.io.InputStream;
25import java.lang.Process;
26import java.util.ArrayList;
27import java.util.List;
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029/**
30 * Native calls for sending requests to the supplicant daemon, and for
31 * receiving asynchronous events. All methods of the form "xxxxCommand()"
32 * must be single-threaded, to avoid requests and responses initiated
33 * from multiple threads from being intermingled.
34 * <p/>
35 * Note that methods whose names are not of the form "xxxCommand()" do
36 * not talk to the supplicant daemon.
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -080037 * Also, note that all WifiNative calls should happen in the
38 * WifiStateTracker class except for waitForEvent() call which is
39 * on a separate monitor channel for WifiMonitor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 *
repo sync55bc5f32011-06-24 14:23:07 -070041 * TODO: clean up the API and move the functionality from JNI to here. We should
42 * be able to get everything done with doBooleanCommand, doIntCommand and
43 * doStringCommand native commands
44 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 * {@hide}
46 */
47public class WifiNative {
48
49 static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED = 0;
50 static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED = 1;
51 static final int BLUETOOTH_COEXISTENCE_MODE_SENSE = 2;
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 public native static String getErrorString(int errorCode);
54
55 public native static boolean loadDriver();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070056
57 public native static boolean isDriverLoaded();
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 public native static boolean unloadDriver();
60
61 public native static boolean startSupplicant();
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080062
Irfan Sherifff42c39b2011-08-26 14:45:23 -070063 public native static boolean startP2pSupplicant();
64
65 /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080066 *
67 * Note that underneath we use a harsh-sounding "terminate" supplicant command
68 * for a graceful stop and a mild-sounding "stop" interface
69 * to kill the process
70 */
71 public native static boolean stopSupplicant();
72
73 /* Sends a kill signal to supplicant. To be used when we have lost connection
74 or when the supplicant is hung */
Irfan Sheriff96071a72010-12-14 11:29:23 -080075 public native static boolean killSupplicant();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77 public native static boolean connectToSupplicant();
78
79 public native static void closeSupplicantConnection();
80
81 public native static boolean pingCommand();
82
Mike Lockwooda5ec95c2009-07-08 17:11:17 -040083 public native static boolean scanCommand(boolean forceActive);
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public native static boolean setScanModeCommand(boolean setActive);
86
87 public native static String listNetworksCommand();
88
89 public native static int addNetworkCommand();
90
91 public native static boolean setNetworkVariableCommand(int netId, String name, String value);
92
93 public native static String getNetworkVariableCommand(int netId, String name);
94
95 public native static boolean removeNetworkCommand(int netId);
96
97 public native static boolean enableNetworkCommand(int netId, boolean disableOthers);
Irfan Sheriff5d001ea2010-12-15 10:29:49 -080098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public native static boolean disableNetworkCommand(int netId);
100
101 public native static boolean reconnectCommand();
102
103 public native static boolean reassociateCommand();
104
105 public native static boolean disconnectCommand();
106
107 public native static String statusCommand();
108
109 public native static int getRssiCommand();
110
Robert Greenwalt91f22f902009-06-08 18:15:21 -0700111 public native static int getRssiApproxCommand();
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public native static int getLinkSpeedCommand();
114
115 public native static String getMacAddressCommand();
116
117 public native static String scanResultsCommand();
118
119 public native static boolean startDriverCommand();
120
121 public native static boolean stopDriverCommand();
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124 /**
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -0700125 * Start filtering out Multicast V4 packets
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 * @return {@code true} if the operation succeeded, {@code false} otherwise
127 */
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -0700128 public native static boolean startFilteringMulticastV4Packets();
129
130 /**
131 * Stop filtering out Multicast V4 packets.
132 * @return {@code true} if the operation succeeded, {@code false} otherwise
133 */
134 public native static boolean stopFilteringMulticastV4Packets();
135
136 /**
137 * Start filtering out Multicast V6 packets
138 * @return {@code true} if the operation succeeded, {@code false} otherwise
139 */
140 public native static boolean startFilteringMulticastV6Packets();
141
142 /**
143 * Stop filtering out Multicast V6 packets.
144 * @return {@code true} if the operation succeeded, {@code false} otherwise
145 */
146 public native static boolean stopFilteringMulticastV6Packets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 public native static boolean setPowerModeCommand(int mode);
149
Irfan Sheriff25c9bf22010-09-02 12:48:20 -0700150 public native static int getBandCommand();
151
152 public native static boolean setBandCommand(int band);
153
Mikael Kanstrupea8bd1d2010-04-07 16:45:58 +0200154 public native static int getPowerModeCommand();
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /**
157 * Sets the bluetooth coexistence mode.
Irfan Sheriff5d001ea2010-12-15 10:29:49 -0800158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
160 * {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
161 * {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
162 * @return Whether the mode was successfully set.
163 */
164 public native static boolean setBluetoothCoexistenceModeCommand(int mode);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700165
166 /**
167 * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
168 * some of the low-level scan parameters used by the driver are changed to
169 * reduce interference with A2DP streaming.
170 *
171 * @param isSet whether to enable or disable this mode
172 * @return {@code true} if the command succeeded, {@code false} otherwise.
173 */
174 public native static boolean setBluetoothCoexistenceScanModeCommand(boolean setCoexScanMode);
Irfan Sheriff5d001ea2010-12-15 10:29:49 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public native static boolean saveConfigCommand();
177
178 public native static boolean reloadConfigCommand();
179
180 public native static boolean setScanResultHandlingCommand(int mode);
181
182 public native static boolean addToBlacklistCommand(String bssid);
183
184 public native static boolean clearBlacklistCommand();
185
Irfan Sheriff5ee89802010-09-16 17:53:34 -0700186 public native static boolean startWpsPbcCommand(String bssid);
187
Irfan Sheriff02fb46a2010-12-08 11:27:37 -0800188 public native static boolean startWpsWithPinFromAccessPointCommand(String bssid, String apPin);
Irfan Sherifff235c5a2010-10-21 16:44:48 -0700189
Irfan Sheriff02fb46a2010-12-08 11:27:37 -0800190 public native static String startWpsWithPinFromDeviceCommand(String bssid);
Irfan Sheriff5ee89802010-09-16 17:53:34 -0700191
Irfan Sheriff5876a422010-08-12 20:26:23 -0700192 public native static boolean setSuspendOptimizationsCommand(boolean enabled);
193
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700194 public native static boolean setCountryCodeCommand(String countryCode);
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
197 * Wait for the supplicant to send an event, returning the event string.
198 * @return the event string sent by the supplicant.
199 */
200 public native static String waitForEvent();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800201
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700202 public native static void enableBackgroundScanCommand(boolean enable);
203
204 public native static void setScanIntervalCommand(int scanInterval);
repo sync55bc5f32011-06-24 14:23:07 -0700205
206 private native static boolean doBooleanCommand(String command);
207
repo sync55bc5f32011-06-24 14:23:07 -0700208 private native static int doIntCommand(String command);
209
210 private native static String doStringCommand(String command);
211
repo sync2b8edd02011-08-16 21:39:24 -0700212 public static boolean wpsPbc() {
213 return doBooleanCommand("WPS_PBC");
214 }
215
216 public static boolean wpsPin(String pin) {
217 return doBooleanCommand("WPS_PIN any " + pin);
218 }
219
repo sync55bc5f32011-06-24 14:23:07 -0700220 public static boolean p2pFind() {
repo sync2b8edd02011-08-16 21:39:24 -0700221 return doBooleanCommand("P2P_FIND");
repo sync55bc5f32011-06-24 14:23:07 -0700222 }
223
224 public static boolean p2pFind(int timeout) {
225 if (timeout <= 0) {
226 return p2pFind();
227 }
repo sync2b8edd02011-08-16 21:39:24 -0700228 return doBooleanCommand("P2P_FIND " + timeout);
repo sync55bc5f32011-06-24 14:23:07 -0700229 }
230
231 public static boolean p2pListen() {
repo sync2b8edd02011-08-16 21:39:24 -0700232 return doBooleanCommand("P2P_LISTEN");
repo sync55bc5f32011-06-24 14:23:07 -0700233 }
234
235 public static boolean p2pListen(int timeout) {
236 if (timeout <= 0) {
237 return p2pListen();
238 }
repo sync2b8edd02011-08-16 21:39:24 -0700239 return doBooleanCommand("P2P_LISTEN " + timeout);
repo sync55bc5f32011-06-24 14:23:07 -0700240 }
241
242 public static boolean p2pFlush() {
repo sync2b8edd02011-08-16 21:39:24 -0700243 return doBooleanCommand("P2P_FLUSH");
repo sync55bc5f32011-06-24 14:23:07 -0700244 }
245
246 /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
247 [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
Irfan Sheriffea5b16a2011-08-24 12:30:20 -0700248 public static String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
repo sync55bc5f32011-06-24 14:23:07 -0700249 if (config == null) return null;
250 List<String> args = new ArrayList<String>();
251 WpsConfiguration wpsConfig = config.wpsConfig;
252 args.add(config.deviceAddress);
253
254 switch (wpsConfig.setup) {
255 case PBC:
256 args.add("pbc");
257 break;
258 case DISPLAY:
259 //TODO: pass the pin back for display
260 args.add("pin");
261 args.add("display");
262 break;
263 case KEYPAD:
264 args.add(wpsConfig.pin);
265 args.add("keypad");
266 break;
267 case LABEL:
268 args.add(wpsConfig.pin);
269 args.add("label");
270 default:
271 break;
272 }
273
Irfan Sheriffea5b16a2011-08-24 12:30:20 -0700274 /* Persist unless there is an explicit request to not do so*/
275 if (config.persist != WifiP2pConfig.Persist.NO) args.add("persistent");
276 if (joinExistingGroup) args.add("join");
repo sync55bc5f32011-06-24 14:23:07 -0700277
repo sync2b8edd02011-08-16 21:39:24 -0700278 int groupOwnerIntent = config.groupOwnerIntent;
279 if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
280 groupOwnerIntent = 3; //default value
281 }
282 args.add("go_intent=" + groupOwnerIntent);
repo sync55bc5f32011-06-24 14:23:07 -0700283
repo sync2b8edd02011-08-16 21:39:24 -0700284 String command = "P2P_CONNECT ";
repo sync55bc5f32011-06-24 14:23:07 -0700285 for (String s : args) command += s + " ";
286
repo sync2b8edd02011-08-16 21:39:24 -0700287 return doStringCommand(command);
repo sync55bc5f32011-06-24 14:23:07 -0700288 }
289
290 public static boolean p2pGroupAdd() {
repo sync2b8edd02011-08-16 21:39:24 -0700291 return doBooleanCommand("P2P_GROUP_ADD");
repo sync55bc5f32011-06-24 14:23:07 -0700292 }
293
294 public static boolean p2pGroupRemove(String iface) {
295 if (iface == null) return false;
repo sync2b8edd02011-08-16 21:39:24 -0700296 return doBooleanCommand("P2P_GROUP_REMOVE " + iface);
repo sync55bc5f32011-06-24 14:23:07 -0700297 }
298
299 public static boolean p2pReject(String deviceAddress) {
repo sync2b8edd02011-08-16 21:39:24 -0700300 return doBooleanCommand("P2P_REJECT " + deviceAddress);
repo sync55bc5f32011-06-24 14:23:07 -0700301 }
302
303 /* Invite a peer to a group */
304 public static boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
Irfan Sheriffea5b16a2011-08-24 12:30:20 -0700305 if (deviceAddress == null) return false;
306
307 if (group == null) {
308 return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
309 } else {
310 return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
311 + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
312 }
repo sync55bc5f32011-06-24 14:23:07 -0700313 }
repo sync8c57bcd2011-08-18 16:56:36 -0700314
Irfan Sheriffea5b16a2011-08-24 12:30:20 -0700315 /* Reinvoke a persistent connection */
316 public static boolean p2pReinvoke(int netId, String deviceAddress) {
317 if (deviceAddress == null || netId < 0) return false;
318
319 return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
320 }
321
322
repo sync8c57bcd2011-08-18 16:56:36 -0700323 public static String p2pGetInterfaceAddress(String deviceAddress) {
324 if (deviceAddress == null) return null;
325
326 // "p2p_peer deviceAddress" returns a multi-line result containing
327 // intended_addr=fa:7b:7a:42:82:13
328 String peerInfo = p2pPeer(deviceAddress);
329 if (peerInfo == null) return null;
330 String[] tokens= peerInfo.split("\n");
331
332 for (String token : tokens) {
333 //TODO: update from interface_addr when wpa_supplicant implementation is fixed
334 if (token.startsWith("intended_addr=")) {
335 String[] nameValue = token.split("=");
336 if (nameValue.length != 2) break;
337 return nameValue[1];
338 }
339 }
340 return null;
341 }
342
343 public static String p2pPeer(String deviceAddress) {
344 return doStringCommand("P2P_PEER " + deviceAddress);
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346}